ServiceLocatorTestCase.php 2.9 KB

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