diff options
Diffstat (limited to 'vendor/symfony/service-contracts/Test/ServiceLocatorTestCase.php')
-rw-r--r-- | vendor/symfony/service-contracts/Test/ServiceLocatorTestCase.php | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/vendor/symfony/service-contracts/Test/ServiceLocatorTestCase.php b/vendor/symfony/service-contracts/Test/ServiceLocatorTestCase.php new file mode 100644 index 0000000..65a3fe3 --- /dev/null +++ b/vendor/symfony/service-contracts/Test/ServiceLocatorTestCase.php | |||
@@ -0,0 +1,96 @@ | |||
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\Contracts\Service\Test; | ||
13 | |||
14 | use PHPUnit\Framework\TestCase; | ||
15 | use Psr\Container\ContainerExceptionInterface; | ||
16 | use Psr\Container\ContainerInterface; | ||
17 | use Psr\Container\NotFoundExceptionInterface; | ||
18 | use Symfony\Contracts\Service\ServiceLocatorTrait; | ||
19 | |||
20 | abstract class ServiceLocatorTestCase extends TestCase | ||
21 | { | ||
22 | protected function getServiceLocator(array $factories): ContainerInterface | ||
23 | { | ||
24 | return new class($factories) implements ContainerInterface { | ||
25 | use ServiceLocatorTrait; | ||
26 | }; | ||
27 | } | ||
28 | |||
29 | public function testHas() | ||
30 | { | ||
31 | $locator = $this->getServiceLocator([ | ||
32 | 'foo' => fn () => 'bar', | ||
33 | 'bar' => fn () => 'baz', | ||
34 | fn () => 'dummy', | ||
35 | ]); | ||
36 | |||
37 | $this->assertTrue($locator->has('foo')); | ||
38 | $this->assertTrue($locator->has('bar')); | ||
39 | $this->assertFalse($locator->has('dummy')); | ||
40 | } | ||
41 | |||
42 | public function testGet() | ||
43 | { | ||
44 | $locator = $this->getServiceLocator([ | ||
45 | 'foo' => fn () => 'bar', | ||
46 | 'bar' => fn () => 'baz', | ||
47 | ]); | ||
48 | |||
49 | $this->assertSame('bar', $locator->get('foo')); | ||
50 | $this->assertSame('baz', $locator->get('bar')); | ||
51 | } | ||
52 | |||
53 | public function testGetDoesNotMemoize() | ||
54 | { | ||
55 | $i = 0; | ||
56 | $locator = $this->getServiceLocator([ | ||
57 | 'foo' => function () use (&$i) { | ||
58 | ++$i; | ||
59 | |||
60 | return 'bar'; | ||
61 | }, | ||
62 | ]); | ||
63 | |||
64 | $this->assertSame('bar', $locator->get('foo')); | ||
65 | $this->assertSame('bar', $locator->get('foo')); | ||
66 | $this->assertSame(2, $i); | ||
67 | } | ||
68 | |||
69 | public function testThrowsOnUndefinedInternalService() | ||
70 | { | ||
71 | $locator = $this->getServiceLocator([ | ||
72 | 'foo' => function () use (&$locator) { return $locator->get('bar'); }, | ||
73 | ]); | ||
74 | |||
75 | if (!$this->getExpectedException()) { | ||
76 | $this->expectException(NotFoundExceptionInterface::class); | ||
77 | $this->expectExceptionMessage('The service "foo" has a dependency on a non-existent service "bar". This locator only knows about the "foo" service.'); | ||
78 | } | ||
79 | |||
80 | $locator->get('foo'); | ||
81 | } | ||
82 | |||
83 | public function testThrowsOnCircularReference() | ||
84 | { | ||
85 | $locator = $this->getServiceLocator([ | ||
86 | 'foo' => function () use (&$locator) { return $locator->get('bar'); }, | ||
87 | 'bar' => function () use (&$locator) { return $locator->get('baz'); }, | ||
88 | 'baz' => function () use (&$locator) { return $locator->get('bar'); }, | ||
89 | ]); | ||
90 | |||
91 | $this->expectException(ContainerExceptionInterface::class); | ||
92 | $this->expectExceptionMessage('Circular reference detected for service "bar", path: "bar -> baz -> bar".'); | ||
93 | |||
94 | $locator->get('foo'); | ||
95 | } | ||
96 | } | ||