4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Tests\Dumper;
15 use PHPUnit\Framework\TestCase;
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
17 use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
18 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
19 use Symfony\Component\DependencyInjection\Reference;
20 use Symfony\Component\DependencyInjection\Definition;
21 use Symfony\Component\DependencyInjection\Variable;
22 use Symfony\Component\ExpressionLanguage\Expression;
24 require_once __DIR__.'/../Fixtures/includes/classes.php';
26 class PhpDumperTest extends TestCase
28 protected static $fixturesPath;
30 public static function setUpBeforeClass()
32 self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
35 public function testDump()
37 $dumper = new PhpDumper(new ContainerBuilder());
39 $this->assertStringEqualsFile(self::$fixturesPath.'/php/services1.php', $dumper->dump(), '->dump() dumps an empty container as an empty PHP class');
40 $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');
43 public function testDumpOptimizationString()
45 $definition = new Definition();
46 $definition->setClass('stdClass');
47 $definition->addArgument(array(
49 'concatenation as value' => '.\'\'.',
50 'concatenation from the start value' => '\'\'.',
51 '.' => 'dot as a key',
52 '.\'\'.' => 'concatenation as a key',
53 '\'\'.' => 'concatenation from the start key',
54 'optimize concatenation' => 'string1%some_string%string2',
55 'optimize concatenation with empty string' => 'string1%empty_value%string2',
56 'optimize concatenation from the start' => '%empty_value%start',
57 'optimize concatenation at the end' => 'end%empty_value%',
60 $container = new ContainerBuilder();
61 $container->setResourceTracking(false);
62 $container->setDefinition('test', $definition);
63 $container->setParameter('empty_value', '');
64 $container->setParameter('some_string', '-');
65 $container->compile();
67 $dumper = new PhpDumper($container);
68 $this->assertStringEqualsFile(self::$fixturesPath.'/php/services10.php', $dumper->dump(), '->dump() dumps an empty container as an empty PHP class');
71 public function testDumpRelativeDir()
73 $definition = new Definition();
74 $definition->setClass('stdClass');
75 $definition->addArgument('%foo%');
76 $definition->addArgument(array('%foo%' => '%buz%/'));
78 $container = new ContainerBuilder();
79 $container->setDefinition('test', $definition);
80 $container->setParameter('foo', 'wiz'.dirname(__DIR__));
81 $container->setParameter('bar', __DIR__);
82 $container->setParameter('baz', '%bar%/PhpDumperTest.php');
83 $container->setParameter('buz', dirname(dirname(__DIR__)));
84 $container->compile();
86 $dumper = new PhpDumper($container);
87 $this->assertStringEqualsFile(self::$fixturesPath.'/php/services12.php', $dumper->dump(array('file' => __FILE__)), '->dump() dumps __DIR__ relative strings');
91 * @dataProvider provideInvalidParameters
92 * @expectedException \InvalidArgumentException
94 public function testExportParameters($parameters)
96 $dumper = new PhpDumper(new ContainerBuilder(new ParameterBag($parameters)));
100 public function provideInvalidParameters()
103 array(array('foo' => new Definition('stdClass'))),
104 array(array('foo' => new Expression('service("foo").foo() ~ (container.hasParameter("foo") ? parameter("foo") : "default")'))),
105 array(array('foo' => new Reference('foo'))),
106 array(array('foo' => new Variable('foo'))),
110 public function testAddParameters()
112 $container = include self::$fixturesPath.'/containers/container8.php';
113 $dumper = new PhpDumper($container);
114 $this->assertStringEqualsFile(self::$fixturesPath.'/php/services8.php', $dumper->dump(), '->dump() dumps parameters');
117 public function testAddService()
119 // without compilation
120 $container = include self::$fixturesPath.'/containers/container9.php';
121 $dumper = new PhpDumper($container);
122 $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');
125 $container = include self::$fixturesPath.'/containers/container9.php';
126 $container->compile();
127 $dumper = new PhpDumper($container);
128 $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');
130 $dumper = new PhpDumper($container = new ContainerBuilder());
131 $container->register('foo', 'FooClass')->addArgument(new \stdClass());
134 $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
135 } catch (\Exception $e) {
136 $this->assertInstanceOf('\Symfony\Component\DependencyInjection\Exception\RuntimeException', $e, '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
137 $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');
144 public function testLegacySynchronizedServices()
146 $container = include self::$fixturesPath.'/containers/container20.php';
147 $dumper = new PhpDumper($container);
148 $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');
151 public function testServicesWithAnonymousFactories()
153 $container = include self::$fixturesPath.'/containers/container19.php';
154 $dumper = new PhpDumper($container);
156 $this->assertStringEqualsFile(self::$fixturesPath.'/php/services19.php', $dumper->dump(), '->dump() dumps services with anonymous factories');
160 * @expectedException \InvalidArgumentException
161 * @expectedExceptionMessage Service id "bar$" cannot be converted to a valid PHP method name.
163 public function testAddServiceInvalidServiceId()
165 $container = new ContainerBuilder();
166 $container->register('bar$', 'FooClass');
167 $dumper = new PhpDumper($container);
172 * @dataProvider provideInvalidFactories
173 * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
174 * @expectedExceptionMessage Cannot dump definition
176 public function testInvalidFactories($factory)
178 $container = new ContainerBuilder();
179 $def = new Definition('stdClass');
180 $def->setFactory($factory);
181 $container->setDefinition('bar', $def);
182 $dumper = new PhpDumper($container);
186 public function provideInvalidFactories()
189 array(array('', 'method')),
190 array(array('class', '')),
191 array(array('...', 'method')),
192 array(array('class', '...')),
196 public function testAliases()
198 $container = include self::$fixturesPath.'/containers/container9.php';
199 $container->compile();
200 $dumper = new PhpDumper($container);
201 eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Aliases')));
203 $container = new \Symfony_DI_PhpDumper_Test_Aliases();
204 $container->set('foo', $foo = new \stdClass());
205 $this->assertSame($foo, $container->get('foo'));
206 $this->assertSame($foo, $container->get('alias_for_foo'));
207 $this->assertSame($foo, $container->get('alias_for_alias'));
210 public function testFrozenContainerWithoutAliases()
212 $container = new ContainerBuilder();
213 $container->compile();
215 $dumper = new PhpDumper($container);
216 eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Frozen_No_Aliases')));
218 $container = new \Symfony_DI_PhpDumper_Test_Frozen_No_Aliases();
219 $this->assertFalse($container->has('foo'));
222 public function testOverrideServiceWhenUsingADumpedContainer()
224 require_once self::$fixturesPath.'/php/services9.php';
225 require_once self::$fixturesPath.'/includes/foo.php';
227 $container = new \ProjectServiceContainer();
228 $container->set('bar', $bar = new \stdClass());
229 $container->setParameter('foo_bar', 'foo_bar');
231 $this->assertSame($bar, $container->get('bar'), '->set() overrides an already defined service');
234 public function testOverrideServiceWhenUsingADumpedContainerAndServiceIsUsedFromAnotherOne()
236 require_once self::$fixturesPath.'/php/services9.php';
237 require_once self::$fixturesPath.'/includes/foo.php';
238 require_once self::$fixturesPath.'/includes/classes.php';
240 $container = new \ProjectServiceContainer();
241 $container->set('bar', $bar = new \stdClass());
243 $this->assertSame($bar, $container->get('foo')->bar, '->set() overrides an already defined service');
247 * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
249 public function testCircularReference()
251 $container = new ContainerBuilder();
252 $container->register('foo', 'stdClass')->addArgument(new Reference('bar'));
253 $container->register('bar', 'stdClass')->setPublic(false)->addMethodCall('setA', array(new Reference('baz')));
254 $container->register('baz', 'stdClass')->addMethodCall('setA', array(new Reference('foo')));
255 $container->compile();
257 $dumper = new PhpDumper($container);
261 public function testDumpAutowireData()
263 $container = include self::$fixturesPath.'/containers/container24.php';
264 $dumper = new PhpDumper($container);
266 $this->assertEquals(file_get_contents(self::$fixturesPath.'/php/services24.php'), $dumper->dump());
269 public function testInlinedDefinitionReferencingServiceContainer()
271 $container = new ContainerBuilder();
272 $container->register('foo', 'stdClass')->addMethodCall('add', array(new Reference('service_container')))->setPublic(false);
273 $container->register('bar', 'stdClass')->addArgument(new Reference('foo'));
274 $container->compile();
276 $dumper = new PhpDumper($container);
277 $this->assertStringEqualsFile(self::$fixturesPath.'/php/services13.php', $dumper->dump(), '->dump() dumps inline definitions which reference service_container');
280 public function testInitializePropertiesBeforeMethodCalls()
282 require_once self::$fixturesPath.'/includes/classes.php';
284 $container = new ContainerBuilder();
285 $container->register('foo', 'stdClass');
286 $container->register('bar', 'MethodCallClass')
287 ->setProperty('simple', 'bar')
288 ->setProperty('complex', new Reference('foo'))
289 ->addMethodCall('callMe');
290 $container->compile();
292 $dumper = new PhpDumper($container);
293 eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Properties_Before_Method_Calls')));
295 $container = new \Symfony_DI_PhpDumper_Test_Properties_Before_Method_Calls();
296 $this->assertTrue($container->get('bar')->callPassed(), '->dump() initializes properties before method calls');
299 public function testCircularReferenceAllowanceForLazyServices()
301 $container = new ContainerBuilder();
302 $container->register('foo', 'stdClass')->addArgument(new Reference('bar'));
303 $container->register('bar', 'stdClass')->setLazy(true)->addArgument(new Reference('foo'));
304 $container->compile();
306 $dumper = new PhpDumper($container);
309 $this->addToAssertionCount(1);
312 public function testCircularReferenceAllowanceForInlinedDefinitionsForLazyServices()
316 * [connection] -> [event_manager] --> [entity_manager](lazy)
318 * --(call)- addEventListener ("@lazy_service")
320 * [lazy_service](lazy) -> [entity_manager](lazy)
324 $container = new ContainerBuilder();
326 $eventManagerDefinition = new Definition('stdClass');
328 $connectionDefinition = $container->register('connection', 'stdClass');
329 $connectionDefinition->addArgument($eventManagerDefinition);
331 $container->register('entity_manager', 'stdClass')
333 ->addArgument(new Reference('connection'));
335 $lazyServiceDefinition = $container->register('lazy_service', 'stdClass');
336 $lazyServiceDefinition->setLazy(true);
337 $lazyServiceDefinition->addArgument(new Reference('entity_manager'));
339 $eventManagerDefinition->addMethodCall('addEventListener', array(new Reference('lazy_service')));
341 $container->compile();
343 $dumper = new PhpDumper($container);
345 $dumper->setProxyDumper(new DummyProxyDumper());
348 $this->addToAssertionCount(1);