1
|
|
|
<?php |
2
|
|
|
namespace AcMailerTest\Service; |
3
|
|
|
|
4
|
|
|
use AcMailer\Exception\InvalidArgumentException; |
5
|
|
|
use AcMailer\Service\MailServiceInterface; |
6
|
|
|
use AcMailer\View\DefaultLayout; |
7
|
|
|
use AcMailerTest\Event\MailListenerMock; |
8
|
|
|
use Zend\Mail\Message; |
9
|
|
|
use AcMailerTest\Mail\Transport\MockTransport; |
10
|
|
|
use Zend\View\Model\ViewModel; |
11
|
|
|
use Zend\View\Renderer\PhpRenderer; |
12
|
|
|
use AcMailer\Service\MailService; |
13
|
|
|
use Zend\Mime; |
14
|
|
|
use AcMailer\Result\MailResult; |
15
|
|
|
use Zend\View\Resolver\TemplatePathStack; |
16
|
|
|
use PHPUnit_Framework_TestCase as TestCase; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Mail service test case |
20
|
|
|
* @author Alejandro Celaya Alastrué |
21
|
|
|
* @link http://www.alejandrocelaya.com |
22
|
|
|
*/ |
23
|
|
|
class MailServiceTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var \AcMailerTest\Mail\Transport\MockTransport |
27
|
|
|
*/ |
28
|
|
|
private $transport; |
29
|
|
|
/** |
30
|
|
|
* @var \AcMailer\Service\MailService |
31
|
|
|
*/ |
32
|
|
|
private $mailService; |
33
|
|
|
|
34
|
|
|
public function setUp() |
35
|
|
|
{ |
36
|
|
|
$this->transport = new MockTransport(); |
37
|
|
|
$config = include __DIR__ . '/../../config/module.config.php'; |
38
|
|
|
$renderer = new PhpRenderer(); |
39
|
|
|
$renderer->setResolver(new TemplatePathStack($config['view_manager']['template_path_stack'])); |
40
|
|
|
$this->mailService = new MailService(new Message(), $this->transport, $renderer); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testMimePartBodyCasting() |
44
|
|
|
{ |
45
|
|
|
$this->mailService->setBody(new Mime\Part('Foo')); |
46
|
|
|
$this->assertTrue($this->mailService->getMessage()->getBody() instanceof Mime\Message); |
47
|
|
|
|
48
|
|
|
/** @var Mime\Message $body */ |
49
|
|
|
$body = $this->mailService->getMessage()->getBody(); |
50
|
|
|
$this->assertNull($body->getParts()[0]->charset); |
51
|
|
|
|
52
|
|
|
$this->mailService->setBody(new Mime\Part('Foo'), MailServiceInterface::DEFAULT_CHARSET); |
53
|
|
|
/** @var Mime\Message $body */ |
54
|
|
|
$body = $this->mailService->getMessage()->getBody(); |
55
|
|
|
$this->assertEquals(MailServiceInterface::DEFAULT_CHARSET, $body->getParts()[0]->charset); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testHtmlBodyCasting() |
59
|
|
|
{ |
60
|
|
|
$this->mailService->setBody('<div>Html body</div>'); |
61
|
|
|
$this->assertTrue($this->mailService->getMessage()->getBody() instanceof Mime\Message); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testStringBodyCasting() |
65
|
|
|
{ |
66
|
|
|
$expected = 'String body'; |
67
|
|
|
$this->mailService->setBody($expected); |
68
|
|
|
$this->assertTrue($this->mailService->getMessage()->getBody() instanceof Mime\Message); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testMimeMessageBodyRemainsUnchanged() |
72
|
|
|
{ |
73
|
|
|
$part = new Mime\Part('Foo'); |
74
|
|
|
$message = new Mime\Message(); |
75
|
|
|
$message->addPart($part); |
76
|
|
|
$this->mailService->setBody($message); |
77
|
|
|
|
78
|
|
|
$this->assertTrue($this->mailService->getMessage()->getBody() instanceof Mime\Message); |
79
|
|
|
$this->assertEquals($message, $this->mailService->getMessage()->getBody()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testCharsetIsRespectedWhenSettingHtmlStringBody() |
83
|
|
|
{ |
84
|
|
|
$expected = 'foo'; |
85
|
|
|
$this->mailService->setBody('<h2>string</h2>', $expected); |
86
|
|
|
/** @var Mime\Message $body */ |
87
|
|
|
$body = $this->mailService->getMessage()->getBody(); |
88
|
|
|
$part = $body->getParts(); |
89
|
|
|
$this->assertCount(1, $part); |
90
|
|
|
|
91
|
|
|
/** @var Mime\Part $part */ |
92
|
|
|
$part = $part[0]; |
93
|
|
|
$this->assertEquals($expected, $part->charset); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @expectedException InvalidArgumentException |
98
|
|
|
*/ |
99
|
|
|
public function testInvalidBodyThrowsException() |
100
|
|
|
{ |
101
|
|
|
$this->mailService->setBody(new \stdClass()); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testSetSubject() |
105
|
|
|
{ |
106
|
|
|
$expected = 'This is the subject'; |
107
|
|
|
|
108
|
|
|
$this->assertEquals($this->mailService, $this->mailService->setSubject($expected)); |
|
|
|
|
109
|
|
|
$this->assertEquals($expected, $this->mailService->getMessage()->getSubject()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testSuccessfulSending() |
113
|
|
|
{ |
114
|
|
|
$result = $this->mailService->send(); |
115
|
|
|
|
116
|
|
|
$this->assertTrue($result->isValid()); |
117
|
|
|
$this->assertEquals(MailResult::DEFAULT_MESSAGE, $result->getMessage()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testSendingWithError() |
121
|
|
|
{ |
122
|
|
|
$this->transport->setForceError(true); |
123
|
|
|
$result = $this->mailService->send(); |
124
|
|
|
|
125
|
|
|
$this->assertFalse($result->isValid()); |
126
|
|
|
$this->assertEquals(MockTransport::ERROR_MESSAGE, $result->getMessage()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @expectedException \Exception |
131
|
|
|
*/ |
132
|
|
|
public function testWithUncatchedException() |
133
|
|
|
{ |
134
|
|
|
$this->transport->setForceError(true, new \Exception()); |
135
|
|
|
$this->mailService->send(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function testZendMailExceptionsAreNotRethrown() |
139
|
|
|
{ |
140
|
|
|
$this->transport->setForceError(true, new \Zend\Mail\Exception\InvalidArgumentException()); |
141
|
|
|
$result = $this->mailService->send(); |
142
|
|
|
$this->assertFalse($result->isValid()); |
143
|
|
|
|
144
|
|
|
$this->transport->setForceError(true, new \Zend\Mail\Exception\BadMethodCallException()); |
145
|
|
|
$result = $this->mailService->send(); |
146
|
|
|
$this->assertFalse($result->isValid()); |
147
|
|
|
|
148
|
|
|
$this->transport->setForceError(true, new \Zend\Mail\Protocol\Exception\InvalidArgumentException()); |
149
|
|
|
$result = $this->mailService->send(); |
150
|
|
|
$this->assertFalse($result->isValid()); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function testSetTransport() |
154
|
|
|
{ |
155
|
|
|
$this->assertSame($this->transport, $this->mailService->getTransport()); |
156
|
|
|
$anotherTransport = new MockTransport(); |
157
|
|
|
$this->assertSame($this->mailService, $this->mailService->setTransport($anotherTransport)); |
158
|
|
|
$this->assertSame($anotherTransport, $this->mailService->getTransport()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function testSetRenderer() |
162
|
|
|
{ |
163
|
|
|
$this->assertInstanceOf('Zend\View\Renderer\PhpRenderer', $this->mailService->getRenderer()); |
164
|
|
|
$anotherRenderer = new PhpRenderer(); |
165
|
|
|
$this->assertSame($this->mailService, $this->mailService->setRenderer($anotherRenderer)); |
166
|
|
|
$this->assertSame($anotherRenderer, $this->mailService->getRenderer()); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function testSuccesfulMailEvent() |
170
|
|
|
{ |
171
|
|
|
$mailListener = new MailListenerMock(); |
172
|
|
|
$this->mailService->attachMailListener($mailListener); |
173
|
|
|
$result = $this->mailService->send(); |
174
|
|
|
|
175
|
|
|
$this->assertTrue($result->isValid()); |
176
|
|
|
$this->assertTrue($mailListener->isOnPreSendCalled()); |
177
|
|
|
$this->assertTrue($mailListener->isOnPostSendCalled()); |
178
|
|
|
$this->assertFalse($mailListener->isOnSendErrorCalled()); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
View Code Duplication |
public function testMailEventWithError() |
|
|
|
|
182
|
|
|
{ |
183
|
|
|
$mailListener = new MailListenerMock(); |
184
|
|
|
$this->transport->setForceError(true); |
185
|
|
|
$this->mailService->attachMailListener($mailListener); |
186
|
|
|
$result = $this->mailService->send(); |
187
|
|
|
|
188
|
|
|
$this->assertFalse($result->isValid()); |
189
|
|
|
$this->assertTrue($mailListener->isOnPreSendCalled()); |
190
|
|
|
$this->assertFalse($mailListener->isOnPostSendCalled()); |
191
|
|
|
$this->assertTrue($mailListener->isOnSendErrorCalled()); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
View Code Duplication |
public function testDetachedMailListenerIsNotTriggered() |
|
|
|
|
195
|
|
|
{ |
196
|
|
|
$mailListener = new MailListenerMock(); |
197
|
|
|
$this->mailService->attachMailListener($mailListener); |
198
|
|
|
$this->mailService->detachMailListener($mailListener); |
199
|
|
|
$result = $this->mailService->send(); |
200
|
|
|
|
201
|
|
|
$this->assertTrue($result->isValid()); |
202
|
|
|
$this->assertFalse($mailListener->isOnPreSendCalled()); |
203
|
|
|
$this->assertFalse($mailListener->isOnPostSendCalled()); |
204
|
|
|
$this->assertFalse($mailListener->isOnSendErrorCalled()); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function testValidTemplateMakesBodyToBeMimeMessage() |
208
|
|
|
{ |
209
|
|
|
$resolver = new TemplatePathStack(); |
210
|
|
|
$resolver->addPath(__DIR__ . '/../../view'); |
211
|
|
|
$this->mailService->getRenderer()->setResolver($resolver); |
212
|
|
|
$this->mailService->setTemplate('ac-mailer/mail-templates/mail.phtml'); |
213
|
|
|
|
214
|
|
|
$this->assertInstanceOf('Zend\Mime\Message', $this->mailService->getMessage()->getBody()); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @expectedException \Zend\View\Exception\RuntimeException |
219
|
|
|
*/ |
220
|
|
|
public function testInvalidTemplateThrowsException() |
221
|
|
|
{ |
222
|
|
|
$this->mailService->setTemplate('foo/bar'); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function testAttachmentsTotal() |
226
|
|
|
{ |
227
|
|
|
$this->assertCount(0, $this->mailService->getAttachments()); |
228
|
|
|
|
229
|
|
|
$this->mailService->setAttachments(['one', 'two', 'three']); |
230
|
|
|
$this->mailService->addAttachments(['four', 'five', 'six']); |
231
|
|
|
$this->mailService->addAttachment('seven'); |
232
|
|
|
$this->mailService->addAttachment('eight', 'with-alias'); |
233
|
|
|
$this->assertCount(8, $this->mailService->getAttachments()); |
234
|
|
|
|
235
|
|
|
$this->mailService->setAttachments(['one', 'two']); |
236
|
|
|
$this->assertCount(2, $this->mailService->getAttachments()); |
237
|
|
|
|
238
|
|
|
$this->mailService->addAttachments(['three', 'four']); |
239
|
|
|
$this->assertCount(4, $this->mailService->getAttachments()); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function testAttachmentsAreAddedAsMimeParts() |
243
|
|
|
{ |
244
|
|
|
$cwd = getcwd(); |
245
|
|
|
chdir(dirname(__DIR__)); |
246
|
|
|
$this->mailService->setAttachments([ |
247
|
|
|
'attachments/file1', |
248
|
|
|
'attachments/file2', |
249
|
|
|
'attachments/dir/file3', |
250
|
|
|
'invalid/attachment' |
251
|
|
|
]); |
252
|
|
|
$this->mailService->setBody('Body as string'); |
253
|
|
|
$result = $this->mailService->send(); |
254
|
|
|
$this->assertTrue($result->isValid()); |
255
|
|
|
|
256
|
|
|
/* @var Mime\Message $body */ |
257
|
|
|
$body = $this->mailService->getMessage()->getBody(); |
258
|
|
|
$this->assertInstanceOf('Zend\Mime\Message', $body); |
259
|
|
|
// The body and the three attached files make it a total of 4 parts |
260
|
|
|
$this->assertCount(4, $body->getParts()); |
261
|
|
|
chdir($cwd); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function testStringBypassedBodyIsWrappedIntoMimePartWithAttachments() |
265
|
|
|
{ |
266
|
|
|
$cwd = getcwd(); |
267
|
|
|
chdir(dirname(__DIR__)); |
268
|
|
|
$this->mailService->setAttachments([ |
269
|
|
|
'attachments/file1', |
270
|
|
|
'attachments/file2' |
271
|
|
|
]); |
272
|
|
|
$this->mailService->getMessage()->setBody('Btpassed body as string'); |
273
|
|
|
$result = $this->mailService->send(); |
274
|
|
|
$this->assertTrue($result->isValid()); |
275
|
|
|
|
276
|
|
|
/* @var Mime\Message $body */ |
277
|
|
|
$body = $this->mailService->getMessage()->getBody(); |
278
|
|
|
$this->assertInstanceOf('Zend\Mime\Message', $body); |
279
|
|
|
chdir($cwd); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
public function testWithDefaultLayout() |
283
|
|
|
{ |
284
|
|
|
$resolver = new TemplatePathStack(); |
285
|
|
|
$resolver->addPath(__DIR__ . '/../../view'); |
286
|
|
|
$this->mailService->getRenderer()->setResolver($resolver); |
287
|
|
|
|
288
|
|
|
$model = new ViewModel(); |
289
|
|
|
$model->setTemplate('ac-mailer/mail-templates/layout.phtml'); |
290
|
|
|
$this->mailService->setDefaultLayout(new DefaultLayout($model)); |
291
|
|
|
$this->mailService->setTemplate('ac-mailer/mail-templates/mail.phtml'); |
292
|
|
|
$this->assertInstanceOf('Zend\Mime\Message', $this->mailService->getMessage()->getBody()); |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: