From de59f045314ee2b177a17fa575e6493b437e2889 Mon Sep 17 00:00:00 2001 From: Kamil Kokot Date: Wed, 16 Jan 2019 22:28:49 +0100 Subject: [PATCH] Fallback to the "test" environment if no server/env variable is set --- .../configuring_application_kernel.feature | 9 ++++++ src/ServiceContainer/SymfonyExtension.php | 30 ++++++++++++------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/features/configuration/configuring_application_kernel.feature b/features/configuration/configuring_application_kernel.feature index 4d43179..fe11a19 100644 --- a/features/configuration/configuring_application_kernel.feature +++ b/features/configuration/configuring_application_kernel.feature @@ -36,6 +36,14 @@ Feature: Configuring application kernel assert($this->kernel->isDebug() === $map[$state]); } + + /** @Then the server and environment variable :variable is :value */ + public function environmentVariableIs(string $variable, string $value): void + { + assert($_SERVER[$variable] === $value); + assert($_ENV[$variable] === $value); + assert(getenv($variable) === $value); + } } """ And a YAML services file containing: @@ -53,6 +61,7 @@ Feature: Configuring application kernel Feature: Scenario: Then the application kernel should have environment "test" + And the server and environment variable "APP_ENV" is "test" And the application kernel should have debug enabled """ When I run Behat diff --git a/src/ServiceContainer/SymfonyExtension.php b/src/ServiceContainer/SymfonyExtension.php index 8748933..1a5f165 100644 --- a/src/ServiceContainer/SymfonyExtension.php +++ b/src/ServiceContainer/SymfonyExtension.php @@ -69,7 +69,9 @@ final class SymfonyExtension implements Extension public function load(ContainerBuilder $container, array $config): void { - $this->processBootstrap($this->autodiscoverBootstrap($config['bootstrap'])); + $this->fallbackToTestEnvironment(); + + $this->loadBootstrap($this->autodiscoverBootstrap($config['bootstrap'])); $this->loadKernel($container, $this->autodiscoverKernelConfiguration($config['kernel'])); $this->loadDriverKernel($container); @@ -161,6 +163,23 @@ final class SymfonyExtension implements Extension $container->setDefinition('fob_symfony.mink.parameters', $minkParametersDefinition); } + private function loadBootstrap(?string $bootstrap): void + { + if ($bootstrap === null) { + return; + } + + require_once $bootstrap; + } + + private function fallbackToTestEnvironment(): void + { + // If there's no defined server / environment variable with an environment, default to test + if (($_SERVER['APP_ENV'] ?? $_ENV['APP_ENV']) === null) { + putenv('APP_ENV=' . $_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = 'test'); + } + } + private function autodiscoverKernelConfiguration(array $config): array { if ($config['class'] !== null) { @@ -229,13 +248,4 @@ final class SymfonyExtension implements Extension return is_string($bootstrap) ? $bootstrap : null; } - - private function processBootstrap(?string $bootstrap): void - { - if ($bootstrap === null) { - return; - } - - require_once $bootstrap; - } }