Level 61
Use dependency injection and pass in mocked objects.
I want to write a unit test using mockery for following example code block.
protected function evaluate(): EvaluationResult
{
try {
$smtpTransport = \App::make(\Swift_SmtpTransport::class, [
'host' => config('mail.host'),
'port' => config('mail.port'),
'encryption' => config('mail.encryption')
])->setUsername(config('mail.username'))->setPassword(config('mail.password'));
$mailer = \App::make(\Swift_Mailer::class, ['transport' => $smtpTransport]);
$mailer->getTransport()->start();
$connectionmade = true;
} catch (\Exception $e) {
$connectionmade = false;
}
return new EvaluationResult(
($connectionmade) ? SystemHealthStatus::SUCCESS() : SystemHealthStatus::FAILED(),
($connectionmade) ? 'Mail server connection established' : 'No mail server connection could be established',
new \DateTimeImmutable()
);
}
Please or to participate in this conversation.