PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

https://gitlab.com/Isaki/le331.fr
PHP | 221 lines | 162 code | 36 blank | 23 comment | 0 complexity | 6c338a74cfbb6e2cd919fe283b0d83d5 MD5 | raw file
  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', 'namespace' => 'Symfony\Component\DependencyInjection\Dump')), '->dump() takes a class and a base_class options');
  28. $container = new ContainerBuilder();
  29. new PhpDumper($container);
  30. }
  31. public function testDumpOptimizationString()
  32. {
  33. $definition = new Definition();
  34. $definition->setClass('stdClass');
  35. $definition->addArgument(array(
  36. 'only dot' => '.',
  37. 'concatenation as value' => '.\'\'.',
  38. 'concatenation from the start value' => '\'\'.',
  39. '.' => 'dot as a key',
  40. '.\'\'.' => 'concatenation as a key',
  41. '\'\'.' => 'concatenation from the start key',
  42. 'optimize concatenation' => 'string1%some_string%string2',
  43. 'optimize concatenation with empty string' => 'string1%empty_value%string2',
  44. 'optimize concatenation from the start' => '%empty_value%start',
  45. 'optimize concatenation at the end' => 'end%empty_value%',
  46. ));
  47. $container = new ContainerBuilder();
  48. $container->setResourceTracking(false);
  49. $container->setDefinition('test', $definition);
  50. $container->setParameter('empty_value', '');
  51. $container->setParameter('some_string', '-');
  52. $container->compile();
  53. $dumper = new PhpDumper($container);
  54. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services10.php', $dumper->dump(), '->dump() dumps an empty container as an empty PHP class');
  55. }
  56. public function testDumpRelativeDir()
  57. {
  58. $definition = new Definition();
  59. $definition->setClass('stdClass');
  60. $definition->addArgument('%foo%');
  61. $definition->addArgument(array('%foo%' => '%buz%/'));
  62. $container = new ContainerBuilder();
  63. $container->setDefinition('test', $definition);
  64. $container->setParameter('foo', 'wiz'.dirname(__DIR__));
  65. $container->setParameter('bar', __DIR__);
  66. $container->setParameter('baz', '%bar%/PhpDumperTest.php');
  67. $container->setParameter('buz', dirname(dirname(__DIR__)));
  68. $container->compile();
  69. $dumper = new PhpDumper($container);
  70. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services12.php', $dumper->dump(array('file' => __FILE__)), '->dump() dumps __DIR__ relative strings');
  71. }
  72. /**
  73. * @expectedException \InvalidArgumentException
  74. */
  75. public function testExportParameters()
  76. {
  77. $dumper = new PhpDumper(new ContainerBuilder(new ParameterBag(array('foo' => new Reference('foo')))));
  78. $dumper->dump();
  79. }
  80. public function testAddParameters()
  81. {
  82. $container = include self::$fixturesPath.'/containers/container8.php';
  83. $dumper = new PhpDumper($container);
  84. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services8.php', $dumper->dump(), '->dump() dumps parameters');
  85. }
  86. public function testAddService()
  87. {
  88. // without compilation
  89. $container = include self::$fixturesPath.'/containers/container9.php';
  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.php')), $dumper->dump(), '->dump() dumps services');
  92. // with compilation
  93. $container = include self::$fixturesPath.'/containers/container9.php';
  94. $container->compile();
  95. $dumper = new PhpDumper($container);
  96. $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');
  97. $dumper = new PhpDumper($container = new ContainerBuilder());
  98. $container->register('foo', 'FooClass')->addArgument(new \stdClass());
  99. try {
  100. $dumper->dump();
  101. $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  102. } catch (\Exception $e) {
  103. $this->assertInstanceOf('\Symfony\Component\DependencyInjection\Exception\RuntimeException', $e, '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  104. $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');
  105. }
  106. }
  107. /**
  108. * @group legacy
  109. */
  110. public function testLegacySynchronizedServices()
  111. {
  112. $container = include self::$fixturesPath.'/containers/container20.php';
  113. $dumper = new PhpDumper($container);
  114. $this->assertEquals(str_replace('%path%', str_replace('\\', '\\\\', self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR), file_get_contents(self::$fixturesPath.'/php/services20.php')), $dumper->dump(), '->dump() dumps services');
  115. }
  116. public function testServicesWithAnonymousFactories()
  117. {
  118. $container = include self::$fixturesPath.'/containers/container19.php';
  119. $dumper = new PhpDumper($container);
  120. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services19.php', $dumper->dump(), '->dump() dumps services with anonymous factories');
  121. }
  122. /**
  123. * @expectedException \InvalidArgumentException
  124. * @expectedExceptionMessage Service id "bar$" cannot be converted to a valid PHP method name.
  125. */
  126. public function testAddServiceInvalidServiceId()
  127. {
  128. $container = new ContainerBuilder();
  129. $container->register('bar$', 'FooClass');
  130. $dumper = new PhpDumper($container);
  131. $dumper->dump();
  132. }
  133. public function testAliases()
  134. {
  135. $container = include self::$fixturesPath.'/containers/container9.php';
  136. $container->compile();
  137. $dumper = new PhpDumper($container);
  138. eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Aliases')));
  139. $container = new \Symfony_DI_PhpDumper_Test_Aliases();
  140. $container->set('foo', $foo = new \stdClass());
  141. $this->assertSame($foo, $container->get('foo'));
  142. $this->assertSame($foo, $container->get('alias_for_foo'));
  143. $this->assertSame($foo, $container->get('alias_for_alias'));
  144. }
  145. public function testFrozenContainerWithoutAliases()
  146. {
  147. $container = new ContainerBuilder();
  148. $container->compile();
  149. $dumper = new PhpDumper($container);
  150. eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Frozen_No_Aliases')));
  151. $container = new \Symfony_DI_PhpDumper_Test_Frozen_No_Aliases();
  152. $this->assertFalse($container->has('foo'));
  153. }
  154. public function testOverrideServiceWhenUsingADumpedContainer()
  155. {
  156. require_once self::$fixturesPath.'/php/services9.php';
  157. require_once self::$fixturesPath.'/includes/foo.php';
  158. $container = new \ProjectServiceContainer();
  159. $container->set('bar', $bar = new \stdClass());
  160. $container->setParameter('foo_bar', 'foo_bar');
  161. $this->assertEquals($bar, $container->get('bar'), '->set() overrides an already defined service');
  162. }
  163. public function testOverrideServiceWhenUsingADumpedContainerAndServiceIsUsedFromAnotherOne()
  164. {
  165. require_once self::$fixturesPath.'/php/services9.php';
  166. require_once self::$fixturesPath.'/includes/foo.php';
  167. require_once self::$fixturesPath.'/includes/classes.php';
  168. $container = new \ProjectServiceContainer();
  169. $container->set('bar', $bar = new \stdClass());
  170. $this->assertSame($bar, $container->get('foo')->bar, '->set() overrides an already defined service');
  171. }
  172. /**
  173. * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
  174. */
  175. public function testCircularReference()
  176. {
  177. $container = new ContainerBuilder();
  178. $container->register('foo', 'stdClass')->addArgument(new Reference('bar'));
  179. $container->register('bar', 'stdClass')->setPublic(false)->addMethodCall('setA', array(new Reference('baz')));
  180. $container->register('baz', 'stdClass')->addMethodCall('setA', array(new Reference('foo')));
  181. $container->compile();
  182. $dumper = new PhpDumper($container);
  183. $dumper->dump();
  184. }
  185. }