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/MailerServiceProviderTest.php | 177 ++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 tests/Unit/Mail/MailerServiceProviderTest.php (limited to 'tests/Unit/Mail/MailerServiceProviderTest.php') 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) + ); + } +} -- cgit v1.2.3-54-g00ecf