[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/symfony/service-contracts/Test/ -> ServiceLocatorTest.php (source)

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


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer