$subject =
$this->
randomString(64
);
$body =
$this->
randomString(128
);
$message =
[ 'id' => 'drupal_mail_test',
'headers' =>
['Content-type' => 'text/html'
],
'subject' =>
$subject,
'to' => 'foobar@example.com',
'body' =>
$body,
];
// Before we send the email, drupalGetMails should return an empty array.
$captured_emails =
$this->
drupalGetMails();
$this->
assertCount(0,
$captured_emails, 'The captured emails queue is empty.'
);
// Send the email.
\Drupal::
service('plugin.manager.mail'
)->
getInstance(['module' => 'test', 'key' => 'drupal_mail_test'
])->
mail($message);
// Ensure that there is one email in the captured emails array.
$captured_emails =
$this->
drupalGetMails();
$this->
assertCount(1,
$captured_emails, 'One email was captured.'
);
// Assert that the email was sent by iterating over the message properties
// and ensuring that they are captured intact.