2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Behat basic functions
20 * It does not include MOODLE_INTERNAL because is part of the bootstrap
24 * @copyright 2012 David MonllaĆ³
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require_once(__DIR__
. '/../testing/lib.php');
30 define('BEHAT_EXITCODE_CONFIG', 250);
31 define('BEHAT_EXITCODE_REQUIREMENT', 251);
32 define('BEHAT_EXITCODE_PERMISSIONS', 252);
33 define('BEHAT_EXITCODE_REINSTALL', 253);
34 define('BEHAT_EXITCODE_INSTALL', 254);
35 define('BEHAT_EXITCODE_COMPOSER', 255);
38 * Exits with an error code
40 * @param mixed $errorcode
42 * @return void Stops execution with error code
44 function behat_error($errorcode, $text = '') {
46 // Adding error prefixes.
48 case BEHAT_EXITCODE_CONFIG
:
49 $text = 'Behat config error: ' . $text;
51 case BEHAT_EXITCODE_REQUIREMENT
:
52 $text = 'Behat requirement not satisfied: ' . $text;
54 case BEHAT_EXITCODE_PERMISSIONS
:
55 $text = 'Behat permissions problem: ' . $text . ', check the permissions';
57 case BEHAT_EXITCODE_REINSTALL
:
58 $path = testing_cli_argument_path('/admin/tool/behat/cli/util.php');
59 $text = "Reinstall Behat: ".$text.", use:\n php ".$path." --drop \n php ".$path." --install";
61 case BEHAT_EXITCODE_INSTALL
:
62 $path = testing_cli_argument_path('/admin/tool/behat/cli/util.php');
63 $text = "Install Behat before enabling it, use:\n php ".$path." --install";
66 $text = 'Unknown error ' . $errorcode . ' ' . $text;
70 testing_error($errorcode, $text);
74 * PHP errors handler to use when running behat tests.
76 * Adds specific CSS classes to identify
80 * @param string $errstr
81 * @param string $errfile
83 * @param array $errcontext
86 function behat_error_handler($errno, $errstr, $errfile, $errline, $errcontext) {
89 // Only after something has been writen.
90 if (!$OUTPUT->has_started()) {
94 // If is preceded by an @ we don't show it.
95 if (!error_reporting()) {
99 // Using the default one in case there is a fatal catchable error.
100 default_error_handler($errno, $errstr, $errfile, $errline, $errcontext);
104 $errnostr = 'Fatal error';
108 $errnostr = 'Warning';
113 $errnostr = 'Notice';
115 case E_RECOVERABLE_ERROR
:
116 $errnostr = 'Catchable';
119 $errnostr = 'Unknown error type';
122 // Wrapping the output.
123 echo '<div class="phpdebugmessage">' . PHP_EOL
;
124 echo "$errnostr: $errstr in $errfile on line $errline" . PHP_EOL
;
127 // Also use the internal error handler so we keep the usual behaviour.