Skip to content

Commit

Permalink
MDL-46891 behat: Added support for $CFG->behat_profiles
Browse files Browse the repository at this point in the history
This config will be supported to avoid broken configuration
in future. As this is limited, we need will still use
->behat_config to set config values which can't be set by
behat_profiles
  • Loading branch information
Rajesh Taneja committed Mar 11, 2016
1 parent e01012e commit 0e1c34e
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 12 deletions.
4 changes: 2 additions & 2 deletions admin/tool/behat/cli/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@

if ($options['profile']) {
$profile = $options['profile'];
if (!isset($CFG->behat_config[$profile])) {
echo "Invalid profile passed: " . $profile;
if (!isset($CFG->behat_config[$profile]) && !isset($CFG->behat_profiles[$profile])) {
echo "Invalid profile passed: " . $profile . PHP_EOL;
exit(1);
}
$extraopts[] = '--profile="' . $profile . '"';
Expand Down
5 changes: 5 additions & 0 deletions admin/tool/behat/cli/util_single_run.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@

// This is only displayed once for parallel install.
if (empty($options['run'])) {
// Notify user that 2.5 profile has been converted to 3.5.
if (behat_config_manager::$autoprofileconversion) {
mtrace("2.5 behat profile detected, automatically converted to current 3.x format");
}

$runtestscommand = behat_command::get_behat_command(true, !empty($options['run']));

$runtestscommand .= ' --config ' . behat_config_manager::get_behat_cli_config_filepath();
Expand Down
23 changes: 14 additions & 9 deletions config-dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -673,15 +673,6 @@
// params hierarchy. More info: http://docs.behat.org/guides/7.config.html
// Example:
// $CFG->behat_config = array(
// 'default' => array(
// 'formatter' => array(
// 'name' => 'pretty',
// 'parameters' => array(
// 'decorated' => true,
// 'verbose' => false
// )
// )
// ),
// 'Mac-Firefox' => array(
// 'suites' => array (
// 'default' => array(
Expand Down Expand Up @@ -716,6 +707,20 @@
// )
// )
// );
// You can also use the following config to override default Moodle configuration for Behat.
// This config is limited to default suite and will be supported in later versions.
// It will have precedence over $CFG->behat_config.
// $CFG->behat_profiles = array(
// 'phantomjs' => array(
// 'browser' => 'phantomjs',
// 'tags' => '~@_file_upload&&~@_alert&&~@_bug_phantomjs',
// 'wd_host' => 'http://127.0.0.1:4443/wd/hub',
// 'capabilities' => array(
// 'platform' => 'Linux',
// 'version' => 2.1
// )
// ),
// );
//
// You can force the browser session (not user's sessions) to restart after N seconds. This could
// be useful if you are using a cloud-based service with time restrictions in the browser side.
Expand Down
123 changes: 122 additions & 1 deletion lib/behat/classes/behat_config_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
*/
class behat_config_manager {

/**
* @var bool Keep track of the automatic profile conversion. So we can notify user.
*/
public static $autoprofileconversion = false;

/**
* Updates a config file
*
Expand Down Expand Up @@ -429,12 +434,128 @@ protected static function get_config_file_contents($features, $stepsdefinitions)

// In case user defined overrides respect them over our default ones.
if (!empty($CFG->behat_config)) {
$config = self::merge_config($config, $CFG->behat_config);
foreach ($CFG->behat_config as $profile => $values) {
$config = self::merge_config($config, self::merge_behat_config($profile, $values));
}
}
// Check for Moodle custom ones.
if (!empty($CFG->behat_profiles) && is_array($CFG->behat_profiles)) {
foreach ($CFG->behat_profiles as $profile => $values) {
$config = self::merge_config($config, self::get_behat_profile($profile, $values));
}
}

return Symfony\Component\Yaml\Yaml::dump($config, 10, 2);
}

/**
* Parse $CFG->behat_config and return the array with required config structure for behat.yml
*
* @param string $profile profile name
* @param array $values values for profile
* @return array
*/
protected static function merge_behat_config($profile, $values) {
// Only add profile which are compatible with Behat 3.x
// Just check if any of Bheat 2.5 config is set. Not checking for 3.x as it might have some other configs
// Like : rerun_cache etc.
if (!isset($values['filters']['tags']) && !isset($values['extensions']['Behat\MinkExtension\Extension'])) {
return array($profile => $values);
}

// Parse 2.5 format and get related values.
$oldconfigvalues = array();
if (isset($values['extensions']['Behat\MinkExtension\Extension'])) {
$extensionvalues = $values['extensions']['Behat\MinkExtension\Extension'];
if (isset($extensionvalues['selenium2']['browser'])) {
$oldconfigvalues['browser'] = $extensionvalues['selenium2']['browser'];
}
if (isset($extensionvalues['selenium2']['wd_host'])) {
$oldconfigvalues['wd_host'] = $extensionvalues['selenium2']['wd_host'];
}
if (isset($extensionvalues['capabilities'])) {
$oldconfigvalues['capabilities'] = $extensionvalues['capabilities'];
}
}

if (isset($values['filters']['tags'])) {
$oldconfigvalues['tags'] = $values['filters']['tags'];
}

if (!empty($oldconfigvalues)) {
self::$autoprofileconversion = true;
return self::get_behat_profile($profile, $oldconfigvalues);
}

// If nothing set above then return empty array.
return array();
}

/**
* Parse $CFG->behat_profile and return the array with required config structure for behat.yml.
*
* $CFG->behat_profiles = array(
* 'profile' = array(
* 'browser' => 'firefox',
* 'tags' => '@javascript',
* 'wd_host' => 'http://127.0.0.1:4444/wd/hub',
* 'capabilities' => array(
* 'platform' => 'Linux',
* 'version' => 44
* )
* )
* );
*
* @param string $profile profile name
* @param array $values values for profile.
* @return array
*/
protected static function get_behat_profile($profile, $values) {
// Values should be an array.
if (!is_array($values)) {
return array();
}

// Check suite values.
$behatprofilesuites = array();
// Fill tags information.
if (isset($values['tags'])) {
$behatprofilesuites = array(
'suites' => array(
'default' => array(
'filters' => array(
'tags' => $values['tags'],
)
)
)
);
}

// Selenium2 config values.
$behatprofileextension = array();
$seleniumconfig = array();
if (isset($values['browser'])) {
$seleniumconfig['browser'] = $values['browser'];
}
if (isset($values['wd_host'])) {
$seleniumconfig['wd_host'] = $values['wd_host'];
}
if (isset($values['capabilities'])) {
$seleniumconfig['capabilities'] = $values['capabilities'];
}
if (!empty($seleniumconfig)) {
$behatprofileextension = array(
'extensions' => array(
'Behat\MinkExtension' => array(
'selenium2' => $seleniumconfig,
)
)
);
}

return array($profile => array_merge($behatprofilesuites, $behatprofileextension));
}

/**
* Attempt to split feature list into fairish buckets using timing information, if available.
* Simply add each one to lightest buckets until all files allocated.
Expand Down

0 comments on commit 0e1c34e

Please sign in to comment.