summaryrefslogtreecommitdiff
path: root/tests/Unit
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Unit')
-rw-r--r--tests/Unit/Mail/EngelsystemMailerTest.php40
-rw-r--r--tests/Unit/Mail/MailerServiceProviderTest.php177
-rw-r--r--tests/Unit/Mail/MailerTest.php77
-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
6 files changed, 472 insertions, 0 deletions
diff --git a/tests/Unit/Mail/EngelsystemMailerTest.php b/tests/Unit/Mail/EngelsystemMailerTest.php
new file mode 100644
index 00000000..aae6e267
--- /dev/null
+++ b/tests/Unit/Mail/EngelsystemMailerTest.php
@@ -0,0 +1,40 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Mail;
+
+use Engelsystem\Mail\EngelsystemMailer;
+use Engelsystem\Renderer\Renderer;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+use Swift_Mailer as SwiftMailer;
+
+class EngelsystemMailerTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Mail\EngelsystemMailer::__construct
+ * @covers \Engelsystem\Mail\EngelsystemMailer::sendView
+ */
+ public function testSendView()
+ {
+ /** @var Renderer|MockObject $view */
+ $view = $this->createMock(Renderer::class);
+ /** @var SwiftMailer|MockObject $swiftMailer */
+ $swiftMailer = $this->createMock(SwiftMailer::class);
+ /** @var EngelsystemMailer|MockObject $mailer */
+ $mailer = $this->getMockBuilder(EngelsystemMailer::class)
+ ->setConstructorArgs(['mailer' => $swiftMailer, 'view' => $view])
+ ->setMethods(['send'])
+ ->getMock();
+ $mailer->expects($this->once())
+ ->method('send')
+ ->with('foo@bar.baz', 'Lorem dolor', 'Rendered Stuff!')
+ ->willReturn(1);
+ $view->expects($this->once())
+ ->method('render')
+ ->with('test/template.tpl', ['dev' => true])
+ ->willReturn('Rendered Stuff!');
+
+ $return = $mailer->sendView('foo@bar.baz', 'Lorem dolor', 'test/template.tpl', ['dev' => true]);
+ $this->equalTo(1, $return);
+ }
+}
diff --git a/tests/Unit/Mail/MailerServiceProviderTest.php b/tests/Unit/Mail/MailerServiceProviderTest.php
new file mode 100644
index 00000000..0c841e9e
--- /dev/null
+++ b/tests/Unit/Mail/MailerServiceProviderTest.php
@@ -0,0 +1,177 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Middleware;
+
+use Engelsystem\Application;
+use Engelsystem\Config\Config;
+use Engelsystem\Mail\EngelsystemMailer;
+use Engelsystem\Mail\Mailer;
+use Engelsystem\Mail\MailerServiceProvider;
+use Engelsystem\Mail\Transport\LogTransport;
+use Engelsystem\Test\Unit\ServiceProviderTest;
+use InvalidArgumentException;
+use Psr\Log\LoggerInterface;
+use Swift_Mailer as SwiftMailer;
+use Swift_SendmailTransport as SendmailTransport;
+use Swift_SmtpTransport as SmtpTransport;
+use Swift_Transport as Transport;
+
+class MailerServiceProviderTest extends ServiceProviderTest
+{
+ /** @var array */
+ protected $defaultConfig = [
+ 'email' => [
+ 'driver' => 'mail',
+ 'from' => [
+ 'name' => 'Engelsystem',
+ 'address' => 'foo@bar.batz',
+ ],
+ 'sendmail' => '/opt/bin/sendmail -bs',
+ ],
+ ];
+
+ /** @var array */
+ protected $smtpConfig = [
+ 'email' => [
+ 'driver' => 'smtp',
+ 'host' => 'mail.foo.bar',
+ 'port' => 587,
+ 'encryption' => 'tls',
+ 'username' => 'foobar',
+ 'password' => 'LoremIpsum123',
+ ],
+ ];
+
+ /**
+ * @covers \Engelsystem\Mail\MailerServiceProvider::register
+ */
+ public function testRegister()
+ {
+ $app = $this->getApplication();
+
+ $serviceProvider = new MailerServiceProvider($app);
+ $serviceProvider->register();
+
+ $this->assertExistsInContainer(['mailer.transport', Transport::class], $app);
+ $this->assertExistsInContainer(['mailer.swift', SwiftMailer::class], $app);
+ $this->assertExistsInContainer(['mailer', EngelsystemMailer::class, Mailer::class], $app);
+
+ /** @var EngelsystemMailer $mailer */
+ $mailer = $app->get('mailer');
+ $this->assertEquals('Engelsystem', $mailer->getFromName());
+ $this->assertEquals('foo@bar.batz', $mailer->getFromAddress());
+
+ /** @var SendmailTransport $transport */
+ $transport = $app->get('mailer.transport');
+ $this->assertEquals($this->defaultConfig['email']['sendmail'], $transport->getCommand());
+ }
+
+ /**
+ * @return array
+ */
+ public function provideTransports()
+ {
+ return [
+ [LogTransport::class, ['email' => ['driver' => 'log']]],
+ [SendmailTransport::class, ['email' => ['driver' => 'mail']]],
+ [SendmailTransport::class, ['email' => ['driver' => 'sendmail']]],
+ [
+ SmtpTransport::class,
+ $this->smtpConfig
+ ],
+ ];
+ }
+
+ /**
+ * @covers \Engelsystem\Mail\MailerServiceProvider::getTransport
+ * @param string $class
+ * @param array $emailConfig
+ * @dataProvider provideTransports
+ */
+ public function testGetTransport($class, $emailConfig = [])
+ {
+ $app = $this->getApplication($emailConfig);
+
+ $serviceProvider = new MailerServiceProvider($app);
+ $serviceProvider->register();
+
+ $transport = $app->get('mailer.transport');
+ $this->assertInstanceOf($class, $transport);
+ }
+
+ /**
+ * @covers \Engelsystem\Mail\MailerServiceProvider::getTransport
+ */
+ public function testGetTransportNotFound()
+ {
+ $app = $this->getApplication(['email' => ['driver' => 'foo-bar-batz']]);
+ $this->expectException(InvalidArgumentException::class);
+
+ $serviceProvider = new MailerServiceProvider($app);
+ $serviceProvider->register();
+ }
+
+ /**
+ * @covers \Engelsystem\Mail\MailerServiceProvider::getSmtpTransport
+ */
+ public function testGetSmtpTransport()
+ {
+ $app = $this->getApplication($this->smtpConfig);
+
+ $serviceProvider = new MailerServiceProvider($app);
+ $serviceProvider->register();
+
+ /** @var SmtpTransport $transport */
+ $transport = $app->get('mailer.transport');
+
+ $this->assertEquals($this->smtpConfig['email']['host'], $transport->getHost());
+ $this->assertEquals($this->smtpConfig['email']['port'], $transport->getPort());
+ $this->assertEquals($this->smtpConfig['email']['encryption'], $transport->getEncryption());
+ $this->assertEquals($this->smtpConfig['email']['username'], $transport->getUsername());
+ $this->assertEquals($this->smtpConfig['email']['password'], $transport->getPassword());
+ }
+
+ /**
+ * @param array $configuration
+ * @return Application
+ */
+ protected function getApplication($configuration = []): Application
+ {
+ $app = new Application();
+
+ $configuration = new Config(array_replace_recursive($this->defaultConfig, $configuration));
+ $app->instance('config', $configuration);
+
+ $logger = $this->getMockForAbstractClass(LoggerInterface::class);
+ $app->instance(LoggerInterface::class, $logger);
+
+ return $app;
+ }
+
+ /**
+ * @param string[] $abstracts
+ * @param Application $container
+ */
+ protected function assertExistsInContainer($abstracts, $container)
+ {
+ $first = array_shift($abstracts);
+ $this->assertContainerHas($first, $container);
+
+ foreach ($abstracts as $abstract) {
+ $this->assertContainerHas($abstract, $container);
+ $this->assertEquals($container->get($first), $container->get($abstract));
+ }
+ }
+
+ /**
+ * @param string $abstract
+ * @param Application $container
+ */
+ protected function assertContainerHas($abstract, $container)
+ {
+ $this->assertTrue(
+ $container->has($abstract) || $container->hasMethodBinding($abstract),
+ sprintf('Container does not contain abstract %s', $abstract)
+ );
+ }
+}
diff --git a/tests/Unit/Mail/MailerTest.php b/tests/Unit/Mail/MailerTest.php
new file mode 100644
index 00000000..24c6f69a
--- /dev/null
+++ b/tests/Unit/Mail/MailerTest.php
@@ -0,0 +1,77 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Mail;
+
+use Engelsystem\Mail\Mailer;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+use Swift_Mailer as SwiftMailer;
+use Swift_Message as SwiftMessage;
+
+class MailerTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Mail\Mailer::__construct
+ * @covers \Engelsystem\Mail\Mailer::getFromAddress
+ * @covers \Engelsystem\Mail\Mailer::setFromAddress
+ * @covers \Engelsystem\Mail\Mailer::getFromName
+ * @covers \Engelsystem\Mail\Mailer::setFromName
+ */
+ public function testInitAndSettersAndGetters()
+ {
+ /** @var SwiftMailer|MockObject $swiftMailer */
+ $swiftMailer = $this->createMock(SwiftMailer::class);
+
+ $mailer = new Mailer($swiftMailer);
+
+ $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
+ */
+ public function testSend()
+ {
+ /** @var SwiftMessage|MockObject $message */
+ $message = $this->createMock(SwiftMessage::class);
+ /** @var SwiftMailer|MockObject $swiftMailer */
+ $swiftMailer = $this->createMock(SwiftMailer::class);
+ $swiftMailer->expects($this->once())
+ ->method('createMessage')
+ ->willReturn($message);
+ $swiftMailer->expects($this->once())
+ ->method('send')
+ ->willReturn(1);
+
+ $message->expects($this->once())
+ ->method('setTo')
+ ->with(['to@xam.pel'])
+ ->willReturn($message);
+
+ $message->expects($this->once())
+ ->method('setFrom')
+ ->with('foo@bar.baz', 'Lorem Ipsum')
+ ->willReturn($message);
+
+ $message->expects($this->once())
+ ->method('setSubject')
+ ->with('Foo Bar')
+ ->willReturn($message);
+
+ $message->expects($this->once())
+ ->method('setBody')
+ ->with('Lorem Ipsum!')
+ ->willReturn($message);
+
+ $mailer = new Mailer($swiftMailer);
+ $mailer->setFromAddress('foo@bar.baz');
+ $mailer->setFromName('Lorem Ipsum');
+
+ $return = $mailer->send('to@xam.pel', 'Foo Bar', 'Lorem Ipsum!');
+ $this->equalTo(1, $return);
+ }
+}
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);
+ }
+}