From b1059a520c8b706b3fa01f638e39fddd0b5fdf60 Mon Sep 17 00:00:00 2001 From: Arnaud Langlade Date: Wed, 28 Feb 2018 13:33:34 +0100 Subject: [PATCH 1/2] Symfony4 support --- README.md | 25 ++++++++++++++++++ composer.json | 3 ++- features/not_crashing_behat.feature | 32 +++++++++++++++++++++++ src/ServiceContainer/SymfonyExtension.php | 25 ++++++++++++++++-- 4 files changed, 82 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 740fd17..0e705e6 100644 --- a/README.md +++ b/README.md @@ -27,4 +27,29 @@ ensures that application behaviour will not be affected by stateful services. FriendsOfBehat\SymfonyExtension: ~ ``` +**Symfony 3 configuration** + +``` +FriendsOfBehat\SymfonyExtension: + kernel: + bootstrap: 'var/bootstrap.php.cache' + path: app/AppKernel.php + class: 'AppKernel' + env: test + debug: true +``` + +**Symfony 4 configuration** + +``` +FriendsOfBehat\SymfonyExtension: + env_file: .env + kernel: + class: 'MyTrip\Kernel' + path: src/Kernel.php + debug: true +``` + +Symfony 4 does not have bootstrap file anymore and the environment is configured in the .env file. + 3. Good luck & have fun! diff --git a/composer.json b/composer.json index 2152b6f..ec36bf9 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ "php": "^7.1", "behat/behat": "^3.1", - "symfony/http-kernel": "^3.3|^4.0" + "symfony/http-kernel": "^3.3|^4.0", + "symfony/dotenv": "^4.0" }, "require-dev": { "behat/mink": "^1.7", diff --git a/features/not_crashing_behat.feature b/features/not_crashing_behat.feature index a973f8d..1386842 100644 --- a/features/not_crashing_behat.feature +++ b/features/not_crashing_behat.feature @@ -56,3 +56,35 @@ Feature: Not crashing Behat And a feature file with passing scenario When I run Behat Then it should pass + + Scenario: This extension boot a Symfony4 kernel + Given a Behat configuration containing: + """ + default: + extensions: + FriendsOfBehat\SymfonyExtension: + env_file: .env_in_memory + kernel: + path: src/MyKernel.php + class: MyKernel + """ + And a file ".env_in_memory" containing: + """ + APP_ENV=dev + """ + And a file "src/MyKernel.php" containing: + """ + addDefaultsIfNotSet() ->children() + ->scalarNode('env_file')->defaultNull()->end() ->arrayNode('kernel') ->addDefaultsIfNotSet() ->children() @@ -110,6 +112,15 @@ final class SymfonyExtension implements Extension */ public function load(ContainerBuilder $container, array $config): void { + if (null !== $config['env_file']) { + $this->loadEnvVars($container, $config['env_file']); + + $environment = getenv('APP_ENV'); + $config['kernel']['env'] = $environment ?? 'test'; + } else { + $this->requireKernelBootstrapFile($container->getParameter('paths.base'), $config['bootstrap']); + } + $this->loadKernel($container, $config['kernel']); $this->loadKernelContainer($container); @@ -129,6 +140,16 @@ final class SymfonyExtension implements Extension { } + /** + * @param ContainerBuilder $container + * @param string $fileName + */ + private function loadEnvVars(ContainerBuilder $container, string $fileName): void + { + $envFilePath = sprintf('%s/%s', $container->getParameter('paths.base'), $fileName); + (new Dotenv())->load($envFilePath); + } + /** * @param ContainerBuilder $container */ @@ -142,8 +163,6 @@ final class SymfonyExtension implements Extension $definition->setFile($this->getKernelFile($container->getParameter('paths.base'), $config['path'])); $container->setDefinition(self::KERNEL_ID, $definition); - - $this->requireKernelBootstrapFile($container->getParameter('paths.base'), $config['bootstrap']); } /** @@ -203,6 +222,8 @@ final class SymfonyExtension implements Extension /** * @param ContainerBuilder $container + * + * @throws \Exception */ private function declareSymfonyContainers(ContainerBuilder $container): void { From 1a39d31f5e457cc6b44701a7bef99ba14f5c0cb2 Mon Sep 17 00:00:00 2001 From: Arnaud Langlade Date: Tue, 13 Mar 2018 11:45:25 +0100 Subject: [PATCH 2/2] Fix review feedback --- .travis.yml | 1 + composer.json | 2 +- features/not_crashing_behat.feature | 1 + src/ServiceContainer/SymfonyExtension.php | 23 ++++++++++++++++++----- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index e937fc1..3ae989d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,6 +18,7 @@ before_install: install: - composer require symfony/http-kernel:${SYMFONY_VERSION} --no-update --no-scripts --prefer-dist + - composer require symfony/dotenv:${SYMFONY_VERSION} --no-update --no-scripts --prefer-dist - composer require --dev symfony/framework-bundle:${SYMFONY_VERSION} --no-update --no-scripts --prefer-dist - composer update --prefer-dist diff --git a/composer.json b/composer.json index ec36bf9..d6784c0 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "behat/behat": "^3.1", "symfony/http-kernel": "^3.3|^4.0", - "symfony/dotenv": "^4.0" + "symfony/dotenv": "^3.3|^4.0" }, "require-dev": { "behat/mink": "^1.7", diff --git a/features/not_crashing_behat.feature b/features/not_crashing_behat.feature index 1386842..7fc7f9b 100644 --- a/features/not_crashing_behat.feature +++ b/features/not_crashing_behat.feature @@ -67,6 +67,7 @@ Feature: Not crashing Behat kernel: path: src/MyKernel.php class: MyKernel + bootstrap: ~ """ And a file ".env_in_memory" containing: """ diff --git a/src/ServiceContainer/SymfonyExtension.php b/src/ServiceContainer/SymfonyExtension.php index 35bdabd..10afb25 100644 --- a/src/ServiceContainer/SymfonyExtension.php +++ b/src/ServiceContainer/SymfonyExtension.php @@ -61,6 +61,16 @@ final class SymfonyExtension implements Extension */ const SHARED_KERNEL_CONTAINER_ID = 'sylius_symfony_extension.shared_kernel.container'; + /** + * Default symfony environment used to run your suites. + */ + private const DEFAULT_ENV = 'test'; + + /** + * Enable or disable the debug mode + */ + private const DEBUG_MODE = false; + /** * @var CrossContainerProcessor|null */ @@ -115,10 +125,11 @@ final class SymfonyExtension implements Extension if (null !== $config['env_file']) { $this->loadEnvVars($container, $config['env_file']); - $environment = getenv('APP_ENV'); - $config['kernel']['env'] = $environment ?? 'test'; - } else { - $this->requireKernelBootstrapFile($container->getParameter('paths.base'), $config['bootstrap']); + $environment = false !== getenv('APP_ENV') ? getenv('APP_ENV') : self::DEFAULT_ENV; + $debugMode = false !== getenv('APP_DEBUG') ? getenv('APP_DEBUG') : self::DEBUG_MODE; + + $config['kernel']['env'] = $environment; + $config['kernel']['kernel'] = $debugMode; } $this->loadKernel($container, $config['kernel']); @@ -142,7 +153,7 @@ final class SymfonyExtension implements Extension /** * @param ContainerBuilder $container - * @param string $fileName + * @param string $fileName */ private function loadEnvVars(ContainerBuilder $container, string $fileName): void { @@ -163,6 +174,8 @@ final class SymfonyExtension implements Extension $definition->setFile($this->getKernelFile($container->getParameter('paths.base'), $config['path'])); $container->setDefinition(self::KERNEL_ID, $definition); + + $this->requireKernelBootstrapFile($container->getParameter('paths.base'), $config['bootstrap']); } /**