FIX TestMailer instantiation

This commit is contained in:
Steve Boyd 2022-10-20 12:21:45 +13:00
parent 5c73330ba7
commit bc5974e551
2 changed files with 17 additions and 11 deletions

View File

@ -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,16 +321,14 @@ class TestSessionEnvironment
}
// Mailer
$mailer = (isset($state->mailer)) ? $state->mailer : null;
if ($mailer) {
if (!class_exists($mailer ?? '') || !is_subclass_of($mailer, 'SilverStripe\\Control\\Email\\Mailer')) {
$mailer = $state->mailer ?? null;
if ($mailer && !is_a($mailer, MailerInterface::class, true)) {
throw new InvalidArgumentException(sprintf(
'Class "%s" is not a valid class, or subclass of Mailer',
$mailer
'Class "%s" does not implement %s',
$mailer,
MailerInterface::class
));
}
}
// Date and time
if (isset($state->datetime)) {

View File

@ -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');
}