MDL-39846 implement event sink for unit testing

This commit is contained in:
Petr Škoda
2013-07-19 08:43:29 +02:00
parent a85258cac8
commit 62401e8fd5
6 changed files with 199 additions and 0 deletions
+59
View File
@@ -43,6 +43,9 @@ class phpunit_util extends testing_util {
/** @var phpunit_message_sink alternative target for moodle messaging */
protected static $messagesink = null;
/** @var phpunit_message_sink alternative target for moodle messaging */
protected static $eventsink = null;
/**
* @var array Files to skip when resetting dataroot folder
*/
@@ -95,6 +98,9 @@ class phpunit_util extends testing_util {
// Stop any message redirection.
phpunit_util::stop_message_redirection();
// Stop any message redirection.
phpunit_util::stop_event_redirection();
// Release memory and indirectly call destroy() methods to release resource handles, etc.
gc_collect_cycles();
@@ -661,4 +667,57 @@ class phpunit_util extends testing_util {
self::$messagesink->add_message($message);
}
}
/**
* Start event redirection.
*
* @private
* Note: Do not call directly from tests,
* use $sink = $this->redirectEvents() instead.
*
* @return phpunit_event_sink
*/
public static function start_event_redirection() {
if (self::$eventsink) {
self::stop_event_redirection();
}
self::$eventsink = new phpunit_event_sink();
return self::$eventsink;
}
/**
* End event redirection.
*
* @private
* Note: Do not call directly from tests,
* use $sink->close() instead.
*/
public static function stop_event_redirection() {
self::$eventsink = null;
}
/**
* Are events redirected to some sink?
*
* Note: to be called from \core\event\base only!
*
* @private
* @return bool
*/
public static function is_redirecting_events() {
return !empty(self::$eventsink);
}
/**
* To be called from \core\event\base only!
*
* @private
* @param \core\event\base $event record from event_read table
* @return bool true means send event, false means event "sent" to sink.
*/
public static function event_triggered(\core\event\base $event) {
if (self::$eventsink) {
self::$eventsink->add_event($event);
}
}
}