From 7e8021af04cbaa9df20609d7103052a481c10401 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Wed, 19 Dec 2012 15:47:39 +0100 Subject: [PATCH] NEW Mailer class and mock date support --- README.md | 4 +++- _config.php | 16 ++++++------- code/TestSessionController.php | 41 ++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c583cc7..e431961 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/_config.php b/_config.php index d07c59a..d28b30d 100644 --- a/_config.php +++ b/_config.php @@ -1,16 +1,16 @@ mailer = new SilverStripe\BehatExtension\Utility\TestMailer(); - Email::set_mailer($this->mailer); - Email::send_all_emails_to(null); - + 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); } } \ No newline at end of file diff --git a/code/TestSessionController.php b/code/TestSessionController.php index af3dd43..cd28761 100644 --- a/code/TestSessionController.php +++ b/code/TestSessionController.php @@ -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); }