06d4c70ad2e08c8fd9d20ca8f0d07e329c7ee5a5
[yaffs-website] / vendor / symfony / dependency-injection / Tests / Dumper / PhpDumperTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\DependencyInjection\Tests\Dumper;
13
14 use DummyProxyDumper;
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;
23
24 require_once __DIR__.'/../Fixtures/includes/classes.php';
25
26 class PhpDumperTest extends TestCase
27 {
28     protected static $fixturesPath;
29
30     public static function setUpBeforeClass()
31     {
32         self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
33     }
34
35     public function testDump()
36     {
37         $dumper = new PhpDumper(new ContainerBuilder());
38
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');
41     }
42
43     public function testDumpOptimizationString()
44     {
45         $definition = new Definition();
46         $definition->setClass('stdClass');
47         $definition->addArgument(array(
48             'only dot' => '.',
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%',
58         ));
59
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();
66
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');
69     }
70
71     public function testDumpRelativeDir()
72     {
73         $definition = new Definition();
74         $definition->setClass('stdClass');
75         $definition->addArgument('%foo%');
76         $definition->addArgument(array('%foo%' => '%buz%/'));
77
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();
85
86         $dumper = new PhpDumper($container);
87         $this->assertStringEqualsFile(self::$fixturesPath.'/php/services12.php', $dumper->dump(array('file' => __FILE__)), '->dump() dumps __DIR__ relative strings');
88     }
89
90     /**
91      * @dataProvider provideInvalidParameters
92      * @expectedException \InvalidArgumentException
93      */
94     public function testExportParameters($parameters)
95     {
96         $dumper = new PhpDumper(new ContainerBuilder(new ParameterBag($parameters)));
97         $dumper->dump();
98     }
99
100     public function provideInvalidParameters()
101     {
102         return array(
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'))),
107         );
108     }
109
110     public function testAddParameters()
111     {
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');
115     }
116
117     public function testAddService()
118     {
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');
123
124         // with compilation
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');
129
130         $dumper = new PhpDumper($container = new ContainerBuilder());
131         $container->register('foo', 'FooClass')->addArgument(new \stdClass());
132         try {
133             $dumper->dump();
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');
138         }
139     }
140
141     /**
142      * @group legacy
143      */
144     public function testLegacySynchronizedServices()
145     {
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');
149     }
150
151     public function testServicesWithAnonymousFactories()
152     {
153         $container = include self::$fixturesPath.'/containers/container19.php';
154         $dumper = new PhpDumper($container);
155
156         $this->assertStringEqualsFile(self::$fixturesPath.'/php/services19.php', $dumper->dump(), '->dump() dumps services with anonymous factories');
157     }
158
159     /**
160      * @expectedException \InvalidArgumentException
161      * @expectedExceptionMessage Service id "bar$" cannot be converted to a valid PHP method name.
162      */
163     public function testAddServiceInvalidServiceId()
164     {
165         $container = new ContainerBuilder();
166         $container->register('bar$', 'FooClass');
167         $dumper = new PhpDumper($container);
168         $dumper->dump();
169     }
170
171     /**
172      * @dataProvider provideInvalidFactories
173      * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
174      * @expectedExceptionMessage Cannot dump definition
175      */
176     public function testInvalidFactories($factory)
177     {
178         $container = new ContainerBuilder();
179         $def = new Definition('stdClass');
180         $def->setFactory($factory);
181         $container->setDefinition('bar', $def);
182         $dumper = new PhpDumper($container);
183         $dumper->dump();
184     }
185
186     public function provideInvalidFactories()
187     {
188         return array(
189             array(array('', 'method')),
190             array(array('class', '')),
191             array(array('...', 'method')),
192             array(array('class', '...')),
193         );
194     }
195
196     public function testAliases()
197     {
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')));
202
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'));
208     }
209
210     public function testFrozenContainerWithoutAliases()
211     {
212         $container = new ContainerBuilder();
213         $container->compile();
214
215         $dumper = new PhpDumper($container);
216         eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Frozen_No_Aliases')));
217
218         $container = new \Symfony_DI_PhpDumper_Test_Frozen_No_Aliases();
219         $this->assertFalse($container->has('foo'));
220     }
221
222     public function testOverrideServiceWhenUsingADumpedContainer()
223     {
224         require_once self::$fixturesPath.'/php/services9.php';
225         require_once self::$fixturesPath.'/includes/foo.php';
226
227         $container = new \ProjectServiceContainer();
228         $container->set('bar', $bar = new \stdClass());
229         $container->setParameter('foo_bar', 'foo_bar');
230
231         $this->assertSame($bar, $container->get('bar'), '->set() overrides an already defined service');
232     }
233
234     public function testOverrideServiceWhenUsingADumpedContainerAndServiceIsUsedFromAnotherOne()
235     {
236         require_once self::$fixturesPath.'/php/services9.php';
237         require_once self::$fixturesPath.'/includes/foo.php';
238         require_once self::$fixturesPath.'/includes/classes.php';
239
240         $container = new \ProjectServiceContainer();
241         $container->set('bar', $bar = new \stdClass());
242
243         $this->assertSame($bar, $container->get('foo')->bar, '->set() overrides an already defined service');
244     }
245
246     /**
247      * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
248      */
249     public function testCircularReference()
250     {
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();
256
257         $dumper = new PhpDumper($container);
258         $dumper->dump();
259     }
260
261     public function testDumpAutowireData()
262     {
263         $container = include self::$fixturesPath.'/containers/container24.php';
264         $dumper = new PhpDumper($container);
265
266         $this->assertEquals(file_get_contents(self::$fixturesPath.'/php/services24.php'), $dumper->dump());
267     }
268
269     public function testInlinedDefinitionReferencingServiceContainer()
270     {
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();
275
276         $dumper = new PhpDumper($container);
277         $this->assertStringEqualsFile(self::$fixturesPath.'/php/services13.php', $dumper->dump(), '->dump() dumps inline definitions which reference service_container');
278     }
279
280     public function testInitializePropertiesBeforeMethodCalls()
281     {
282         require_once self::$fixturesPath.'/includes/classes.php';
283
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();
291
292         $dumper = new PhpDumper($container);
293         eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Properties_Before_Method_Calls')));
294
295         $container = new \Symfony_DI_PhpDumper_Test_Properties_Before_Method_Calls();
296         $this->assertTrue($container->get('bar')->callPassed(), '->dump() initializes properties before method calls');
297     }
298
299     public function testCircularReferenceAllowanceForLazyServices()
300     {
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();
305
306         $dumper = new PhpDumper($container);
307         $dumper->dump();
308
309         $this->addToAssertionCount(1);
310     }
311
312     public function testCircularReferenceAllowanceForInlinedDefinitionsForLazyServices()
313     {
314         /*
315          *   test graph:
316          *              [connection] -> [event_manager] --> [entity_manager](lazy)
317          *                                                           |
318          *                                                           --(call)- addEventListener ("@lazy_service")
319          *
320          *              [lazy_service](lazy) -> [entity_manager](lazy)
321          *
322          */
323
324         $container = new ContainerBuilder();
325
326         $eventManagerDefinition = new Definition('stdClass');
327
328         $connectionDefinition = $container->register('connection', 'stdClass');
329         $connectionDefinition->addArgument($eventManagerDefinition);
330
331         $container->register('entity_manager', 'stdClass')
332             ->setLazy(true)
333             ->addArgument(new Reference('connection'));
334
335         $lazyServiceDefinition = $container->register('lazy_service', 'stdClass');
336         $lazyServiceDefinition->setLazy(true);
337         $lazyServiceDefinition->addArgument(new Reference('entity_manager'));
338
339         $eventManagerDefinition->addMethodCall('addEventListener', array(new Reference('lazy_service')));
340
341         $container->compile();
342
343         $dumper = new PhpDumper($container);
344
345         $dumper->setProxyDumper(new DummyProxyDumper());
346         $dumper->dump();
347
348         $this->addToAssertionCount(1);
349     }
350 }