8000 minor #50104 [Contracts] Rename ServiceLocatorTest (nicolas-grekas) · symfony/symfony@c9f5f8f · GitHub
[go: up one dir, main page]

Skip to content

Commit c9f5f8f

Browse files
minor #50104 [Contracts] Rename ServiceLocatorTest (nicolas-grekas)
This PR was merged into the 5.4 branch. Discussion ---------- [Contracts] Rename ServiceLocatorTest | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Commits ------- 43d058b [Contracts] Rename ServiceLocatorTest
2 parents d1d7539 + 43d058b commit c9f5f8f

File tree

3 files changed

+106
-78
lines changed

3 files changed

+106
-78
lines changed

src/Symfony/Component/DependencyInjection/Tests/ServiceLocatorTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@
1818
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
1919
use Symfony\Component\DependencyInjection\ServiceLocator;
2020
use Symfony\Contracts\Service\ServiceSubscriberInterface;
21-
use Symfony\Contracts\Service\Test\ServiceLocatorTest as BaseServiceLocatorTest;
21+
use Symfony\Contracts\Service\Test\ServiceLocatorTest as LegacyServiceLocatorTestCase;
22+
use Symfony\Contracts\Service\Test\ServiceLocatorTestCase;
2223

23-
class ServiceLocatorTest extends BaseServiceLocatorTest
24+
if (!class_exists(ServiceLocatorTestCase::class)) {
25+
class_alias(LegacyServiceLocatorTestCase::class, ServiceLocatorTestCase::class);
26+
}
27+
28+
class ServiceLocatorTest extends ServiceLocatorTestCase
2429
{
2530
public function getServiceLocator(array $factories): ContainerInterface
2631
{

src/Symfony/Contracts/Service/Test/ServiceLocatorTest.php

Lines changed: 4 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -11,85 +11,13 @@
1111

1212
namespace Symfony\Contracts\Service\Test;
1313

14-
use PHPUnit\Framework\TestCase;
15-
use Psr\Container\ContainerInterface;
16-
use Symfony\Contracts\Service\ServiceLocatorTrait;
14+
class_alias(ServiceLocatorTestCase::class, ServiceLocatorTest::class);
1715

18-
abstract class ServiceLocatorTest extends TestCase
19-
{
16+
if (false) {
2017
/**
21-
* @return ContainerInterface
18+
* @deprecated since PHPUnit 9.6
2219
*/
23-
protected function getServiceLocator(array $factories)
20+
class ServiceLocatorTest
2421
{
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');
9422
}
9523
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
abstract class ServiceLocatorTestCase 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+
}

0 commit comments

Comments
 (0)
0