Merge pull request #81 from creative-commoners/pulls/3/mailer

FIX TestMailer instantiation
This commit is contained in:
Guy Sartorelli 2022-10-20 12:42:08 +13:00 committed by GitHub
commit 55b0071a9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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\DB;
use SilverStripe\ORM\FieldType\DBDatetime; use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\Versioned\Versioned; use SilverStripe\Versioned\Versioned;
use Symfony\Component\Mailer\MailerInterface;
use stdClass; use stdClass;
/** /**
@ -320,15 +321,13 @@ class TestSessionEnvironment
} }
// Mailer // Mailer
$mailer = (isset($state->mailer)) ? $state->mailer : null; $mailer = $state->mailer ?? null;
if ($mailer && !is_a($mailer, MailerInterface::class, true)) {
if ($mailer) { throw new InvalidArgumentException(sprintf(
if (!class_exists($mailer ?? '') || !is_subclass_of($mailer, 'SilverStripe\\Control\\Email\\Mailer')) { 'Class "%s" does not implement %s',
throw new InvalidArgumentException(sprintf( $mailer,
'Class "%s" is not a valid class, or subclass of Mailer', MailerInterface::class
$mailer ));
));
}
} }
// Date and time // Date and time

View File

@ -4,12 +4,14 @@ namespace SilverStripe\TestSession;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
use SilverStripe\Control\Email\Email; use SilverStripe\Control\Email\Email;
use SilverStripe\Control\Email\Mailer;
use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Middleware\HTTPMiddleware; use SilverStripe\Control\Middleware\HTTPMiddleware;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DB; use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBDatetime; 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}. * Sets state previously initialized through {@link TestSessionController}.
@ -67,7 +69,12 @@ class TestSessionHTTPMiddleware implements HTTPMiddleware
// Register mailer // Register mailer
if (isset($testState->mailer)) { if (isset($testState->mailer)) {
$mailer = $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()->set("send_all_emails_to", null);
Email::config()->update('admin_email', 'no-reply@example.com'); Email::config()->update('admin_email', 'no-reply@example.com');
} }