I am writing tests for a small Symfony 6 project. The project contains a custom class representing a web form request. The class has methods for sending various emails using the Symfony MailerInterface. For example:
public function sendConfirmationEmail(): array
{
if (!$this->validated) {
return [
'success' => false,
'message' => 'Inquiry has not been validated. Use Inquiry->validate() first',
];
}
if (!$this->email) {
return [
'success' => false,
'message' => 'Benutzer hat keine Emailadresse angegeben'
];
}
$email = (new TemplatedEmail())
->to($this->email)
->subject('Eingangsbestätigung')
->htmlTemplate('confirmationEmail.html.twig')
->textTemplate('confirmationEmail.txt.twig')
->context([
'name' => $this->name,
'mail' => $this->email,
'phone' => $this->phone,
'subject' => $this->subject,
'message' => $this->message,
]);
try {
$this->mailer->send($email);
$this->logger->debug('Confirmation mail sent');
return [
'success' => true,
'message' => 'Email wurde gesendet',
];
} catch (TransportExceptionInterface $e) {
$this->logger->debug('Error sending confirmation email: ' . $e);
return [
'success' => false,
'message' => 'Email konnte nicht gesendet werden: ' . $e,
];
}
}
The mailer
is passed to the constructor of the class as a read only variable:
public function __construct(
private readonly LoggerInterface $logger,
private readonly MailerInterface $mailer,
private readonly array $officeRecipients,
private readonly ValidatorInterface $validator,
) {
}
I have no trouble testing the successful cases using the Symfony KernelTestCase
class, but I also want to test the catch
block of the method in a unit test.
How can I simulate the TransportException
during testing to trigger the catch
block?