Skip to content

Commit

Permalink
added profiles support
Browse files Browse the repository at this point in the history
  • Loading branch information
everzet committed Apr 2, 2011
1 parent 932bcb5 commit bfe236d
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 26 deletions.
69 changes: 65 additions & 4 deletions src/Behat/Behat/Console/Command/BehatCommand.php
Expand Up @@ -77,6 +77,12 @@ protected function configure()
'Specify external configuration file to load. ' .
'<info>behat.yml</info> or <info>config/behat.yml</info> will be used by default.'
),
new InputOption('--profile', null,
InputOption::VALUE_REQUIRED,
' ' .
'Specify configuration profile name to use. ' .
'Define configuration profiles in <info>behat.yml</info>.'
),
new InputOption('--out', null,
InputOption::VALUE_REQUIRED,
' ' .
Expand Down Expand Up @@ -171,7 +177,7 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$container = $this->configureContainer($input->getOption('config'));
$container = $this->configureContainer($input->getOption('config'), $input->getOption('profile'));

if ($input->getOption('usage')) {
$this->printUsageExample($input, $container, $output);
Expand Down Expand Up @@ -217,7 +223,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
*
* @return Symfony\Component\DependencyInjection\ContainerInterface
*/
protected function configureContainer($configFile = null)
protected function configureContainer($configFile = null, $profile = null)
{
$container = new ContainerBuilder();
$extension = new BehatExtension();
Expand All @@ -226,6 +232,10 @@ protected function configureContainer($configFile = null)

$this->pathTokens['BEHAT_WORK_PATH'] = $cwd;

if (null === $profile) {
$profile = 'default';
}

if (null === $configFile) {
if (is_file($cwd . DIRECTORY_SEPARATOR . 'behat.yml')) {
$configFile = $cwd . DIRECTORY_SEPARATOR . 'behat.yml';
Expand All @@ -236,15 +246,44 @@ protected function configureContainer($configFile = null)

if (null !== $configFile) {
$this->pathTokens['BEHAT_CONFIG_PATH'] = dirname($configFile);
$config = array(Yaml::load($configFile));
$config = $this->loadConfigurationFile($configFile, $profile);
}

$extension->load($config, $container);
$extension->load(array($config), $container);
$container->compile();

return $container;
}

/**
* Loads information from YAML configuration file.
*
* @param string $configFile path to the config file
* @param string $profile name of the config profile to use
*
* @return array
*/
protected function loadConfigurationFile($configFile, $profile = 'default')
{
$config = Yaml::load($configFile);

$resultConfig = isset($config['default']) ? $config['default'] : array();
if ('default' !== $profile && isset($config[$profile])) {
$resultConfig = $this->arrayMergeRecursiveWithOverwrites($resultConfig, $config[$profile]);
}

if (isset($config['imports'])) {
foreach ($config['imports'] as $importConfigFile) {
$importConfigFile = $this->preparePath($importConfigFile);
$resultConfig = $this->arrayMergeRecursiveWithOverwrites(
$resultConfig, $this->loadConfigurationFile($importConfigFile, $profile)
);
}
}

return $resultConfig;
}

/**
* Prints features usage example in specified language (--lang) to the console.
*
Expand Down Expand Up @@ -754,4 +793,26 @@ protected function preparePath($path, $allowRelativePaths = false)

return $path;
}

/**
* Merges two arrays into one with overwrite. It works the same as array_merge_recursive, but
* overwrites non-array values instead of turning them into arrays.
*
* @param array $array1 to
* @param array $array2 from
*
* @return array
*/
private function arrayMergeRecursiveWithOverwrites($array1, $array2)
{
foreach($array2 as $key => $val) {
if (array_key_exists($key, $array1) && is_array($val)) {
$array1[$key] = $this->arrayMergeRecursiveWithOverwrites($array1[$key], $val);
} else {
$array1[$key] = $val;
}
}

return $array1;
}
}
1 change: 1 addition & 0 deletions src/Behat/Behat/DependencyInjection/BehatExtension.php
Expand Up @@ -56,6 +56,7 @@ public function load(array $configs, ContainerBuilder $container)
$tree = $this->configuration->getConfigTree();
$config = $this->processor->process($tree, $configs);

// load configs DIC
foreach ($config as $ns => $subconfig) {
foreach ($subconfig as $key => $value) {
$parameterName = "behat.$ns.$key";
Expand Down
34 changes: 18 additions & 16 deletions src/Behat/Behat/DependencyInjection/Configuration.php
Expand Up @@ -42,33 +42,34 @@ public function getConfigTree()
scalarNode('support')->
defaultValue('%behat.paths.features%/support')->
end()->
variableNode('steps')->
defaultValue('%behat.paths.features%/steps')->
arrayNode('steps')->
prototype('scalar')->end()->
defaultValue(array('%behat.paths.features%/steps'))->
end()->
variableNode('steps-i18n')->
defaultValue('%behat.paths.features%/steps/i18n')->
arrayNode('steps-i18n')->
prototype('scalar')->end()->
defaultValue(array('%behat.paths.features%/steps/i18n'))->
end()->
variableNode('bootstrap')->
defaultValue('%behat.paths.support%/bootstrap.php')->
arrayNode('bootstrap')->
prototype('scalar')->end()->
defaultValue(array('%behat.paths.support%/bootstrap.php'))->
end()->
variableNode('environment')->
defaultValue('%behat.paths.support%/env.php')->
arrayNode('environment')->
prototype('scalar')->end()->
defaultValue(array('%behat.paths.support%/env.php'))->
end()->
variableNode('hooks')->
defaultValue('%behat.paths.support%/hooks.php')->
arrayNode('hooks')->
prototype('scalar')->end()->
defaultValue(array('%behat.paths.support%/hooks.php'))->
end()->
end()->
end()->
end()->
children()->
arrayNode('filters')->
children()->
scalarNode('name')->
defaultNull()->
end()->
scalarNode('tags')->
defaultNull()->
end()->
scalarNode('name')->defaultNull()->end()->
scalarNode('tags')->defaultNull()->end()->
end()->
end()->
end()->
Expand Down Expand Up @@ -106,6 +107,7 @@ public function getConfigTree()
end()->
children()->
arrayNode('environment')->
fixXmlConfig('parameter')->
children()->
scalarNode('class')->
defaultValue('Behat\Behat\Environment\Environment')->
Expand Down
22 changes: 16 additions & 6 deletions src/Behat/Behat/DependencyInjection/config/behat.xml
Expand Up @@ -9,13 +9,23 @@
<parameter key="behat.paths.i18n">%behat.paths.lib%/i18n</parameter>

<parameter key="behat.paths.features">BEHAT_BASE_PATH</parameter>
<parameter key="behat.paths.steps">%behat.paths.features%/steps</parameter>
<parameter key="behat.paths.steps.i18n">%behat.paths.features%/steps/i18n</parameter>

<parameter key="behat.paths.support">%behat.paths.features%/support</parameter>
<parameter key="behat.paths.bootstrap">%behat.paths.support%/bootstrap.php</parameter>
<parameter key="behat.paths.environment">%behat.paths.support%/env.php</parameter>
<parameter key="behat.paths.hooks">%behat.paths.support%/hooks.php</parameter>

<parameter key="behat.paths.steps" type="collection">
<parameter>%behat.paths.features%/steps</parameter>
</parameter>
<parameter key="behat.paths.steps.i18n" type="collection">
<parameter>%behat.paths.features%/steps/i18n</parameter>
</parameter>
<parameter key="behat.paths.bootstrap" type="collection">
<parameter>%behat.paths.support%/bootstrap.php</parameter>
</parameter>
<parameter key="behat.paths.environment" type="collection">
<parameter>%behat.paths.support%/env.php</parameter>
</parameter>
<parameter key="behat.paths.hooks" type="collection">
<parameter>%behat.paths.support%/hooks.php</parameter>
</parameter>

<!-- Formatter parameters -->
<parameter key="behat.formatter.name">pretty</parameter>
Expand Down

0 comments on commit bfe236d

Please sign in to comment.