Skip to content

Commit

Permalink
FIX TestMailer instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Oct 19, 2022
1 parent 5c73330 commit bc5974e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
17 changes: 8 additions & 9 deletions src/TestSessionEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\Versioned\Versioned;
use Symfony\Component\Mailer\MailerInterface;
use stdClass;

/**
Expand Down Expand Up @@ -320,15 +321,13 @@ public function applyState($state)
}

// 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
Expand Down
11 changes: 9 additions & 2 deletions src/TestSessionHTTPMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

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}.
Expand Down Expand Up @@ -67,7 +69,12 @@ protected function loadTestState(HTTPRequest $request)
// 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', '[email protected]');
}
Expand Down

0 comments on commit bc5974e

Please sign in to comment.