NEW Mailer class and mock date support

This commit is contained in:
Ingo Schommer 2012-12-19 15:47:39 +01:00
parent a2d7675e57
commit 7e8021af04
3 changed files with 52 additions and 9 deletions

View File

@ -51,7 +51,9 @@ Parameters for "dev/testsession/start":
Note: The database names are limited to a specific naming convention as a security measure:
The "ss_tmpdb" prefix and a random sequence of seven digits.
This avoids the user gaining access to other production databases available on the same connection.
* `mailer`: Subclass of `Mailer`, typically used to record emails instead of actually sending them.
* `date`: Sets a simulated date used for all framework operations.
Format as "yyyy-MM-dd HH:mm:ss" (Example: "2012-12-31 18:40:59").
Example usage with parameters:

View File

@ -1,16 +1,16 @@
<?php
if(DB::get_alternative_database_name()) {
Session::start();
require_once BASE_PATH . '/vendor/autoload.php';
// Register mailer
$this->mailer = new SilverStripe\BehatExtension\Utility\TestMailer();
Email::set_mailer($this->mailer);
if($mailer = Session::get('testsession.mailer')) {
Email::set_mailer(new $mailer());
Email::send_all_emails_to(null);
}
// Set mock date and time
$mockDate = Session::get('behat.mockDate');
if($mockDate) {
SS_Datetime::set_mock_now($mockDate);
$date = Session::get('testsession.date');
if($date) {
SS_Datetime::set_mock_now($date);
}
}

View File

@ -136,6 +136,35 @@ class TestSessionController extends Controller {
array_shift($dataClasses);
foreach($dataClasses as $dataClass) singleton($dataClass)->requireDefaultRecords();
}
// Mailer
$mailer = (isset($data['mailer'])) ? $data['mailer'] : null;
if($mailer) {
if(!class_exists($mailer) || !is_subclass_of($mailer, 'Mailer')) {
throw new InvalidArgumentException(sprintf(
'Class "%s" is not a valid class, or subclass of Mailer',
$mailer
));
}
// Configured through testsession/_config.php
Session::set('testsession.mailer', $mailer);
}
// Date
$date = (isset($data['date'])) ? $data['date'] : null;
if($date) {
require_once 'Zend/Date.php';
if(!Zend_Date::isDate($date, 'yyyy-MM-dd HH:mm:ss')) {
throw new LogicException(sprintf(
'Invalid date format "%s", use yyyy-MM-dd HH:mm:ss',
$date
));
}
// Configured through testsession/_config.php
Session::set('testsession.date', $date);
}
}
/**
@ -155,6 +184,18 @@ class TestSessionController extends Controller {
'Value' => implode(',', array_unique($fixtures)),
));
}
if($mailer = Session::get('testsession.mailer')) {
$state[] = new ArrayData(array(
'Name' => 'Mailer Class',
'Value' => $mailer,
));
}
if($date = Session::get('testsession.date')) {
$state[] = new ArrayData(array(
'Name' => 'Date',
'Value' => $date,
));
}
return new ArrayList($state);
}