|
| 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\ContainerInterface; |
| 16 | +use Symfony\Contracts\Service\ServiceLocatorTrait; |
| 17 | + |
| 18 | +abstract class ServiceLocatorTest extends TestCase |
| 19 | +{ |
| 20 | + protected function getServiceLocator(array $factories): ContainerInterface |
| 21 | + { |
| 22 | + return new class($factories) implements ContainerInterface { |
| 23 | + use ServiceLocatorTrait; |
| 24 | + }; |
| 25 | + } |
| 26 | + |
| 27 | + public function testHas() |
| 28 | + { |
| 29 | + $locator = $this->getServiceLocator([ |
| 30 | + 'foo' => function () { return 'bar'; }, |
| 31 | + 'bar' => function () { return 'baz'; }, |
| 32 | + function () { return 'dummy'; }, |
| 33 | + ]); |
| 34 | + |
| 35 | + $this->assertTrue($locator->has('foo')); |
| 36 | + $this->assertTrue($locator->has('bar')); |
| 37 | + $this->assertFalse($locator->has('dummy')); |
| 38 | + } |
| 39 | + |
| 40 | + public function testGet() |
| 41 | + { |
| 42 | + $locator = $this->getServiceLocator([ |
| 43 | + 'foo' => function () { return 'bar'; }, |
| 44 | + 'bar' => function () { return 'baz'; }, |
| 45 | + ]); |
| 46 | + |
| 47 | + $this->assertSame('bar', $locator->get('foo')); |
| 48 | + $this->assertSame('baz', $locator->get('bar')); |
| 49 | + } |
| 50 | + |
| 51 | + public function testGetDoesNotMemoize() |
| 52 | + { |
| 53 | + $i = 0; |
| 54 | + $locator = $this->getServiceLocator([ |
| 55 | + 'foo' => function () use (&$i) { |
| 56 | + ++$i; |
| 57 | + |
| 58 | + return 'bar'; |
| 59 | + }, |
| 60 | + ]); |
| 61 | + |
| 62 | + $this->assertSame('bar', $locator->get('foo')); |
| 63 | + $this->assertSame('bar', $locator->get('foo')); |
| 64 | + $this->assertSame(2, $i); |
| 65 | + } |
| 66 | + |
| 67 | + public function testThrowsOnUndefinedInternalService() |
| 68 | + { |
| 69 | + if (!$this->getExpectedException()) { |
| 70 | + $this->expectException(\Psr\Container\NotFoundExceptionInterface::class); |
| 71 | + $this->expectExceptionMessage('The service "foo" has a dependency on a non-existent service "bar". This locator only knows about the "foo" service.'); |
| 72 | + } |
| 73 | + $locator = $this->getServiceLocator([ |
| 74 | + 'foo' => function () use (&$locator) { return $locator->get('bar'); }, |
| 75 | + ]); |
| 76 | + |
| 77 | + $locator->get('foo'); |
| 78 | + } |
| 79 | + |
| 80 | + public function testThrowsOnCircularReference() |
| 81 | + { |
| 82 | + $this->expectException(\Psr\Container\ContainerExceptionInterface::class); |
| 83 | + $this->expectExceptionMessage('Circular reference detected for service "bar", path: "bar -> baz -> bar".'); |
| 84 | + $locator = $this->getServiceLocator([ |
| 85 | + 'foo' => function () use (&$locator) { return $locator->get('bar'); }, |
| 86 | + 'bar' => function () use (&$locator) { return $locator->get('baz'); }, |
| 87 | + 'baz' => function () use (&$locator) { return $locator->get('bar'); }, |
| 88 | + ]); |
| 89 | + |
| 90 | + $locator->get('foo'); |
| 91 | + } |
| 92 | +} |
0 commit comments