summaryrefslogtreecommitdiff
path: root/tests/Unit/Mail/Transport
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Unit/Mail/Transport')
-rw-r--r--tests/Unit/Mail/Transport/LogTransportTest.php60
-rw-r--r--tests/Unit/Mail/Transport/Stub/TransportImplementation.php35
-rw-r--r--tests/Unit/Mail/Transport/TransportTest.php83
3 files changed, 178 insertions, 0 deletions
diff --git a/tests/Unit/Mail/Transport/LogTransportTest.php b/tests/Unit/Mail/Transport/LogTransportTest.php
new file mode 100644
index 00000000..5eb3a667
--- /dev/null
+++ b/tests/Unit/Mail/Transport/LogTransportTest.php
@@ -0,0 +1,60 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Mail\Transport;
+
+use Engelsystem\Mail\Transport\LogTransport;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+use Psr\Log\LoggerInterface;
+use Swift_Mime_SimpleMessage as SimpleMessage;
+
+class LogTransportTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Mail\Transport\LogTransport::__construct
+ * @covers \Engelsystem\Mail\Transport\LogTransport::send
+ */
+ public function testSend()
+ {
+ /** @var LoggerInterface|MockObject $logger */
+ $logger = $this->getMockForAbstractClass(LoggerInterface::class);
+ /** @var SimpleMessage|MockObject $message */
+ $message = $this->createMock(SimpleMessage::class);
+
+ $message->expects($this->once())
+ ->method('getSubject')
+ ->willReturn('Some subject');
+ $message->expects($this->once())
+ ->method('getHeaders')
+ ->willReturn('Head: er');
+ $message->expects($this->once())
+ ->method('toString')
+ ->willReturn('Message body');
+
+ $logger->expects($this->once())
+ ->method('debug')
+ ->willReturnCallback(function ($message, $context = []) {
+ foreach (array_keys($context) as $key) {
+ $this->assertContains(sprintf('{%s}', $key), $message);
+ }
+
+ $this->assertEquals('Some subject', $context['title']);
+ $this->assertEquals('foo@bar.batz,Lorem Ipsum <lor@em.ips>', $context['recipients']);
+ $this->assertContains('Head: er', $context['content']);
+ $this->assertContains('Message body', $context['content']);
+ });
+
+ /** @var LogTransport|MockObject $transport */
+ $transport = $this->getMockBuilder(LogTransport::class)
+ ->setConstructorArgs(['logger' => $logger])
+ ->setMethods(['allRecipients'])
+ ->getMock();
+ $transport->expects($this->exactly(2))
+ ->method('allRecipients')
+ ->with($message)
+ ->willReturn(['foo@bar.batz' => null, 'lor@em.ips' => 'Lorem Ipsum']);
+
+ $return = $transport->send($message);
+ $this->equalTo(2, $return);
+ }
+}
diff --git a/tests/Unit/Mail/Transport/Stub/TransportImplementation.php b/tests/Unit/Mail/Transport/Stub/TransportImplementation.php
new file mode 100644
index 00000000..e3667c6e
--- /dev/null
+++ b/tests/Unit/Mail/Transport/Stub/TransportImplementation.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Mail\Transport\Stub;
+
+use Engelsystem\Mail\Transport\Transport;
+use Swift_Mime_SimpleMessage as SimpleMessage;
+
+class TransportImplementation extends Transport
+{
+ /**
+ * {@inheritdoc}
+ */
+ public function send(SimpleMessage $message, &$failedRecipients = null)
+ {
+ return 0;
+ }
+
+ /**
+ * @param SimpleMessage $message
+ * @return array
+ */
+ public function getAllRecipients(SimpleMessage $message)
+ {
+ return $this->allRecipients($message);
+ }
+
+ /**
+ * @param SimpleMessage $message
+ * @return string
+ */
+ public function getGetTo(SimpleMessage $message)
+ {
+ return $this->getTo($message);
+ }
+}
diff --git a/tests/Unit/Mail/Transport/TransportTest.php b/tests/Unit/Mail/Transport/TransportTest.php
new file mode 100644
index 00000000..60f2079d
--- /dev/null
+++ b/tests/Unit/Mail/Transport/TransportTest.php
@@ -0,0 +1,83 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Mail\Transport;
+
+use Engelsystem\Test\Unit\Mail\Transport\Stub\TransportImplementation;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+use Swift_Mime_SimpleMessage as SimpleMessage;
+
+class TransportTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Mail\Transport\Transport::isStarted
+ * @covers \Engelsystem\Mail\Transport\Transport::ping
+ */
+ public function testMethods()
+ {
+ $transport = new TransportImplementation();
+
+ $this->assertTrue($transport->isStarted());
+ $this->assertTrue($transport->ping());
+ }
+
+ /**
+ * @covers \Engelsystem\Mail\Transport\Transport::allRecipients
+ */
+ public function testAllRecipients()
+ {
+ /** @var SimpleMessage|MockObject $message */
+ $message = $this->createMock(SimpleMessage::class);
+ $transport = new TransportImplementation();
+ $message->expects($this->once())
+ ->method('getTo')
+ ->willReturn([
+ 'foo@bar.batz' => 'Foo Bar',
+ 'lorem@ipsum.dolor' => null,
+ ]);
+ $message->expects($this->once())
+ ->method('getCc')
+ ->willReturn([
+ 'to@bar.batz' => null,
+ ]);
+ $message->expects($this->once())
+ ->method('getBcc')
+ ->willReturn([
+ 'secret@bar.batz' => 'I\'m secret!',
+ ]);
+
+ $this->assertEquals(
+ [
+ 'foo@bar.batz' => 'Foo Bar',
+ 'lorem@ipsum.dolor' => null,
+ 'to@bar.batz' => null,
+ 'secret@bar.batz' => 'I\'m secret!',
+ ],
+ $transport->getAllRecipients($message)
+ );
+ }
+
+ /**
+ * @covers \Engelsystem\Mail\Transport\Transport::getTo
+ * @covers \Engelsystem\Mail\Transport\Transport::formatTo
+ */
+ public function testGetTo()
+ {
+ /** @var SimpleMessage|MockObject $message */
+ $message = $this->createMock(SimpleMessage::class);
+ /** @var TransportImplementation|MockObject $transport */
+ $transport = $this->getMockBuilder(TransportImplementation::class)
+ ->setMethods(['allRecipients'])
+ ->getMock();
+ $transport->expects($this->once())
+ ->method('allRecipients')
+ ->with($message)
+ ->willReturn([
+ 'foo@bar.batz' => null,
+ 'lorem@ipsum.dolor' => 'Developer',
+ ]);
+
+ $return = $transport->getGetTo($message);
+ $this->assertEquals('foo@bar.batz,Developer <lorem@ipsum.dolor>', $return);
+ }
+}