Skip to content

Instantly share code, notes, and snippets.

@66Ton99
Created May 31, 2012 16:08
Show Gist options
  • Save 66Ton99/2844446 to your computer and use it in GitHub Desktop.
Save 66Ton99/2844446 to your computer and use it in GitHub Desktop.
Swift memory transport
<?php
/**
* Memory Swift maller transport for unit tests
*
* @package Lib
* @subpackage Swift
* @author Ton Sharp <forma@66ton99.org.ua>
*/
class Swift_MemTransport implements Swift_Transport
{
private static $testMessArray = array();
private static $_enable = true;
/**
* Test if this Transport mechanism has started.
*
* @return boolean
*/
public function isStarted()
{
return true;
}
/**
*
* Enter description here ...
* @param bool $var
*/
public static function setEnable($var)
{
self::$_enable = (bool)$var;
}
/**
* Start this Transport mechanism.
*/
public function start() {}
/**
* Stop this Transport mechanism.
*/
public function stop() {}
/**
* Send the given Message.
*
* Recipient/sender data will be retreived from the Message API.
* The return value is the number of recipients who were accepted for delivery.
*
* @param Swift_Mime_Message $message
* @param string[] &$failedRecipients to collect failures by-reference
* @return int
*/
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
{
if (self::$_enable) {
self::$testMessArray[] = array('message' => $message, 'failedRecipients' => ($failedRecipients));
}
}
/**
* Register a plugin in the Transport.
*
* @param Swift_Events_EventListener $plugin
*/
public function registerPlugin(Swift_Events_EventListener $plugin) {}
public static function getMess()
{
return self::$testMessArray;
}
public static function clearMess()
{
self::$testMessArray = array();
}
/**
* Default checking
*
* @return bool|string - trun if OK or error
*/
public static function checkMails()
{
foreach (self::getMess() as $mail) {
$cc = $mail['message']->getCc();
if (!empty($cc)) return 'CC not empty';
$bcc = $mail['message']->getBcc();
if (!empty($bcc)) return 'BCC not empty';
$recipients = $mail['message']->getTo();
if (!is_array($recipients)) {
$recipients = explode(',', $recipients);
}
if (1 < count($recipients)) return 'Worong number of recipients';
/*foreach ($recipients as $adress) {
if (false !== strpos($adress, ',') ||
false !== strpos($adress, ';') ||
false !== strpos($adress, ' ')) return "Wrong chars in email adress {$adress}";
}*/
if (!trim($mail['message']->getBody())) return 'Empty body';
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment