8000 [FrameworkBundle] Fix test-container on kernel reboot, revert to retu… · symfony/symfony@6764d4e · GitHub
[go: up one dir, main page]

Skip to content

Commit 6764d4e

Browse files
[FrameworkBundle] Fix test-container on kernel reboot, revert to returning the real container from Client::getContainer()
1 parent 582165f commit 6764d4e

File tree

8 files changed

+82
-47
lines changed

8 files changed

+82
-47
lines changed

src/Symfony/Bundle/FrameworkBundle/Client.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,13 @@ class Client extends BaseClient
3030
private $hasPerformedRequest = false;
3131
private $profiler = false;
3232
private $reboot = true;
33-
private $testContainerId;
3433

3534
/**
3635
* {@inheritdoc}
3736
*/
38-
public function __construct(KernelInterface $kernel, array $server = array(), History $history = null, CookieJar $cookieJar = null, string $testContainerId = null)
37+
public function __construct(KernelInterface $kernel, array $server = array(), History $history = null, CookieJar $cookieJar = null)
3938
{
4039
parent::__construct($kernel, $server, $history, $cookieJar);
41-
$this->testContainerId = $testContainerId;
4240
}
4341

4442
/**
@@ -48,9 +46,7 @@ public function __construct(KernelInterface $kernel, array $server = array(), Hi
4846
*/
4947
public function getContainer()
5048
{
51-
$container = $this->kernel->getContainer();
52-
53-
return null !== $this->testContainerId ? $container->get($this->testContainerId) : $container;
49+
return $this->kernel->getContainer();
5450
}
5551

5652
/**

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TestServiceContainerRealRefPass.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,11 @@ class TestServiceContainerRealRefPass implements CompilerPassInterface
2222
{
2323
public function process(ContainerBuilder $container)
2424
{
25-
if (!$container->hasDefinition('test.service_container')) {
25+
if (!$container->hasDefinition('test.private_services_locator')) {
2626
return;
2727
}
2828

29-
$testContainer = $container->getDefinition('test.service_container');
30-
$privateContainer = $testContainer->getArgument(2);
31-
if ($privateContainer instanceof Reference) {
32-
$privateContainer = $container->getDefinition((string) $privateContainer);
33-
}
29+
$privateContainer = $container->getDefinition('test.private_services_locator');
3430
$definitions = $container->getDefinitions();
3531
$privateServices = $privateContainer->getArgument(0);
3632

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TestServiceContainerWeakRefPass.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class TestServiceContainerWeakRefPass implements CompilerPassInterface
2323
{
2424
public function process(ContainerBuilder $container)
2525
{
26-
if (!$container->hasDefinition('test.service_container')) {
26+
if (!$container->hasDefinition('test.private_services_locator')) {
2727
return;
2828
}
2929

@@ -50,7 +50,7 @@ public function process(ContainerBuilder $container)
5050
}
5151

5252
if ($privateServices) {
53-
$definitions[(string) $definitions['test.service_container']->getArgument(2)]->replaceArgument(0, $privateServices);
53+
$definitions['test.private_services_locator']->replaceArgument(0, $privateServices);
5454
}
5555
}
5656
}

src/Symfony/Bundle/FrameworkBundle/Resources/config/test.xml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
<argument>%test.client.parameters%</argument>
1717
<argument type="service" id="test.client.history" />
1818
<argument type="service" id="test.client.cookiejar" />
19-
<argument>test.service_container</argument>
2019
</service>
2120

2221
<service id="test.client.history" class="Symfony\Component\BrowserKit\History" shared="false" />
@@ -36,13 +35,12 @@
3635
</service>
3736

3837
<service id="test.service_container" class="Symfony\Bundle\FrameworkBundle\Test\TestContainer" public="true">
39-
<argument type="service" id="parameter_bag" on-invalid="null" />
40-
<argument type="service" id="service_container" />
41-
<argument type="service">
42-
<service class="Symfony\Component\DependencyInjection\ServiceLocator">
43-
<argument type="collection" />
44-
</service>
45-
</argument>
38+
<argument type="service" id="kernel" />
39+
<argument>test.private_services_locator</argument>
40+
</service>
41+
42+
<service id="test.private_services_locator" class="Symfony\Component\DependencyInjection\ServiceLocator" public="true">
43+
<argument type="collection" />
4644
</service>
4745
</services>
4846
</container>

src/Symfony/Bundle/FrameworkBundle/Test/TestContainer.php

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,95 +11,140 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Test;
1313

14-
use Psr\Container\ContainerInterface as PsrContainerInterface;
1514
use Symfony\Component\DependencyInjection\Container;
16-
use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
17-
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
15+
use Symfony\Component\HttpKernel\KernelInterface;
1816

1917 EF57
/**
2018
* @author Nicolas Grekas <p@tchwork.com>
19+
*
20+
* @internal
2121
*/
2222
class TestContainer extends Container
2323
{
24-
private $publicContainer;
25-
private $privateContainer;
24+
private $kernel;
25+
private $privateServicesLocatorId;
2626

27-
public function __construct(?ParameterBagInterface $parameterBag, SymfonyContainerInterface $publicContainer, PsrContainerInterface $privateContainer)
27+
public function __construct(KernelInterface $kernel, string $privateServicesLocatorId)
2828
{
29-
$this->parameterBag = $parameterBag ?? $publicContainer->getParameterBag();
30-
$this->publicContainer = $publicContainer;
31-
$this->privateContainer = $privateContainer;
29+
$this->kernel = $kernel;
30+
$this->privateServicesLocatorId = $privateServicesLocatorId;
3231
}
3332

3433
/**
3534
* {@inheritdoc}
3635
*/
3736
public function compile()
3837
{
39-
$this->publicContainer->compile();
38+
$this->getPublicContainer()->compile();
4039
}
4140

4241
/**
4342
* {@inheritdoc}
4443
*/
4544
public function isCompiled()
4645
{
47-
return $this->publicContainer->isCompiled();
46+
return $this->getPublicContainer()->isCompiled();
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function getParameterBag()
53+
{
54+
return $this->getPublicContainer()->getParameterBag();
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
public function getParameter($name)
61+
{
62+
return $this->getPublicContainer()->getParameter($name);
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public function hasParameter($name)
69+
{
70+
return $this->getPublicContainer()->hasParameter($name);
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
public function setParameter($name, $value)
77+
{
78+
$this->getPublicContainer()->setParameter($name, $value);
4879
}
4980

5081
/**
5182
* {@inheritdoc}
5283
*/
5384
public function set($id, $service)
5485
{
55-
$this->publicContainer->set($id, $service);
86+
$this->getPublicContainer()->set($id, $service);
5687
}
5788

5889
/**
5990
* {@inheritdoc}
6091
*/
6192
public function has($id)
6293
{
63-
return $this->publicContainer->has($id) || $this->privateContainer->has($id);
94+
return $this->getPublicContainer()->has($id) || $this->getPrivateContainer()->has($id);
6495
}
6596

6697
/**
6798
* {@inheritdoc}
6899
*/
69100
public function get($id, $invalidBehavior = /* self::EXCEPTION_ON_INVALID_REFERENCE */ 1)
70101
{
71-
return $this->privateContainer->has($id) ? $this->privateContainer->get($id) : $this->publicContainer->get($id, $invalidBehavior);
102+
return $this->getPrivateContainer()->has($id) ? $this->getPrivateContainer()->get($id) : $this->getPublicContainer()->get($id, $invalidBehavior);
72103
}
73104

74105
/**
75106
* {@inheritdoc}
76107
*/
77108
public function initialized($id)
78109
{
79-
return $this->publicContainer->initialized($id);
110+
return $this->getPublicContainer()->initialized($id);
80111
}
81112

82113
/**
83114
* {@inheritdoc}
84115
*/
85116
public function reset()
86117
{
87-
$this->publicContainer->reset();
118+
$this->getPublicContainer()->reset();
88119
}
89120

90121
/**
91122
* {@inheritdoc}
92123
*/
93124
public function getServiceIds()
94125
{
95-
return $this->publicContainer->getServiceIds();
126+
return $this->getPublicContainer()->getServiceIds();
96127
}
97128

98129
/**
99130
* {@inheritdoc}
100131
*/
101132
public function getRemovedIds()
102133
{
103-
return $this->publicContainer->getRemovedIds();
134+
return $this->getPublicContainer()->getRemovedIds();
135+
}
136+
137+
private function getPublicContainer()
138+
{
139+
if (null === $container = $this->kernel->getContainer()) {
140+
throw new \LogicException('Cannot access the container on a non-booted kernel. Did you forget to boot it?');
141+
}
142+
143+
return $container;
144+
}
145+
146+
private function getPrivateContainer()
147+
{
148+
return $this->getPublicContainer()->get($this->privateServicesLocatorId);
104149
}
105150
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ContainerDumpTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ public function testContainerCompilationInDebug()
2020
{
2121
$client = $this->createClient(array('test_case' => 'ContainerDump', 'root_config' => 'config.yml'));
2222

23-
$this->assertTrue($client->getContainer()->has('serializer'));
23+
$this->assertTrue(static::$container->has('serializer'));
2424
}
2525

2626
public function testContainerCompilation()
2727
{
2828
$client = $this->createClient(array('test_case' => 'ContainerDump', 'root_config' => 'config.yml', 'debug' => false));
2929

30-
$this->assertTrue($client->getContainer()->has('serializer'));
30+
$this->assertTrue(static::$container->has('serializer'));
3131
}
3232
}

src/Symfony/Bundle/SecurityBundle/Tests/Functional/LogoutTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,18 @@ public function testSessionLessRememberMeLogout()
3535
public function testCsrfTokensAreClearedOnLogout()
3636
{
3737
$client = $this->createClient(array('test_case' => 'LogoutWithoutSessionInvalidation', 'root_config' => 'config.yml'));
38-
$client->getContainer()->get('security.csrf.token_storage')->setToken('foo', 'bar');
38+
static::$container->get('security.csrf.token_storage')->setToken('foo', 'bar');
3939

4040
$client->request('POST', '/login', array(
4141
'_username' => 'johannes',
4242
'_password' => 'test',
4343
));
4444

45-
$this->assertTrue($client->getContainer()->get('security.csrf.token_storage')->hasToken('foo'));
46-
$this->assertSame('bar', $client->getContainer()->get('security.csrf.token_storage')->getToken('foo'));
45+
$this->assertTrue(static::$container->get('security.csrf.token_storage')->hasToken('foo'));
46+
$this->assertSame('bar', static::$container->get('security.csrf.token_storage')->getToken('foo'));
4747

4848
$client->request('GET', '/logout');
4949

50-
$this->assertFalse($client->getContainer()->get('security.csrf.token_storage')->hasToken('foo'));
50+
$this->assertFalse(static::$container->get('security.csrf.token_storage')->hasToken('foo'));
5151
}
5252
}

src/Symfony/Bundle/SecurityBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"symfony/security": "4.1.0-beta1|4.1.0-beta2",
4848
"symfony/var-dumper": "<3.4",
4949
"symfony/event-dispatcher": "<3.4",
50-
"symfony/framework-bundle": "<=4.1-beta2",
50+
"symfony/framework-bundle": "<4.1.1",
5151
"symfony/console": "<3.4"
5252
},
5353
"autoload": {

0 commit comments

Comments
 (0)
0