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
42 changes: 26 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,14 @@ 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(['config' => static function(AppKernel $kernel){
// Add some other bundles we depend on
$kernel->addBundle(OtherBundle::class);

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

// ...
}
Expand Down
18 changes: 13 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 @@ -208,4 +209,11 @@ public function setRoutingFile($routingFile)
{
$this->routingFile = $routingFile;
}

public function handleOptions(array $options): void
{
if (array_key_exists('config', $options) && is_callable($configCallable = $options['config'])) {
$configCallable($this);
}
}
}
113 changes: 0 additions & 113 deletions src/BaseBundleTestCase.php

This file was deleted.

31 changes: 22 additions & 9 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 @@ -38,10 +48,13 @@ 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(['config' => function (AppKernel $kernel) use ($config) {
$kernel->addConfigFile($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'));
}
}
27 changes: 22 additions & 5 deletions tests/Functional/BundleRoutingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,35 @@

namespace Nyholm\BundleTest\Tests\Functional;

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

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

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

return $kernel;
}

public function testSetRoutingFile()
{
$this->setRoutingFile(__DIR__.'/../Fixtures/Resources/Routing/routes.yml');
$kernel = self::bootKernel([
'config' => static function (AppKernel $kernel) {
$kernel->setRoutingFile(__DIR__.'/../Fixtures/Resources/Routing/routes.yml');
},
]);

$this->bootKernel();
$container = $this->getContainer();
$container = $kernel->getContainer();
$container = $container->get('test.service_container');
/**
* @var RouterInterface $router
Expand Down