bug #41719 [FrameworkBundle] fix Could not find service "test.service_container" (smilesrg)

This PR was merged into the 5.2 branch.

Discussion
----------

[FrameworkBundle] fix Could not find service "test.service_container"

| Q             | A
| ------------- | ---
| Branch?       |  5.2, needs to be ported to 5.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #41715
| License       | MIT

Partial backport of https://github.com/symfony/symfony/pull/41530
Related to https://github.com/symfony/symfony/issues/41715 and https://github.com/symfony/symfony/pull/41530

When launching phpunit, got an error:
`LogicException: Could not find service "test.service_container". Try updating the "framework.test" config to "true".`

```
There was 1 error:

1) App\Symfony\Bundle\Tests\Integration\IntegrationTest::testServiceWiringWithConfiguration
LogicException: Could not find service "test.service_container". Try updating the "framework.test" config to "true".

/Projects/app/vendor/symfony/framework-bundle/Test/KernelTestCase.php:109
/Projects/app/tests/integration/IntegrationTest.php:23

Caused by
Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "test.service_container". Did you mean this: "service_container"?

/Projects/app/vendor/symfony/dependency-injection/Container.php:280
/Projects/app/vendor/symfony/dependency-injection/Container.php:228
/Projects/app/vendor/symfony/framework-bundle/Test/KernelTestCase.php:107
/Projects/app/tests/integration/IntegrationTest.php:23
```

/cc `@xabbuh`  `@nicolas`-grekas

Commits
-------

0748b5247e bug #41715: [FrameworkBundle] Partial backport of PR#41530
This commit is contained in:
Nicolas Grekas 2021-06-17 14:08:19 +02:00
commit a026c672e7

View File

@ -49,7 +49,9 @@ class KernelBrowser extends HttpKernelBrowser
*/
public function getContainer()
{
return $this->kernel->getContainer();
$container = $this->kernel->getContainer();
return $container->has('test.service_container') ? $container->get('test.service_container') : $container;
}
/**
@ -69,11 +71,11 @@ class KernelBrowser extends HttpKernelBrowser
*/
public function getProfile()
{
if (null === $this->response || !$this->kernel->getContainer()->has('profiler')) {
if (null === $this->response || !$this->getContainer()->has('profiler')) {
return false;
}
return $this->kernel->getContainer()->get('profiler')->loadProfileFromResponse($this->response);
return $this->getContainer()->get('profiler')->loadProfileFromResponse($this->response);
}
/**
@ -83,7 +85,7 @@ class KernelBrowser extends HttpKernelBrowser
*/
public function enableProfiler()
{
if ($this->kernel->getContainer()->has('profiler')) {
if ($this->getContainer()->has('profiler')) {
$this->profiler = true;
}
}
@ -123,7 +125,7 @@ class KernelBrowser extends HttpKernelBrowser
$token = new TestBrowserToken($user->getRoles(), $user, $firewallContext);
$token->setAuthenticated(true);
$container = $this->kernel->getContainer()->get('test.service_container');
$container = $this->getContainer()->get('test.service_container');
$container->get('security.untracked_token_storage')->setToken($token);
if (!$container->has('session')) {
@ -161,7 +163,7 @@ class KernelBrowser extends HttpKernelBrowser
$this->profiler = false;
$this->kernel->boot();
$this->kernel->getContainer()->get('profiler')->enable();
$this->getContainer()->get('profiler')->enable();
}
return parent::doRequest($request);
@ -220,7 +222,11 @@ class KernelBrowser extends HttpKernelBrowser
$profilerCode = '';
if ($this->profiler) {
$profilerCode = '$kernel->getContainer()->get(\'profiler\')->enable();';
$profilerCode = <<<'EOF'
$container = $kernel->getContainer();
$container = $container->has('test.service_container') ? $container->get('test.service_container') : $container;
$container->get('profiler')->enable();
EOF;
}
$code = <<<EOF