From bc5974e5510d0826604633fa6d7e7c38a530c943 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Thu, 20 Oct 2022 12:21:45 +1300 Subject: [PATCH] FIX TestMailer instantiation --- src/TestSessionEnvironment.php | 17 ++++++++--------- src/TestSessionHTTPMiddleware.php | 11 +++++++++-- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/TestSessionEnvironment.php b/src/TestSessionEnvironment.php index 0a729e3..4d63233 100644 --- a/src/TestSessionEnvironment.php +++ b/src/TestSessionEnvironment.php @@ -21,6 +21,7 @@ use SilverStripe\ORM\DatabaseAdmin; use SilverStripe\ORM\DB; use SilverStripe\ORM\FieldType\DBDatetime; use SilverStripe\Versioned\Versioned; +use Symfony\Component\Mailer\MailerInterface; use stdClass; /** @@ -320,15 +321,13 @@ class TestSessionEnvironment } // Mailer - $mailer = (isset($state->mailer)) ? $state->mailer : null; - - if ($mailer) { - if (!class_exists($mailer ?? '') || !is_subclass_of($mailer, 'SilverStripe\\Control\\Email\\Mailer')) { - throw new InvalidArgumentException(sprintf( - 'Class "%s" is not a valid class, or subclass of Mailer', - $mailer - )); - } + $mailer = $state->mailer ?? null; + if ($mailer && !is_a($mailer, MailerInterface::class, true)) { + throw new InvalidArgumentException(sprintf( + 'Class "%s" does not implement %s', + $mailer, + MailerInterface::class + )); } // Date and time diff --git a/src/TestSessionHTTPMiddleware.php b/src/TestSessionHTTPMiddleware.php index e3d20a8..26ae43a 100644 --- a/src/TestSessionHTTPMiddleware.php +++ b/src/TestSessionHTTPMiddleware.php @@ -4,12 +4,14 @@ namespace SilverStripe\TestSession; use SilverStripe\Control\Director; use SilverStripe\Control\Email\Email; -use SilverStripe\Control\Email\Mailer; use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\Middleware\HTTPMiddleware; use SilverStripe\Core\Injector\Injector; use SilverStripe\ORM\DB; use SilverStripe\ORM\FieldType\DBDatetime; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\Mailer\MailerInterface; +use Symfony\Component\Mailer\Transport\NullTransport; /** * Sets state previously initialized through {@link TestSessionController}. @@ -67,7 +69,12 @@ class TestSessionHTTPMiddleware implements HTTPMiddleware // Register mailer if (isset($testState->mailer)) { $mailer = $testState->mailer; - Injector::inst()->registerService(new $mailer(), Mailer::class); + $dispatcher = Injector::inst()->get(EventDispatcherInterface::class . '.mailer'); + $transport = new NullTransport($dispatcher); + Injector::inst()->registerService( + new $mailer($transport, $dispatcher), + MailerInterface::class + ); Email::config()->set("send_all_emails_to", null); Email::config()->update('admin_email', 'no-reply@example.com'); }