PhpDumperTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\DependencyInjection\Dumper;
  11. use Symfony\Component\DependencyInjection\ContainerBuilder;
  12. use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
  13. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. use Symfony\Component\DependencyInjection\InterfaceInjector;
  16. class PhpDumperTest extends \PHPUnit_Framework_TestCase
  17. {
  18. static protected $fixturesPath;
  19. static public function setUpBeforeClass()
  20. {
  21. self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
  22. }
  23. public function testDump()
  24. {
  25. $dumper = new PhpDumper($container = new ContainerBuilder());
  26. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services1.php', $dumper->dump(), '->dump() dumps an empty container as an empty PHP class');
  27. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services1-1.php', $dumper->dump(array('class' => 'Container', 'base_class' => 'AbstractContainer')), '->dump() takes a class and a base_class options');
  28. $container = new ContainerBuilder();
  29. $dumper = new PhpDumper($container);
  30. }
  31. /**
  32. * @expectedException InvalidArgumentException
  33. */
  34. public function testExportParameters()
  35. {
  36. $dumper = new PhpDumper($container = new ContainerBuilder(new ParameterBag(array('foo' => new Reference('foo')))));
  37. $dumper->dump();
  38. }
  39. public function testAddParameters()
  40. {
  41. $container = include self::$fixturesPath.'/containers/container8.php';
  42. $dumper = new PhpDumper($container);
  43. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services8.php', $dumper->dump(), '->dump() dumps parameters');
  44. }
  45. public function testAddService()
  46. {
  47. $container = include self::$fixturesPath.'/containers/container9.php';
  48. $dumper = new PhpDumper($container);
  49. $this->assertEquals(str_replace('%path%', str_replace('\\','\\\\',self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR), file_get_contents(self::$fixturesPath.'/php/services9.php')), $dumper->dump(), '->dump() dumps services');
  50. $dumper = new PhpDumper($container = new ContainerBuilder());
  51. $container->register('foo', 'FooClass')->addArgument(new \stdClass());
  52. try {
  53. $dumper->dump();
  54. $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  55. } catch (\Exception $e) {
  56. $this->assertInstanceOf('\RuntimeException', $e, '->dump() returns a LogicException if the dump() method has not been overriden by a children class');
  57. $this->assertEquals('Unable to dump a service container if a parameter is an object or a resource.', $e->getMessage(), '->dump() returns a LogicException if the dump() method has not been overriden by a children class');
  58. }
  59. }
  60. public function testOverrideServiceWhenUsingADumpedContainer()
  61. {
  62. require_once self::$fixturesPath.'/php/services9.php';
  63. require_once self::$fixturesPath.'/includes/foo.php';
  64. $container = new \ProjectServiceContainer();
  65. $container->set('bar', $bar = new \stdClass());
  66. $container->setParameter('foo_bar', 'foo_bar');
  67. $this->assertEquals($bar, $container->get('bar'), '->set() overrides an already defined service');
  68. }
  69. public function testOverrideServiceWhenUsingADumpedContainerAndServiceIsUsedFromAnotherOne()
  70. {
  71. require_once self::$fixturesPath.'/php/services9.php';
  72. require_once self::$fixturesPath.'/includes/foo.php';
  73. require_once self::$fixturesPath.'/includes/classes.php';
  74. $container = new \ProjectServiceContainer();
  75. $container->set('bar', $bar = new \stdClass());
  76. $this->assertSame($bar, $container->get('foo')->bar, '->set() overrides an already defined service');
  77. }
  78. public function testInterfaceInjectors()
  79. {
  80. $interfaceInjector = new InterfaceInjector('FooClass');
  81. $interfaceInjector->addMethodCall('setBar', array('someValue'));
  82. $container = include self::$fixturesPath.'/containers/interfaces1.php';
  83. $container->addInterfaceInjector($interfaceInjector);
  84. $dumper = new PhpDumper($container);
  85. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_interfaces-1.php', $dumper->dump(), '->dump() dumps interface injectors');
  86. }
  87. public function testInterfaceInjectorsAndServiceFactories()
  88. {
  89. $interfaceInjector = new InterfaceInjector('BarClass');
  90. $interfaceInjector->addMethodCall('setFoo', array('someValue'));
  91. $container = include self::$fixturesPath.'/containers/interfaces2.php';
  92. $container->addInterfaceInjector($interfaceInjector);
  93. $dumper = new PhpDumper($container);
  94. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_interfaces-2.php', $dumper->dump(), '->dump() dumps interface injectors');
  95. }
  96. public function testFrozenContainerInterfaceInjectors()
  97. {
  98. $interfaceInjector = new InterfaceInjector('FooClass');
  99. $interfaceInjector->addMethodCall('setBar', array('someValue'));
  100. $container = include self::$fixturesPath.'/containers/interfaces1.php';
  101. $container->addInterfaceInjector($interfaceInjector);
  102. $container->compile();
  103. $dumper = new PhpDumper($container);
  104. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_interfaces-1-1.php', $dumper->dump(), '->dump() dumps interface injectors');
  105. }
  106. }