ServiceLocatorTestCase.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Contracts\Service\Test;
  11. use PHPUnit\Framework\TestCase;
  12. use Psr\Container\ContainerExceptionInterface;
  13. use Psr\Container\ContainerInterface;
  14. use Psr\Container\NotFoundExceptionInterface;
  15. use Symfony\Contracts\Service\ServiceLocatorTrait;
  16. abstract class ServiceLocatorTestCase extends TestCase
  17. {
  18. /**
  19. * @param array<string, callable> $factories
  20. */
  21. protected function getServiceLocator(array $factories): ContainerInterface
  22. {
  23. return new class($factories) implements ContainerInterface {
  24. use ServiceLocatorTrait;
  25. };
  26. }
  27. public function testHas()
  28. {
  29. $locator = $this->getServiceLocator([
  30. 'foo' => fn () => 'bar',
  31. 'bar' => fn () => 'baz',
  32. fn () => 'dummy',
  33. ]);
  34. $this->assertTrue($locator->has('foo'));
  35. $this->assertTrue($locator->has('bar'));
  36. $this->assertFalse($locator->has('dummy'));
  37. }
  38. public function testGet()
  39. {
  40. $locator = $this->getServiceLocator([
  41. 'foo' => fn () => 'bar',
  42. 'bar' => fn () => 'baz',
  43. ]);
  44. $this->assertSame('bar', $locator->get('foo'));
  45. $this->assertSame('baz', $locator->get('bar'));
  46. }
  47. public function testGetDoesNotMemoize()
  48. {
  49. $i = 0;
  50. $locator = $this->getServiceLocator([
  51. 'foo' => function () use (&$i) {
  52. ++$i;
  53. return 'bar';
  54. },
  55. ]);
  56. $this->assertSame('bar', $locator->get('foo'));
  57. $this->assertSame('bar', $locator->get('foo'));
  58. $this->assertSame(2, $i);
  59. }
  60. public function testThrowsOnUndefinedInternalService()
  61. {
  62. $locator = $this->getServiceLocator([
  63. 'foo' => function () use (&$locator) { return $locator->get('bar'); },
  64. ]);
  65. $this->expectException(NotFoundExceptionInterface::class);
  66. $this->expectExceptionMessage('The service "foo" has a dependency on a non-existent service "bar". This locator only knows about the "foo" service.');
  67. $locator->get('foo');
  68. }
  69. public function testThrowsOnCircularReference()
  70. {
  71. $locator = $this->getServiceLocator([
  72. 'foo' => function () use (&$locator) { return $locator->get('bar'); },
  73. 'bar' => function () use (&$locator) { return $locator->get('baz'); },
  74. 'baz' => function () use (&$locator) { return $locator->get('bar'); },
  75. ]);
  76. $this->expectException(ContainerExceptionInterface::class);
  77. $this->expectExceptionMessage('Circular reference detected for service "bar", path: "bar -> baz -> bar".');
  78. $locator->get('foo');
  79. }
  80. }