Skip to content

Commit

Permalink
feat: Make AppKernel with KernelTestCase from the FrameworkBundle com…
Browse files Browse the repository at this point in the history
…patible

- Add deprecation layer for AppKernel constructor
- Add tests for usage with KernelTestCase
- Deprecate BaseBundleTestCase
- Update example in Readme
  • Loading branch information
chapterjason committed Jul 30, 2021
1 parent bcf6a3a commit 7739be9
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 43 deletions.
42 changes: 26 additions & 16 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,34 @@ $ composer require --dev nyholm/symfony-bundle-test

```php

use Nyholm\BundleTest\BaseBundleTestCase;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Nyholm\BundleTest\AppKernel;
use Acme\AcmeFooBundle;
use Acme\Service\Foo;

class BundleInitializationTest extends BaseBundleTestCase
class BundleInitializationTest extends KernelTestCase
{
protected function getBundleClass()
protected static function createKernel(array $options = [])
{
return AcmeFooBundle::class;
KernelTestCase::$class = AppKernel::class;

/**
* @var AppKernel $kernel
*/
$kernel = parent::createKernel($options);
$kernel->addBundle(AcmeFooBundle::class);
$kernel->handleOptions($options);

return $kernel;
}

public function testInitBundle()
{
// Boot the kernel.
$this->bootKernel();
self::bootKernel();

// Get the container
$container = $this->getContainer();
$container = self::getContainer();

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

public function testBundleWithDifferentConfiguration()
{
// Create a new Kernel
$kernel = $this->createKernel();

// Add some configuration
$kernel->addConfigFile(__DIR__.'/config.yml');

// Add some other bundles we depend on
$kernel->addBundle(OtherBundle::class);

// Boot the kernel as normal ...
$this->bootKernel();
$kernel = self::bootKernel([
'bundles' => [
// Add some other bundles we depend on
OtherBundle::class,
],
'configFiles' => [
// Add some configuration
__DIR__.'/config.yml',
],
]);

// ...
}
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
"require": {
"php": "^5.5 || ^7.0 || ^8.0",
"symfony/dependency-injection": "^3.4 || ^4.3 || ^5.0 || ^6.0",
"symfony/deprecation-contracts": "^2.4",
"symfony/framework-bundle": "^3.4 || ^4.3 || ^5.0 || ^6.0",
"symfony/http-kernel": "^3.4 || ^4.3 || ^5.0 || ^6.0",
"symfony/yaml": "^3.4 || ^4.3 || ^5.0 || ^6.0"
"symfony/yaml": "^3.4 || ^4.3 || ^5.0 || ^6.0",
},
"require-dev": {
"phpunit/phpunit": "^8.5 || ^9.4"
Expand Down
47 changes: 42 additions & 5 deletions src/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,23 @@ class AppKernel extends Kernel
private $routingFile = null;

/**
* @param string $cachePrefix
* {@inheritDoc}
*/
public function __construct($cachePrefix)
public function __construct(/*string $environment, bool $debug*/)
{
parent::__construct($cachePrefix, true);
$this->cachePrefix = $cachePrefix;
$this->addBundle(FrameworkBundle::class);
$args = \func_get_args();

if (1 === \func_num_args()) {
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);

parent::__construct($args[0], true);
} elseif (2 === \func_num_args()) {
parent::__construct($args[0], $args[1]);

$this->cachePrefix = uniqid('cache', true);
}

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

public function setCachePrefix($cachePrefix)
{
$this->cachePrefix = $cachePrefix;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -208,4 +222,27 @@ public function setRoutingFile($routingFile)
{
$this->routingFile = $routingFile;
}

public function handleOptions(array $options)
{
if (array_key_exists('bundles', $options)) {
foreach ($options['bundles'] as $bundle) {
$this->addBundle($bundle);
}
}

if (array_key_exists('configFiles', $options)) {
foreach ($options['configFiles'] as $bundle) {
$this->addConfigFile($bundle);
}
}

if (array_key_exists('compilerPasses', $options)) {
$this->addCompilerPasses($options['compilerPasses']);
}

if (array_key_exists('routingFile', $options)) {
$this->setRoutingFile($options['routingFile']);
}
}
}
5 changes: 5 additions & 0 deletions src/BaseBundleTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
namespace Nyholm\BundleTest;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ResettableContainerInterface;
use Symfony\Component\HttpKernel\Kernel;

trigger_deprecation('nyholm/symfony-bundle-test', '1.9', 'Deprecated since 1.9 and will be removed in 2.0, use %s instead.', KernelTestCase::class);

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*
* @deprecated Deprecated since 1.9 and will be removed in 2.0, use {@link KernelTestCase} instead.
*/
abstract class BaseBundleTestCase extends TestCase
{
Expand Down
46 changes: 31 additions & 15 deletions tests/Functional/BundleConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@

namespace Nyholm\BundleTest\Tests\Functional;

use Nyholm\BundleTest\BaseBundleTestCase;
use Nyholm\BundleTest\AppKernel;
use Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle\ConfigurationBundle;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* @author Laurent VOULLEMIER <laurent.voullemier@gmail.com>
*/
final class BundleConfigurationTest extends BaseBundleTestCase
final class BundleConfigurationTest extends KernelTestCase
{
protected function getBundleClass()
protected static function createKernel(array $options = [])
{
return ConfigurationBundle::class;
KernelTestCase::$class = AppKernel::class;

/**
* @var AppKernel $kernel
*/
$kernel = parent::createKernel($options);
$kernel->addBundle(ConfigurationBundle::class);
$kernel->handleOptions($options);

return $kernel;
}

public function provideBundleWithDifferentConfigurationFormats()
Expand All @@ -22,12 +32,17 @@ public function provideBundleWithDifferentConfigurationFormats()
[__DIR__.'/../Fixtures/Resources/ConfigurationBundle/config.yml'],
[__DIR__.'/../Fixtures/Resources/ConfigurationBundle/config.xml'],
[__DIR__.'/../Fixtures/Resources/ConfigurationBundle/config.php'],
[function (ContainerBuilder $container) {
$container->loadFromExtension('configuration', [
'foo' => 'val1',
'bar' => ['val2', 'val3'],
]);
}],
[
function (ContainerBuilder $container) {
$container->loadFromExtension(
'configuration',
[
'foo' => 'val1',
'bar' => ['val2', 'val3'],
]
);
},
],
];
}

Expand All @@ -38,10 +53,11 @@ public function provideBundleWithDifferentConfigurationFormats()
*/
public function testBundleWithDifferentConfigurationFormats($config)
{
$kernel = $this->createKernel();
$kernel->addConfigFile($config);
$this->bootKernel();
$this->assertEquals('val1', $kernel->getContainer()->getParameter('app.foo'));
$this->assertEquals(['val2', 'val3'], $kernel->getContainer()->getParameter('app.bar'));
self::bootKernel(['configFiles' => [$config]]);

$container = self::getContainer();

$this->assertEquals('val1', $container->getParameter('app.foo'));
$this->assertEquals(['val2', 'val3'], $container->getParameter('app.bar'));
}
}
21 changes: 15 additions & 6 deletions tests/Functional/BundleInitializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,32 @@

namespace Nyholm\BundleTest\Tests\Functional;

use Nyholm\BundleTest\BaseBundleTestCase;
use Nyholm\BundleTest\AppKernel;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class BundleInitializationTest extends BaseBundleTestCase
class BundleInitializationTest extends KernelTestCase
{
protected function getBundleClass()
protected static function createKernel(array $options = [])
{
return FrameworkBundle::class;
KernelTestCase::$class = AppKernel::class;

/**
* @var AppKernel $kernel
*/
$kernel = parent::createKernel($options);
$kernel->addBundle(FrameworkBundle::class);

return $kernel;
}

public function testRegisterBundle()
{
$this->bootKernel();
$container = $this->getContainer();
self::bootKernel();
$container = self::getContainer();
$this->assertTrue($container->has('kernel'));
}
}
47 changes: 47 additions & 0 deletions tests/Functional/LegacyBundleConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Nyholm\BundleTest\Tests\Functional;

use Nyholm\BundleTest\BaseBundleTestCase;
use Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle\ConfigurationBundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* @author Laurent VOULLEMIER <laurent.voullemier@gmail.com>
*/
final class LegacyBundleConfigurationTest extends BaseBundleTestCase
{
protected function getBundleClass()
{
return ConfigurationBundle::class;
}

public function provideBundleWithDifferentConfigurationFormats()
{
return [
[__DIR__.'/../Fixtures/Resources/ConfigurationBundle/config.yml'],
[__DIR__.'/../Fixtures/Resources/ConfigurationBundle/config.xml'],
[__DIR__.'/../Fixtures/Resources/ConfigurationBundle/config.php'],
[function (ContainerBuilder $container) {
$container->loadFromExtension('configuration', [
'foo' => 'val1',
'bar' => ['val2', 'val3'],
]);
}],
];
}

/**
* @dataProvider provideBundleWithDifferentConfigurationFormats
*
* @param string|callable $config
*/
public function testBundleWithDifferentConfigurationFormats($config)
{
$kernel = $this->createKernel();
$kernel->addConfigFile($config);
$this->bootKernel();
$this->assertEquals('val1', $kernel->getContainer()->getParameter('app.foo'));
$this->assertEquals(['val2', 'val3'], $kernel->getContainer()->getParameter('app.bar'));
}
}
24 changes: 24 additions & 0 deletions tests/Functional/LegacyBundleInitializationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Nyholm\BundleTest\Tests\Functional;

use Nyholm\BundleTest\BaseBundleTestCase;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class LegacyBundleInitializationTest extends BaseBundleTestCase
{
protected function getBundleClass()
{
return FrameworkBundle::class;
}

public function testRegisterBundle()
{
$this->bootKernel();
$container = $this->getContainer();
$this->assertTrue($container->has('kernel'));
}
}

0 comments on commit 7739be9

Please sign in to comment.