8000 feat: Make AppKernel with KernelTestCase from the FrameworkBundle com… · SymfonyTest/symfony-bundle-test@7739be9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7739be9

Browse files
committed
feat: Make AppKernel with KernelTestCase from the FrameworkBundle compatible
- Add deprecation layer for AppKernel constructor - Add tests for usage with KernelTestCase - Deprecate BaseBundleTestCase - Update example in Readme
1 parent bcf6a3a commit 7739be9

8 files changed

+192
-43
lines changed

Readme.md

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,34 @@ $ composer require --dev nyholm/symfony-bundle-test
2525

2626
```php
2727

28-
use Nyholm\BundleTest\BaseBundleTestCase;
28+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
29+
use Nyholm\BundleTest\AppKernel;
2930
use Acme\AcmeFooBundle;
3031
use Acme\Service\Foo;
3132

32-
class BundleInitializationTest extends BaseBundleTestCase
33+
class BundleInitializationTest extends KernelTestCase
3334
{
34-
protected function getBundleClass()
35+
protected static function createKernel(array $options = [])
3536
{
36-
return AcmeFooBundle::class;
37+
KernelTestCase::$class = AppKernel::class;
38+
39+
/**
40+
* @var AppKernel $kernel
41+
*/
42+
$kernel = parent::createKernel($options);
43+
$kernel->addBundle(AcmeFooBundle::class);
44+
$kernel->handleOptions($options);
45+
46+
return $kernel;
3747
}
3848

3949
public function testInitBundle()
4050
{
4151
// Boot the kernel.
42-
$this->bootKernel();
52+
self::bootKernel();
4353

4454
// Get the container
45-
$container = $this->getContainer();
55+
$container = self::getContainer();
4656

4757
// Test if you services exists
4858
$this->assertTrue($container->has('acme.foo'));
@@ -52,17 +62,17 @@ class BundleInitializationTest extends BaseBundleTestCase
5262

5363
public function testBundleWithDifferentConfiguration()
5464
{
55-
// Create a new Kernel
56-
$kernel = $this->createKernel();
57-
58-
// Add some configuration
59-
$kernel->addConfigFile(__DIR__.'/config.yml');
60-
61-
// Add some other bundles we depend on
62-
$kernel->addBundle(OtherBundle::class);
63-
6465
// Boot the kernel as normal ...
65-
$this->bootKernel();
66+
$kernel = self::bootKernel([
67+
'bundles' => [
68+
// Add some other bundles we depend on
69+
OtherBundle::class,
70+
],
71+
'configFiles' => [
72+
// Add some configuration
73+
__DIR__.'/config.yml',
74+
],
75+
]);
6676

6777
// ...
6878
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
"require": {
1313
"php": "^5.5 || ^7.0 || ^8.0",
1414
"symfony/dependency-injection": "^3.4 || ^4.3 || ^5.0 || ^6.0",
15+
"symfony/deprecation-contracts": "^2.4",
1516
"symfony/framework-bundle": "^3.4 || ^4.3 || ^5.0 || ^6.0",
1617
"symfony/http-kernel": "^3.4 || ^4.3 || ^5.0 || ^6.0",
17-
"symfony/yaml": "^3.4 || ^4.3 || ^5.0 || ^6.0"
18+
"symfony/yaml": "^3.4 || ^4.3 || ^5.0 || ^6.0",
1819
},
1920
"require-dev": {
2021
"phpunit/phpunit": "^8.5 || ^9.4"

src/AppKernel.php

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,23 @@ class AppKernel extends Kernel
4646
private $routingFile = null;
4747

4848
/**
49-
* @param string $cachePrefix
49+
* {@inheritDoc}
5050
*/
51-
public function __construct($cachePrefix)
51+
public function __construct(/*string $environment, bool $debug*/)
5252
{
53-
parent::__construct($cachePrefix, true);
54-
$this->cachePrefix = $cachePrefix;
55-
$this->addBundle(FrameworkBundle::class);
53+
$args = \func_get_args();
54+
55+
if (1 === \func_num_args()) {
56+
trigger_deprecation('nyholm/symfony-bundle-test', '1.9', 'The signature of the "%s($cachePrefix)" constructor is deprecated, use the constructor with 2 arguments: "string $environment, bool $debug".', self::class);
57+
58+
parent::__construct($args[0], true);
59+
} elseif (2 === \func_num_args()) {
60+
parent::__construct($args[0], $args[1]);
61+
62+
$this->cachePrefix = uniqid('cache', true);
63+
}
5664

65+
$this->addBundle(FrameworkBundle::class);
5766
$this->addConfigFile(__DIR__.'/config/framework.yml');
5867
if (class_exists(ConfigBuilderCacheWarmer::class)) {
5968
$this->addConfigFile(__DIR__.'/config/framework-53.yml');
@@ -179,6 +188,11 @@ public function loadRoutes(LoaderInterface $loader)
179188
return $routes->build();
180189
}
181190

191+
public function setCachePrefix($cachePrefix)
192+
{
193+
$this->cachePrefix = $cachePrefix;
194+
}
195+
182196
/**
183197
* {@inheritdoc}
184198
*/
@@ -208,4 +222,27 @@ public function setRoutingFile($routingFile)
208222
{
209223
$this->routingFile = $routingFile;
210224
}
225+
226+
public function handleOptions(array $options)
227+
{
228+
if (array_key_exists('bundles', $options)) {
229+
foreach ($options['bundles'] as $bundle) {
230+
$this->addBundle($bundle);
231+
}
232+
}
233+
234+
if (array_key_exists('configFiles', $options)) {
235+
foreach ($options['configFiles'] as $bundle) {
236+
$this->addConfigFile($bundle);
237+
}
238+
}
239+
240+
if (array_key_exists('compilerPasses', $options)) {
241+
$this->addCompilerPasses($options['compilerPasses']);
242+
}
243+
244+
if (array_key_exists('routingFile', $options)) {
245+
$this->setRoutingFile($options['routingFile']);
246+
}
247+
}
211248
}

src/BaseBundleTestCase.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
namespace Nyholm\BundleTest;
44

55
use PHPUnit\Framework\TestCase;
6+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
67
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
78
use Symfony\Component\DependencyInjection\ResettableContainerInterface;
89
use Symfony\Component\HttpKernel\Kernel;
910

11+
trigger_deprecation('nyholm/symfony-bundle-test', '1.9', 'Deprecated since 1.9 and will be removed in 2.0, use %s instead.', KernelTestCase::class);
12+
1013
/**
1114
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
15+
*
16+
* @deprecated Deprecated since 1.9 and will be removed in 2.0, use {@link KernelTestCase} instead.
1217
*/
1318
abstract class BaseBundleTestCase extends TestCase
1419
{

tests/Functional/BundleConfigurationTest.php

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,28 @@
22

33
namespace Nyholm\BundleTest\Tests\Functional;
44

5-
use Nyholm\BundleTest\BaseBundleTestCase;
5+
use Nyholm\BundleTest\AppKernel;
66
use Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle\ConfigurationBundle;
7+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
78
use Symfony\Component\DependencyInjection\ContainerBuilder;
89

910
/**
1011
* @author Laurent VOULLEMIER <laurent.voullemier@gmail.com>
1112
*/
12-
final class BundleConfigurationTest extends BaseBundleTestCase
13+
final class BundleConfigurationTest extends KernelTestCase
1314
{
14-
protected function getBundleClass()
15+
protected static function createKernel(array $options = [])
1516
{
16-
return ConfigurationBundle::class;
17+
KernelTestCase::$class = AppKernel::class;
18+
19+
/**
20+
* @var AppKernel $kernel
21+
*/
22+
$kernel = parent::createKernel($options);
23+
$kernel->addBundle(ConfigurationBundle::class);
24+
$kernel->handleOptions($options);
25+
26+
return $kernel;
1727
}
1828

1929
public function provideBundleWithDifferentConfigurationFormats()
@@ -22,12 +32,17 @@ public function provideBundleWithDifferentConfigurationFormats()
2232
[__DIR__.'/../Fixtures/Resources/ConfigurationBundle/config.yml'],
2333
[__DIR__.'/../Fixtures/Resources/ConfigurationBundle/config.xml'],
2434
[__DIR__.'/../Fixtures/Resources/ConfigurationBundle/config.php'],
25-
[function (ContainerBuilder $container) {
26-
$container->loadFromExtension('configuration', [
27-
'foo' => 'val1',
28-
'bar' => ['val2', 'val3'],
29-
]);
30-
}],
35+
[
36+
function (ContainerBuilder $container) {
37+
$container->loadFromExtension(
38+
'configuration',
39+
[
40+
'foo' => 'val1',
41+
'bar' => ['val2', 'val3'],
42+
]
43+
);
44+
},
45+
],
3146
];
3247
}
3348

@@ -38,10 +53,11 @@ public function provideBundleWithDifferentConfigurationFormats()
3853
*/
3954
public function testBundleWithDifferentConfigurationFormats($config)
4055
{
41-
$kernel = $this->createKernel();
42-
$kernel->addConfigFile($config);
43-
$this->bootKernel();
44-
$this->assertEquals('val1', $kernel->getContainer()->getParameter('app.foo'));
45-
$this->assertEquals(['val2', 'val3'], $kernel->getContainer()->getParameter('app.bar'));
56+
self::bootKernel(['configFiles' => [$config]]);
57+
58+
$container = self::getContainer();
59+
60+
$this->assertEquals('val1', $container->getParameter('app.foo'));
61+
$this->assertEquals(['val2', 'val3'], $container->getParameter('app.bar'));
4662
}
4763
}

tests/Functional/BundleInitializationTest.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,32 @@
22

33
namespace Nyholm\BundleTest\Tests\Functional;
44

5-
use Nyholm\BundleTest\BaseBundleTestCase;
5+
use Nyholm\BundleTest\AppKernel;
66
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
7+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
78

89
/**
910
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
1011
*/
11-
class BundleInitializationTest extends BaseBundleTestCase
12+
class BundleInitializationTest extends KernelTestCase
1213
{
13-
protected function getBundleClass()
14+
protected static function createKernel(array $options = [])
1415
{
15-
return FrameworkBundle::class;
16+
KernelTestCase::$class = AppKernel::class;
17+
18+
/**
19+
* @var AppKernel $kernel
20+
*/
21+
$kernel = parent::createKernel($options);
22+
$kernel->addBundle(FrameworkBundle::class);
23+
24+
return $kernel;
1625
}
1726

1827
public function testRegisterBundle()
1928
{
20-
$this->bootKernel();
21-
$container = $this->getContainer();
29+
self::bootKernel();
30+
$container = self::getContainer();
2231
$this->assertTrue($container->has('kernel'));
2332
}
2433
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Nyholm\BundleTest\Tests\Functional;
4+
5+
use Nyholm\BundleTest\BaseBundleTestCase;
6+
use Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle\ConfigurationBundle;
7+
use Symfony\Component\DependencyInjection\ContainerBuilder;
8+
9+
/**
10+
* @author Laurent VOULLEMIER <laurent.voullemier@gmail.com>
11+
*/
12+
final class LegacyBundleConfigurationTest extends BaseBundleTestCase
13+
{
14+
protected function getBundleClass()
15+
{
16+
return ConfigurationBundle::class;
17+
}
18+
19+
public function provideBundleWithDifferentConfigurationFormats()
20+
{
21+
return [
22+
[__DIR__.'/../Fixtures/Resources/ConfigurationBundle/config.yml'],
23+
[__DIR__.'/../Fixtures/Resources/ConfigurationBundle/config.xml'],
24+
[__DIR__.'/../Fixtures/Resources/ConfigurationBundle/config.php'],
25+
[function (ContainerBuilder $container) {
26+
$container->loadFromExtension('configuration', [
27+
'foo' => 'val1',
28+
'bar' => ['val2', 'val3'],
29+
]);
30+
}],
31+
];
32+
}
33+
34+
/**
35+
* @dataProvider provideBundleWithDifferentConfigurationFormats
36+
*
37+
* @param string|callable $config
38+
*/
39+
public function testBundleWithDifferentConfigurationFormats($config)
40+
{
41+
$kernel = $this->createKernel();
42+
$kernel->addConfigFile($config);
43+
$this->bootKernel();
44+
$this->assertEquals('val1', $kernel->getContainer()->getParameter('app.foo'));
45+
$this->assertEquals(['val2', 'val3'], $kernel->getContainer()->getParameter('app.bar'));
46+
}
47+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Nyholm\BundleTest\Tests\Functional;
4+
5+
use Nyholm\BundleTest\BaseBundleTestCase;
6+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
7+
8+
/**
9+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
10+
*/
11+
class LegacyBundleIni 7D31 tializationTest extends BaseBundleTestCase
12+
{
13+
protected function getBundleClass()
14+
{
15+
return FrameworkBundle::class;
16+
}
17+
18+
public function testRegisterBundle()
19+
{
20+
$this->bootKernel();
21+
$container = $this->getContainer();
22+
$this->assertTrue($container->has('kernel'));
23+
}
24+
}

0 commit comments

Comments
 (0)
0