db backup prior to drupal security update
[yaffs-website] / vendor / symfony / dependency-injection / Tests / ContainerTest.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;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\Scope;
16 use Symfony\Component\DependencyInjection\Container;
17 use Symfony\Component\DependencyInjection\ContainerInterface;
18 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
19 use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
20
21 class ContainerTest extends TestCase
22 {
23     public function testConstructor()
24     {
25         $sc = new Container();
26         $this->assertSame($sc, $sc->get('service_container'), '__construct() automatically registers itself as a service');
27
28         $sc = new Container(new ParameterBag(array('foo' => 'bar')));
29         $this->assertEquals(array('foo' => 'bar'), $sc->getParameterBag()->all(), '__construct() takes an array of parameters as its first argument');
30     }
31
32     /**
33      * @dataProvider dataForTestCamelize
34      */
35     public function testCamelize($id, $expected)
36     {
37         $this->assertEquals($expected, Container::camelize($id), sprintf('Container::camelize("%s")', $id));
38     }
39
40     public function dataForTestCamelize()
41     {
42         return array(
43             array('foo_bar', 'FooBar'),
44             array('foo.bar', 'Foo_Bar'),
45             array('foo.bar_baz', 'Foo_BarBaz'),
46             array('foo._bar', 'Foo_Bar'),
47             array('foo_.bar', 'Foo_Bar'),
48             array('_foo', 'Foo'),
49             array('.foo', '_Foo'),
50             array('foo_', 'Foo'),
51             array('foo.', 'Foo_'),
52             array('foo\bar', 'Foo_Bar'),
53         );
54     }
55
56     /**
57      * @dataProvider dataForTestUnderscore
58      */
59     public function testUnderscore($id, $expected)
60     {
61         $this->assertEquals($expected, Container::underscore($id), sprintf('Container::underscore("%s")', $id));
62     }
63
64     public function dataForTestUnderscore()
65     {
66         return array(
67             array('FooBar', 'foo_bar'),
68             array('Foo_Bar', 'foo.bar'),
69             array('Foo_BarBaz', 'foo.bar_baz'),
70             array('FooBar_BazQux', 'foo_bar.baz_qux'),
71             array('_Foo', '.foo'),
72             array('Foo_', 'foo.'),
73         );
74     }
75
76     public function testCompile()
77     {
78         $sc = new Container(new ParameterBag(array('foo' => 'bar')));
79         $this->assertFalse($sc->getParameterBag()->isResolved(), '->compile() resolves the parameter bag');
80         $sc->compile();
81         $this->assertTrue($sc->getParameterBag()->isResolved(), '->compile() resolves the parameter bag');
82         $this->assertInstanceOf('Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag', $sc->getParameterBag(), '->compile() changes the parameter bag to a FrozenParameterBag instance');
83         $this->assertEquals(array('foo' => 'bar'), $sc->getParameterBag()->all(), '->compile() copies the current parameters to the new parameter bag');
84     }
85
86     public function testIsFrozen()
87     {
88         $sc = new Container(new ParameterBag(array('foo' => 'bar')));
89         $this->assertFalse($sc->isFrozen(), '->isFrozen() returns false if the parameters are not frozen');
90         $sc->compile();
91         $this->assertTrue($sc->isFrozen(), '->isFrozen() returns true if the parameters are frozen');
92     }
93
94     public function testGetParameterBag()
95     {
96         $sc = new Container();
97         $this->assertEquals(array(), $sc->getParameterBag()->all(), '->getParameterBag() returns an empty array if no parameter has been defined');
98     }
99
100     public function testGetSetParameter()
101     {
102         $sc = new Container(new ParameterBag(array('foo' => 'bar')));
103         $sc->setParameter('bar', 'foo');
104         $this->assertEquals('foo', $sc->getParameter('bar'), '->setParameter() sets the value of a new parameter');
105
106         $sc->setParameter('foo', 'baz');
107         $this->assertEquals('baz', $sc->getParameter('foo'), '->setParameter() overrides previously set parameter');
108
109         $sc->setParameter('Foo', 'baz1');
110         $this->assertEquals('baz1', $sc->getParameter('foo'), '->setParameter() converts the key to lowercase');
111         $this->assertEquals('baz1', $sc->getParameter('FOO'), '->getParameter() converts the key to lowercase');
112
113         try {
114             $sc->getParameter('baba');
115             $this->fail('->getParameter() thrown an \InvalidArgumentException if the key does not exist');
116         } catch (\Exception $e) {
117             $this->assertInstanceOf('\InvalidArgumentException', $e, '->getParameter() thrown an \InvalidArgumentException if the key does not exist');
118             $this->assertEquals('You have requested a non-existent parameter "baba".', $e->getMessage(), '->getParameter() thrown an \InvalidArgumentException if the key does not exist');
119         }
120     }
121
122     public function testGetServiceIds()
123     {
124         $sc = new Container();
125         $sc->set('foo', $obj = new \stdClass());
126         $sc->set('bar', $obj = new \stdClass());
127         $this->assertEquals(array('service_container', 'foo', 'bar'), $sc->getServiceIds(), '->getServiceIds() returns all defined service ids');
128
129         $sc = new ProjectServiceContainer();
130         $sc->set('foo', $obj = new \stdClass());
131         $this->assertEquals(array('scoped', 'scoped_foo', 'scoped_synchronized_foo', 'inactive', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'service_container', 'foo'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by getXXXService() methods, followed by service ids defined by set()');
132     }
133
134     public function testSet()
135     {
136         $sc = new Container();
137         $sc->set('foo', $foo = new \stdClass());
138         $this->assertSame($foo, $sc->get('foo'), '->set() sets a service');
139     }
140
141     public function testSetWithNullResetTheService()
142     {
143         $sc = new Container();
144         $sc->set('foo', null);
145         $this->assertFalse($sc->has('foo'), '->set() with null service resets the service');
146     }
147
148     /**
149      * @expectedException \InvalidArgumentException
150      * @group legacy
151      */
152     public function testSetDoesNotAllowPrototypeScope()
153     {
154         $c = new Container();
155         $c->set('foo', new \stdClass(), Container::SCOPE_PROTOTYPE);
156     }
157
158     /**
159      * @expectedException \RuntimeException
160      * @group legacy
161      */
162     public function testSetDoesNotAllowInactiveScope()
163     {
164         $c = new Container();
165         $c->addScope(new Scope('foo'));
166         $c->set('foo', new \stdClass(), 'foo');
167     }
168
169     /**
170      * @group legacy
171      */
172     public function testSetAlsoSetsScopedService()
173     {
174         $c = new Container();
175         $c->addScope(new Scope('foo'));
176         $c->enterScope('foo');
177         $c->set('foo', $foo = new \stdClass(), 'foo');
178
179         $scoped = $this->getField($c, 'scopedServices');
180         $this->assertTrue(isset($scoped['foo']['foo']), '->set() sets a scoped service');
181         $this->assertSame($foo, $scoped['foo']['foo'], '->set() sets a scoped service');
182     }
183
184     /**
185      * @group legacy
186      */
187     public function testSetAlsoCallsSynchronizeService()
188     {
189         $c = new ProjectServiceContainer();
190         $c->addScope(new Scope('foo'));
191         $c->enterScope('foo');
192         $c->set('scoped_synchronized_foo', $bar = new \stdClass(), 'foo');
193         $this->assertTrue($c->synchronized, '->set() calls synchronize*Service() if it is defined for the service');
194     }
195
196     public function testSetReplacesAlias()
197     {
198         $c = new ProjectServiceContainer();
199
200         $c->set('alias', $foo = new \stdClass());
201         $this->assertSame($foo, $c->get('alias'), '->set() replaces an existing alias');
202     }
203
204     public function testGet()
205     {
206         $sc = new ProjectServiceContainer();
207         $sc->set('foo', $foo = new \stdClass());
208         $this->assertSame($foo, $sc->get('foo'), '->get() returns the service for the given id');
209         $this->assertSame($foo, $sc->get('Foo'), '->get() returns the service for the given id, and converts id to lowercase');
210         $this->assertSame($sc->__bar, $sc->get('bar'), '->get() returns the service for the given id');
211         $this->assertSame($sc->__foo_bar, $sc->get('foo_bar'), '->get() returns the service if a get*Method() is defined');
212         $this->assertSame($sc->__foo_baz, $sc->get('foo.baz'), '->get() returns the service if a get*Method() is defined');
213         $this->assertSame($sc->__foo_baz, $sc->get('foo\\baz'), '->get() returns the service if a get*Method() is defined');
214
215         $sc->set('bar', $bar = new \stdClass());
216         $this->assertSame($bar, $sc->get('bar'), '->get() prefers to return a service defined with set() than one defined with a getXXXMethod()');
217
218         try {
219             $sc->get('');
220             $this->fail('->get() throws a \InvalidArgumentException exception if the service is empty');
221         } catch (\Exception $e) {
222             $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException', $e, '->get() throws a ServiceNotFoundException exception if the service is empty');
223         }
224         $this->assertNull($sc->get('', ContainerInterface::NULL_ON_INVALID_REFERENCE), '->get() returns null if the service is empty');
225     }
226
227     public function testGetThrowServiceNotFoundException()
228     {
229         $sc = new ProjectServiceContainer();
230         $sc->set('foo', $foo = new \stdClass());
231         $sc->set('baz', $foo = new \stdClass());
232
233         try {
234             $sc->get('foo1');
235             $this->fail('->get() throws an Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException if the key does not exist');
236         } catch (\Exception $e) {
237             $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException', $e, '->get() throws an Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException if the key does not exist');
238             $this->assertEquals('You have requested a non-existent service "foo1". Did you mean this: "foo"?', $e->getMessage(), '->get() throws an Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException with some advices');
239         }
240
241         try {
242             $sc->get('bag');
243             $this->fail('->get() throws an Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException if the key does not exist');
244         } catch (\Exception $e) {
245             $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException', $e, '->get() throws an Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException if the key does not exist');
246             $this->assertEquals('You have requested a non-existent service "bag". Did you mean one of these: "bar", "baz"?', $e->getMessage(), '->get() throws an Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException with some advices');
247         }
248     }
249
250     public function testGetCircularReference()
251     {
252         $sc = new ProjectServiceContainer();
253         try {
254             $sc->get('circular');
255             $this->fail('->get() throws a ServiceCircularReferenceException if it contains circular reference');
256         } catch (\Exception $e) {
257             $this->assertInstanceOf('\Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException', $e, '->get() throws a ServiceCircularReferenceException if it contains circular reference');
258             $this->assertStringStartsWith('Circular reference detected for service "circular"', $e->getMessage(), '->get() throws a \LogicException if it contains circular reference');
259         }
260     }
261
262     /**
263      * @group legacy
264      */
265     public function testGetReturnsNullOnInactiveScope()
266     {
267         $sc = new ProjectServiceContainer();
268         $this->assertNull($sc->get('inactive', ContainerInterface::NULL_ON_INVALID_REFERENCE));
269     }
270
271     /**
272      * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
273      * @expectedExceptionMessage You have requested a synthetic service ("request"). The DIC does not know how to construct this service.
274      */
275     public function testGetSyntheticServiceAlwaysThrows()
276     {
277         require_once __DIR__.'/Fixtures/php/services9.php';
278
279         $container = new \ProjectServiceContainer();
280         $container->get('request', ContainerInterface::NULL_ON_INVALID_REFERENCE);
281     }
282
283     public function testHas()
284     {
285         $sc = new ProjectServiceContainer();
286         $sc->set('foo', new \stdClass());
287         $this->assertFalse($sc->has('foo1'), '->has() returns false if the service does not exist');
288         $this->assertTrue($sc->has('foo'), '->has() returns true if the service exists');
289         $this->assertTrue($sc->has('bar'), '->has() returns true if a get*Method() is defined');
290         $this->assertTrue($sc->has('foo_bar'), '->has() returns true if a get*Method() is defined');
291         $this->assertTrue($sc->has('foo.baz'), '->has() returns true if a get*Method() is defined');
292         $this->assertTrue($sc->has('foo\\baz'), '->has() returns true if a get*Method() is defined');
293     }
294
295     public function testInitialized()
296     {
297         $sc = new ProjectServiceContainer();
298         $sc->set('foo', new \stdClass());
299         $this->assertTrue($sc->initialized('foo'), '->initialized() returns true if service is loaded');
300         $this->assertFalse($sc->initialized('foo1'), '->initialized() returns false if service is not loaded');
301         $this->assertFalse($sc->initialized('bar'), '->initialized() returns false if a service is defined, but not currently loaded');
302         $this->assertFalse($sc->initialized('alias'), '->initialized() returns false if an aliased service is not initialized');
303
304         $sc->set('bar', new \stdClass());
305         $this->assertTrue($sc->initialized('alias'), '->initialized() returns true for alias if aliased service is initialized');
306     }
307
308     public function testReset()
309     {
310         $c = new Container();
311         $c->set('bar', new \stdClass());
312
313         $c->reset();
314
315         $this->assertNull($c->get('bar', ContainerInterface::NULL_ON_INVALID_REFERENCE));
316     }
317
318     /**
319      * @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException
320      * @expectedExceptionMessage Resetting the container is not allowed when a scope is active.
321      * @group legacy
322      */
323     public function testCannotResetInActiveScope()
324     {
325         $c = new Container();
326         $c->addScope(new Scope('foo'));
327         $c->set('bar', new \stdClass());
328
329         $c->enterScope('foo');
330
331         $c->reset();
332     }
333
334     /**
335      * @group legacy
336      */
337     public function testResetAfterLeavingScope()
338     {
339         $c = new Container();
340         $c->addScope(new Scope('foo'));
341         $c->set('bar', new \stdClass());
342
343         $c->enterScope('foo');
344         $c->leaveScope('foo');
345
346         $c->reset();
347
348         $this->assertNull($c->get('bar', ContainerInterface::NULL_ON_INVALID_REFERENCE));
349     }
350
351     /**
352      * @group legacy
353      */
354     public function testEnterLeaveCurrentScope()
355     {
356         $container = new ProjectServiceContainer();
357         $container->addScope(new Scope('foo'));
358
359         $container->enterScope('foo');
360         $container->set('foo', new \stdClass(), 'foo');
361         $scoped1 = $container->get('scoped');
362         $scopedFoo1 = $container->get('scoped_foo');
363
364         $container->enterScope('foo');
365         $container->set('foo', new \stdClass(), 'foo');
366         $scoped2 = $container->get('scoped');
367         $scoped3 = $container->get('SCOPED');
368         $scopedFoo2 = $container->get('scoped_foo');
369
370         $container->set('foo', null, 'foo');
371         $container->leaveScope('foo');
372         $scoped4 = $container->get('scoped');
373         $scopedFoo3 = $container->get('scoped_foo');
374
375         $this->assertNotSame($scoped1, $scoped2);
376         $this->assertSame($scoped2, $scoped3);
377         $this->assertSame($scoped1, $scoped4);
378         $this->assertNotSame($scopedFoo1, $scopedFoo2);
379         $this->assertSame($scopedFoo1, $scopedFoo3);
380     }
381
382     /**
383      * @group legacy
384      */
385     public function testEnterLeaveScopeWithChildScopes()
386     {
387         $container = new Container();
388         $container->addScope(new Scope('foo'));
389         $container->addScope(new Scope('bar', 'foo'));
390
391         $this->assertFalse($container->isScopeActive('foo'));
392
393         $container->enterScope('foo');
394         $container->enterScope('bar');
395
396         $this->assertTrue($container->isScopeActive('foo'));
397         $this->assertFalse($container->has('a'));
398
399         $a = new \stdClass();
400         $container->set('a', $a, 'bar');
401
402         $scoped = $this->getField($container, 'scopedServices');
403         $this->assertTrue(isset($scoped['bar']['a']));
404         $this->assertSame($a, $scoped['bar']['a']);
405         $this->assertTrue($container->has('a'));
406
407         $container->leaveScope('foo');
408
409         $scoped = $this->getField($container, 'scopedServices');
410         $this->assertFalse(isset($scoped['bar']));
411         $this->assertFalse($container->isScopeActive('foo'));
412         $this->assertFalse($container->has('a'));
413     }
414
415     /**
416      * @group legacy
417      */
418     public function testEnterScopeRecursivelyWithInactiveChildScopes()
419     {
420         $container = new Container();
421         $container->addScope(new Scope('foo'));
422         $container->addScope(new Scope('bar', 'foo'));
423
424         $this->assertFalse($container->isScopeActive('foo'));
425
426         $container->enterScope('foo');
427
428         $this->assertTrue($container->isScopeActive('foo'));
429         $this->assertFalse($container->isScopeActive('bar'));
430         $this->assertFalse($container->has('a'));
431
432         $a = new \stdClass();
433         $container->set('a', $a, 'foo');
434
435         $scoped = $this->getField($container, 'scopedServices');
436         $this->assertTrue(isset($scoped['foo']['a']));
437         $this->assertSame($a, $scoped['foo']['a']);
438         $this->assertTrue($container->has('a'));
439
440         $container->enterScope('foo');
441
442         $scoped = $this->getField($container, 'scopedServices');
443         $this->assertFalse(isset($scoped['a']));
444         $this->assertTrue($container->isScopeActive('foo'));
445         $this->assertFalse($container->isScopeActive('bar'));
446         $this->assertFalse($container->has('a'));
447
448         $container->enterScope('bar');
449
450         $this->assertTrue($container->isScopeActive('bar'));
451
452         $container->leaveScope('foo');
453
454         $this->assertTrue($container->isScopeActive('foo'));
455         $this->assertFalse($container->isScopeActive('bar'));
456         $this->assertTrue($container->has('a'));
457     }
458
459     /**
460      * @group legacy
461      */
462     public function testEnterChildScopeRecursively()
463     {
464         $container = new Container();
465         $container->addScope(new Scope('foo'));
466         $container->addScope(new Scope('bar', 'foo'));
467
468         $container->enterScope('foo');
469         $container->enterScope('bar');
470
471         $this->assertTrue($container->isScopeActive('bar'));
472         $this->assertFalse($container->has('a'));
473
474         $a = new \stdClass();
475         $container->set('a', $a, 'bar');
476
477         $scoped = $this->getField($container, 'scopedServices');
478         $this->assertTrue(isset($scoped['bar']['a']));
479         $this->assertSame($a, $scoped['bar']['a']);
480         $this->assertTrue($container->has('a'));
481
482         $container->enterScope('bar');
483
484         $scoped = $this->getField($container, 'scopedServices');
485         $this->assertFalse(isset($scoped['a']));
486         $this->assertTrue($container->isScopeActive('foo'));
487         $this->assertTrue($container->isScopeActive('bar'));
488         $this->assertFalse($container->has('a'));
489
490         $container->leaveScope('bar');
491
492         $this->assertTrue($container->isScopeActive('foo'));
493         $this->assertTrue($container->isScopeActive('bar'));
494         $this->assertTrue($container->has('a'));
495     }
496
497     /**
498      * @expectedException \InvalidArgumentException
499      * @group legacy
500      */
501     public function testEnterScopeNotAdded()
502     {
503         $container = new Container();
504         $container->enterScope('foo');
505     }
506
507     /**
508      * @expectedException \RuntimeException
509      * @group legacy
510      */
511     public function testEnterScopeDoesNotAllowInactiveParentScope()
512     {
513         $container = new Container();
514         $container->addScope(new Scope('foo'));
515         $container->addScope(new Scope('bar', 'foo'));
516         $container->enterScope('bar');
517     }
518
519     /**
520      * @group legacy
521      */
522     public function testLeaveScopeNotActive()
523     {
524         $container = new Container();
525         $container->addScope(new Scope('foo'));
526
527         try {
528             $container->leaveScope('foo');
529             $this->fail('->leaveScope() throws a \LogicException if the scope is not active yet');
530         } catch (\Exception $e) {
531             $this->assertInstanceOf('\LogicException', $e, '->leaveScope() throws a \LogicException if the scope is not active yet');
532             $this->assertEquals('The scope "foo" is not active.', $e->getMessage(), '->leaveScope() throws a \LogicException if the scope is not active yet');
533         }
534
535         try {
536             $container->leaveScope('bar');
537             $this->fail('->leaveScope() throws a \LogicException if the scope does not exist');
538         } catch (\Exception $e) {
539             $this->assertInstanceOf('\LogicException', $e, '->leaveScope() throws a \LogicException if the scope does not exist');
540             $this->assertEquals('The scope "bar" is not active.', $e->getMessage(), '->leaveScope() throws a \LogicException if the scope does not exist');
541         }
542     }
543
544     /**
545      * @expectedException \InvalidArgumentException
546      * @dataProvider getLegacyBuiltInScopes
547      * @group legacy
548      */
549     public function testAddScopeDoesNotAllowBuiltInScopes($scope)
550     {
551         $container = new Container();
552         $container->addScope(new Scope($scope));
553     }
554
555     /**
556      * @expectedException \InvalidArgumentException
557      * @group legacy
558      */
559     public function testAddScopeDoesNotAllowExistingScope()
560     {
561         $container = new Container();
562         $container->addScope(new Scope('foo'));
563         $container->addScope(new Scope('foo'));
564     }
565
566     /**
567      * @expectedException \InvalidArgumentException
568      * @dataProvider getLegacyInvalidParentScopes
569      * @group legacy
570      */
571     public function testAddScopeDoesNotAllowInvalidParentScope($scope)
572     {
573         $c = new Container();
574         $c->addScope(new Scope('foo', $scope));
575     }
576
577     /**
578      * @group legacy
579      */
580     public function testAddScope()
581     {
582         $c = new Container();
583         $c->addScope(new Scope('foo'));
584         $c->addScope(new Scope('bar', 'foo'));
585
586         $this->assertSame(array('foo' => 'container', 'bar' => 'foo'), $this->getField($c, 'scopes'));
587         $this->assertSame(array('foo' => array('bar'), 'bar' => array()), $this->getField($c, 'scopeChildren'));
588
589         $c->addScope(new Scope('baz', 'bar'));
590
591         $this->assertSame(array('foo' => 'container', 'bar' => 'foo', 'baz' => 'bar'), $this->getField($c, 'scopes'));
592         $this->assertSame(array('foo' => array('bar', 'baz'), 'bar' => array('baz'), 'baz' => array()), $this->getField($c, 'scopeChildren'));
593     }
594
595     /**
596      * @group legacy
597      */
598     public function testHasScope()
599     {
600         $c = new Container();
601
602         $this->assertFalse($c->hasScope('foo'));
603         $c->addScope(new Scope('foo'));
604         $this->assertTrue($c->hasScope('foo'));
605     }
606
607     /**
608      * @expectedException \Exception
609      * @expectedExceptionMessage Something went terribly wrong!
610      */
611     public function testGetThrowsException()
612     {
613         $c = new ProjectServiceContainer();
614
615         try {
616             $c->get('throw_exception');
617         } catch (\Exception $e) {
618             // Do nothing.
619         }
620
621         // Retry, to make sure that get*Service() will be called.
622         $c->get('throw_exception');
623     }
624
625     public function testGetThrowsExceptionOnServiceConfiguration()
626     {
627         $c = new ProjectServiceContainer();
628
629         try {
630             $c->get('throws_exception_on_service_configuration');
631         } catch (\Exception $e) {
632             // Do nothing.
633         }
634
635         $this->assertFalse($c->initialized('throws_exception_on_service_configuration'));
636
637         // Retry, to make sure that get*Service() will be called.
638         try {
639             $c->get('throws_exception_on_service_configuration');
640         } catch (\Exception $e) {
641             // Do nothing.
642         }
643         $this->assertFalse($c->initialized('throws_exception_on_service_configuration'));
644     }
645
646     /**
647      * @group legacy
648      */
649     public function testIsScopeActive()
650     {
651         $c = new Container();
652
653         $this->assertFalse($c->isScopeActive('foo'));
654         $c->addScope(new Scope('foo'));
655
656         $this->assertFalse($c->isScopeActive('foo'));
657         $c->enterScope('foo');
658
659         $this->assertTrue($c->isScopeActive('foo'));
660         $c->leaveScope('foo');
661
662         $this->assertFalse($c->isScopeActive('foo'));
663     }
664
665     public function getLegacyInvalidParentScopes()
666     {
667         return array(
668             array(ContainerInterface::SCOPE_PROTOTYPE),
669             array('bar'),
670         );
671     }
672
673     public function getLegacyBuiltInScopes()
674     {
675         return array(
676             array(ContainerInterface::SCOPE_CONTAINER),
677             array(ContainerInterface::SCOPE_PROTOTYPE),
678         );
679     }
680
681     protected function getField($obj, $field)
682     {
683         $reflection = new \ReflectionProperty($obj, $field);
684         $reflection->setAccessible(true);
685
686         return $reflection->getValue($obj);
687     }
688
689     public function testAlias()
690     {
691         $c = new ProjectServiceContainer();
692
693         $this->assertTrue($c->has('alias'));
694         $this->assertSame($c->get('alias'), $c->get('bar'));
695     }
696
697     public function testThatCloningIsNotSupported()
698     {
699         $class = new \ReflectionClass('Symfony\Component\DependencyInjection\Container');
700         $clone = $class->getMethod('__clone');
701         if (\PHP_VERSION_ID >= 50400) {
702             $this->assertFalse($class->isCloneable());
703         }
704         $this->assertTrue($clone->isPrivate());
705     }
706 }
707
708 class ProjectServiceContainer extends Container
709 {
710     public $__bar;
711     public $__foo_bar;
712     public $__foo_baz;
713     public $synchronized;
714
715     public function __construct()
716     {
717         parent::__construct();
718
719         $this->__bar = new \stdClass();
720         $this->__foo_bar = new \stdClass();
721         $this->__foo_baz = new \stdClass();
722         $this->synchronized = false;
723         $this->aliases = array('alias' => 'bar');
724     }
725
726     protected function getScopedService()
727     {
728         if (!$this->isScopeActive('foo')) {
729             throw new \RuntimeException('Invalid call');
730         }
731
732         return $this->services['scoped'] = $this->scopedServices['foo']['scoped'] = new \stdClass();
733     }
734
735     protected function getScopedFooService()
736     {
737         if (!$this->isScopeActive('foo')) {
738             throw new \RuntimeException('invalid call');
739         }
740
741         return $this->services['scoped_foo'] = $this->scopedServices['foo']['scoped_foo'] = new \stdClass();
742     }
743
744     protected function getScopedSynchronizedFooService()
745     {
746         if (!$this->isScopeActive('foo')) {
747             throw new \RuntimeException('invalid call');
748         }
749
750         return $this->services['scoped_bar'] = $this->scopedServices['foo']['scoped_bar'] = new \stdClass();
751     }
752
753     protected function synchronizeFooService()
754     {
755         // Typically get the service to pass it to a setter
756         $this->get('foo');
757     }
758
759     protected function synchronizeScopedSynchronizedFooService()
760     {
761         $this->synchronized = true;
762     }
763
764     protected function getInactiveService()
765     {
766         throw new InactiveScopeException('request', 'request');
767     }
768
769     protected function getBarService()
770     {
771         return $this->__bar;
772     }
773
774     protected function getFooBarService()
775     {
776         return $this->__foo_bar;
777     }
778
779     protected function getFoo_BazService()
780     {
781         return $this->__foo_baz;
782     }
783
784     protected function getCircularService()
785     {
786         return $this->get('circular');
787     }
788
789     protected function getThrowExceptionService()
790     {
791         throw new \Exception('Something went terribly wrong!');
792     }
793
794     protected function getThrowsExceptionOnServiceConfigurationService()
795     {
796         $this->services['throws_exception_on_service_configuration'] = $instance = new \stdClass();
797
798         throw new \Exception('Something was terribly wrong while trying to configure the service!');
799     }
800 }