2018-09-05 13:40:03 +02:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-09-05 13:40:03 +02:00
|
|
|
namespace Engelsystem\Test\Unit\Mail;
|
|
|
|
|
|
|
|
use Engelsystem\Mail\Mailer;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2023-10-01 21:52:42 +02:00
|
|
|
use Psr\Log\NullLogger;
|
|
|
|
use Psr\Log\Test\TestLogger;
|
2021-12-20 21:07:00 +01:00
|
|
|
use Symfony\Component\Mailer\Envelope;
|
2023-10-01 21:52:42 +02:00
|
|
|
use Symfony\Component\Mailer\Exception\TransportException;
|
2021-12-20 21:07:00 +01:00
|
|
|
use Symfony\Component\Mailer\MailerInterface;
|
|
|
|
use Symfony\Component\Mime\RawMessage;
|
2018-09-05 13:40:03 +02:00
|
|
|
|
|
|
|
class MailerTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Mail\Mailer::__construct
|
|
|
|
* @covers \Engelsystem\Mail\Mailer::getFromAddress
|
|
|
|
* @covers \Engelsystem\Mail\Mailer::getFromName
|
2019-04-24 11:01:37 +02:00
|
|
|
* @covers \Engelsystem\Mail\Mailer::setFromAddress
|
2018-09-05 13:40:03 +02:00
|
|
|
* @covers \Engelsystem\Mail\Mailer::setFromName
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testInitAndSettersAndGetters(): void
|
2018-09-05 13:40:03 +02:00
|
|
|
{
|
2023-10-01 21:52:42 +02:00
|
|
|
$log = new NullLogger();
|
2021-12-20 21:07:00 +01:00
|
|
|
/** @var MailerInterface|MockObject $symfonyMailer */
|
|
|
|
$symfonyMailer = $this->createMock(MailerInterface::class);
|
2018-09-05 13:40:03 +02:00
|
|
|
|
2023-10-01 21:52:42 +02:00
|
|
|
$mailer = new Mailer($log, $symfonyMailer);
|
2018-09-05 13:40:03 +02:00
|
|
|
|
|
|
|
$mailer->setFromName('From Name');
|
|
|
|
$this->assertEquals('From Name', $mailer->getFromName());
|
|
|
|
|
|
|
|
$mailer->setFromAddress('from@foo.bar');
|
|
|
|
$this->assertEquals('from@foo.bar', $mailer->getFromAddress());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Mail\Mailer::send
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testSend(): void
|
2018-09-05 13:40:03 +02:00
|
|
|
{
|
2023-10-01 21:52:42 +02:00
|
|
|
$log = new NullLogger();
|
2021-12-20 21:07:00 +01:00
|
|
|
/** @var MailerInterface|MockObject $symfonyMailer */
|
|
|
|
$symfonyMailer = $this->createMock(MailerInterface::class);
|
|
|
|
$symfonyMailer->expects($this->once())
|
2018-09-05 13:40:03 +02:00
|
|
|
->method('send')
|
2022-12-14 19:15:20 +01:00
|
|
|
->willReturnCallback(function (RawMessage $message, Envelope $envelope = null): void {
|
2021-12-20 21:07:00 +01:00
|
|
|
$this->assertStringContainsString('to@xam.pel', $message->toString());
|
|
|
|
$this->assertStringContainsString('foo@bar.baz', $message->toString());
|
|
|
|
$this->assertStringContainsString('Test Tester', $message->toString());
|
|
|
|
$this->assertStringContainsString('Foo Bar', $message->toString());
|
|
|
|
$this->assertStringContainsString('Lorem Ipsum!', $message->toString());
|
|
|
|
});
|
|
|
|
|
2023-10-01 21:52:42 +02:00
|
|
|
$mailer = new Mailer($log, $symfonyMailer);
|
2018-09-05 13:40:03 +02:00
|
|
|
$mailer->setFromAddress('foo@bar.baz');
|
2021-12-20 21:07:00 +01:00
|
|
|
$mailer->setFromName('Test Tester');
|
2018-09-05 13:40:03 +02:00
|
|
|
|
2023-10-01 21:52:42 +02:00
|
|
|
$status = $mailer->send('to@xam.pel', 'Foo Bar', 'Lorem Ipsum!');
|
|
|
|
$this->assertTrue($status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Mail\Mailer::send
|
|
|
|
*/
|
|
|
|
public function testSendException(): void
|
|
|
|
{
|
|
|
|
$log = new TestLogger();
|
|
|
|
/** @var MailerInterface|MockObject $symfonyMailer */
|
|
|
|
$symfonyMailer = $this->createMock(MailerInterface::class);
|
|
|
|
$symfonyMailer->expects($this->once())
|
|
|
|
->method('send')
|
|
|
|
->willReturnCallback(function (RawMessage $message, Envelope $envelope = null): void {
|
|
|
|
throw new TransportException('Unable to connect to port 42');
|
|
|
|
});
|
|
|
|
|
|
|
|
$mailer = new Mailer($log, $symfonyMailer);
|
|
|
|
$mailer->setFromAddress('foo@bar.baz');
|
|
|
|
$mailer->setFromName('Test Tester');
|
|
|
|
|
|
|
|
$status = $mailer->send('to@xam.pel', 'Foo Bar', 'Lorem Ipsum!');
|
|
|
|
$this->assertFalse($status);
|
|
|
|
|
|
|
|
$this->assertTrue($log->hasErrorThatContains('Unable to send e-mail'));
|
2018-09-05 13:40:03 +02:00
|
|
|
}
|
|
|
|
}
|