Skip to content

Commit 191afdc

Browse files
fix tests
1 parent 10bac77 commit 191afdc

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

Test/ServiceLocatorTest.php

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

0 commit comments

Comments
 (0)