PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/pr0055/symfonypizza
PHP | 298 lines | 229 code | 47 blank | 22 comment | 0 complexity | a18ecf9ea15d80307a73d2f6e60c8873 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. use Symfony\Component\DependencyInjection\Variable;
  17. use Symfony\Component\ExpressionLanguage\Expression;
  18. class PhpDumperTest extends \PHPUnit_Framework_TestCase
  19. {
  20. protected static $fixturesPath;
  21. public static function setUpBeforeClass()
  22. {
  23. self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
  24. }
  25. public function testDump()
  26. {
  27. $dumper = new PhpDumper($container = new ContainerBuilder());
  28. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services1.php', $dumper->dump(), '->dump() dumps an empty container as an empty PHP class');
  29. $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');
  30. $container = new ContainerBuilder();
  31. new PhpDumper($container);
  32. }
  33. public function testDumpOptimizationString()
  34. {
  35. $definition = new Definition();
  36. $definition->setClass('stdClass');
  37. $definition->addArgument(array(
  38. 'only dot' => '.',
  39. 'concatenation as value' => '.\'\'.',
  40. 'concatenation from the start value' => '\'\'.',
  41. '.' => 'dot as a key',
  42. '.\'\'.' => 'concatenation as a key',
  43. '\'\'.' => 'concatenation from the start key',
  44. 'optimize concatenation' => 'string1%some_string%string2',
  45. 'optimize concatenation with empty string' => 'string1%empty_value%string2',
  46. 'optimize concatenation from the start' => '%empty_value%start',
  47. 'optimize concatenation at the end' => 'end%empty_value%',
  48. ));
  49. $container = new ContainerBuilder();
  50. $container->setResourceTracking(false);
  51. $container->setDefinition('test', $definition);
  52. $container->setParameter('empty_value', '');
  53. $container->setParameter('some_string', '-');
  54. $container->compile();
  55. $dumper = new PhpDumper($container);
  56. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services10.php', $dumper->dump(), '->dump() dumps an empty container as an empty PHP class');
  57. }
  58. public function testDumpRelativeDir()
  59. {
  60. $definition = new Definition();
  61. $definition->setClass('stdClass');
  62. $definition->addArgument('%foo%');
  63. $definition->addArgument(array('%foo%' => '%buz%/'));
  64. $container = new ContainerBuilder();
  65. $container->setDefinition('test', $definition);
  66. $container->setParameter('foo', 'wiz'.dirname(__DIR__));
  67. $container->setParameter('bar', __DIR__);
  68. $container->setParameter('baz', '%bar%/PhpDumperTest.php');
  69. $container->setParameter('buz', dirname(dirname(__DIR__)));
  70. $container->compile();
  71. $dumper = new PhpDumper($container);
  72. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services12.php', $dumper->dump(array('file' => __FILE__)), '->dump() dumps __DIR__ relative strings');
  73. }
  74. /**
  75. * @dataProvider provideInvalidParameters
  76. * @expectedException \InvalidArgumentException
  77. */
  78. public function testExportParameters($parameters)
  79. {
  80. $dumper = new PhpDumper(new ContainerBuilder(new ParameterBag($parameters)));
  81. $dumper->dump();
  82. }
  83. public function provideInvalidParameters()
  84. {
  85. return array(
  86. array(array('foo' => new Definition('stdClass'))),
  87. array(array('foo' => new Expression('service("foo").foo() ~ (container.hasParameter("foo") ? parameter("foo") : "default")'))),
  88. array(array('foo' => new Reference('foo'))),
  89. array(array('foo' => new Variable('foo'))),
  90. );
  91. }
  92. public function testAddParameters()
  93. {
  94. $container = include self::$fixturesPath.'/containers/container8.php';
  95. $dumper = new PhpDumper($container);
  96. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services8.php', $dumper->dump(), '->dump() dumps parameters');
  97. }
  98. public function testAddService()
  99. {
  100. // without compilation
  101. $container = include self::$fixturesPath.'/containers/container9.php';
  102. $dumper = new PhpDumper($container);
  103. $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');
  104. // with compilation
  105. $container = include self::$fixturesPath.'/containers/container9.php';
  106. $container->compile();
  107. $dumper = new PhpDumper($container);
  108. $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');
  109. $dumper = new PhpDumper($container = new ContainerBuilder());
  110. $container->register('foo', 'FooClass')->addArgument(new \stdClass());
  111. try {
  112. $dumper->dump();
  113. $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  114. } catch (\Exception $e) {
  115. $this->assertInstanceOf('\Symfony\Component\DependencyInjection\Exception\RuntimeException', $e, '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  116. $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');
  117. }
  118. }
  119. public function testServicesWithAnonymousFactories()
  120. {
  121. $container = include self::$fixturesPath.'/containers/container19.php';
  122. $dumper = new PhpDumper($container);
  123. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services19.php', $dumper->dump(), '->dump() dumps services with anonymous factories');
  124. }
  125. public function testAddServiceIdWithUnsupportedCharacters()
  126. {
  127. $class = 'Symfony_DI_PhpDumper_Test_Unsupported_Characters';
  128. $container = new ContainerBuilder();
  129. $container->register('bar$', 'FooClass');
  130. $container->register('bar$!', 'FooClass');
  131. $dumper = new PhpDumper($container);
  132. eval('?>'.$dumper->dump(array('class' => $class)));
  133. $this->assertTrue(method_exists($class, 'getBarService'));
  134. $this->assertTrue(method_exists($class, 'getBar2Service'));
  135. }
  136. public function testConflictingServiceIds()
  137. {
  138. $class = 'Symfony_DI_PhpDumper_Test_Conflicting_Service_Ids';
  139. $container = new ContainerBuilder();
  140. $container->register('foo_bar', 'FooClass');
  141. $container->register('foobar', 'FooClass');
  142. $dumper = new PhpDumper($container);
  143. eval('?>'.$dumper->dump(array('class' => $class)));
  144. $this->assertTrue(method_exists($class, 'getFooBarService'));
  145. $this->assertTrue(method_exists($class, 'getFoobar2Service'));
  146. }
  147. public function testConflictingMethodsWithParent()
  148. {
  149. $class = 'Symfony_DI_PhpDumper_Test_Conflicting_Method_With_Parent';
  150. $container = new ContainerBuilder();
  151. $container->register('bar', 'FooClass');
  152. $container->register('foo_bar', 'FooClass');
  153. $dumper = new PhpDumper($container);
  154. eval('?>'.$dumper->dump(array(
  155. 'class' => $class,
  156. 'base_class' => 'Symfony\Component\DependencyInjection\Tests\Fixtures\containers\CustomContainer',
  157. )));
  158. $this->assertTrue(method_exists($class, 'getBar2Service'));
  159. $this->assertTrue(method_exists($class, 'getFoobar2Service'));
  160. }
  161. /**
  162. * @dataProvider provideInvalidFactories
  163. * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
  164. * @expectedExceptionMessage Cannot dump definition
  165. */
  166. public function testInvalidFactories($factory)
  167. {
  168. $container = new ContainerBuilder();
  169. $def = new Definition('stdClass');
  170. $def->setFactory($factory);
  171. $container->setDefinition('bar', $def);
  172. $dumper = new PhpDumper($container);
  173. $dumper->dump();
  174. }
  175. public function provideInvalidFactories()
  176. {
  177. return array(
  178. array(array('', 'method')),
  179. array(array('class', '')),
  180. array(array('...', 'method')),
  181. array(array('class', '...')),
  182. );
  183. }
  184. public function testAliases()
  185. {
  186. $container = include self::$fixturesPath.'/containers/container9.php';
  187. $container->compile();
  188. $dumper = new PhpDumper($container);
  189. eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Aliases')));
  190. $container = new \Symfony_DI_PhpDumper_Test_Aliases();
  191. $container->set('foo', $foo = new \stdClass());
  192. $this->assertSame($foo, $container->get('foo'));
  193. $this->assertSame($foo, $container->get('alias_for_foo'));
  194. $this->assertSame($foo, $container->get('alias_for_alias'));
  195. }
  196. public function testFrozenContainerWithoutAliases()
  197. {
  198. $container = new ContainerBuilder();
  199. $container->compile();
  200. $dumper = new PhpDumper($container);
  201. eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Frozen_No_Aliases')));
  202. $container = new \Symfony_DI_PhpDumper_Test_Frozen_No_Aliases();
  203. $this->assertFalse($container->has('foo'));
  204. }
  205. public function testOverrideServiceWhenUsingADumpedContainer()
  206. {
  207. require_once self::$fixturesPath.'/php/services9.php';
  208. require_once self::$fixturesPath.'/includes/foo.php';
  209. $container = new \ProjectServiceContainer();
  210. $container->set('bar', $bar = new \stdClass());
  211. $container->setParameter('foo_bar', 'foo_bar');
  212. $this->assertEquals($bar, $container->get('bar'), '->set() overrides an already defined service');
  213. }
  214. public function testOverrideServiceWhenUsingADumpedContainerAndServiceIsUsedFromAnotherOne()
  215. {
  216. require_once self::$fixturesPath.'/php/services9.php';
  217. require_once self::$fixturesPath.'/includes/foo.php';
  218. require_once self::$fixturesPath.'/includes/classes.php';
  219. $container = new \ProjectServiceContainer();
  220. $container->set('bar', $bar = new \stdClass());
  221. $this->assertSame($bar, $container->get('foo')->bar, '->set() overrides an already defined service');
  222. }
  223. /**
  224. * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
  225. */
  226. public function testCircularReference()
  227. {
  228. $container = new ContainerBuilder();
  229. $container->register('foo', 'stdClass')->addArgument(new Reference('bar'));
  230. $container->register('bar', 'stdClass')->setPublic(false)->addMethodCall('setA', array(new Reference('baz')));
  231. $container->register('baz', 'stdClass')->addMethodCall('setA', array(new Reference('foo')));
  232. $container->compile();
  233. $dumper = new PhpDumper($container);
  234. $dumper->dump();
  235. }
  236. public function testDumpAutowireData()
  237. {
  238. $container = include self::$fixturesPath.'/containers/container24.php';
  239. $dumper = new PhpDumper($container);
  240. $this->assertEquals(file_get_contents(self::$fixturesPath.'/php/services24.php'), $dumper->dump());
  241. }
  242. public function testInlinedDefinitionReferencingServiceContainer()
  243. {
  244. $container = new ContainerBuilder();
  245. $container->register('foo', 'stdClass')->addMethodCall('add', array(new Reference('service_container')))->setPublic(false);
  246. $container->register('bar', 'stdClass')->addArgument(new Reference('foo'));
  247. $container->compile();
  248. $dumper = new PhpDumper($container);
  249. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services13.php', $dumper->dump(), '->dump() dumps inline definitions which reference service_container');
  250. }
  251. }