From 90ed22ab70df95c71e1f7cb7ed12835c451ab598 Mon Sep 17 00:00:00 2001 From: David Monllao Date: Mon, 29 Apr 2013 16:01:32 +0800 Subject: [PATCH] MDL-38041 behat: Capturing also PHP debug messages --- lib/behat/lib.php | 57 +++++++++++++++++++++++++++++++++++++++++ lib/setup.php | 6 +++++ lib/tests/behat/behat_hooks.php | 16 ++++++++---- 3 files changed, 74 insertions(+), 5 deletions(-) diff --git a/lib/behat/lib.php b/lib/behat/lib.php index b1a747a31f3..70543f04184 100644 --- a/lib/behat/lib.php +++ b/lib/behat/lib.php @@ -70,3 +70,60 @@ function behat_error($errorcode, $text = '') { testing_error($errorcode, $text); } +/** + * PHP errors handler to use when running behat tests. + * + * Adds specific CSS classes to identify + * the messages. + * + * @param int $errno + * @param string $errstr + * @param string $errfile + * @param int $errline + * @param array $errcontext + * @return bool + */ +function behat_error_handler($errno, $errstr, $errfile, $errline, $errcontext) { + global $OUTPUT; + + // Only after something has been writen. + if (!$OUTPUT->has_started()) { + return false; + } + + // If is preceded by an @ we don't show it. + if (!error_reporting()) { + return true; + } + + // Using the default one in case there is a fatal catchable error. + default_error_handler($errno, $errstr, $errfile, $errline, $errcontext); + + switch ($errno) { + case E_USER_ERROR: + $errnostr = 'Fatal error'; + break; + case E_WARNING: + case E_USER_WARNING: + $errnostr = 'Warning'; + break; + case E_NOTICE: + case E_USER_NOTICE: + case E_STRICT: + $errnostr = 'Notice'; + break; + case E_RECOVERABLE_ERROR: + $errnostr = 'Catchable'; + break; + default: + $errnostr = 'Unknown error type'; + } + + // Wrapping the output. + echo '
' . PHP_EOL; + echo "$errnostr: $errstr in $errfile on line $errline" . PHP_EOL; + echo '
'; + + // Also use the internal error handler so we keep the usual behaviour. + return false; +} diff --git a/lib/setup.php b/lib/setup.php index a5db7803e93..213ac7030ed 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -455,6 +455,12 @@ if (!PHPUNIT_TEST or PHPUNIT_UTIL) { set_error_handler('default_error_handler', E_ALL | E_STRICT); } +// Acceptance tests needs special output to capture the errors. +if (!empty($CFG->originaldataroot) && !defined('BEHAT_RUNNING')) { + require_once(__DIR__ . '/behat/lib.php'); + set_error_handler('behat_error_handler', E_ALL | E_STRICT); +} + // If there are any errors in the standard libraries we want to know! error_reporting(E_ALL | E_STRICT); diff --git a/lib/tests/behat/behat_hooks.php b/lib/tests/behat/behat_hooks.php index a4622e8f42e..c8e60c50ad3 100644 --- a/lib/tests/behat/behat_hooks.php +++ b/lib/tests/behat/behat_hooks.php @@ -192,11 +192,6 @@ class behat_hooks extends behat_base { */ public function i_look_for_exceptions() { - // No need for checking if there is no UI. - if (!$this->getSession()->getPage()) { - return; - } - // Exceptions. if ($errormsg = $this->getSession()->getPage()->find('css', '.errorbox p.errormessage')) { @@ -218,6 +213,17 @@ class behat_hooks extends behat_base { $msg = "debugging() message/s found:\n" . implode("\n", $msgs); throw new \Exception(html_entity_decode($msg)); } + + // PHP debug messages. + if ($phpmessages = $this->getSession()->getPage()->findAll('css', '.phpdebugmessage')) { + + $msgs = array(); + foreach ($phpmessages as $phpmessage) { + $msgs[] = $this->get_debug_text($phpmessage->getHtml()); + } + $msg = "PHP debug message/s found:\n" . implode("\n", $msgs); + throw new \Exception(html_entity_decode($msg)); + } } /** -- 2.11.4.GIT