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 1786ce1 commit f023b00
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/Context/EmailContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Session;
use PHPUnit\Framework\Assert;
use Psr\EventDispatcher\EventDispatcherInterface;
use SilverStripe\BehatExtension\Utility\TestMailer;
use SilverStripe\Core\Injector\Injector;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mailer\Transport\NullTransport;

/**
* Context used to define steps related to email sending.
Expand Down Expand Up @@ -48,7 +50,15 @@ public function before(BeforeScenarioScope $event)
{
// Also set through the 'supportbehat' extension
// to ensure its available both in CLI execution and the tested browser session
$this->mailer = new TestMailer();
# $dispatcher = Injector::inst()->get(EventDispatcherInterface::class . '.mailer');
// class: Symfony\Component\EventDispatcher\EventDispatcher
// calls:
// - [addSubscriber, ['%$SilverStripe\Control\Email\MailerSubscriber']]
$subscriber = Injector::inst()->get('SilverStripe\Control\Email\MailerSubscriber');
$dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
$dispatcher->addSubscriber($subscriber);
$transport = new NullTransport($dispatcher);
$this->mailer = new TestMailer($transport, $dispatcher);
Injector::inst()->registerService($this->mailer, MailerInterface::class);
}

Expand Down
10 changes: 8 additions & 2 deletions src/Utility/TestMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace SilverStripe\BehatExtension\Utility;

use Psr\EventDispatcher\EventDispatcherInterface;
use SilverStripe\Dev\TestMailer as BaseTestMailer;
use SilverStripe\TestSession\TestSessionEnvironment;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mailer\Transport\TransportInterface;

/**
* Same principle as core TestMailer class,
Expand All @@ -17,8 +20,11 @@ class TestMailer extends BaseTestMailer
*/
protected $testSessionEnvironment;

public function __construct()
{
public function __construct(
TransportInterface $transport,
EventDispatcherInterface $dispatcher
) {
parent::__construct($transport, $dispatcher);
$this->testSessionEnvironment = TestSessionEnvironment::singleton();
}

Expand Down

0 comments on commit f023b00

Please sign in to comment.