Skip to content

Commit 8ef420e

Browse files
Nyholmfabpot
authored andcommitted
[FrameworkBundle] Add KernelTestCase::getContainer()
1 parent 83eddd3 commit 8ef420e

15 files changed

+111
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ CHANGELOG
1616
* Add tag `assets.package` to register asset packages
1717
* Add support to use a PSR-6 compatible cache for Doctrine annotations
1818
* Deprecate all other values than "none", "php_array" and "file" for `framework.annotation.cache`
19+
* Add `KernelTestCase::getContainer()` as the best way to get a container in tests
1920

2021
5.2.0
2122
-----

Test/KernelTestCase.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\DependencyInjection\ContainerInterface;
16+
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
1617
use Symfony\Component\HttpKernel\KernelInterface;
1718
use Symfony\Contracts\Service\ResetInterface;
1819

@@ -34,6 +35,8 @@ abstract class KernelTestCase extends TestCase
3435

3536
/**
3637
* @var ContainerInterface
38+
*
39+
* @deprecated since Symfony 5.3, use static::getContainer() instead
3740
*/
3841
protected static $container;
3942

@@ -86,6 +89,27 @@ protected static function bootKernel(array $options = [])
8689
return static::$kernel;
8790
}
8891

92+
/**
93+
* Provides a dedicated test container with access to both public and private
94+
* services. The container will not include private services that has been
95+
* inlined or removed. Private services will be removed when they are not
96+
* used by other services.
97+
*
98+
* Using this method is the best way to get a container from your test code.
99+
*/
100+
protected static function getContainer(): ContainerInterface
101+
{
102+
if (!static::$booted) {
103+
static::bootKernel();
104+
}
105+
106+
try {
107+
return self::$kernelContainer->get('test.service_container');
108+
} catch (ServiceNotFoundException $e) {
109+
throw new \LogicException('Could not find service "test.service_container". Try updating the "framework.test" config to "true".', 0, $e);
110+
}
111+
}
112+
89113
/**
90114
* Creates a Kernel.
91115
*

Test/MailerAssertionsTrait.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,13 @@ public static function getMailerMessage(int $index = 0, string $transport = null
118118

119119
private static function getMessageMailerEvents(): MessageEvents
120120
{
121-
if (self::$container->has('mailer.message_logger_listener')) {
122-
return self::$container->get('mailer.message_logger_listener')->getEvents();
121+
$container = static::getContainer();
122+
if ($container->has('mailer.message_logger_listener')) {
123+
return $container->get('mailer.message_logger_listener')->getEvents();
123124
}
124125

125-
if (self::$container->has('mailer.logger_message_listener')) {
126-
return self::$container->get('mailer.logger_message_listener')->getEvents();
126+
if ($container->has('mailer.logger_message_listener')) {
127+
return $container->get('mailer.logger_message_listener')->getEvents();
127128
}
128129

129130
static::fail('A client must have Mailer enabled to make email assertions. Did you forget to require symfony/mailer?');

Test/TestContainer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
use Symfony\Component\HttpKernel\KernelInterface;
1818

1919
/**
20+
* A special container used in tests. This gives access to both public and
21+
* private services. The container will not include private services that has
22+
* been inlined or removed. Private services will be removed when they are not
23+
* used by other services.
24+
*
2025
* @author Nicolas Grekas <p@tchwork.com>
2126
*
2227
* @internal

Tests/Functional/AutowiringTypesTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,36 @@ public function testAnnotationReaderAutowiring()
2525
{
2626
static::bootKernel(['root_config' => 'no_annotations_cache.yml', 'environment' => 'no_annotations_cache']);
2727

28-
$annotationReader = static::$container->get('test.autowiring_types.autowired_services')->getAnnotationReader();
28+
$annotationReader = self::getContainer()->get('test.autowiring_types.autowired_services')->getAnnotationReader();
2929
$this->assertInstanceOf(AnnotationReader::class, $annotationReader);
3030
}
3131

3232
public function testCachedAnnotationReaderAutowiring()
3333
{
3434
static::bootKernel();
3535

36-
$annotationReader = static::$container->get('test.autowiring_types.autowired_services')->getAnnotationReader();
36+
$annotationReader = self::getContainer()->get('test.autowiring_types.autowired_services')->getAnnotationReader();
3737
$this->assertInstanceOf(class_exists(PsrCachedReader::class) ? PsrCachedReader::class : CachedReader::class, $annotationReader);
3838
}
3939

4040
public function testEventDispatcherAutowiring()
4141
{
4242
static::bootKernel(['debug' => false]);
4343

44-
$autowiredServices = static::$container->get('test.autowiring_types.autowired_services');
44+
$autowiredServices = self::getContainer()->get('test.autowiring_types.autowired_services');
4545
$this->assertInstanceOf(EventDispatcher::class, $autowiredServices->getDispatcher(), 'The event_dispatcher service should be injected if the debug is not enabled');
4646

4747
static::bootKernel(['debug' => true]);
4848

49-
$autowiredServices = static::$container->get('test.autowiring_types.autowired_services');
49+
$autowiredServices = self::getContainer()->get('test.autowiring_types.autowired_services');
5050
$this->assertInstanceOf(TraceableEventDispatcher::class, $autowiredServices->getDispatcher(), 'The debug.event_dispatcher service should be injected if the debug is enabled');
5151
}
5252

5353
public function testCacheAutowiring()
5454
{
5555
static::bootKernel();
5656

57-
$autowiredServices = static::$container->get('test.autowiring_types.autowired_services');
57+
$autowiredServices = self::getContainer()->get('test.autowiring_types.autowired_services');
5858
$this->assertInstanceOf(FilesystemAdapter::class, $autowiredServices->getCachePool());
5959
}
6060

Tests/Functional/BundlePathsTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public function testBundlePublicDir()
4040
public function testBundleTwigTemplatesDir()
4141
{
4242
static::bootKernel(['test_case' => 'BundlePaths']);
43-
$twig = static::$container->get('twig.alias');
44-
$bundlesMetadata = static::$container->getParameter('kernel.bundles_metadata');
43+
$twig = static::getContainer()->get('twig.alias');
44+
$bundlesMetadata = static::getContainer()->getParameter('kernel.bundles_metadata');
4545

4646
$this->assertSame([$bundlesMetadata['LegacyBundle']['path'].'/Resources/views'], $twig->getLoader()->getPaths('Legacy'));
4747
$this->assertSame("OK\n", $twig->render('@Legacy/index.html.twig'));
@@ -53,7 +53,7 @@ public function testBundleTwigTemplatesDir()
5353
public function testBundleTranslationsDir()
5454
{
5555
static::bootKernel(['test_case' => 'BundlePaths']);
56-
$translator = static::$container->get('translator.alias');
56+
$translator = static::getContainer()->get('translator.alias');
5757

5858
$this->assertSame('OK', $translator->trans('ok_label', [], 'legacy'));
5959
$this->assertSame('OK', $translator->trans('ok_label', [], 'modern'));
@@ -62,7 +62,7 @@ public function testBundleTranslationsDir()
6262
public function testBundleValidationConfigDir()
6363
{
6464
static::bootKernel(['test_case' => 'BundlePaths']);
65-
$validator = static::$container->get('validator.alias');
65+
$validator = static::getContainer()->get('validator.alias');
6666

6767
$this->assertTrue($validator->hasMetadataFor(LegacyPerson::class));
6868
$this->assertCount(1, $constraintViolationList = $validator->validate(new LegacyPerson('john', 5)));
@@ -76,7 +76,7 @@ public function testBundleValidationConfigDir()
7676
public function testBundleSerializationConfigDir()
7777
{
7878
static::bootKernel(['test_case' => 'BundlePaths']);
79-
$serializer = static::$container->get('serializer.alias');
79+
$serializer = static::getContainer()->get('serializer.alias');
8080

8181
$this->assertEquals(['full_name' => 'john', 'age' => 5], $serializer->normalize(new LegacyPerson('john', 5), 'json'));
8282
$this->assertEquals(['full_name' => 'john', 'age' => 5], $serializer->normalize(new ModernPerson('john', 5), 'json'));

Tests/Functional/CachePoolClearCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function testClearFailed()
8080
{
8181
$tester = $this->createCommandTester();
8282
/** @var FilesystemAdapter $pool */
83-
$pool = static::$container->get('cache.public_pool');
83+
$pool = static::getContainer()->get('cache.public_pool');
8484
$item = $pool->getItem('foo');
8585
$item->set('baz');
8686
$pool->save($item);
@@ -108,7 +108,7 @@ public function testClearFailed()
108108
private function createCommandTester()
109109
{
110110
$application = new Application(static::$kernel);
111-
$application->add(new CachePoolClearCommand(static::$container->get('cache.global_clearer')));
111+
$application->add(new CachePoolClearCommand(static::getContainer()->get('cache.global_clearer')));
112112

113113
return new CommandTester($application->find('cache:pool:clear'));
114114
}

Tests/Functional/CachePoolsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function testRedisCustomCachePools()
7878
private function doTestCachePools($options, $adapterClass)
7979
{
8080
static::bootKernel($options);
81-
$container = static::$container;
81+
$container = static::getContainer();
8282

8383
$pool1 = $container->get('cache.pool1');
8484
$this->assertInstanceOf($adapterClass, $pool1);

Tests/Functional/ContainerDebugCommandTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ public function testDumpContainerIfNotExists()
2727
$application = new Application(static::$kernel);
2828
$application->setAutoExit(false);
2929

30-
@unlink(static::$container->getParameter('debug.container.dump'));
30+
@unlink(static::getContainer()->getParameter('debug.container.dump'));
3131

3232
$tester = new ApplicationTester($application);
3333
$tester->run(['command' => 'debug:container']);
3434

35-
$this->assertFileExists(static::$container->getParameter('debug.container.dump'));
35+
$this->assertFileExists(static::getContainer()->getParameter('debug.container.dump'));
3636
}
3737

3838
public function testNoDebug()
@@ -91,7 +91,7 @@ public function testDescribeEnvVars()
9191
$application = new Application(static::$kernel);
9292
$application->setAutoExit(false);
9393

94-
@unlink(static::$container->getParameter('debug.container.dump'));
94+
@unlink(static::getContainer()->getParameter('debug.container.dump'));
9595

9696
$tester = new ApplicationTester($application);
9797
$tester->run(['command' => 'debug:container', '--env-vars' => true], ['decorated' => false]);
@@ -128,7 +128,7 @@ public function testDescribeEnvVar()
128128
$application = new Application(static::$kernel);
129129
$application->setAutoExit(false);
130130

131-
@unlink(static::$container->getParameter('debug.container.dump'));
131+
@unlink(static::getContainer()->getParameter('debug.container.dump'));
132132

133133
$tester = new ApplicationTester($application);
134134
$tester->run(['command' => 'debug:container', '--env-var' => 'js'], ['decorated' => false]);
@@ -156,7 +156,7 @@ public function testGetDeprecation()
156156
$application = new Application(static::$kernel);
157157
$application->setAutoExit(false);
158158

159-
@unlink(static::$container->getParameter('debug.container.dump'));
159+
@unlink(static::getContainer()->getParameter('debug.container.dump'));
160160

161161
$tester = new ApplicationTester($application);
162162
$tester->run(['command' => 'debug:container', '--deprecations' => true]);
@@ -176,7 +176,7 @@ public function testGetDeprecationNone()
176176
$application = new Application(static::$kernel);
177177
$application->setAutoExit(false);
178178

179-
@unlink(static::$container->getParameter('debug.container.dump'));
179+
@unlink(static::getContainer()->getParameter('debug.container.dump'));
180180

181181
$tester = new ApplicationTester($application);
182182
$tester->run(['command' => 'debug:container', '--deprecations' => true]);
@@ -194,7 +194,7 @@ public function testGetDeprecationNoFile()
194194
$application = new Application(static::$kernel);
195195
$application->setAutoExit(false);
196196

197-
@unlink(static::$container->getParameter('debug.container.dump'));
197+
@unlink(static::getContainer()->getParameter('debug.container.dump'));
198198

199199
$tester = new ApplicationTester($application);
200200
$tester->run(['command' => 'debug:container', '--deprecations' => true]);

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
$this->createClient(['test_case' => 'ContainerDump', 'root_config' => 'config.yml']);
2222

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

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

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

0 commit comments

Comments
 (0)