From 3f7f9ccdbb9e8a3a6a9c487715896ed80bc3db78 Mon Sep 17 00:00:00 2001 From: Kamil Kokot Date: Thu, 10 Jan 2019 22:43:54 +0100 Subject: [PATCH 01/11] Test loading kernel based on classname --- ...ading_kernel_based_on_configurtion.feature | 97 +++++++++++++++++++ tests/Behat/Context/TestContext.php | 32 +++--- 2 files changed, 117 insertions(+), 12 deletions(-) create mode 100644 features/configuration/loading_kernel_based_on_configurtion.feature diff --git a/features/configuration/loading_kernel_based_on_configurtion.feature b/features/configuration/loading_kernel_based_on_configurtion.feature new file mode 100644 index 0000000..219c74f --- /dev/null +++ b/features/configuration/loading_kernel_based_on_configurtion.feature @@ -0,0 +1,97 @@ +Feature: Loading kernel based on configuration + + Background: + Given a standard Symfony autoloader configured + And a feature file containing: + """ + Feature: + Scenario: + Then the passed service should be an instance of "\Psr\Container\ContainerInterface" + """ + And a Behat configuration containing: + """ + default: + suites: + default: + contexts: + - App\Tests\SomeContext + """ + And a context file "tests/SomeContext.php" containing: + """ + service = $service; } + + /** @Then the passed service should be an instance of :expected */ + public function serviceShouldBe(string $expected): void + { + assert(is_object($this->service)); + assert($this->service instanceof $expected); + } + } + """ + And a services file "config/services.yaml" containing: + """ + services: + App\Tests\SomeContext: + public: true + arguments: + - "@service_container" + """ + + Scenario: Loading kernel by its classname + Given a Behat configuration containing: + """ + default: + extensions: + FriendsOfBehat\SymfonyExtension: + kernel: + class: App\Custom\Kernel + """ + And a kernel file "src/Custom/Kernel.php" containing: + """ + loadFromExtension('framework', [ + 'test' => $this->getEnvironment() === 'test', + 'secret' => 'Pigeon', + ]); + + $loader->load(__DIR__ . '/../../config/services.yaml'); + } + + protected function configureRoutes(RouteCollectionBuilder $routes): void {} + } + """ + When I run Behat + Then it should pass diff --git a/tests/Behat/Context/TestContext.php b/tests/Behat/Context/TestContext.php index 1398c1f..0857bdf 100644 --- a/tests/Behat/Context/TestContext.php +++ b/tests/Behat/Context/TestContext.php @@ -51,6 +51,25 @@ final class TestContext implements Context self::$filesystem->remove(self::$workingDir); } + /** + * @Given a standard Symfony autoloader configured + */ + public function standardSymfonyAutoloaderConfigured(): void + { + $this->thereIsFile('vendor/autoload.php', sprintf(<<<'CON' +addPsr4('App\\', __DIR__ . '/../src/'); +$loader->addPsr4('App\\Tests\\', __DIR__ . '/../tests/'); + +return $loader; +CON + , __DIR__ . '/../../../vendor/autoload.php')); + } + /** * @Given a working Symfony application with SymfonyExtension configured */ @@ -65,18 +84,7 @@ default: CON ); - $this->thereIsFile('vendor/autoload.php', sprintf(<<<'CON' -addPsr4('App\\', __DIR__ . '/../src/'); -$loader->addPsr4('App\\Tests\\', __DIR__ . '/../tests/'); - -return $loader; -CON - , __DIR__ . '/../../../vendor/autoload.php')); + $this->standardSymfonyAutoloaderConfigured(); $this->thereIsFile('src/Kernel.php', <<<'CON' Date: Thu, 10 Jan 2019 22:47:17 +0100 Subject: [PATCH 02/11] Test loading kernel based on classname and path --- ...ading_kernel_based_on_configurtion.feature | 49 +++++++++++++++++++ src/ServiceContainer/SymfonyExtension.php | 7 ++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/features/configuration/loading_kernel_based_on_configurtion.feature b/features/configuration/loading_kernel_based_on_configurtion.feature index 219c74f..8efc077 100644 --- a/features/configuration/loading_kernel_based_on_configurtion.feature +++ b/features/configuration/loading_kernel_based_on_configurtion.feature @@ -95,3 +95,52 @@ Feature: Loading kernel based on configuration """ When I run Behat Then it should pass + + Scenario: Loading kernel from custom path + Given a Behat configuration containing: + """ + default: + extensions: + FriendsOfBehat\SymfonyExtension: + kernel: + path: app/Nested/Kernel.php + class: AppKernel + """ + And a kernel file "app/Nested/Kernel.php" containing: + """ + loadFromExtension('framework', [ + 'test' => $this->getEnvironment() === 'test', + 'secret' => 'Pigeon', + ]); + + $loader->load(__DIR__ . '/../../config/services.yaml'); + } + + protected function configureRoutes(RouteCollectionBuilder $routes): void {} + } + """ + When I run Behat + Then it should pass diff --git a/src/ServiceContainer/SymfonyExtension.php b/src/ServiceContainer/SymfonyExtension.php index 60ab56b..bc6140b 100644 --- a/src/ServiceContainer/SymfonyExtension.php +++ b/src/ServiceContainer/SymfonyExtension.php @@ -55,7 +55,8 @@ final class SymfonyExtension implements Extension ->arrayNode('kernel') ->addDefaultsIfNotSet() ->children() - ->scalarNode('class')->end() + ->scalarNode('path')->defaultNull()->end() + ->scalarNode('class')->isRequired()->end() ->end() ->end() ->end() @@ -103,6 +104,10 @@ final class SymfonyExtension implements Extension $definition->addMethodCall('boot'); $definition->setPublic(true); + if ($config['path'] !== null) { + $definition->setFile($config['path']); + } + $container->setDefinition(self::KERNEL_ID, $definition); } From 7687e756f1d5b929b148f2c73c4ea40fb660d4ce Mon Sep 17 00:00:00 2001 From: Kamil Kokot Date: Thu, 10 Jan 2019 23:03:02 +0100 Subject: [PATCH 03/11] Implement kernel autodiscovering --- ...autodiscovering_application_kernel.feature | 214 ++++++++++++++++++ ...ing_configured_application_kernel.feature} | 2 +- src/ServiceContainer/SymfonyExtension.php | 35 ++- 3 files changed, 248 insertions(+), 3 deletions(-) create mode 100644 features/configuration/autodiscovering_application_kernel.feature rename features/configuration/{loading_kernel_based_on_configurtion.feature => loading_configured_application_kernel.feature} (99%) diff --git a/features/configuration/autodiscovering_application_kernel.feature b/features/configuration/autodiscovering_application_kernel.feature new file mode 100644 index 0000000..30cb9eb --- /dev/null +++ b/features/configuration/autodiscovering_application_kernel.feature @@ -0,0 +1,214 @@ +Feature: Autodiscovering the application kernel + + Background: + Given a standard Symfony autoloader configured + And a feature file containing: + """ + Feature: + Scenario: + Then the passed service should be an instance of "\Psr\Container\ContainerInterface" + """ + And a Behat configuration containing: + """ + default: + extensions: + FriendsOfBehat\SymfonyExtension: ~ + + suites: + default: + contexts: + - App\Tests\SomeContext + """ + And a context file "tests/SomeContext.php" containing: + """ + service = $service; } + + /** @Then the passed service should be an instance of :expected */ + public function serviceShouldBe(string $expected): void + { + assert(is_object($this->service)); + assert($this->service instanceof $expected); + } + } + """ + And a services file "config/services.yaml" containing: + """ + services: + App\Tests\SomeContext: + public: true + arguments: + - "@service_container" + """ + + Scenario: Autodiscovering kernel in Symfony 3 directory structure application + Given a kernel file "src/Kernel.php" containing: + """ + loadFromExtension('framework', [ + 'test' => $this->getEnvironment() === 'test', + 'secret' => 'Pigeon', + ]); + + $loader->load(__DIR__ . '/../config/services.yaml'); + } + + protected function configureRoutes(RouteCollectionBuilder $routes): void {} + } + """ + When I run Behat + Then it should pass + + Scenario: Autodiscovering kernel in Symfony 4 directory structure application + Given a kernel file "app/AppKernel.php" containing: + """ + loadFromExtension('framework', [ + 'test' => $this->getEnvironment() === 'test', + 'secret' => 'Pigeon', + ]); + + $loader->load(__DIR__ . '/../config/services.yaml'); + } + + protected function configureRoutes(RouteCollectionBuilder $routes): void {} + } + """ + When I run Behat + Then it should pass + + Scenario: Failing to autodiscover the kernel + When I run Behat + Then it should fail with "Could not autodiscover the application kernel" + + Scenario: Failing to autodiscover the kernel + Given a kernel file "src/Kernel.php" containing: + """ + loadFromExtension('framework', [ + 'test' => $this->getEnvironment() === 'test', + 'secret' => 'Pigeon', + ]); + + $loader->load(__DIR__ . '/../config/services.yaml'); + } + + protected function configureRoutes(RouteCollectionBuilder $routes): void {} + } + """ + And a kernel file "app/AppKernel.php" containing: + """ + loadFromExtension('framework', [ + 'test' => $this->getEnvironment() === 'test', + 'secret' => 'Pigeon', + ]); + + $loader->load(__DIR__ . '/../config/services.yaml'); + } + + protected function configureRoutes(RouteCollectionBuilder $routes): void {} + } + """ + When I run Behat + Then it should fail with "Could not autodiscover the application kernel" diff --git a/features/configuration/loading_kernel_based_on_configurtion.feature b/features/configuration/loading_configured_application_kernel.feature similarity index 99% rename from features/configuration/loading_kernel_based_on_configurtion.feature rename to features/configuration/loading_configured_application_kernel.feature index 8efc077..69f0e02 100644 --- a/features/configuration/loading_kernel_based_on_configurtion.feature +++ b/features/configuration/loading_configured_application_kernel.feature @@ -1,4 +1,4 @@ -Feature: Loading kernel based on configuration +Feature: Loading configured application kernel Background: Given a standard Symfony autoloader configured diff --git a/src/ServiceContainer/SymfonyExtension.php b/src/ServiceContainer/SymfonyExtension.php index bc6140b..b96a5bb 100644 --- a/src/ServiceContainer/SymfonyExtension.php +++ b/src/ServiceContainer/SymfonyExtension.php @@ -56,7 +56,7 @@ final class SymfonyExtension implements Extension ->addDefaultsIfNotSet() ->children() ->scalarNode('path')->defaultNull()->end() - ->scalarNode('class')->isRequired()->end() + ->scalarNode('class')->defaultNull()->end() ->end() ->end() ->end() @@ -65,7 +65,7 @@ final class SymfonyExtension implements Extension public function load(ContainerBuilder $container, array $config): void { - $this->loadKernel($container, $config['kernel']); + $this->loadKernel($container, $this->processKernelConfiguration($config['kernel'])); $this->loadDriverKernel($container); $this->loadKernelRebooter($container); @@ -154,4 +154,35 @@ final class SymfonyExtension implements Extension $container->setDefinition('fob_symfony.mink.parameters', $minkParametersDefinition); } + + private function processKernelConfiguration(array $config): array + { + if ($config['class'] !== null) { + return $config; + } + + $autoconfigured = 0; + + if (class_exists('\App\Kernel')) { + $config['class'] = '\App\Kernel'; + + ++$autoconfigured; + } + + if (file_exists('app/AppKernel.php')) { + $config['class'] = '\AppKernel'; + $config['path'] = 'app/AppKernel.php'; + + ++$autoconfigured; + } + + if ($autoconfigured !== 1) { + throw new \RuntimeException( + 'Could not autodiscover the application kernel. ' . + 'Please define it manually with "FriendsOfBehat\SymfonyExtension.kernel" configuration option.' + ); + } + + return $config; + } } From 01f458149eb8354b10ba1b6e509597ee7bef2a27 Mon Sep 17 00:00:00 2001 From: Kamil Kokot Date: Thu, 10 Jan 2019 23:22:06 +0100 Subject: [PATCH 04/11] Test default kernel configuration --- .../configuring_application_kernel.feature | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 features/configuration/configuring_application_kernel.feature diff --git a/features/configuration/configuring_application_kernel.feature b/features/configuration/configuring_application_kernel.feature new file mode 100644 index 0000000..fea4bc1 --- /dev/null +++ b/features/configuration/configuring_application_kernel.feature @@ -0,0 +1,59 @@ +Feature: Configuring application kernel + + Background: + Given a working Symfony application with SymfonyExtension configured + And a Behat configuration containing: + """ + default: + suites: + default: + contexts: + - App\Tests\SomeContext + """ + And a context file "tests/SomeContext.php" containing: + """ + kernel = $kernel; } + + /** @Then the application kernel should have environment :environment */ + public function kernelEnvironmentShouldBe(string $environment): void { assert($this->kernel->getEnvironment() === $environment); } + + /** @Then the application kernel should have debug :state*/ + public function kernelDebugShouldBe(string $state): void + { + $map = ['enabled' => true, 'disabled' => false]; + + if (!array_key_exists($state, $map)) { throw new \Exception('Invalid state passed!'); } + + assert($this->kernel->isDebug() === $map[$state]); + } + } + """ + And a YAML services file containing: + """ + services: + App\Tests\SomeContext: + public: true + arguments: + - "@kernel" + """ + + Scenario: Using test environment with debug enabled by default + Given a feature file containing: + """ + Feature: + Scenario: + Then the application kernel should have environment "test" + And the application kernel should have debug enabled + """ + When I run Behat + Then it should pass From 8dcd3e9d4dedddfe10b4ef21cf426ed9e7cb97d3 Mon Sep 17 00:00:00 2001 From: Kamil Kokot Date: Thu, 10 Jan 2019 23:25:19 +0100 Subject: [PATCH 05/11] Allow to configure the application kernel's custom environment --- .../autodiscovering_application_kernel.feature | 8 ++++---- .../configuring_application_kernel.feature | 18 ++++++++++++++++++ ...ading_configured_application_kernel.feature | 4 ++-- src/ServiceContainer/SymfonyExtension.php | 3 ++- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/features/configuration/autodiscovering_application_kernel.feature b/features/configuration/autodiscovering_application_kernel.feature index 30cb9eb..43df648 100644 --- a/features/configuration/autodiscovering_application_kernel.feature +++ b/features/configuration/autodiscovering_application_kernel.feature @@ -78,7 +78,7 @@ Feature: Autodiscovering the application kernel protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void { $container->loadFromExtension('framework', [ - 'test' => $this->getEnvironment() === 'test', + 'test' => true, 'secret' => 'Pigeon', ]); @@ -118,7 +118,7 @@ Feature: Autodiscovering the application kernel protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void { $container->loadFromExtension('framework', [ - 'test' => $this->getEnvironment() === 'test', + 'test' => true, 'secret' => 'Pigeon', ]); @@ -164,7 +164,7 @@ Feature: Autodiscovering the application kernel protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void { $container->loadFromExtension('framework', [ - 'test' => $this->getEnvironment() === 'test', + 'test' => true, 'secret' => 'Pigeon', ]); @@ -200,7 +200,7 @@ Feature: Autodiscovering the application kernel protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void { $container->loadFromExtension('framework', [ - 'test' => $this->getEnvironment() === 'test', + 'test' => true, 'secret' => 'Pigeon', ]); diff --git a/features/configuration/configuring_application_kernel.feature b/features/configuration/configuring_application_kernel.feature index fea4bc1..a6d23e5 100644 --- a/features/configuration/configuring_application_kernel.feature +++ b/features/configuration/configuring_application_kernel.feature @@ -57,3 +57,21 @@ Feature: Configuring application kernel """ When I run Behat Then it should pass + + Scenario: Using configured environment + Given a Behat configuration containing: + """ + default: + extensions: + FriendsOfBehat\SymfonyExtension: + kernel: + environment: custom + """ + And a feature file containing: + """ + Feature: + Scenario: + Then the application kernel should have environment "custom" + """ + When I run Behat + Then it should pass diff --git a/features/configuration/loading_configured_application_kernel.feature b/features/configuration/loading_configured_application_kernel.feature index 69f0e02..67e6908 100644 --- a/features/configuration/loading_configured_application_kernel.feature +++ b/features/configuration/loading_configured_application_kernel.feature @@ -83,7 +83,7 @@ Feature: Loading configured application kernel protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void { $container->loadFromExtension('framework', [ - 'test' => $this->getEnvironment() === 'test', + 'test' => true, 'secret' => 'Pigeon', ]); @@ -132,7 +132,7 @@ Feature: Loading configured application kernel protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void { $container->loadFromExtension('framework', [ - 'test' => $this->getEnvironment() === 'test', + 'test' => true, 'secret' => 'Pigeon', ]); diff --git a/src/ServiceContainer/SymfonyExtension.php b/src/ServiceContainer/SymfonyExtension.php index b96a5bb..2d7777f 100644 --- a/src/ServiceContainer/SymfonyExtension.php +++ b/src/ServiceContainer/SymfonyExtension.php @@ -57,6 +57,7 @@ final class SymfonyExtension implements Extension ->children() ->scalarNode('path')->defaultNull()->end() ->scalarNode('class')->defaultNull()->end() + ->scalarNode('environment')->defaultValue('test')->end() ->end() ->end() ->end() @@ -98,7 +99,7 @@ final class SymfonyExtension implements Extension private function loadKernel(ContainerBuilder $container, array $config): void { $definition = new Definition($config['class'], [ - 'test', + $config['environment'], true, ]); $definition->addMethodCall('boot'); From be8081d580e3585695b95c8c8f59ba250da4411c Mon Sep 17 00:00:00 2001 From: Kamil Kokot Date: Thu, 10 Jan 2019 23:29:11 +0100 Subject: [PATCH 06/11] Allow to configure the application kernel's debug setting --- .../configuring_application_kernel.feature | 18 ++++++++++++++++++ src/ServiceContainer/SymfonyExtension.php | 3 ++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/features/configuration/configuring_application_kernel.feature b/features/configuration/configuring_application_kernel.feature index a6d23e5..ef8e489 100644 --- a/features/configuration/configuring_application_kernel.feature +++ b/features/configuration/configuring_application_kernel.feature @@ -75,3 +75,21 @@ Feature: Configuring application kernel """ When I run Behat Then it should pass + + Scenario: Using configured debug setting + Given a Behat configuration containing: + """ + default: + extensions: + FriendsOfBehat\SymfonyExtension: + kernel: + debug: false + """ + And a feature file containing: + """ + Feature: + Scenario: + And the application kernel should have debug disabled + """ + When I run Behat + Then it should pass diff --git a/src/ServiceContainer/SymfonyExtension.php b/src/ServiceContainer/SymfonyExtension.php index 2d7777f..0a8288d 100644 --- a/src/ServiceContainer/SymfonyExtension.php +++ b/src/ServiceContainer/SymfonyExtension.php @@ -58,6 +58,7 @@ final class SymfonyExtension implements Extension ->scalarNode('path')->defaultNull()->end() ->scalarNode('class')->defaultNull()->end() ->scalarNode('environment')->defaultValue('test')->end() + ->booleanNode('debug')->defaultTrue()->end() ->end() ->end() ->end() @@ -100,7 +101,7 @@ final class SymfonyExtension implements Extension { $definition = new Definition($config['class'], [ $config['environment'], - true, + $config['debug'], ]); $definition->addMethodCall('boot'); $definition->setPublic(true); From fa3c02720ce1cd1d7f2f9b93325eabef968def3d Mon Sep 17 00:00:00 2001 From: Kamil Kokot Date: Fri, 11 Jan 2019 00:10:32 +0100 Subject: [PATCH 07/11] Implement bootstrap loading --- .../loading_configured_bootstrap_file.feature | 56 +++++++++++++++++++ src/ServiceContainer/SymfonyExtension.php | 18 +++++- 2 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 features/configuration/loading_configured_bootstrap_file.feature diff --git a/features/configuration/loading_configured_bootstrap_file.feature b/features/configuration/loading_configured_bootstrap_file.feature new file mode 100644 index 0000000..505e827 --- /dev/null +++ b/features/configuration/loading_configured_bootstrap_file.feature @@ -0,0 +1,56 @@ +Feature: Loading configured bootstrap file + + Scenario: Loading configured bootstrap file + Given a working Symfony application with SymfonyExtension configured + And a Behat configuration containing: + """ + default: + extensions: + FriendsOfBehat\SymfonyExtension: + bootstrap: custom/bootstrap.php + + suites: + default: + contexts: + - App\Tests\SomeContext + """ + And a boostrap file "custom/bootstrap.php" containing: + """ + parameter = $parameter; } + + /** @Then the passed parameter should be :expected */ + public function parameterShouldBe(string $expected): void { assert($this->parameter === $expected); } + } + """ + And a YAML services file containing: + """ + services: + App\Tests\SomeContext: + public: true + arguments: + - "%env(CUSTOM_VARIABLE)%" + """ + And a feature file containing: + """ + Feature: + Scenario: + Then the passed parameter should be "lol2" + """ + When I run Behat + Then it should pass diff --git a/src/ServiceContainer/SymfonyExtension.php b/src/ServiceContainer/SymfonyExtension.php index 0a8288d..fea3651 100644 --- a/src/ServiceContainer/SymfonyExtension.php +++ b/src/ServiceContainer/SymfonyExtension.php @@ -51,7 +51,9 @@ final class SymfonyExtension implements Extension public function configure(ArrayNodeDefinition $builder): void { $builder + ->addDefaultsIfNotSet() ->children() + ->scalarNode('bootstrap')->defaultNull()->end() ->arrayNode('kernel') ->addDefaultsIfNotSet() ->children() @@ -67,7 +69,9 @@ final class SymfonyExtension implements Extension public function load(ContainerBuilder $container, array $config): void { - $this->loadKernel($container, $this->processKernelConfiguration($config['kernel'])); + $container->setParameter('fob_symfony.bootstrap', $config['bootstrap']); + + $this->loadKernel($container, $this->autodiscoverKernelConfiguration($config['kernel'])); $this->loadDriverKernel($container); $this->loadKernelRebooter($container); @@ -82,6 +86,7 @@ final class SymfonyExtension implements Extension public function process(ContainerBuilder $container): void { + $this->processBootstrap($container->getParameter('fob_symfony.bootstrap')); } private function registerMinkDriver(ExtensionManager $extensionManager): void @@ -157,7 +162,7 @@ final class SymfonyExtension implements Extension $container->setDefinition('fob_symfony.mink.parameters', $minkParametersDefinition); } - private function processKernelConfiguration(array $config): array + private function autodiscoverKernelConfiguration(array $config): array { if ($config['class'] !== null) { return $config; @@ -187,4 +192,13 @@ final class SymfonyExtension implements Extension return $config; } + + private function processBootstrap(?string $bootstrap): void + { + if ($bootstrap === null) { + return; + } + + require_once $bootstrap; + } } From d6701d785c5caebdb8543c195b48a254618eb16b Mon Sep 17 00:00:00 2001 From: Kamil Kokot Date: Fri, 11 Jan 2019 00:24:59 +0100 Subject: [PATCH 08/11] Implement bootstrap file autodiscovering --- ...autodiscovering_application_kernel.feature | 4 +- .../autodiscovering_bootstrap_file.feature | 101 ++++++++++++++++++ src/ServiceContainer/SymfonyExtension.php | 40 ++++++- 3 files changed, 142 insertions(+), 3 deletions(-) create mode 100644 features/configuration/autodiscovering_bootstrap_file.feature diff --git a/features/configuration/autodiscovering_application_kernel.feature b/features/configuration/autodiscovering_application_kernel.feature index 43df648..04b8dad 100644 --- a/features/configuration/autodiscovering_application_kernel.feature +++ b/features/configuration/autodiscovering_application_kernel.feature @@ -49,7 +49,7 @@ Feature: Autodiscovering the application kernel - "@service_container" """ - Scenario: Autodiscovering kernel in Symfony 3 directory structure application + Scenario: Autodiscovering kernel in Symfony 4 directory structure application Given a kernel file "src/Kernel.php" containing: """ parameter = $parameter; } + + /** @Then the passed parameter should be :expected */ + public function parameterShouldBe(string $expected): void { assert($this->parameter === $expected); } + } + """ + And a YAML services file containing: + """ + services: + App\Tests\SomeContext: + public: true + arguments: + - "%env(CUSTOM_VARIABLE)%" + """ + And a feature file containing: + """ + Feature: + Scenario: + Then the passed parameter should be "lol2" + """ + + Scenario: Autodiscovering bootstrap file in Symfony 4 directory structure application + Given a boostrap file "config/bootstrap.php" containing: + """ + processBootstrap($container->getParameter('fob_symfony.bootstrap')); + $this->processBootstrap($this->autodiscoverBootstrap($container->getParameter('fob_symfony.bootstrap'))); } private function registerMinkDriver(ExtensionManager $extensionManager): void @@ -193,6 +193,44 @@ final class SymfonyExtension implements Extension return $config; } + /** + * @param string|bool|null $bootstrap + */ + private function autodiscoverBootstrap($bootstrap): ?string + { + if (is_string($bootstrap)) { + return $bootstrap; + } + + if ($bootstrap === false) { + return null; + } + + $autoconfigured = 0; + + if (file_exists('config/bootstrap.php')) { + $bootstrap = 'config/bootstrap.php'; + + ++$autoconfigured; + } + + if (file_exists('app/autoload.php')) { + $bootstrap = 'app/autoload.php'; + + ++$autoconfigured; + } + + if ($autoconfigured === 2) { + throw new \RuntimeException( + 'Could not autodiscover the bootstrap file. ' . + 'Please define it manually with "FriendsOfBehat\SymfonyExtension.bootstrap" configuration option. ' . + 'Setting that option to "false" disables autodiscovering.' + ); + } + + return is_string($bootstrap) ? $bootstrap : null; + } + private function processBootstrap(?string $bootstrap): void { if ($bootstrap === null) { From e6064b15b5d76d6d637a089f547e31619d6a3c64 Mon Sep 17 00:00:00 2001 From: Kamil Kokot Date: Fri, 11 Jan 2019 00:25:48 +0100 Subject: [PATCH 09/11] Make PHPStan passing --- phpstan.neon | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/phpstan.neon b/phpstan.neon index d944ec3..3da1825 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,5 +4,6 @@ parameters: ignoreErrors: - '/Cannot access offset 0 on callable\./' - '/Cannot access offset 1 on callable\./' - - '/Method FriendsOfBehat\\SymfonyExtension\\Context\\Environment\\InitialisedContextServiceEnvironment::bindCallee\(\) should return callable/' - '/Cannot call method [a-zA-Z0-9]+\(\) on Symfony\\Component\\Config\\Definition\\Builder\\NodeParentInterface|null\./' + - '/Method FriendsOfBehat\\SymfonyExtension\\Context\\Environment\\InitialisedContextServiceEnvironment::bindCallee\(\) should return callable/' + - '/Strict comparison using === between 0\|1 and 2 will always evaluate to false\./' From b7119b84bb102a10e5483cd44942c3ceeba4bbeb Mon Sep 17 00:00:00 2001 From: Kamil Kokot Date: Fri, 11 Jan 2019 00:26:34 +0100 Subject: [PATCH 10/11] Removed unused "symfony/dotenv" dependency --- .travis.yml | 1 - composer.json | 1 - 2 files changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1a9df4b..eb6680c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,7 +17,6 @@ before_install: - phpenv config-rm xdebug.ini || true install: - - composer require symfony/dotenv:${SYMFONY_VERSION} --no-update --no-scripts --prefer-dist - composer require symfony/http-kernel:${SYMFONY_VERSION} --no-update --no-scripts --prefer-dist - composer require symfony/proxy-manager-bridge:${SYMFONY_VERSION} --no-update --no-scripts --prefer-dist - composer require --dev symfony/framework-bundle:${SYMFONY_VERSION} --no-update --no-scripts --prefer-dist diff --git a/composer.json b/composer.json index 998fa7e..dc29dc6 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,6 @@ "require": { "php": "^7.1", "behat/behat": "^3.4", - "symfony/dotenv": "^3.4|^4.1", "symfony/http-kernel": "^3.4|^4.1", "symfony/proxy-manager-bridge": "^3.4|^4.1" }, From 24493cc58c321a3401ae79bdf6f1d37feeb807a4 Mon Sep 17 00:00:00 2001 From: Kamil Kokot Date: Fri, 11 Jan 2019 00:28:22 +0100 Subject: [PATCH 11/11] Minor fixes to the nomenclature --- src/ServiceContainer/SymfonyExtension.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ServiceContainer/SymfonyExtension.php b/src/ServiceContainer/SymfonyExtension.php index 045a137..8c5512a 100644 --- a/src/ServiceContainer/SymfonyExtension.php +++ b/src/ServiceContainer/SymfonyExtension.php @@ -168,22 +168,22 @@ final class SymfonyExtension implements Extension return $config; } - $autoconfigured = 0; + $autodiscovered = 0; if (class_exists('\App\Kernel')) { $config['class'] = '\App\Kernel'; - ++$autoconfigured; + ++$autodiscovered; } if (file_exists('app/AppKernel.php')) { $config['class'] = '\AppKernel'; $config['path'] = 'app/AppKernel.php'; - ++$autoconfigured; + ++$autodiscovered; } - if ($autoconfigured !== 1) { + if ($autodiscovered !== 1) { throw new \RuntimeException( 'Could not autodiscover the application kernel. ' . 'Please define it manually with "FriendsOfBehat\SymfonyExtension.kernel" configuration option.' @@ -206,21 +206,21 @@ final class SymfonyExtension implements Extension return null; } - $autoconfigured = 0; + $autodiscovered = 0; if (file_exists('config/bootstrap.php')) { $bootstrap = 'config/bootstrap.php'; - ++$autoconfigured; + ++$autodiscovered; } if (file_exists('app/autoload.php')) { $bootstrap = 'app/autoload.php'; - ++$autoconfigured; + ++$autodiscovered; } - if ($autoconfigured === 2) { + if ($autodiscovered === 2) { throw new \RuntimeException( 'Could not autodiscover the bootstrap file. ' . 'Please define it manually with "FriendsOfBehat\SymfonyExtension.bootstrap" configuration option. ' .