Skip to content

Commit 2de9df0

Browse files
Merge branch '6.2' into 6.3
* 6.2: [Cache] Send Predis SSL options in the $hosts parameter [DependencyInjection] Do not ignore tags `name` attribute when it does not define their name [Contracts] Rename ServiceLocatorTest [Cache] Fix success interpretation when pruning cache Fix Psalm errors Make onAuthenticationSuccess Response optional [Security] Fix return type of AuthenticationSuccessHandlerInterface::onAuthenticationSuccess()
2 parents e70d0d4 + 8aa07d3 commit 2de9df0

File tree

2 files changed

+98
-75
lines changed

2 files changed

+98
-75
lines changed

Test/ServiceLocatorTest.php

Lines changed: 6 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -11,82 +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-
{
20-
protected function getServiceLocator(array $factories): ContainerInterface
16+
if (false) {
17+
/**
18+
* @deprecated since PHPUnit 9.6
19+
*/
20+
class ServiceLocatorTest
2121
{
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-
public function testThrowsOnUndefinedInternalService()
68-
{
69-
if (!$this->getExpectedException()) {
70-
$this->expectException(\Psr\Container\NotFoundExceptionInterface::class);
71-
$this->expectExceptionMessage('The service "foo" has a dependency on a non-existent service "bar". This locator only knows about the "foo" service.');
72-
}
73-
$locator = $this->getServiceLocator([
74-
'foo' => function () use (&$locator) { return $locator->get('bar'); },
75-
]);
76-
77-
$locator->get('foo');
78-
}
79-
80-
public function testThrowsOnCircularReference()
81-
{
82-
$this->expectException(\Psr\Container\ContainerExceptionInterface::class);
83-
$this->expectExceptionMessage('Circular reference detected for service "bar", path: "bar -> baz -> bar".');
84-
$locator = $this->getServiceLocator([
85-
'foo' => function () use (&$locator) { return $locator->get('bar'); },
86-
'bar' => function () use (&$locator) { return $locator->get('baz'); },
87-
'baz' => function () use (&$locator) { return $locator->get('bar'); },
88-
]);
89-
90-
$locator->get('foo');
9122
}
9223
}

Test/ServiceLocatorTestCase.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 ServiceLocatorTest extends TestCase
19+
{
20+
protected function getServiceLocator(array $factories): ContainerInterface
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+
public function testThrowsOnUndefinedInternalService()
68+
{
69+
if (!$this->getExpectedException()) {
70+
$this->expectException(\Psr\Container\NotFoundExceptionInterface::class);
71+
$this->expectExceptionMessage('The service "foo" has a dependency on a non-existent service "bar". This locator only knows about the "foo" service.');
72+
}
73+
$locator = $this->getServiceLocator([
74+
'foo' => function () use (&$locator) { return $locator->get('bar'); },
75+
]);
76+
77+
$locator->get('foo');
78+
}
79+
80+
public function testThrowsOnCircularReference()
81+
{
82+
$this->expectException(\Psr\Container\ContainerExceptionInterface::class);
83+
$this->expectExceptionMessage('Circular reference detected for service "bar", path: "bar -> baz -> bar".');
84+
$locator = $this->getServiceLocator([
85+
'foo' => function () use (&$locator) { return $locator->get('bar'); },
86+
'bar' => function () use (&$locator) { return $locator->get('baz'); },
87+
'baz' => function () use (&$locator) { return $locator->get('bar'); },
88+
]);
89+
90+
$locator->get('foo');
91+
}
92+
}

0 commit comments

Comments
 (0)