You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR was squashed before being merged into the 5.3-dev branch.
Discussion
----------
[FrameworkBundle] Add KernelTestCase::getContainer()
| Q | A
| ------------- | ---
| Branch? | 5.x
| Bug fix? | no
| New feature? | yes
| Deprecations? | yes
| Tickets |
| License | MIT
| Doc PR | symfony/symfony-docs#14731
There are at least 3 ways to get the container in a test class:
```php
class FooTest extends WebTestCase
{
public function testGetContainerA()
{
$kernel = self::bootKernel();
$container = $kernel->getContainer();
}
public function testGetContainerB()
{
self::bootKernel();
$container = self::$container;
}
public function testGetContainerC()
{
$client = self::createClient();
$container = $client->getContainer();
}
}
```
I suggest to add a fourth =)
Basically, in tests you should always use the `test.service_container`. It is hard to remove A and C, but I can deprecate C and add a helper function.
```php
class FooTest extends WebTestCase
{
public function testGetContainerTheOnlyWayYouShouldUse()
{
$container = $this->getContainer();
}
}
```
This new way will also boot your kernel if it is not already booted.
Commits
-------
f4c9724 [FrameworkBundle] Add KernelTestCase::getContainer()
Copy file name to clipboardExpand all lines: UPGRADE-5.3.md
+1
Original file line number
Diff line number
Diff line change
@@ -35,6 +35,7 @@ FrameworkBundle
35
35
* Deprecate the `session.storage` alias and `session.storage.*` services, use the `session.storage.factory` alias and `session.storage.factory.*` services instead
36
36
* Deprecate the `framework.session.storage_id` configuration option, use the `framework.session.storage_factory_id` configuration option instead
37
37
* Deprecate the `session` service and the `SessionInterface` alias, use the `\Symfony\Component\HttpFoundation\Request::getSession()` or the new `\Symfony\Component\HttpFoundation\RequestStack::getSession()` methods instead
38
+
* Deprecate the `KernelTestCase::$container` property, use `KernelTestCase::getContainer()` instead
Copy file name to clipboardExpand all lines: UPGRADE-6.0.md
+1
Original file line number
Diff line number
Diff line change
@@ -78,6 +78,7 @@ FrameworkBundle
78
78
* The `form.factory`, `form.type.file`, `translator`, `security.csrf.token_manager`, `serializer`,
79
79
`cache_clearer`, `filesystem` and `validator` services are now private.
80
80
* Removed the `lock.RESOURCE_NAME` and `lock.RESOURCE_NAME.store` services and the `lock`, `LockInterface`, `lock.store` and `PersistingStoreInterface` aliases, use `lock.RESOURCE_NAME.factory`, `lock.factory` or `LockFactory` instead.
81
+
* Remove the `KernelTestCase::$container` property, use `KernelTestCase::getContainer()` instead
$this->assertInstanceOf(EventDispatcher::class, $autowiredServices->getDispatcher(), 'The event_dispatcher service should be injected if the debug is not enabled');
$this->assertInstanceOf(TraceableEventDispatcher::class, $autowiredServices->getDispatcher(), 'The debug.event_dispatcher service should be injected if the debug is enabled');
$this->assertInstanceOf(AccessDecisionManager::class, $autowiredServices->getAccessDecisionManager(), 'The security.access.decision_manager service should be injected in debug mode');
$this->assertInstanceOf(TraceableAccessDecisionManager::class, $autowiredServices->getAccessDecisionManager(), 'The debug.security.access.decision_manager service should be injected in non-debug mode');
0 commit comments