PhpDumperTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Symfony\Tests\Components\DependencyInjection\Dumper;
  10. require_once __DIR__.'/../../../bootstrap.php';
  11. use Symfony\Components\DependencyInjection\Builder;
  12. use Symfony\Components\DependencyInjection\Dumper\PhpDumper;
  13. class PhpDumperTest extends \PHPUnit_Framework_TestCase
  14. {
  15. static protected $fixturesPath;
  16. static public function setUpBeforeClass()
  17. {
  18. self::$fixturesPath = realpath(__DIR__.'/../../../../../fixtures/Symfony/Components/DependencyInjection/');
  19. }
  20. public function testDump()
  21. {
  22. $dumper = new PhpDumper($container = new Builder());
  23. $this->assertEquals(file_get_contents(self::$fixturesPath.'/php/services1.php'), $dumper->dump(), '->dump() dumps an empty container as an empty PHP class');
  24. $this->assertEquals(file_get_contents(self::$fixturesPath.'/php/services1-1.php'), $dumper->dump(array('class' => 'Container', 'base_class' => 'AbstractContainer')), '->dump() takes a class and a base_class options');
  25. $container = new Builder();
  26. $dumper = new PhpDumper($container);
  27. }
  28. public function testAddParameters()
  29. {
  30. $container = include self::$fixturesPath.'/containers/container8.php';
  31. $dumper = new PhpDumper($container);
  32. $this->assertEquals(file_get_contents(self::$fixturesPath.'/php/services8.php'), $dumper->dump(), '->dump() dumps parameters');
  33. }
  34. public function testAddService()
  35. {
  36. $container = include self::$fixturesPath.'/containers/container9.php';
  37. $dumper = new PhpDumper($container);
  38. $this->assertEquals(str_replace('%path%', self::$fixturesPath.'/includes', file_get_contents(self::$fixturesPath.'/php/services9.php')), $dumper->dump(), '->dump() dumps services');
  39. $dumper = new PhpDumper($container = new Builder());
  40. $container->register('foo', 'FooClass')->addArgument(new \stdClass());
  41. try
  42. {
  43. $dumper->dump();
  44. $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  45. }
  46. catch (\RuntimeException $e)
  47. {
  48. }
  49. }
  50. }