From 49b1de0dda0edb3b0e7a34b09763f56981e97241 Mon Sep 17 00:00:00 2001 From: chapterjason Date: Fri, 30 Jul 2021 06:18:23 +0200 Subject: [PATCH 01/12] 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 --- Readme.md | 42 ++++++++++------- src/AppKernel.php | 47 +++++++++++++++++-- src/BaseBundleTestCase.php | 5 ++ tests/Functional/BundleConfigurationTest.php | 46 ++++++++++++------ tests/Functional/BundleInitializationTest.php | 21 ++++++--- .../LegacyBundleConfigurationTest.php | 47 +++++++++++++++++++ .../LegacyBundleInitializationTest.php | 24 ++++++++++ 7 files changed, 190 insertions(+), 42 deletions(-) create mode 100644 tests/Functional/LegacyBundleConfigurationTest.php create mode 100644 tests/Functional/LegacyBundleInitializationTest.php diff --git a/Readme.md b/Readme.md index 1bc31d3..747e881 100644 --- a/Readme.md +++ b/Readme.md @@ -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')); @@ -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', + ], + ]); // ... } diff --git a/src/AppKernel.php b/src/AppKernel.php index 6a6446b..1770672 100644 --- a/src/AppKernel.php +++ b/src/AppKernel.php @@ -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'); @@ -179,6 +188,11 @@ public function loadRoutes(LoaderInterface $loader) return $routes->build(); } + public function setCachePrefix($cachePrefix) + { + $this->cachePrefix = $cachePrefix; + } + /** * {@inheritdoc} */ @@ -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']); + } + } } diff --git a/src/BaseBundleTestCase.php b/src/BaseBundleTestCase.php index 4c75ffd..b24b40a 100644 --- a/src/BaseBundleTestCase.php +++ b/src/BaseBundleTestCase.php @@ -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 + * + * @deprecated Deprecated since 1.9 and will be removed in 2.0, use {@link KernelTestCase} instead. */ abstract class BaseBundleTestCase extends TestCase { diff --git a/tests/Functional/BundleConfigurationTest.php b/tests/Functional/BundleConfigurationTest.php index 6cc31a6..9a0c4d9 100644 --- a/tests/Functional/BundleConfigurationTest.php +++ b/tests/Functional/BundleConfigurationTest.php @@ -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 */ -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() @@ -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'], + ] + ); + }, + ], ]; } @@ -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')); } } diff --git a/tests/Functional/BundleInitializationTest.php b/tests/Functional/BundleInitializationTest.php index 9a9977a..d5ef7cd 100644 --- a/tests/Functional/BundleInitializationTest.php +++ b/tests/Functional/BundleInitializationTest.php @@ -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 */ -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')); } } diff --git a/tests/Functional/LegacyBundleConfigurationTest.php b/tests/Functional/LegacyBundleConfigurationTest.php new file mode 100644 index 0000000..2effe41 --- /dev/null +++ b/tests/Functional/LegacyBundleConfigurationTest.php @@ -0,0 +1,47 @@ + + */ +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')); + } +} diff --git a/tests/Functional/LegacyBundleInitializationTest.php b/tests/Functional/LegacyBundleInitializationTest.php new file mode 100644 index 0000000..1425c8b --- /dev/null +++ b/tests/Functional/LegacyBundleInitializationTest.php @@ -0,0 +1,24 @@ + + */ +class LegacyBundleInitializationTest extends BaseBundleTestCase +{ + protected function getBundleClass() + { + return FrameworkBundle::class; + } + + public function testRegisterBundle() + { + $this->bootKernel(); + $container = $this->getContainer(); + $this->assertTrue($container->has('kernel')); + } +} From bf8507a36aa951da7306ce35acb32c4acc0431f4 Mon Sep 17 00:00:00 2001 From: chapterjason Date: Fri, 30 Jul 2021 07:08:59 +0200 Subject: [PATCH 02/12] feat: Add cachePrefix in handleOptions --- src/AppKernel.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/AppKernel.php b/src/AppKernel.php index 1770672..29ea46d 100644 --- a/src/AppKernel.php +++ b/src/AppKernel.php @@ -244,5 +244,9 @@ public function handleOptions(array $options) if (array_key_exists('routingFile', $options)) { $this->setRoutingFile($options['routingFile']); } + + if (array_key_exists('cachePrefix', $options)) { + $this->setCachePrefix($options['cachePrefix']); + } } } From db7a1ff51b5b925bdf228bdfe6290b8b2b100695 Mon Sep 17 00:00:00 2001 From: chapterjason Date: Fri, 30 Jul 2021 07:46:12 +0200 Subject: [PATCH 03/12] test: Fix for older KernelTestCase --- Readme.md | 7 +++++-- tests/Functional/BundleConfigurationTest.php | 5 ++--- tests/Functional/BundleInitializationTest.php | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Readme.md b/Readme.md index 747e881..7e84015 100644 --- a/Readme.md +++ b/Readme.md @@ -49,10 +49,13 @@ class BundleInitializationTest extends KernelTestCase public function testInitBundle() { // Boot the kernel. - self::bootKernel(); + $kernel = self::bootKernel(); // Get the container - $container = self::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')); diff --git a/tests/Functional/BundleConfigurationTest.php b/tests/Functional/BundleConfigurationTest.php index 9a0c4d9..e697626 100644 --- a/tests/Functional/BundleConfigurationTest.php +++ b/tests/Functional/BundleConfigurationTest.php @@ -53,9 +53,8 @@ function (ContainerBuilder $container) { */ public function testBundleWithDifferentConfigurationFormats($config) { - self::bootKernel(['configFiles' => [$config]]); - - $container = self::getContainer(); + $kernel = self::bootKernel(['configFiles' => [$config]]); + $container = $kernel->getContainer(); $this->assertEquals('val1', $container->getParameter('app.foo')); $this->assertEquals(['val2', 'val3'], $container->getParameter('app.bar')); diff --git a/tests/Functional/BundleInitializationTest.php b/tests/Functional/BundleInitializationTest.php index d5ef7cd..8220d84 100644 --- a/tests/Functional/BundleInitializationTest.php +++ b/tests/Functional/BundleInitializationTest.php @@ -26,8 +26,8 @@ protected static function createKernel(array $options = []) public function testRegisterBundle() { - self::bootKernel(); - $container = self::getContainer(); + $kernel = self::bootKernel(); + $container = $kernel->getContainer(); $this->assertTrue($container->has('kernel')); } } From c4afea5e8d03ce11d00d2e41b8c8ab6cd074786b Mon Sep 17 00:00:00 2001 From: chapterjason Date: Mon, 2 Aug 2021 03:18:52 +0200 Subject: [PATCH 04/12] enhance: Improve deprecation logging --- src/AppKernel.php | 2 +- src/BaseBundleTestCase.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AppKernel.php b/src/AppKernel.php index 29ea46d..115fa7c 100644 --- a/src/AppKernel.php +++ b/src/AppKernel.php @@ -53,7 +53,7 @@ public function __construct(/*string $environment, bool $debug*/) $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); + printf('The signature of the method "%s($cachePrefix)" is deprecated since 1.9 and will be removed in 2.0, use with 2 arguments instead: "string $environment, bool $debug".', __METHOD__); parent::__construct($args[0], true); } elseif (2 === \func_num_args()) { diff --git a/src/BaseBundleTestCase.php b/src/BaseBundleTestCase.php index b24b40a..94732e6 100644 --- a/src/BaseBundleTestCase.php +++ b/src/BaseBundleTestCase.php @@ -8,7 +8,7 @@ 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); +printf('%s is deprecated since 1.9 and will be removed in 2.0, use "%s" instead.', BaseBundleTestCase::class, KernelTestCase::class); /** * @author Tobias Nyholm From 8ff35b8a8f0e70acf85a002ae7b8f6bcb0364d11 Mon Sep 17 00:00:00 2001 From: chapterjason Date: Mon, 2 Aug 2021 03:29:48 +0200 Subject: [PATCH 05/12] cut: Remove deprecation reporting --- src/BaseBundleTestCase.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/BaseBundleTestCase.php b/src/BaseBundleTestCase.php index 94732e6..40bc3f9 100644 --- a/src/BaseBundleTestCase.php +++ b/src/BaseBundleTestCase.php @@ -8,8 +8,6 @@ use Symfony\Component\DependencyInjection\ResettableContainerInterface; use Symfony\Component\HttpKernel\Kernel; -printf('%s is deprecated since 1.9 and will be removed in 2.0, use "%s" instead.', BaseBundleTestCase::class, KernelTestCase::class); - /** * @author Tobias Nyholm * From 0c13b2ed9f834925834c941495ae5b5afe8e976a Mon Sep 17 00:00:00 2001 From: chapterjason Date: Mon, 2 Aug 2021 20:46:45 +0200 Subject: [PATCH 06/12] cut: Remove legacy BaseBundleTestCase --- src/BaseBundleTestCase.php | 116 ------------------ .../LegacyBundleConfigurationTest.php | 47 ------- .../LegacyBundleInitializationTest.php | 24 ---- 3 files changed, 187 deletions(-) delete mode 100644 src/BaseBundleTestCase.php delete mode 100644 tests/Functional/LegacyBundleConfigurationTest.php delete mode 100644 tests/Functional/LegacyBundleInitializationTest.php diff --git a/src/BaseBundleTestCase.php b/src/BaseBundleTestCase.php deleted file mode 100644 index 40bc3f9..0000000 --- a/src/BaseBundleTestCase.php +++ /dev/null @@ -1,116 +0,0 @@ - - * - * @deprecated Deprecated since 1.9 and will be removed in 2.0, use {@link KernelTestCase} instead. - */ -abstract class BaseBundleTestCase extends TestCase -{ - /** - * @var AppKernel - */ - private $kernel; - - /** - * @var CompilerPassInterface[] - */ - private $compilerPasses = []; - - /** - * @return string - */ - abstract protected function getBundleClass(); - - /** - * @var string|null - */ - private $routingFile = null; - - /** - * Boots the Kernel for this test. - * - * @param array $options - */ - protected function bootKernel() - { - $this->ensureKernelShutdown(); - - if (null === $this->kernel) { - $this->createKernel(); - } - - $this->kernel->boot(); - } - - /** - * @return \Symfony\Component\DependencyInjection\ContainerInterface - */ - protected function getContainer() - { - return $this->kernel->getContainer(); - } - - /** - * Get a kernel which you may configure with your bundle and services. - * - * @return AppKernel - */ - protected function createKernel() - { - if (!class_exists(Kernel::class)) { - throw new \LogicException('You must install symfony/symfony to run the bundle test.'); - } - - require_once __DIR__.'/AppKernel.php'; - $class = 'Nyholm\BundleTest\AppKernel'; - - $this->kernel = new $class(uniqid('cache')); - $this->kernel->addBundle($this->getBundleClass()); - $this->kernel->addCompilerPasses($this->compilerPasses); - $this->kernel->setRoutingFile($this->routingFile); - - return $this->kernel; - } - - /** - * Shuts the kernel down if it was used in the test. - * - * @after - */ - public function ensureKernelShutdown() - { - if (null !== $this->kernel) { - try { - $container = $this->kernel->getContainer(); - } catch (\LogicException $e) { - $container = null; - } - $this->kernel->shutdown(); - if ($container instanceof ResettableContainerInterface) { - $container->reset(); - } - } - } - - protected function addCompilerPass(CompilerPassInterface $compilerPass) - { - $this->compilerPasses[] = $compilerPass; - } - - /** - * @param string|null $routingFile - */ - public function setRoutingFile($routingFile) - { - $this->routingFile = $routingFile; - } -} diff --git a/tests/Functional/LegacyBundleConfigurationTest.php b/tests/Functional/LegacyBundleConfigurationTest.php deleted file mode 100644 index 2effe41..0000000 --- a/tests/Functional/LegacyBundleConfigurationTest.php +++ /dev/null @@ -1,47 +0,0 @@ - - */ -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')); - } -} diff --git a/tests/Functional/LegacyBundleInitializationTest.php b/tests/Functional/LegacyBundleInitializationTest.php deleted file mode 100644 index 1425c8b..0000000 --- a/tests/Functional/LegacyBundleInitializationTest.php +++ /dev/null @@ -1,24 +0,0 @@ - - */ -class LegacyBundleInitializationTest extends BaseBundleTestCase -{ - protected function getBundleClass() - { - return FrameworkBundle::class; - } - - public function testRegisterBundle() - { - $this->bootKernel(); - $container = $this->getContainer(); - $this->assertTrue($container->has('kernel')); - } -} From 424499c3837e76e12b2cbb572ac6f191d69065db Mon Sep 17 00:00:00 2001 From: chapterjason Date: Mon, 2 Aug 2021 20:48:18 +0200 Subject: [PATCH 07/12] cut: Remove deprecation layer --- src/AppKernel.php | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/AppKernel.php b/src/AppKernel.php index 115fa7c..f642034 100644 --- a/src/AppKernel.php +++ b/src/AppKernel.php @@ -48,19 +48,11 @@ class AppKernel extends Kernel /** * {@inheritDoc} */ - public function __construct(/*string $environment, bool $debug*/) + public function __construct(string $environment, bool $debug) { - $args = \func_get_args(); + parent::__construct($environment, $debug); - if (1 === \func_num_args()) { - printf('The signature of the method "%s($cachePrefix)" is deprecated since 1.9 and will be removed in 2.0, use with 2 arguments instead: "string $environment, bool $debug".', __METHOD__); - - parent::__construct($args[0], true); - } elseif (2 === \func_num_args()) { - parent::__construct($args[0], $args[1]); - - $this->cachePrefix = uniqid('cache', true); - } + $this->cachePrefix = uniqid('cache', true); $this->addBundle(FrameworkBundle::class); $this->addConfigFile(__DIR__.'/config/framework.yml'); @@ -248,5 +240,9 @@ public function handleOptions(array $options) if (array_key_exists('cachePrefix', $options)) { $this->setCachePrefix($options['cachePrefix']); } + + if (array_key_exists('projectDir', $options)) { + $this->setProjectDir($options['projectDir']); + } } } From 2826783550914c4363f0bef7e0c762089153d0f8 Mon Sep 17 00:00:00 2001 From: chapterjason Date: Mon, 2 Aug 2021 20:51:30 +0200 Subject: [PATCH 08/12] test: Update test --- tests/Functional/BundleRoutingTest.php | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/tests/Functional/BundleRoutingTest.php b/tests/Functional/BundleRoutingTest.php index ba17b87..b264148 100644 --- a/tests/Functional/BundleRoutingTest.php +++ b/tests/Functional/BundleRoutingTest.php @@ -2,18 +2,34 @@ namespace Nyholm\BundleTest\Tests\Functional; -use Nyholm\BundleTest\BaseBundleTestCase; +use Nyholm\BundleTest\AppKernel; +use Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle\ConfigurationBundle; 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'); + self::bootKernel([ + 'routingFile' => __DIR__.'/../Fixtures/Resources/Routing/routes.yml', + ]); - $this->bootKernel(); - $container = $this->getContainer(); + $container = self::getContainer(); $container = $container->get('test.service_container'); /** * @var RouterInterface $router From a77203faf56fcc6bfb51b07925df31d4e09a42f0 Mon Sep 17 00:00:00 2001 From: chapterjason Date: Mon, 2 Aug 2021 20:54:04 +0200 Subject: [PATCH 09/12] test: Fix routing tests --- tests/Functional/BundleRoutingTest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/Functional/BundleRoutingTest.php b/tests/Functional/BundleRoutingTest.php index b264148..6ef24ae 100644 --- a/tests/Functional/BundleRoutingTest.php +++ b/tests/Functional/BundleRoutingTest.php @@ -3,7 +3,6 @@ namespace Nyholm\BundleTest\Tests\Functional; use Nyholm\BundleTest\AppKernel; -use Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle\ConfigurationBundle; use Symfony\Bundle\FrameworkBundle\FrameworkBundle; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Component\Routing\RouterInterface; @@ -25,11 +24,11 @@ protected static function createKernel(array $options = []) public function testSetRoutingFile() { - self::bootKernel([ + $kernel = self::bootKernel([ 'routingFile' => __DIR__.'/../Fixtures/Resources/Routing/routes.yml', ]); - $container = self::getContainer(); + $container = $kernel->getContainer(); $container = $container->get('test.service_container'); /** * @var RouterInterface $router From f5dab953ab5725354ffb8563839e50809e80ded1 Mon Sep 17 00:00:00 2001 From: chapterjason Date: Tue, 3 Aug 2021 03:05:14 +0200 Subject: [PATCH 10/12] Revert cs changes --- tests/Functional/BundleConfigurationTest.php | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/tests/Functional/BundleConfigurationTest.php b/tests/Functional/BundleConfigurationTest.php index e697626..0fba90c 100644 --- a/tests/Functional/BundleConfigurationTest.php +++ b/tests/Functional/BundleConfigurationTest.php @@ -32,17 +32,12 @@ 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'], + ]); + }], ]; } From ecbc67e5a9aae9d4a3e8f830b1f0455e2eabced6 Mon Sep 17 00:00:00 2001 From: chapterjason Date: Tue, 3 Aug 2021 03:12:50 +0200 Subject: [PATCH 11/12] cut: Remove cache prefix setter --- src/AppKernel.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/AppKernel.php b/src/AppKernel.php index f642034..267597d 100644 --- a/src/AppKernel.php +++ b/src/AppKernel.php @@ -180,11 +180,6 @@ public function loadRoutes(LoaderInterface $loader) return $routes->build(); } - public function setCachePrefix($cachePrefix) - { - $this->cachePrefix = $cachePrefix; - } - /** * {@inheritdoc} */ @@ -237,10 +232,6 @@ public function handleOptions(array $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']); } From 1c997e3d90d8cec99cbd58b081fb509b4ce94631 Mon Sep 17 00:00:00 2001 From: chapterjason Date: Tue, 3 Aug 2021 03:21:24 +0200 Subject: [PATCH 12/12] enhance: Rework per test passthrough --- Readme.md | 17 ++++++------- src/AppKernel.php | 26 +++----------------- tests/Functional/BundleConfigurationTest.php | 5 +++- tests/Functional/BundleRoutingTest.php | 4 ++- 4 files changed, 17 insertions(+), 35 deletions(-) diff --git a/Readme.md b/Readme.md index 7e84015..e806231 100644 --- a/Readme.md +++ b/Readme.md @@ -66,16 +66,13 @@ class BundleInitializationTest extends KernelTestCase public function testBundleWithDifferentConfiguration() { // Boot the kernel as normal ... - $kernel = self::bootKernel([ - 'bundles' => [ - // Add some other bundles we depend on - OtherBundle::class, - ], - 'configFiles' => [ - // Add some configuration - __DIR__.'/config.yml', - ], - ]); + $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'); + }]); // ... } diff --git a/src/AppKernel.php b/src/AppKernel.php index 267597d..71aa4d1 100644 --- a/src/AppKernel.php +++ b/src/AppKernel.php @@ -210,30 +210,10 @@ public function setRoutingFile($routingFile) $this->routingFile = $routingFile; } - public function handleOptions(array $options) + public function handleOptions(array $options): void { - 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('projectDir', $options)) { - $this->setProjectDir($options['projectDir']); + if (array_key_exists('config', $options) && is_callable($configCallable = $options['config'])) { + $configCallable($this); } } } diff --git a/tests/Functional/BundleConfigurationTest.php b/tests/Functional/BundleConfigurationTest.php index 0fba90c..7b02346 100644 --- a/tests/Functional/BundleConfigurationTest.php +++ b/tests/Functional/BundleConfigurationTest.php @@ -48,7 +48,10 @@ public function provideBundleWithDifferentConfigurationFormats() */ public function testBundleWithDifferentConfigurationFormats($config) { - $kernel = self::bootKernel(['configFiles' => [$config]]); + $kernel = self::bootKernel(['config' => function (AppKernel $kernel) use ($config) { + $kernel->addConfigFile($config); + }]); + $container = $kernel->getContainer(); $this->assertEquals('val1', $container->getParameter('app.foo')); diff --git a/tests/Functional/BundleRoutingTest.php b/tests/Functional/BundleRoutingTest.php index 6ef24ae..336a853 100644 --- a/tests/Functional/BundleRoutingTest.php +++ b/tests/Functional/BundleRoutingTest.php @@ -25,7 +25,9 @@ protected static function createKernel(array $options = []) public function testSetRoutingFile() { $kernel = self::bootKernel([ - 'routingFile' => __DIR__.'/../Fixtures/Resources/Routing/routes.yml', + 'config' => static function (AppKernel $kernel) { + $kernel->setRoutingFile(__DIR__.'/../Fixtures/Resources/Routing/routes.yml'); + }, ]); $container = $kernel->getContainer();