PhpDumperTest.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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\Definition;
  16. class PhpDumperTest extends \PHPUnit_Framework_TestCase
  17. {
  18. protected static $fixturesPath;
  19. public static 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. new PhpDumper($container);
  30. }
  31. public function testDumpFrozenContainerWithNoParameter()
  32. {
  33. $container = new ContainerBuilder();
  34. $container->register('foo', 'stdClass');
  35. $container->compile();
  36. $dumper = new PhpDumper($container);
  37. $dumpedString = $dumper->dump();
  38. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services11.php', $dumpedString, '->dump() does not add getDefaultParameters() method call if container have no parameters.');
  39. $this->assertNotRegexp("/function getDefaultParameters\(/", $dumpedString, '->dump() does not add getDefaultParameters() method definition.');
  40. }
  41. public function testDumpOptimizationString()
  42. {
  43. $definition = new Definition();
  44. $definition->setClass('stdClass');
  45. $definition->addArgument(array(
  46. 'only dot' => '.',
  47. 'concatenation as value' => '.\'\'.',
  48. 'concatenation from the start value' => '\'\'.',
  49. '.' => 'dot as a key',
  50. '.\'\'.' => 'concatenation as a key',
  51. '\'\'.' =>'concatenation from the start key',
  52. 'optimize concatenation' => "string1%some_string%string2",
  53. 'optimize concatenation with empty string' => "string1%empty_value%string2",
  54. 'optimize concatenation from the start' => '%empty_value%start',
  55. 'optimize concatenation at the end' => 'end%empty_value%',
  56. ));
  57. $container = new ContainerBuilder();
  58. $container->setDefinition('test', $definition);
  59. $container->setParameter('empty_value', '');
  60. $container->setParameter('some_string', '-');
  61. $container->compile();
  62. $dumper = new PhpDumper($container);
  63. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services10.php', $dumper->dump(), '->dump() dumps an empty container as an empty PHP class');
  64. }
  65. /**
  66. * @expectedException InvalidArgumentException
  67. */
  68. public function testExportParameters()
  69. {
  70. $dumper = new PhpDumper(new ContainerBuilder(new ParameterBag(array('foo' => new Reference('foo')))));
  71. $dumper->dump();
  72. }
  73. public function testAddParameters()
  74. {
  75. $container = include self::$fixturesPath.'/containers/container8.php';
  76. $dumper = new PhpDumper($container);
  77. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services8.php', $dumper->dump(), '->dump() dumps parameters');
  78. }
  79. public function testAddService()
  80. {
  81. // without compilation
  82. $container = include self::$fixturesPath.'/containers/container9.php';
  83. $dumper = new PhpDumper($container);
  84. $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');
  85. // with compilation
  86. $container = include self::$fixturesPath.'/containers/container9.php';
  87. $container->compile();
  88. $dumper = new PhpDumper($container);
  89. $this->assertEquals(str_replace('%path%', str_replace('\\','\\\\',self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR), file_get_contents(self::$fixturesPath.'/php/services9_compiled.php')), $dumper->dump(), '->dump() dumps services');
  90. $dumper = new PhpDumper($container = new ContainerBuilder());
  91. $container->register('foo', 'FooClass')->addArgument(new \stdClass());
  92. try {
  93. $dumper->dump();
  94. $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  95. } catch (\Exception $e) {
  96. $this->assertInstanceOf('\RuntimeException', $e, '->dump() returns a LogicException if the dump() method has not been overridden by a children class');
  97. $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 overridden by a children class');
  98. }
  99. }
  100. public function testOverrideServiceWhenUsingADumpedContainer()
  101. {
  102. require_once self::$fixturesPath.'/php/services9.php';
  103. require_once self::$fixturesPath.'/includes/foo.php';
  104. $container = new \ProjectServiceContainer();
  105. $container->set('bar', $bar = new \stdClass());
  106. $container->setParameter('foo_bar', 'foo_bar');
  107. $this->assertEquals($bar, $container->get('bar'), '->set() overrides an already defined service');
  108. }
  109. public function testOverrideServiceWhenUsingADumpedContainerAndServiceIsUsedFromAnotherOne()
  110. {
  111. require_once self::$fixturesPath.'/php/services9.php';
  112. require_once self::$fixturesPath.'/includes/foo.php';
  113. require_once self::$fixturesPath.'/includes/classes.php';
  114. $container = new \ProjectServiceContainer();
  115. $container->set('bar', $bar = new \stdClass());
  116. $this->assertSame($bar, $container->get('foo')->bar, '->set() overrides an already defined service');
  117. }
  118. }