feature #48 Allow to configure kernel using server & env variables (pamil)

This PR was merged into the 2.0-dev branch.

Discussion
----------

The feature is described in feature files, the resolution priority is:
 - Behat configuration
 - SERVER variable
 - ENV variable
 - default hardcoded value

In the case of Sylius, it would allow us to get rid of `cached` profile and depend on APP_ENV settings only, resulting in less configuration to maintain.

Commits
-------

39cdb2074a Allow to configure kernel using server & env variables
This commit is contained in:
Kamil Kokot
2019-01-16 17:30:07 +01:00
committed by GitHub
4 changed files with 151 additions and 10 deletions

View File

@@ -27,4 +27,4 @@ script:
- vendor/bin/phpstan analyse -c phpstan.neon -l max src -vvv - 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

View File

@@ -58,7 +58,7 @@ Feature: Configuring application kernel
When I run Behat When I run Behat
Then it should pass Then it should pass
Scenario: Using configured environment Scenario: Using environment based on Behat configuration
Given a Behat configuration containing: Given a Behat configuration containing:
""" """
default: default:
@@ -76,7 +76,61 @@ Feature: Configuring application kernel
When I run Behat When I run Behat
Then it should pass 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: Given a Behat configuration containing:
""" """
default: default:
@@ -93,3 +147,57 @@ Feature: Configuring application kernel
""" """
When I run Behat When I run Behat
Then it should pass 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

View File

@@ -59,8 +59,8 @@ final class SymfonyExtension implements Extension
->children() ->children()
->scalarNode('path')->defaultNull()->end() ->scalarNode('path')->defaultNull()->end()
->scalarNode('class')->defaultNull()->end() ->scalarNode('class')->defaultNull()->end()
->scalarNode('environment')->defaultValue('test')->end() ->scalarNode('environment')->defaultNull()->end()
->booleanNode('debug')->defaultTrue()->end() ->booleanNode('debug')->defaultNull()->end()
->end() ->end()
->end() ->end()
->end() ->end()
@@ -105,8 +105,8 @@ final class SymfonyExtension implements Extension
private function loadKernel(ContainerBuilder $container, array $config): void private function loadKernel(ContainerBuilder $container, array $config): void
{ {
$definition = new Definition($config['class'], [ $definition = new Definition($config['class'], [
$config['environment'], $config['environment'] ?? $_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? 'test',
$config['debug'], (bool) ($config['debug'] ?? $_SERVER['APP_DEBUG'] ?? $_ENV['APP_DEBUG'] ?? true),
]); ]);
$definition->addMethodCall('boot'); $definition->addMethodCall('boot');
$definition->setPublic(true); $definition->setPublic(true);

View File

@@ -24,6 +24,9 @@ final class TestContext implements Context
/** @var Process */ /** @var Process */
private $process; private $process;
/** @var array */
private $variables = [];
/** /**
* @BeforeFeature * @BeforeFeature
*/ */
@@ -136,6 +139,14 @@ CON
$this->thereIsFile('config/services.yaml', ''); $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:$/ * @Given /^a YAML services file containing:$/
*/ */
@@ -167,9 +178,13 @@ CON
/** /**
* @Given /^a (?:.+ |)file "([^"]+)" containing(?: "([^"]+)"|:)$/ * @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 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 = '<?php ';
foreach ($this->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->setWorkingDirectory(self::$workingDir);
$this->process->start(); $this->process->start();
$this->process->wait(); $this->process->wait();