Sen descrición

ServiceLocatorTest.php 2.9KB

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