ServiceLocatorTest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. abstract class ServiceLocatorTest extends TestCase
  15. {
  16. protected 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. public function testThrowsOnUndefinedInternalService()
  56. {
  57. if (!$this->getExpectedException()) {
  58. $this->expectException('Psr\Container\NotFoundExceptionInterface');
  59. $this->expectExceptionMessage('The service "foo" has a dependency on a non-existent service "bar". This locator only knows about the "foo" service.');
  60. }
  61. $locator = $this->getServiceLocator([
  62. 'foo' => function () use (&$locator) { return $locator->get('bar'); },
  63. ]);
  64. $locator->get('foo');
  65. }
  66. public function testThrowsOnCircularReference()
  67. {
  68. $this->expectException('Psr\Container\ContainerExceptionInterface');
  69. $this->expectExceptionMessage('Circular reference detected for service "bar", path: "bar -> baz -> bar".');
  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. $locator->get('foo');
  76. }
  77. }