From 39cdb2074ab1e20e23b08fd67af50d317dca274e Mon Sep 17 00:00:00 2001 From: Kamil Kokot Date: Wed, 16 Jan 2019 17:08:30 +0100 Subject: [PATCH] Allow to configure kernel using server & env variables --- .travis.yml | 2 +- .../configuring_application_kernel.feature | 112 +++++++++++++++++- src/ServiceContainer/SymfonyExtension.php | 8 +- tests/Behat/Context/TestContext.php | 39 +++++- 4 files changed, 151 insertions(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index eb6680c..4259e2e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,4 +27,4 @@ script: - vendor/bin/phpstan analyse -c phpstan.neon -l max src -vvv - - vendor/bin/behat --strict -vvv --no-interaction + - vendor/bin/behat -f progress --strict -vvv --no-interaction diff --git a/features/configuration/configuring_application_kernel.feature b/features/configuration/configuring_application_kernel.feature index ef8e489..4033ebe 100644 --- a/features/configuration/configuring_application_kernel.feature +++ b/features/configuration/configuring_application_kernel.feature @@ -58,7 +58,7 @@ Feature: Configuring application kernel When I run Behat Then it should pass - Scenario: Using configured environment + Scenario: Using environment based on Behat configuration Given a Behat configuration containing: """ default: @@ -76,7 +76,61 @@ Feature: Configuring application kernel When I run Behat Then it should pass - Scenario: Using configured debug setting + Scenario: Using environment based on a server variable + Given a feature file containing: + """ + Feature: + Scenario: + And the application kernel should have environment "custom" + """ + And a server variable "APP_ENV" set to "custom" + When I run Behat + Then it should pass + + Scenario: Using environment based on an environment variable + Given a feature file containing: + """ + Feature: + Scenario: + And the application kernel should have environment "custom" + """ + And an environment variable "APP_ENV" set to "custom" + When I run Behat + Then it should pass + + Scenario: Using environment based on a server variable over an environment variable is also found + Given a feature file containing: + """ + Feature: + Scenario: + And the application kernel should have environment "custom_ser" + """ + And a server variable "APP_ENV" set to "custom_ser" + And an environment variable "APP_ENV" set to "custom_env" + When I run Behat + Then it should pass + + Scenario: Using environment based on Behat configuration over server or environment variable + Given a feature file containing: + """ + Feature: + Scenario: + And the application kernel should have environment "custom_conf" + """ + And a Behat configuration containing: + """ + default: + extensions: + FriendsOfBehat\SymfonyExtension: + kernel: + environment: custom_conf + """ + And a server variable "APP_ENV" set to "custom_ser" + And an environment variable "APP_ENV" set to "custom_env" + When I run Behat + Then it should pass + + Scenario: Using debug based on Behat configuration Given a Behat configuration containing: """ default: @@ -93,3 +147,57 @@ Feature: Configuring application kernel """ When I run Behat Then it should pass + + Scenario: Using debug based on a server variable + Given a feature file containing: + """ + Feature: + Scenario: + And the application kernel should have debug disabled + """ + And a server variable "APP_DEBUG" set to "0" + When I run Behat + Then it should pass + + Scenario: Using debug based on an environment variable + Given a feature file containing: + """ + Feature: + Scenario: + And the application kernel should have debug disabled + """ + And an environment variable "APP_DEBUG" set to "0" + When I run Behat + Then it should pass + + Scenario: Using debug based on a server variable over an environment variable is also found + Given a feature file containing: + """ + Feature: + Scenario: + And the application kernel should have debug disabled + """ + And a server variable "APP_DEBUG" set to "0" + And an environment variable "APP_DEBUG" set to "1" + When I run Behat + Then it should pass + + Scenario: Using debug based on Behat configuration over server or environment variable + Given a feature file containing: + """ + Feature: + Scenario: + And the application kernel should have debug disabled + """ + And a Behat configuration containing: + """ + default: + extensions: + FriendsOfBehat\SymfonyExtension: + kernel: + debug: false + """ + And a server variable "APP_DEBUG" set to "1" + And an environment variable "APP_DEBUG" set to "1" + When I run Behat + Then it should pass diff --git a/src/ServiceContainer/SymfonyExtension.php b/src/ServiceContainer/SymfonyExtension.php index 8c5512a..f52df10 100644 --- a/src/ServiceContainer/SymfonyExtension.php +++ b/src/ServiceContainer/SymfonyExtension.php @@ -59,8 +59,8 @@ final class SymfonyExtension implements Extension ->children() ->scalarNode('path')->defaultNull()->end() ->scalarNode('class')->defaultNull()->end() - ->scalarNode('environment')->defaultValue('test')->end() - ->booleanNode('debug')->defaultTrue()->end() + ->scalarNode('environment')->defaultNull()->end() + ->booleanNode('debug')->defaultNull()->end() ->end() ->end() ->end() @@ -105,8 +105,8 @@ final class SymfonyExtension implements Extension private function loadKernel(ContainerBuilder $container, array $config): void { $definition = new Definition($config['class'], [ - $config['environment'], - $config['debug'], + $config['environment'] ?? $_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? 'test', + (bool) ($config['debug'] ?? $_SERVER['APP_DEBUG'] ?? $_ENV['APP_DEBUG'] ?? true), ]); $definition->addMethodCall('boot'); $definition->setPublic(true); diff --git a/tests/Behat/Context/TestContext.php b/tests/Behat/Context/TestContext.php index 0857bdf..9b5683b 100644 --- a/tests/Behat/Context/TestContext.php +++ b/tests/Behat/Context/TestContext.php @@ -24,6 +24,9 @@ final class TestContext implements Context /** @var Process */ private $process; + /** @var array */ + private $variables = []; + /** * @BeforeFeature */ @@ -136,6 +139,14 @@ CON $this->thereIsFile('config/services.yaml', ''); } + /** + * @Given /^an? (server|environment) variable "([^"]++)" set to "([^"]++)"$/ + */ + public function variableSetTo(string $type, string $name, string $value): void + { + $this->variables[$type][$name] = $value; + } + /** * @Given /^a YAML services file containing:$/ */ @@ -167,9 +178,13 @@ CON /** * @Given /^a (?:.+ |)file "([^"]+)" containing(?: "([^"]+)"|:)$/ */ - public function thereIsFile($file, $content): void + public function thereIsFile($file, $content): string { - self::$filesystem->dumpFile(self::$workingDir . '/' . $file, (string) $content); + $path = self::$workingDir . '/' . $file; + + self::$filesystem->dumpFile($path, (string) $content); + + return $path; } /** @@ -185,7 +200,25 @@ CON */ public function iRunBehat(): void { - $this->process = new Process(sprintf('%s %s --strict -vvv --no-interaction --lang=en', self::$phpBin, escapeshellarg(BEHAT_BIN_PATH))); + $executablePath = BEHAT_BIN_PATH; + + if ($this->variables !== []) { + $content = 'variables['server'] ?? [] as $name => $value) { + $content .= sprintf('$_SERVER["%s"] = "%s"; ', $name, $value); + } + + foreach ($this->variables['environment'] ?? [] as $name => $value) { + $content .= sprintf('$_ENV["%s"] = "%s"; ', $name, $value); + } + + $content .= sprintf('require_once("%s"); ', $executablePath); + + $executablePath = $this->thereIsFile('__executable.php', $content); + } + + $this->process = new Process(sprintf('%s %s --strict -vvv --no-interaction --lang=en', self::$phpBin, escapeshellarg($executablePath))); $this->process->setWorkingDirectory(self::$workingDir); $this->process->start(); $this->process->wait();