From 01e9c22695a3e495f07ab445750221af72e09fe4 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Wed, 5 Sep 2018 13:40:03 +0200 Subject: Implemented mailing abstraction Closes #434 --- tests/Unit/Mail/EngelsystemMailerTest.php | 40 +++++ tests/Unit/Mail/MailerServiceProviderTest.php | 177 +++++++++++++++++++++ tests/Unit/Mail/MailerTest.php | 77 +++++++++ tests/Unit/Mail/Transport/LogTransportTest.php | 60 +++++++ .../Transport/Stub/TransportImplementation.php | 35 ++++ tests/Unit/Mail/Transport/TransportTest.php | 83 ++++++++++ 6 files changed, 472 insertions(+) create mode 100644 tests/Unit/Mail/EngelsystemMailerTest.php create mode 100644 tests/Unit/Mail/MailerServiceProviderTest.php create mode 100644 tests/Unit/Mail/MailerTest.php create mode 100644 tests/Unit/Mail/Transport/LogTransportTest.php create mode 100644 tests/Unit/Mail/Transport/Stub/TransportImplementation.php create mode 100644 tests/Unit/Mail/Transport/TransportTest.php (limited to 'tests/Unit') 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 @@ +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 @@ + [ + '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 @@ +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 @@ +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 ', $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 @@ +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 @@ +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 ', $return); + } +} -- cgit v1.2.3-70-g09d2