56fac643eb40b05a8ff2ee6f1aeb2b83921340e2
[yaffs-website] / vendor / symfony / dependency-injection / Tests / ServiceLocatorTest.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\Container;
16 use Symfony\Component\DependencyInjection\ServiceLocator;
17 use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
18
19 class ServiceLocatorTest extends TestCase
20 {
21     public function testHas()
22     {
23         $locator = new ServiceLocator(array(
24             'foo' => function () { return 'bar'; },
25             'bar' => function () { return 'baz'; },
26             function () { return 'dummy'; },
27         ));
28
29         $this->assertTrue($locator->has('foo'));
30         $this->assertTrue($locator->has('bar'));
31         $this->assertFalse($locator->has('dummy'));
32     }
33
34     public function testGet()
35     {
36         $locator = new ServiceLocator(array(
37             'foo' => function () { return 'bar'; },
38             'bar' => function () { return 'baz'; },
39         ));
40
41         $this->assertSame('bar', $locator->get('foo'));
42         $this->assertSame('baz', $locator->get('bar'));
43     }
44
45     public function testGetDoesNotMemoize()
46     {
47         $i = 0;
48         $locator = new ServiceLocator(array(
49             'foo' => function () use (&$i) {
50                 ++$i;
51
52                 return 'bar';
53             },
54         ));
55
56         $this->assertSame('bar', $locator->get('foo'));
57         $this->assertSame('bar', $locator->get('foo'));
58         $this->assertSame(2, $i);
59     }
60
61     /**
62      * @expectedException        \Psr\Container\NotFoundExceptionInterface
63      * @expectedExceptionMessage Service "dummy" not found: the container inside "Symfony\Component\DependencyInjection\Tests\ServiceLocatorTest" is a smaller service locator that only knows about the "foo" and "bar" services.
64      */
65     public function testGetThrowsOnUndefinedService()
66     {
67         $locator = new ServiceLocator(array(
68             'foo' => function () { return 'bar'; },
69             'bar' => function () { return 'baz'; },
70         ));
71
72         $locator->get('dummy');
73     }
74
75     /**
76      * @expectedException        \Psr\Container\NotFoundExceptionInterface
77      * @expectedExceptionMessage The service "foo" has a dependency on a non-existent service "bar". This locator only knows about the "foo" service.
78      */
79     public function testThrowsOnUndefinedInternalService()
80     {
81         $locator = new ServiceLocator(array(
82             'foo' => function () use (&$locator) { return $locator->get('bar'); },
83         ));
84
85         $locator->get('foo');
86     }
87
88     /**
89      * @expectedException        \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
90      * @expectedExceptionMessage Circular reference detected for service "bar", path: "bar -> baz -> bar".
91      */
92     public function testThrowsOnCircularReference()
93     {
94         $locator = new ServiceLocator(array(
95             'foo' => function () use (&$locator) { return $locator->get('bar'); },
96             'bar' => function () use (&$locator) { return $locator->get('baz'); },
97             'baz' => function () use (&$locator) { return $locator->get('bar'); },
98         ));
99
100         $locator->get('foo');
101     }
102
103     /**
104      * @expectedException        \Psr\Container\NotFoundExceptionInterface
105      * @expectedExceptionMessage Service "foo" not found: even though it exists in the app's container, the container inside "caller" is a smaller service locator that only knows about the "bar" service. Unless you need extra laziness, try using dependency injection instead. Otherwise, you need to declare it using "SomeServiceSubscriber::getSubscribedServices()".
106      */
107     public function testThrowsInServiceSubscriber()
108     {
109         $container = new Container();
110         $container->set('foo', new \stdClass());
111         $subscriber = new SomeServiceSubscriber();
112         $subscriber->container = new ServiceLocator(array('bar' => function () {}));
113         $subscriber->container = $subscriber->container->withContext('caller', $container);
114
115         $subscriber->getFoo();
116     }
117
118     public function testInvoke()
119     {
120         $locator = new ServiceLocator(array(
121             'foo' => function () { return 'bar'; },
122             'bar' => function () { return 'baz'; },
123         ));
124
125         $this->assertSame('bar', $locator('foo'));
126         $this->assertSame('baz', $locator('bar'));
127         $this->assertNull($locator('dummy'), '->__invoke() should return null on invalid service');
128     }
129 }
130
131 class SomeServiceSubscriber implements ServiceSubscriberinterface
132 {
133     public $container;
134
135     public function getFoo()
136     {
137         return $this->container->get('foo');
138     }
139
140     public static function getSubscribedServices()
141     {
142         return array('bar' => 'stdClass');
143     }
144 }