PhpDumperTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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\Component\DependencyInjection\Tests\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->setResourceTracking(false);
  35. $container->register('foo', 'stdClass');
  36. $container->compile();
  37. $dumper = new PhpDumper($container);
  38. $dumpedString = $dumper->dump();
  39. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services11.php', $dumpedString, '->dump() does not add getDefaultParameters() method call if container have no parameters.');
  40. $this->assertNotRegexp("/function getDefaultParameters\(/", $dumpedString, '->dump() does not add getDefaultParameters() method definition.');
  41. }
  42. public function testDumpOptimizationString()
  43. {
  44. $definition = new Definition();
  45. $definition->setClass('stdClass');
  46. $definition->addArgument(array(
  47. 'only dot' => '.',
  48. 'concatenation as value' => '.\'\'.',
  49. 'concatenation from the start value' => '\'\'.',
  50. '.' => 'dot as a key',
  51. '.\'\'.' => 'concatenation as a key',
  52. '\'\'.' =>'concatenation from the start key',
  53. 'optimize concatenation' => "string1%some_string%string2",
  54. 'optimize concatenation with empty string' => "string1%empty_value%string2",
  55. 'optimize concatenation from the start' => '%empty_value%start',
  56. 'optimize concatenation at the end' => 'end%empty_value%',
  57. ));
  58. $container = new ContainerBuilder();
  59. $container->setResourceTracking(false);
  60. $container->setDefinition('test', $definition);
  61. $container->setParameter('empty_value', '');
  62. $container->setParameter('some_string', '-');
  63. $container->compile();
  64. $dumper = new PhpDumper($container);
  65. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services10.php', $dumper->dump(), '->dump() dumps an empty container as an empty PHP class');
  66. }
  67. /**
  68. * @expectedException InvalidArgumentException
  69. */
  70. public function testExportParameters()
  71. {
  72. $dumper = new PhpDumper(new ContainerBuilder(new ParameterBag(array('foo' => new Reference('foo')))));
  73. $dumper->dump();
  74. }
  75. public function testAddParameters()
  76. {
  77. $container = include self::$fixturesPath.'/containers/container8.php';
  78. $dumper = new PhpDumper($container);
  79. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services8.php', $dumper->dump(), '->dump() dumps parameters');
  80. }
  81. public function testAddService()
  82. {
  83. // without compilation
  84. $container = include self::$fixturesPath.'/containers/container9.php';
  85. $dumper = new PhpDumper($container);
  86. $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');
  87. // with compilation
  88. $container = include self::$fixturesPath.'/containers/container9.php';
  89. $container->compile();
  90. $dumper = new PhpDumper($container);
  91. $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');
  92. $dumper = new PhpDumper($container = new ContainerBuilder());
  93. $container->register('foo', 'FooClass')->addArgument(new \stdClass());
  94. try {
  95. $dumper->dump();
  96. $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  97. } catch (\Exception $e) {
  98. $this->assertInstanceOf('\Symfony\Component\DependencyInjection\Exception\RuntimeException', $e, '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  99. $this->assertEquals('Unable to dump a service container if a parameter is an object or a resource.', $e->getMessage(), '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  100. }
  101. }
  102. /**
  103. * @expectedException InvalidArgumentException
  104. * @expectedExceptionMessage Service id "bar$" cannot be converted to a valid PHP method name.
  105. */
  106. public function testAddServiceInvalidServiceId()
  107. {
  108. $container = new ContainerBuilder();
  109. $container->register('bar$', 'FooClass');
  110. $dumper = new PhpDumper($container);
  111. $dumper->dump();
  112. }
  113. public function testAliases()
  114. {
  115. $container = include self::$fixturesPath.'/containers/container9.php';
  116. $container->compile();
  117. $dumper = new PhpDumper($container);
  118. eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Aliases')));
  119. $container = new \Symfony_DI_PhpDumper_Test_Aliases();
  120. $container->set('foo', $foo = new \stdClass);
  121. $this->assertSame($foo, $container->get('foo'));
  122. $this->assertSame($foo, $container->get('alias_for_foo'));
  123. $this->assertSame($foo, $container->get('alias_for_alias'));
  124. }
  125. public function testFrozenContainerWithoutAliases()
  126. {
  127. $container = new ContainerBuilder();
  128. $container->compile();
  129. $dumper = new PhpDumper($container);
  130. eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Frozen_No_Aliases')));
  131. $container = new \Symfony_DI_PhpDumper_Test_Frozen_No_Aliases();
  132. $this->assertFalse($container->has('foo'));
  133. }
  134. public function testOverrideServiceWhenUsingADumpedContainer()
  135. {
  136. require_once self::$fixturesPath.'/php/services9.php';
  137. require_once self::$fixturesPath.'/includes/foo.php';
  138. $container = new \ProjectServiceContainer();
  139. $container->set('bar', $bar = new \stdClass());
  140. $container->setParameter('foo_bar', 'foo_bar');
  141. $this->assertEquals($bar, $container->get('bar'), '->set() overrides an already defined service');
  142. }
  143. public function testOverrideServiceWhenUsingADumpedContainerAndServiceIsUsedFromAnotherOne()
  144. {
  145. require_once self::$fixturesPath.'/php/services9.php';
  146. require_once self::$fixturesPath.'/includes/foo.php';
  147. require_once self::$fixturesPath.'/includes/classes.php';
  148. $container = new \ProjectServiceContainer();
  149. $container->set('bar', $bar = new \stdClass());
  150. $this->assertSame($bar, $container->get('foo')->bar, '->set() overrides an already defined service');
  151. }
  152. }