2018-09-05 13:40:03 +02:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-04-19 20:41:38 +02:00
|
|
|
namespace Engelsystem\Test\Unit\Mail;
|
2018-09-05 13:40:03 +02:00
|
|
|
|
|
|
|
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;
|
2021-12-20 21:07:00 +01:00
|
|
|
use Symfony\Component\Mailer\Mailer as SymfonyMailer;
|
|
|
|
use Symfony\Component\Mailer\Transport\SendmailTransport;
|
|
|
|
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
|
|
|
|
use Symfony\Component\Mailer\Transport\TransportInterface;
|
2018-09-05 13:40:03 +02:00
|
|
|
|
|
|
|
class MailerServiceProviderTest extends ServiceProviderTest
|
|
|
|
{
|
|
|
|
/** @var array */
|
2022-12-15 19:50:56 +01:00
|
|
|
protected array $defaultConfig = [
|
2018-10-05 15:35:14 +02:00
|
|
|
'app_name' => 'Engelsystem App',
|
|
|
|
'email' => [
|
2018-09-05 13:40:03 +02:00
|
|
|
'driver' => 'mail',
|
|
|
|
'from' => [
|
|
|
|
'name' => 'Engelsystem',
|
|
|
|
'address' => 'foo@bar.batz',
|
|
|
|
],
|
|
|
|
'sendmail' => '/opt/bin/sendmail -bs',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
/** @var array */
|
2022-12-15 19:50:56 +01:00
|
|
|
protected array $smtpConfig = [
|
2018-09-05 13:40:03 +02:00
|
|
|
'email' => [
|
|
|
|
'driver' => 'smtp',
|
|
|
|
'host' => 'mail.foo.bar',
|
|
|
|
'port' => 587,
|
2021-12-20 21:07:00 +01:00
|
|
|
'tls' => true,
|
2018-09-05 13:40:03 +02:00
|
|
|
'username' => 'foobar',
|
|
|
|
'password' => 'LoremIpsum123',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Mail\MailerServiceProvider::register
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testRegister(): void
|
2018-09-05 13:40:03 +02:00
|
|
|
{
|
|
|
|
$app = $this->getApplication();
|
|
|
|
|
|
|
|
$serviceProvider = new MailerServiceProvider($app);
|
|
|
|
$serviceProvider->register();
|
|
|
|
|
2021-12-20 21:07:00 +01:00
|
|
|
$this->assertExistsInContainer(['mailer.transport', TransportInterface::class], $app);
|
|
|
|
$this->assertExistsInContainer(['mailer.symfony', SymfonyMailer::class], $app);
|
2018-09-05 13:40:03 +02:00
|
|
|
$this->assertExistsInContainer(['mailer', EngelsystemMailer::class, Mailer::class], $app);
|
|
|
|
|
|
|
|
/** @var EngelsystemMailer $mailer */
|
|
|
|
$mailer = $app->get('mailer');
|
2018-10-05 15:35:14 +02:00
|
|
|
$this->assertEquals('Engelsystem App', $mailer->getSubjectPrefix());
|
2018-09-05 13:40:03 +02:00
|
|
|
$this->assertEquals('Engelsystem', $mailer->getFromName());
|
|
|
|
$this->assertEquals('foo@bar.batz', $mailer->getFromAddress());
|
|
|
|
|
|
|
|
/** @var SendmailTransport $transport */
|
|
|
|
$transport = $app->get('mailer.transport');
|
2021-12-20 21:07:00 +01:00
|
|
|
$this->assertInstanceOf(SendmailTransport::class, $transport);
|
2018-09-05 13:40:03 +02:00
|
|
|
}
|
|
|
|
|
2022-12-14 19:15:20 +01:00
|
|
|
public function provideTransports(): array
|
2018-09-05 13:40:03 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
[LogTransport::class, ['email' => ['driver' => 'log']]],
|
|
|
|
[SendmailTransport::class, ['email' => ['driver' => 'mail']]],
|
|
|
|
[SendmailTransport::class, ['email' => ['driver' => 'sendmail']]],
|
|
|
|
[
|
2021-12-20 21:07:00 +01:00
|
|
|
EsmtpTransport::class,
|
2019-04-24 11:01:37 +02:00
|
|
|
$this->smtpConfig,
|
2018-09-05 13:40:03 +02:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Mail\MailerServiceProvider::getTransport
|
|
|
|
* @dataProvider provideTransports
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testGetTransport(string $class, array $emailConfig = []): void
|
2018-09-05 13:40:03 +02:00
|
|
|
{
|
|
|
|
$app = $this->getApplication($emailConfig);
|
|
|
|
|
|
|
|
$serviceProvider = new MailerServiceProvider($app);
|
|
|
|
$serviceProvider->register();
|
|
|
|
|
|
|
|
$transport = $app->get('mailer.transport');
|
|
|
|
$this->assertInstanceOf($class, $transport);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Mail\MailerServiceProvider::getTransport
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testGetTransportNotFound(): void
|
2018-09-05 13:40:03 +02:00
|
|
|
{
|
|
|
|
$app = $this->getApplication(['email' => ['driver' => 'foo-bar-batz']]);
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
|
|
|
|
$serviceProvider = new MailerServiceProvider($app);
|
|
|
|
$serviceProvider->register();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Mail\MailerServiceProvider::getSmtpTransport
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testGetSmtpTransport(): void
|
2018-09-05 13:40:03 +02:00
|
|
|
{
|
|
|
|
$app = $this->getApplication($this->smtpConfig);
|
|
|
|
|
|
|
|
$serviceProvider = new MailerServiceProvider($app);
|
|
|
|
$serviceProvider->register();
|
|
|
|
|
2021-12-20 21:07:00 +01:00
|
|
|
/** @var EsmtpTransport $transport */
|
2018-09-05 13:40:03 +02:00
|
|
|
$transport = $app->get('mailer.transport');
|
|
|
|
|
|
|
|
$this->assertEquals($this->smtpConfig['email']['username'], $transport->getUsername());
|
|
|
|
$this->assertEquals($this->smtpConfig['email']['password'], $transport->getPassword());
|
|
|
|
}
|
|
|
|
|
2022-12-14 00:28:34 +01:00
|
|
|
protected function getApplication(array $configuration = []): Application
|
2018-09-05 13:40:03 +02:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
protected function assertExistsInContainer(array $abstracts, Application $container): void
|
2018-09-05 13:40:03 +02:00
|
|
|
{
|
|
|
|
$first = array_shift($abstracts);
|
|
|
|
$this->assertContainerHas($first, $container);
|
|
|
|
|
|
|
|
foreach ($abstracts as $abstract) {
|
|
|
|
$this->assertContainerHas($abstract, $container);
|
|
|
|
$this->assertEquals($container->get($first), $container->get($abstract));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-14 19:15:20 +01:00
|
|
|
protected function assertContainerHas(string $abstract, Application $container): void
|
2018-09-05 13:40:03 +02:00
|
|
|
{
|
|
|
|
$this->assertTrue(
|
|
|
|
$container->has($abstract) || $container->hasMethodBinding($abstract),
|
|
|
|
sprintf('Container does not contain abstract %s', $abstract)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|