From cb18fd7100c91070fefc5ec8af01fc3d8cbbee5d Mon Sep 17 00:00:00 2001 From: David Monllao Date: Fri, 30 Nov 2012 16:24:41 +0800 Subject: [PATCH] MDL-37046 behat: Using moodle composer --- admin/tool/behat/lang/en/tool_behat.php | 3 +- admin/tool/behat/locallib.php | 28 ++++----- composer.json | 9 ++- lib/behat/bin/behat | 37 ++++++++++++ .../features/bootstrap/behat_init_context.php | 60 +++++++++++++++++++ lib/setup.php | 2 +- 6 files changed, 119 insertions(+), 20 deletions(-) create mode 100644 lib/behat/bin/behat create mode 100644 lib/behat/features/bootstrap/behat_init_context.php diff --git a/admin/tool/behat/lang/en/tool_behat.php b/admin/tool/behat/lang/en/tool_behat.php index 1f43264fe05..4d1784a3af7 100644 --- a/admin/tool/behat/lang/en/tool_behat.php +++ b/admin/tool/behat/lang/en/tool_behat.php @@ -34,7 +34,6 @@ $string['installinfo'] = 'for installation and tests execution info'; $string['moreinfoin'] = 'More info in'; $string['newstepsinfo'] = 'for info about adding new steps definitions'; $string['newtestsinfo'] = 'for info about writting new tests'; -$string['nobehatpath'] = 'Behat path not found, use \'behatpath\' setting to specify it'; $string['nomoodlesteps'] = 'Generic web application steps'; $string['nostepsdefinitions'] = 'There aren\'t steps definitions matching this filter'; $string['pluginname'] = 'Acceptance testing'; @@ -45,4 +44,4 @@ $string['stepsdefinitionstype'] = 'Type'; $string['theninfo'] = 'Then. Checkings to ensure the outcomes are the expected ones'; $string['viewsteps'] = 'Filter'; $string['wheninfo'] = 'When. Actions that provokes an event'; -$string['wrongbehatsetup'] = 'Something is wrong with the setup, check moodle-acceptance-tests runs well on CLI and check your \'behatpath\' setting value is pointing to the right directory'; +$string['wrongbehatsetup'] = 'Something is wrong with the setup, ensure you ran the composer installer and your /lib/behat/bin/behat file has execution permissions'; diff --git a/admin/tool/behat/locallib.php b/admin/tool/behat/locallib.php index 237f26233e0..138d3f9e681 100644 --- a/admin/tool/behat/locallib.php +++ b/admin/tool/behat/locallib.php @@ -78,7 +78,7 @@ class tool_behat { } $currentcwd = getcwd(); - chdir($CFG->behatpath); + chdir($CFG->dirroot . '/lib/behat'); exec('bin/behat --config="' . self::get_behat_config_filepath() . '" ' . $filteroption, $steps, $code); chdir($currentcwd); @@ -163,9 +163,9 @@ class tool_behat { self::start_test_mode(); $server = self::start_test_server(); - // Runs the tests switching the current working directory to CFG->behatpath. + // Runs the tests switching the current working directory to behat path. $currentcwd = getcwd(); - chdir($CFG->behatpath); + chdir($CFG->dirroot . '/lib/behat'); ob_start(); passthru('bin/behat --ansi --config="' . self::get_behat_config_filepath() .'" ' . $tagsoption . ' ' .$extra, $code); $output = ob_get_contents(); @@ -188,7 +188,7 @@ class tool_behat { private static function update_config_file($component = '') { global $CFG; - $behatpath = rtrim($CFG->behatpath, '/'); + $behatpath = $CFG->dirroot . '/lib/behat'; // Not extra contexts when component is specified. $loadbuiltincontexts = '0'; @@ -202,6 +202,7 @@ class tool_behat { features: ' . $behatpath . '/features bootstrap: ' . $behatpath . '/features/bootstrap context: + class: behat_init_context parameters: loadbuiltincontexts: ' . $loadbuiltincontexts . ' extensions: @@ -209,13 +210,7 @@ class tool_behat { base_url: ' . $CFG->test_wwwroot . ' goutte: ~ selenium2: ~ - Sanpi\Behatch\Extension: - contexts: - browser: ~ - system: ~ - json: ~ - table: ~ - ' . $behatpath . '/vendor/moodlehq/behat-extension/init.php: + ' . $CFG->dirroot . '/vendor/moodlehq/behat-extension/init.php: '; // Gets all the components with features. @@ -329,15 +324,16 @@ class tool_behat { /** * Checks if behat is set up and working * - * It checks the behatpath setting value and runs the - * behat help command to ensure it works as expected + * It checks behat dependencies have been installed and runs + * the behat help command to ensure it works as expected */ private static function check_behat_setup() { global $CFG; // Moodle setting. - if (empty($CFG->behatpath)) { - $msg = get_string('nobehatpath', 'tool_behat'); + if (!is_dir($vendor = __DIR__ . '/../../../vendor')) { + + $msg = get_string('wrongbehatsetup', 'tool_behat'); // With HTML. $docslink = tool_behat::$docsurl; @@ -352,7 +348,7 @@ class tool_behat { // Behat test command. $currentcwd = getcwd(); - chdir($CFG->behatpath); + chdir($CFG->dirroot . '/lib/behat'); exec('bin/behat --help', $output, $code); chdir($currentcwd); diff --git a/composer.json b/composer.json index 5f618de7bba..39087c1513c 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,13 @@ { + "repositories" : [ + { + "type": "vcs", + "url": "https://github.com/moodlehq/moodle-behat-extension" + } + ], "require-dev": { "phpunit/phpunit": "3.7.*", "phpunit/dbUnit": "1.2.*" + "moodlehq/behat-extension": "1.0.3" } -} \ No newline at end of file +} diff --git a/lib/behat/bin/behat b/lib/behat/bin/behat new file mode 100644 index 00000000000..5678d160da2 --- /dev/null +++ b/lib/behat/bin/behat @@ -0,0 +1,37 @@ +#!/usr/bin/env php +. + +/** + * Mimics default bin/behat command to keep it in it's sandbox + * + * @package core + * @category behat + * @copyright 2012 David Monllaó + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +define('BEHAT_PHP_BIN_PATH', getenv('PHP_PEAR_PHP_BIN') ?: '/usr/bin/env php'); +define('BEHAT_BIN_PATH', __FILE__); +define('BEHAT_VERSION', 'DEV'); + +// vendor directory existence checked in tool_behat::check_behat_config(). +$vendor = __DIR__.'/../../../vendor'; +require($vendor.'/autoload.php'); + +$app = new Behat\Behat\Console\BehatApplication(BEHAT_VERSION); +$app->run(); diff --git a/lib/behat/features/bootstrap/behat_init_context.php b/lib/behat/features/bootstrap/behat_init_context.php new file mode 100644 index 00000000000..3ad5360882d --- /dev/null +++ b/lib/behat/features/bootstrap/behat_init_context.php @@ -0,0 +1,60 @@ +. + +/** + * Contexts initializer class + * + * @package core + * @category behat + * @copyright 2012 David Monllaó + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +use Behat\Behat\Context\BehatContext, + Behat\MinkExtension\Context\MinkContext, + Moodle\BehatExtension\Context\MoodleContext; + +/** + * Loads main subcontexts + * + * Loading of moodle subcontexts is done by the Moodle extension + * + * Renamed from behat FeatureContext class according + * to Moodle coding styles conventions + * + * @package core + * @category behat + * @copyright 2012 David Monllaó + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_init_context extends BehatContext { + + /** + * Initializes subcontexts + * + * @param array $parameters context parameters (set them up through behat.yml) + */ + public function __construct(array $parameters) { + + // Only load all subcontext if specified. + if ($parameters['loadbuiltincontexts']) { + $this->useContext('mink', new MinkContext($parameters)); + } + + $this->useContext('moodle', new MoodleContext($parameters)); + } + +} diff --git a/lib/setup.php b/lib/setup.php index edf0ebb3405..6aa94b7799e 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -96,7 +96,7 @@ if (!isset($CFG->test_wwwroot)) { } // Switch to test site only when test environment is enabled. -if ((php_sapi_name() === 'cli-server' || defined('BEHAT_RUNNING')) && +if ((php_sapi_name() === 'cli-server' || defined('BEHAT_RUNNING')) && file_exists($CFG->dataroot . '/behat/test_environment_enabled.txt')) { $CFG->wwwroot = $CFG->test_wwwroot; $CFG->passwordsaltmain = 'phpunit';