MDL-36031 implement message redirection for unit testing

This commit is contained in:
Petr Škoda
2012-10-13 20:00:54 +02:00
parent 2105f57590
commit 4c9e03f0ac
6 changed files with 330 additions and 0 deletions
+55
View File
@@ -60,6 +60,9 @@ class phpunit_util {
/** @var array list of debugging messages triggered during the last test execution */
protected static $debuggings = array();
/** @var phpunit_message_sink alternative target for moodle messaging */
protected static $messagesink = null;
/**
* Prevent parallel test execution - this can not work in Moodle because we modify database and dataroot.
*
@@ -547,6 +550,9 @@ class phpunit_util {
public static function reset_all_data($logchanges = false) {
global $DB, $CFG, $USER, $SITE, $COURSE, $PAGE, $OUTPUT, $SESSION, $GROUPLIB_CACHE;
// Stop any message redirection.
phpunit_util::stop_message_redirection();
// Release memory and indirectly call destroy() methods to release resource handles, etc.
gc_collect_cycles();
@@ -1244,4 +1250,53 @@ class phpunit_util {
return true;
}
/**
* Start message redirection.
*
* Note: Do not call directly from tests,
* use $sink = $this->redirectMessages() instead.
*
* @return phpunit_message_sink
*/
public static function start_message_redirection() {
if (self::$messagesink) {
self::stop_message_redirection();
}
self::$messagesink = new phpunit_message_sink();
return self::$messagesink;
}
/**
* End message redirection.
*
* Note: Do not call directly from tests,
* use $sink->close() instead.
*/
public static function stop_message_redirection() {
self::$messagesink = null;
}
/**
* Are messages redirected to some sink?
*
* Note: to be called from messagelib.php only!
*
* @return bool
*/
public static function is_redirecting_messages() {
return !empty(self::$messagesink);
}
/**
* To be called from messagelib.php only!
*
* @param stdClass $message record from message_read table
* @return bool true means send message, false means message "sent" to sink.
*/
public static function message_sent($message) {
if (self::$messagesink) {
self::$messagesink->add_message($message);
}
}
}