Skip to content

Commit 979be29

Browse files
committed
feature #31202 [FrameworkBundle] WebTestCase KernelBrowser::getContainer null return type (Simperfit)
This PR was merged into the 4.4 branch. Discussion ---------- [FrameworkBundle] WebTestCase KernelBrowser::getContainer null return type | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | yes <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #25920 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | <!-- required for new features --> <!-- Write a short README entry for your feature/bugfix here (replace this comment block.) This will help people understand your PR and can be used as a start of the Doc PR. Additionally: - Bug fixes must be submitted against the lowest branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the master branch. --> As @stof suggested in the #25920 I started deprecating the behaviour of returning null when the container is non booted / null. If this is not wanted we should close both the PR and the issue ;). Commits ------- e169e1a [FrameworkBundle] WebTestCase KernelBrowser::getContainer null return type
2 parents 86d7efa + e169e1a commit 979be29

File tree

9 files changed

+87
-34
lines changed

9 files changed

+87
-34
lines changed

UPGRADE-4.4.md

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ DependencyInjection
6060
```php
6161
new Definition('%my_class%');
6262
```
63-
63+
6464
DoctrineBridge
6565
--------------
6666
* Deprecated injecting `ClassMetadataFactory` in `DoctrineExtractor`, an instance of `EntityManagerInterface` should be
@@ -96,7 +96,7 @@ FrameworkBundle
9696
* Deprecated `routing.loader.service`, use `routing.loader.container` instead.
9797
* Not tagging service route loaders with `routing.route_loader` has been deprecated.
9898
* Overriding the methods `KernelTestCase::tearDown()` and `WebTestCase::tearDown()` without the `void` return-type is deprecated.
99-
99+
100100
HttpClient
101101
----------
102102

@@ -139,7 +139,7 @@ HttpKernel
139139
}
140140
```
141141

142-
As many bundles must be compatible with a range of Symfony versions, the current
142+
As many bundles must be compatible with a range of Symfony versions, the current
143143
directory convention is not deprecated yet, but it will be in the future.
144144

145145
* Deprecated the second and third argument of `KernelInterface::locateResource`
@@ -148,6 +148,7 @@ HttpKernel
148148
fallback directories. Resources like service definitions are usually loaded relative to the
149149
current directory or with a glob pattern. The fallback directories have never been advocated
150150
so you likely do not use those in any app based on the SF Standard or Flex edition.
151+
* Getting the container from a non-booted kernel is deprecated
151152

152153
Lock
153154
----
@@ -171,7 +172,7 @@ MonologBridge
171172
--------------
172173

173174
* The `RouteProcessor` has been marked final.
174-
175+
175176
Process
176177
-------
177178

@@ -195,14 +196,14 @@ Security
195196
* Implementations of `PasswordEncoderInterface` and `UserPasswordEncoderInterface` should add a new `needsRehash()` method
196197
* Deprecated returning a non-boolean value when implementing `Guard\AuthenticatorInterface::checkCredentials()`. Please explicitly return `false` to indicate invalid credentials.
197198
* Deprecated passing more than one attribute to `AccessDecisionManager::decide()` and `AuthorizationChecker::isGranted()` (and indirectly the `is_granted()` Twig and ExpressionLanguage function)
198-
199+
199200
**Before**
200201
```php
201202
if ($this->authorizationChecker->isGranted(['ROLE_USER', 'ROLE_ADMIN'])) {
202203
// ...
203204
}
204205
```
205-
206+
206207
**After**
207208
```php
208209
if ($this->authorizationChecker->isGranted(new Expression("has_role('ROLE_USER') or has_role('ROLE_ADMIN')"))) {}
@@ -230,18 +231,18 @@ TwigBridge
230231
* Deprecated to pass `$rootDir` and `$fileLinkFormatter` as 5th and 6th argument respectively to the
231232
`DebugCommand::__construct()` method, swap the variables position.
232233
* Deprecated accepting STDIN implicitly when using the `lint:twig` command, use `lint:twig -` (append a dash) instead to make it explicit.
233-
234+
234235
TwigBundle
235236
----------
236237

237238
* Deprecated `twig.exception_controller` configuration option, set it to "null" and use `framework.error_controller` instead:
238-
239+
239240
Before:
240241
```yaml
241242
twig:
242243
exception_controller: 'App\Controller\MyExceptionController'
243244
```
244-
245+
245246
After:
246247
```yaml
247248
twig:
@@ -250,36 +251,36 @@ TwigBundle
250251
framework:
251252
error_controller: 'App\Controller\MyExceptionController'
252253
```
253-
254-
The new default exception controller will also change the error response content according to
254+
255+
The new default exception controller will also change the error response content according to
255256
https://tools.ietf.org/html/rfc7807 for `json`, `xml`, `atom` and `txt` formats:
256-
257+
257258
Before:
258259
```json
259-
{
260-
"error": {
261-
"code": 404,
262-
"message": "Sorry, the page you are looking for could not be found"
263-
}
260+
{
261+
"error": {
262+
"code": 404,
263+
"message": "Sorry, the page you are looking for could not be found"
264+
}
264265
}
265266
```
266-
267+
267268
After:
268269
```json
269-
{
270+
{
270271
"title": "Not Found",
271-
"status": 404,
272+
"status": 404,
272273
"detail": "Sorry, the page you are looking for could not be found"
273274
}
274275
```
275-
276+
276277
* Deprecated the `ExceptionController` and `PreviewErrorController` controllers, use `ErrorController` from the HttpKernel component instead
277278
* Deprecated all built-in error templates, use the error renderer mechanism of the `ErrorRenderer` component
278-
* Deprecated loading custom error templates in non-html formats. Custom HTML error pages based on Twig keep working as before:
279+
* Deprecated loading custom error templates in non-html formats. Custom HTML error pages based on Twig keep working as before:
279280

280281
Before (`templates/bundles/TwigBundle/Exception/error.jsonld.twig`):
281282
```twig
282-
{
283+
{
283284
"@id": "https://example.com",
284285
"@type": "error",
285286
"@context": {
@@ -289,7 +290,7 @@ TwigBundle
289290
}
290291
}
291292
```
292-
293+
293294
After (`App\ErrorRenderer\JsonLdErrorRenderer`):
294295
```php
295296
class JsonLdErrorRenderer implements ErrorRendererInterface
@@ -298,7 +299,7 @@ TwigBundle
298299
{
299300
return 'jsonld';
300301
}
301-
302+
302303
public function render(FlattenException $exception): string
303304
{
304305
return json_encode([
@@ -323,7 +324,7 @@ Validator
323324
* Deprecated using anything else than a `string` as the code of a `ConstraintViolation`, a `string` type-hint will
324325
be added to the constructor of the `ConstraintViolation` class and to the `ConstraintViolationBuilder::setCode()`
325326
method in 5.0.
326-
* Deprecated passing an `ExpressionLanguage` instance as the second argument of `ExpressionValidator::__construct()`.
327+
* Deprecated passing an `ExpressionLanguage` instance as the second argument of `ExpressionValidator::__construct()`.
327328
Pass it as the first argument instead.
328329
* The `Length` constraint expects the `allowEmptyString` option to be defined
329330
when the `min` option is used.
@@ -343,7 +344,7 @@ WebServerBundle
343344
---------------
344345

345346
* The bundle is deprecated and will be removed in 5.0.
346-
347+
347348
Yaml
348349
----
349350

UPGRADE-5.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ HttpFoundation
288288
use `Symfony\Component\Mime\FileinfoMimeTypeGuesser` instead.
289289
* `ApacheRequest` has been removed, use the `Request` class instead.
290290
* The third argument of the `HeaderBag::get()` method has been removed, use method `all()` instead.
291+
* Getting the container from a non-booted kernel is not possible anymore.
291292

292293
HttpKernel
293294
----------

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,15 @@ protected static function createKernel(array $options = [])
127127
protected static function ensureKernelShutdown()
128128
{
129129
if (null !== static::$kernel) {
130-
$container = static::$kernel->getContainer();
131-
static::$kernel->shutdown();
132-
static::$booted = false;
133-
if ($container instanceof ResetInterface) {
134-
$container->reset();
130+
$isBooted = (new \ReflectionClass(static::$kernel))->getProperty('booted');
131+
$isBooted->setAccessible(true);
132+
if ($isBooted->getValue(static::$kernel)) {
133+
$container = static::$kernel->getContainer();
134+
static::$kernel->shutdown();
135+
static::$booted = false;
136+
if ($container instanceof ResetInterface) {
137+
$container->reset();
138+
}
135139
}
136140
}
137141
static::$container = null;

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/AppKernel.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Psr\Log\NullLogger;
1515
use Symfony\Component\Config\Loader\LoaderInterface;
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\ContainerInterface;
1718
use Symfony\Component\Filesystem\Filesystem;
1819
use Symfony\Component\HttpKernel\Kernel;
1920

@@ -96,4 +97,13 @@ protected function getKernelParameters(): array
9697

9798
return $parameters;
9899
}
100+
101+
public function getContainer(): ContainerInterface
102+
{
103+
if (!$this->booted) {
104+
throw new \LogicException('Cannot access the container on a non-booted kernel. Did you forget to boot it?');
105+
}
106+
107+
return parent::getContainer();
108+
}
99109
}

src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/AppKernel.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\app;
1313

1414
use Symfony\Component\Config\Loader\LoaderInterface;
15+
use Symfony\Component\DependencyInjection\ContainerInterface;
1516
use Symfony\Component\Filesystem\Filesystem;
1617
use Symfony\Component\HttpKernel\Kernel;
1718

@@ -98,4 +99,13 @@ protected function getKernelParameters(): array
9899

99100
return $parameters;
100101
}
102+
103+
public function getContainer(): ContainerInterface
104+
{
105+
if (!$this->booted) {
106+
throw new \LogicException('Cannot access the container on a non-booted kernel. Did you forget to boot it?');
107+
}
108+
109+
return parent::getContainer();
110+
}
101111
}

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ CHANGELOG
1414
so you likely do not use those in any app based on the SF Standard or Flex edition.
1515
* Marked all dispatched event classes as `@final`
1616
* Added `ErrorController` to enable the preview and error rendering mechanism
17+
* Getting the container from a non-booted kernel is deprecated.
1718

1819
4.3.0
1920
-----

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,10 @@ public function getProjectDir()
380380
*/
381381
public function getContainer()
382382
{
383+
if (!$this->booted) {
384+
@trigger_error('Getting the container from a non-booted kernel is deprecated since Symfony 4.4.', E_USER_DEPRECATED);
385+
}
386+
383387
return $this->container;
384388
}
385389

src/Symfony/Component/HttpKernel/KernelInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function getRootDir();
124124
/**
125125
* Gets the current container.
126126
*
127-
* @return ContainerInterface|null A ContainerInterface instance or null when the Kernel is shutdown
127+
* @return ContainerInterface
128128
*/
129129
public function getContainer();
130130

src/Symfony/Component/HttpKernel/Tests/KernelTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ public function testConstructor()
4646
$this->assertEquals($debug, $kernel->isDebug());
4747
$this->assertFalse($kernel->isBooted());
4848
$this->assertLessThanOrEqual(microtime(true), $kernel->getStartTime());
49+
}
50+
51+
/**
52+
* @group legacy
53+
* @expectedDeprecation Getting the container from a non-booted kernel is deprecated since Symfony 4.4.
54+
*/
55+
public function testGetContainerForANonBootedKernel()
56+
{
57+
$kernel = new KernelForTest('test_env', true);
58+
59+
$this->assertFalse($kernel->isBooted());
4960
$this->assertNull($kernel->getContainer());
5061
}
5162

@@ -61,7 +72,6 @@ public function testClone()
6172
$this->assertEquals($debug, $clone->isDebug());
6273
$this->assertFalse($clone->isBooted());
6374
$this->assertLessThanOrEqual(microtime(true), $clone->getStartTime());
64-
$this->assertNull($clone->getContainer());
6575
}
6676

6777
public function testClassNameValidityGetter()
@@ -486,6 +496,18 @@ public function testTerminateReturnsSilentlyIfKernelIsNotBooted()
486496
$kernel->terminate(Request::create('/'), new Response());
487497
}
488498

499+
/**
500+
* @group legacy
501+
* @expectedDeprecation Getting the container from a non-booted kernel is deprecated since Symfony 4.4.
502+
*/
503+
public function testDeprecatedNullKernel()
504+
{
505+
$kernel = $this->getKernel();
506+
$kernel->shutdown();
507+
508+
$this->assertNull($kernel->getContainer());
509+
}
510+
489511
public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
490512
{
491513
// does not implement TerminableInterface

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy