ServiceLocatorTest.php 2.8 KB

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