Skip to content

Commit

Permalink
MDL-37046 behat: Using moodle composer
Browse files Browse the repository at this point in the history
  • Loading branch information
David Monllao committed Jan 29, 2013
1 parent d46340e commit cb18fd7
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 20 deletions.
3 changes: 1 addition & 2 deletions admin/tool/behat/lang/en/tool_behat.php
Expand Up @@ -34,7 +34,6 @@
$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';
Expand All @@ -45,4 +44,4 @@
$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';
28 changes: 12 additions & 16 deletions admin/tool/behat/locallib.php
Expand Up @@ -78,7 +78,7 @@ public static function stepsdefinitions($type, $component, $filter) {
}

$currentcwd = getcwd();
chdir($CFG->behatpath);
chdir($CFG->dirroot . '/lib/behat');
exec('bin/behat --config="' . self::get_behat_config_filepath() . '" ' . $filteroption, $steps, $code);
chdir($currentcwd);

Expand Down Expand Up @@ -163,9 +163,9 @@ public static function runtests($withjavascript = false, $tags = false, $extra =
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();
Expand All @@ -188,7 +188,7 @@ public static function runtests($withjavascript = false, $tags = false, $extra =
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';
Expand All @@ -202,20 +202,15 @@ private static function update_config_file($component = '') {
features: ' . $behatpath . '/features
bootstrap: ' . $behatpath . '/features/bootstrap
context:
class: behat_init_context
parameters:
loadbuiltincontexts: ' . $loadbuiltincontexts . '
extensions:
Behat\MinkExtension\Extension:
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.
Expand Down Expand Up @@ -329,15 +324,16 @@ private static function test_environment_problem() {
/**
* 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;
Expand All @@ -352,7 +348,7 @@ private static function check_behat_setup() {

// Behat test command.
$currentcwd = getcwd();
chdir($CFG->behatpath);
chdir($CFG->dirroot . '/lib/behat');
exec('bin/behat --help', $output, $code);
chdir($currentcwd);

Expand Down
9 changes: 8 additions & 1 deletion 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"
}
}
}
37 changes: 37 additions & 0 deletions lib/behat/bin/behat
@@ -0,0 +1,37 @@
#!/usr/bin/env php
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* 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();
60 changes: 60 additions & 0 deletions lib/behat/features/bootstrap/behat_init_context.php
@@ -0,0 +1,60 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* 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));
}

}
2 changes: 1 addition & 1 deletion lib/setup.php
Expand Up @@ -96,7 +96,7 @@
}

// 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';
Expand Down

0 comments on commit cb18fd7

Please sign in to comment.