1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
<?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 = [
'app_name' => 'Engelsystem App',
'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 App', $mailer->getSubjectPrefix());
$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)
);
}
}
|