PhpDumperTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\Component\DependencyInjection\Dumper;
  10. use Symfony\Component\DependencyInjection\ContainerBuilder;
  11. use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
  12. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  13. use Symfony\Component\DependencyInjection\Reference;
  14. use Symfony\Component\DependencyInjection\InterfaceInjector;
  15. class PhpDumperTest extends \PHPUnit_Framework_TestCase
  16. {
  17. static protected $fixturesPath;
  18. static public function setUpBeforeClass()
  19. {
  20. self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
  21. }
  22. public function testDump()
  23. {
  24. $dumper = new PhpDumper($container = new ContainerBuilder());
  25. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services1.php', $dumper->dump(), '->dump() dumps an empty container as an empty PHP class');
  26. $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');
  27. $container = new ContainerBuilder();
  28. $dumper = new PhpDumper($container);
  29. }
  30. /**
  31. * @expectedException InvalidArgumentException
  32. */
  33. public function testExportParameters()
  34. {
  35. $dumper = new PhpDumper($container = new ContainerBuilder(new ParameterBag(array('foo' => new Reference('foo')))));
  36. $dumper->dump();
  37. }
  38. public function testAddParameters()
  39. {
  40. $container = include self::$fixturesPath.'/containers/container8.php';
  41. $dumper = new PhpDumper($container);
  42. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services8.php', $dumper->dump(), '->dump() dumps parameters');
  43. }
  44. public function testAddService()
  45. {
  46. $container = include self::$fixturesPath.'/containers/container9.php';
  47. $dumper = new PhpDumper($container);
  48. $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');
  49. $dumper = new PhpDumper($container = new ContainerBuilder());
  50. $container->register('foo', 'FooClass')->addArgument(new \stdClass());
  51. try {
  52. $dumper->dump();
  53. $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  54. } catch (\Exception $e) {
  55. $this->assertInstanceOf('\RuntimeException', $e, '->dump() returns a LogicException if the dump() method has not been overriden by a children class');
  56. $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');
  57. }
  58. }
  59. public function testOverrideServiceWhenUsingADumpedContainer()
  60. {
  61. require_once self::$fixturesPath.'/php/services9.php';
  62. require_once self::$fixturesPath.'/includes/foo.php';
  63. $container = new \ProjectServiceContainer();
  64. $container->set('bar', $bar = new \stdClass());
  65. $container->setParameter('foo_bar', 'foo_bar');
  66. $this->assertEquals($bar, $container->get('bar'), '->set() overrides an already defined service');
  67. }
  68. public function testOverrideServiceWhenUsingADumpedContainerAndServiceIsUsedFromAnotherOne()
  69. {
  70. require_once self::$fixturesPath.'/php/services9.php';
  71. require_once self::$fixturesPath.'/includes/foo.php';
  72. require_once self::$fixturesPath.'/includes/classes.php';
  73. $container = new \ProjectServiceContainer();
  74. $container->set('bar', $bar = new \stdClass());
  75. $this->assertSame($bar, $container->get('foo')->bar, '->set() overrides an already defined service');
  76. }
  77. public function testInterfaceInjectors()
  78. {
  79. $interfaceInjector = new InterfaceInjector('FooClass');
  80. $interfaceInjector->addMethodCall('setBar', array('someValue'));
  81. $container = include self::$fixturesPath.'/containers/interfaces1.php';
  82. $container->addInterfaceInjector($interfaceInjector);
  83. $dumper = new PhpDumper($container);
  84. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_interfaces-1.php', $dumper->dump(), '->dump() dumps interface injectors');
  85. }
  86. public function testInterfaceInjectorsAndServiceFactories()
  87. {
  88. $interfaceInjector = new InterfaceInjector('BarClass');
  89. $interfaceInjector->addMethodCall('setFoo', array('someValue'));
  90. $container = include self::$fixturesPath.'/containers/interfaces2.php';
  91. $container->addInterfaceInjector($interfaceInjector);
  92. $dumper = new PhpDumper($container);
  93. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_interfaces-2.php', $dumper->dump(), '->dump() dumps interface injectors');
  94. }
  95. public function testFrozenContainerInterfaceInjectors()
  96. {
  97. $interfaceInjector = new InterfaceInjector('FooClass');
  98. $interfaceInjector->addMethodCall('setBar', array('someValue'));
  99. $container = include self::$fixturesPath.'/containers/interfaces1.php';
  100. $container->addInterfaceInjector($interfaceInjector);
  101. $container->freeze();
  102. $dumper = new PhpDumper($container);
  103. file_put_contents(self::$fixturesPath.'/php/services_interfaces-1-1.php', $dumper->dump());
  104. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_interfaces-1-1.php', $dumper->dump(), '->dump() dumps interface injectors');
  105. }
  106. }