MDL-41196 Unit Tests: Add a phpmailer message sink

This commit is contained in:
Andrew Nicols
2013-08-18 16:15:58 +01:00
parent 661777c503
commit 6e8edc5af0
5 changed files with 171 additions and 0 deletions
+55
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_phpmailer_sink alternative target for phpmailer messaging */
protected static $phpmailersink = 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_phpmailer_redirection();
// Release memory and indirectly call destroy() methods to release resource handles, etc.
gc_collect_cycles();
@@ -660,4 +666,53 @@ class phpunit_util extends testing_util {
self::$messagesink->add_message($message);
}
}
/**
* Start phpmailer redirection.
*
* Note: Do not call directly from tests,
* use $sink = $this->redirectEmails() instead.
*
* @return phpunit_phpmailer_sink
*/
public static function start_phpmailer_redirection() {
if (self::$phpmailersink) {
self::stop_phpmailer_redirection();
}
self::$phpmailersink = new phpunit_phpmailer_sink();
return self::$phpmailersink;
}
/**
* End phpmailer redirection.
*
* Note: Do not call directly from tests,
* use $sink->close() instead.
*/
public static function stop_phpmailer_redirection() {
self::$phpmailersink = null;
}
/**
* Are messages for phpmailer redirected to some sink?
*
* Note: to be called from moodle_phpmailer.php only!
*
* @return bool
*/
public static function is_redirecting_phpmailer() {
return !empty(self::$phpmailersink);
}
/**
* 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 phpmailer_sent($message) {
if (self::$phpmailersink) {
self::$phpmailersink->add_message($message);
}
}
}