MDL-38041 behat: Capturing also PHP debug messages
[moodle.git] / lib / behat / lib.php
blob70543f041843db3f840ba5fc9890987402079648
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * Behat basic functions
20 * It does not include MOODLE_INTERNAL because is part of the bootstrap
22 * @package core
23 * @category test
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);
37 /**
38 * Exits with an error code
40 * @param mixed $errorcode
41 * @param string $text
42 * @return void Stops execution with error code
44 function behat_error($errorcode, $text = '') {
46 // Adding error prefixes.
47 switch ($errorcode) {
48 case BEHAT_EXITCODE_CONFIG:
49 $text = 'Behat config error: ' . $text;
50 break;
51 case BEHAT_EXITCODE_REQUIREMENT:
52 $text = 'Behat requirement not satisfied: ' . $text;
53 break;
54 case BEHAT_EXITCODE_PERMISSIONS:
55 $text = 'Behat permissions problem: ' . $text . ', check the permissions';
56 break;
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";
60 break;
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";
64 break;
65 default:
66 $text = 'Unknown error ' . $errorcode . ' ' . $text;
67 break;
70 testing_error($errorcode, $text);
73 /**
74 * PHP errors handler to use when running behat tests.
76 * Adds specific CSS classes to identify
77 * the messages.
79 * @param int $errno
80 * @param string $errstr
81 * @param string $errfile
82 * @param int $errline
83 * @param array $errcontext
84 * @return bool
86 function behat_error_handler($errno, $errstr, $errfile, $errline, $errcontext) {
87 global $OUTPUT;
89 // Only after something has been writen.
90 if (!$OUTPUT->has_started()) {
91 return false;
94 // If is preceded by an @ we don't show it.
95 if (!error_reporting()) {
96 return true;
99 // Using the default one in case there is a fatal catchable error.
100 default_error_handler($errno, $errstr, $errfile, $errline, $errcontext);
102 switch ($errno) {
103 case E_USER_ERROR:
104 $errnostr = 'Fatal error';
105 break;
106 case E_WARNING:
107 case E_USER_WARNING:
108 $errnostr = 'Warning';
109 break;
110 case E_NOTICE:
111 case E_USER_NOTICE:
112 case E_STRICT:
113 $errnostr = 'Notice';
114 break;
115 case E_RECOVERABLE_ERROR:
116 $errnostr = 'Catchable';
117 break;
118 default:
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;
125 echo '</div>';
127 // Also use the internal error handler so we keep the usual behaviour.
128 return false;