Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make AppKernel with KernelTestCase compatible #44

Merged
merged 12 commits into from
Aug 19, 2021
45 changes: 29 additions & 16 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,37 @@ $ 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();
$kernel = self::bootKernel();

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

// Or for FrameworkBundle@^5.3.6 to access private services without the PublicCompilerPass
// $container = self::getContainer();

// Test if you services exists
$this->assertTrue($container->has('acme.foo'));
Expand All @@ -52,17 +65,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',
chapterjason marked this conversation as resolved.
Show resolved Hide resolved
],
]);

// ...
}
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,15 @@ 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);
parent::__construct($environment, $debug);

$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 +180,11 @@ public function loadRoutes(LoaderInterface $loader)
return $routes->build();
}

public function setCachePrefix($cachePrefix)
chapterjason marked this conversation as resolved.
Show resolved Hide resolved
{
$this->cachePrefix = $cachePrefix;
}

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

public function handleOptions(array $options)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a new feature you snuck in. Why is it better to have an array of values rather than calling the underlying methods?

Copy link
Contributor Author

@chapterjason chapterjason Aug 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄 Yeah.

I liked the idea of the options in the KernelTestCase, you are right, the methods are better.

The reason why I introduced this is: You only have the possibility to change the config by override the createKernel this limits it to a configuration per file and not per test. (If you don't want to boot it yourself and just want to use static:bootKernel.)

By just call the handleOptions in the createKernel you can actually passthrough everything before the kernel is booted and the container is compiled.

Another Idea, that just came in my mind, we could just create a option like config which must be a callable:

static::bootKernel(['config' => static function(AppKernel $kernel){
    $kernel->setRoutingFile(__DIR__.'/Fixtures/routes.yml');
}]);

Then we can benefit from the methods and save some lines, cause the KernelTestCase makes the booting process.

{
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']);
}

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

if (array_key_exists('projectDir', $options)) {
$this->setProjectDir($options['projectDir']);
}
}
}
113 changes: 0 additions & 113 deletions src/BaseBundleTestCase.php

This file was deleted.

45 changes: 30 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'],
]
);
},
],
chapterjason marked this conversation as resolved.
Show resolved Hide resolved
];
}

Expand All @@ -38,10 +53,10 @@ 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'));
$kernel = self::bootKernel(['configFiles' => [$config]]);
$container = $kernel->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();
$kernel = self::bootKernel();
$container = $kernel->getContainer();
$this->assertTrue($container->has('kernel'));
}
}
Loading