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