first commit
This commit is contained in:
72
tests/Kernel/MailServiceTest.php
Normal file
72
tests/Kernel/MailServiceTest.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Kernel;
|
||||
|
||||
use Netig\Netslim\Kernel\Mail\Infrastructure\MailService;
|
||||
use PHPMailer\PHPMailer\PHPMailer;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ReflectionMethod;
|
||||
use Slim\Views\Twig;
|
||||
use Twig\Loader\ArrayLoader;
|
||||
|
||||
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
|
||||
|
||||
final class MailServiceTest extends TestCase
|
||||
{
|
||||
public function testCreateMailerUsesSslConfiguration(): void
|
||||
{
|
||||
$service = $this->makeService('ssl', 465);
|
||||
$mailer = $this->invokeCreateMailer($service);
|
||||
|
||||
self::assertSame('smtp', $mailer->Mailer);
|
||||
self::assertSame('smtp.example.test', $mailer->Host);
|
||||
self::assertTrue($mailer->SMTPAuth);
|
||||
self::assertSame('mailer-user', $mailer->Username);
|
||||
self::assertSame('mailer-pass', $mailer->Password);
|
||||
self::assertSame(PHPMailer::ENCRYPTION_SMTPS, $mailer->SMTPSecure);
|
||||
self::assertSame(465, $mailer->Port);
|
||||
self::assertSame(PHPMailer::CHARSET_UTF8, $mailer->CharSet);
|
||||
self::assertSame('no-reply@example.test', $mailer->From);
|
||||
self::assertSame('NETslim', $mailer->FromName);
|
||||
}
|
||||
|
||||
public function testCreateMailerUsesStartTlsWhenEncryptionIsNotSsl(): void
|
||||
{
|
||||
$service = $this->makeService('tls', 587);
|
||||
$mailer = $this->invokeCreateMailer($service);
|
||||
|
||||
self::assertSame(PHPMailer::ENCRYPTION_STARTTLS, $mailer->SMTPSecure);
|
||||
self::assertSame(587, $mailer->Port);
|
||||
}
|
||||
|
||||
private function makeService(string $encryption, int $port): MailService
|
||||
{
|
||||
$twig = new Twig(new ArrayLoader([
|
||||
'@Test/emails/test.twig' => '<p>Bonjour {{ name }}</p>',
|
||||
]));
|
||||
|
||||
return new MailService(
|
||||
$twig,
|
||||
'smtp.example.test',
|
||||
$port,
|
||||
'mailer-user',
|
||||
'mailer-pass',
|
||||
$encryption,
|
||||
'no-reply@example.test',
|
||||
'NETslim',
|
||||
);
|
||||
}
|
||||
|
||||
private function invokeCreateMailer(MailService $service): PHPMailer
|
||||
{
|
||||
$method = new ReflectionMethod($service, 'createMailer');
|
||||
$method->setAccessible(true);
|
||||
|
||||
/** @var PHPMailer $mailer */
|
||||
$mailer = $method->invoke($service);
|
||||
|
||||
return $mailer;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user