Using TestSessionEnvironment for saving emails

Easier debugging through JSON file inspection,
which can be logged in its entirety more easily
than a database table. It also keeps data in one place.
This commit is contained in:
Ingo Schommer 2014-03-01 14:47:12 +13:00
parent a0517d7680
commit a40896db8b

View File

@ -4,20 +4,25 @@ namespace SilverStripe\BehatExtension\Utility;
/** /**
* Same principle as core TestMailer class, * Same principle as core TestMailer class,
* but saves emails in the database instead in order * but saves emails in {@link TestSessionEnvironment}
* to share the state between PHP calls (CLI vs. browser). * to share the state between PHP calls (CLI vs. browser).
*/ */
class TestMailer extends \Mailer { class TestMailer extends \Mailer {
protected $table = '_Behat_TestMailer'; /**
* @var TestSessionEnvironment
*/
protected $testSessionEnvironment;
public function __construct() {
$this->testSessionEnvironment = \Injector::inst()->get('TestSessionEnvironment');
}
/** /**
* Send a plain-text email. * Send a plain-text email.
* TestMailer will merely record that the email was asked to be sent, without sending anything. * TestMailer will merely record that the email was asked to be sent, without sending anything.
*/ */
public function sendPlain($to, $from, $subject, $plainContent, $attachedFiles = false, $customHeaders = false) { public function sendPlain($to, $from, $subject, $plainContent, $attachedFiles = false, $customHeaders = false) {
$this->initTable();
$this->saveEmail(array( $this->saveEmail(array(
'Type' => 'plain', 'Type' => 'plain',
'To' => $to, 'To' => $to,
@ -39,8 +44,6 @@ class TestMailer extends \Mailer {
public function sendHTML($to, $from, $subject, $htmlContent, $attachedFiles = false, $customHeaders = false, public function sendHTML($to, $from, $subject, $htmlContent, $attachedFiles = false, $customHeaders = false,
$plainContent = false, $inlineImages = false) { $plainContent = false, $inlineImages = false) {
$this->initTable();
$this->saveEmail(array( $this->saveEmail(array(
'Type' => 'html', 'Type' => 'html',
'To' => $to, 'To' => $to,
@ -59,10 +62,9 @@ class TestMailer extends \Mailer {
* Clear the log of emails sent * Clear the log of emails sent
*/ */
public function clearEmails() { public function clearEmails() {
$this->initTable(); $state = $this->testSessionEnvironment->getState();
if(isset($state->emails)) unset($state->emails);
$db = $this->getDb(); $this->testSessionEnvironment->persistState();
$db->query(sprintf('TRUNCATE TABLE "%s"', $this->table));
} }
/** /**
@ -94,11 +96,9 @@ class TestMailer extends \Mailer {
*/ */
public function findEmails($to = null, $from = null, $subject = null, $content = null) { public function findEmails($to = null, $from = null, $subject = null, $content = null) {
$matches = array(); $matches = array();
$this->initTable();
$args = func_get_args(); $args = func_get_args();
$db = $this->getDb(); $state = $this->testSessionEnvironment->getState();
$emails = $db->query(sprintf('SELECT * FROM "%s"', $this->table)); $emails = isset($state->emails) ? $state->emails : array();
foreach($emails as $email) { foreach($emails as $email) {
$matched = true; $matched = true;
@ -116,41 +116,11 @@ class TestMailer extends \Mailer {
return $matches; return $matches;
} }
protected function initTable() {
$db = $this->getDb();
if(!$db->hasTable($this->table)) {
$db->beginSchemaUpdate();
$db->requireTable($this->table, array(
'Type' => 'Enum("plain,html")',
'From' => 'Text',
'To' => 'Text',
'Subject' => 'Text',
'Content' => 'Text',
'PlainContent' => 'Text',
'AttachedFiles' => 'Text',
'CustomHeaders' => 'Text',
));
$db->endSchemaUpdate();
}
}
protected function saveEmail($data) { protected function saveEmail($data) {
$db = $this->getDb(); $state = $this->testSessionEnvironment->getState();
$data = array_filter($data); if(!isset($state->emails)) $state->emails = array();
$manipulation = array( $state->emails[] = array_filter($data);
$this->table => array( $this->testSessionEnvironment->persistState();
'command' => 'insert',
'fields' => array()
)
);
foreach($data as $k => $v) {
$manipulation[$this->table]['fields'][$k] = $db->prepStringForDB($v);
}
$db->manipulate($manipulation);
}
protected function getDb() {
return \DB::getConn();
} }
} }