function MailerCaptureTest::testMailCaptureTestRunner

Tests collecting mail sent in the test runner.

File

core/modules/mailer/tests/src/Functional/MailerCaptureTest.php, line 40

Class

MailerCaptureTest
Tests capturing of emails when mailer_capture module is installed.

Namespace

Drupal\Tests\mailer\Functional

Code

public function testMailCaptureTestRunner() : void {
  // Create an email.
  $email = new Email();
  $email->from('admin@example.com');
  $email->subject('State machine energy a production like service.');
  $email->text('We name know environmental along agree let. Traditional interest this clearly concern discover.');
  // Before we send the email, getEmails should return an empty array.
  $capturedEmails = $this->getEmails();
  $this->assertCount(0, $capturedEmails, 'The captured emails queue is empty.');
  $mailer = $this->container
    ->get(MailerInterface::class);
  assert($mailer instanceof MailerInterface);
  $mailer->send($email->to('foobar@example.com'));
  // Ensure that there is one email in the captured emails array.
  $capturedEmails = $this->getEmails();
  $this->assertCount(1, $capturedEmails, 'One email was captured.');
  $this->assertEquals([
    new Address('admin@example.com'),
  ], $capturedEmails[0]->getFrom());
  $this->assertEquals([
    new Address('foobar@example.com'),
  ], $capturedEmails[0]->getTo());
  $this->assertEquals('State machine energy a production like service.', $capturedEmails[0]->getSubject());
  $this->assertEquals('We name know environmental along agree let. Traditional interest this clearly concern discover.', (string) $capturedEmails[0]->getTextBody());
  $this->clearCapturedMessages();
  $capturedEmails = $this->getEmails();
  $this->assertCount(0, $capturedEmails, 'The captured emails queue is empty.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.