Total Complexity | 45 |
Total Lines | 680 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like EmailTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EmailTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class EmailTest extends SapphireTest |
||
26 | { |
||
27 | |||
28 | protected function setUp() |
||
29 | { |
||
30 | parent::setUp(); |
||
31 | Director::config()->set('alternate_base_url', 'http://www.mysite.com/'); |
||
32 | } |
||
33 | |||
34 | public function testAddAttachment() |
||
35 | { |
||
36 | $email = new Email(); |
||
37 | |||
38 | $email->addAttachment(__DIR__ . '/EmailTest/attachment.txt', null, 'text/plain'); |
||
39 | |||
40 | $children = $email->getSwiftMessage()->getChildren(); |
||
41 | $this->assertCount(1, $children); |
||
42 | |||
43 | /** @var Swift_Attachment $child */ |
||
44 | $child = reset($children); |
||
45 | |||
46 | $this->assertInstanceOf(Swift_Attachment::class, $child); |
||
47 | $this->assertEquals('text/plain', $child->getContentType()); |
||
48 | $this->assertEquals('attachment.txt', $child->getFilename()); |
||
49 | } |
||
50 | |||
51 | public function testAddAttachmentFromData() |
||
52 | { |
||
53 | $email = new Email(); |
||
54 | |||
55 | $email->addAttachmentFromData('foo bar', 'foo.txt', 'text/plain'); |
||
56 | $children = $email->getSwiftMessage()->getChildren(); |
||
57 | |||
58 | $this->assertCount(1, $children); |
||
59 | |||
60 | /** @var Swift_Attachment $child */ |
||
61 | $child = reset($children); |
||
62 | |||
63 | $this->assertInstanceOf(Swift_Attachment::class, $child); |
||
64 | $this->assertEquals('foo bar', $child->getBody()); |
||
65 | $this->assertEquals('text/plain', $child->getContentType()); |
||
66 | $this->assertEquals('foo.txt', $child->getFilename()); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @dataProvider provideValidEmailAddresses |
||
71 | */ |
||
72 | public function testValidEmailAddress($email) |
||
73 | { |
||
74 | $this->assertTrue(Email::is_valid_address($email)); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @dataProvider provideInvalidEmailAddresses |
||
79 | */ |
||
80 | public function testInvalidEmailAddress($email) |
||
81 | { |
||
82 | $this->assertFalse(Email::is_valid_address($email)); |
||
83 | } |
||
84 | |||
85 | public function provideValidEmailAddresses() |
||
86 | { |
||
87 | return [ |
||
88 | ['[email protected]', '[email protected]'], |
||
89 | ]; |
||
90 | } |
||
91 | |||
92 | public function provideInvalidEmailAddresses() |
||
93 | { |
||
94 | return [ |
||
95 | ['foo.bar@', '@example.com', 'foo@'], |
||
96 | ]; |
||
97 | } |
||
98 | |||
99 | public function testObfuscate() |
||
100 | { |
||
101 | $emailAddress = '[email protected]'; |
||
102 | |||
103 | $direction = Email::obfuscate($emailAddress, 'direction'); |
||
104 | $visible = Email::obfuscate($emailAddress, 'visible'); |
||
105 | $hex = Email::obfuscate($emailAddress, 'hex'); |
||
106 | |||
107 | $this->assertEquals('<span class="codedirection">moc.elpmaxe@1-tset</span>', $direction); |
||
108 | $this->assertEquals('test [dash] 1 [at] example [dot] com', $visible); |
||
109 | $this->assertEquals( |
||
110 | 'test-1@example.com', |
||
111 | $hex |
||
112 | ); |
||
113 | } |
||
114 | |||
115 | public function testSendPlain() |
||
142 | } |
||
143 | |||
144 | public function testSend() |
||
145 | { |
||
146 | /** @var Email|PHPUnit_Framework_MockObject_MockObject $email */ |
||
147 | $email = $this->makeEmailMock('Test send HTML'); |
||
148 | |||
149 | // email should not call render if a body is supplied |
||
150 | $email->expects($this->never())->method('renderWith'); |
||
151 | $successful = $email->send(); |
||
152 | |||
153 | $this->assertTrue($successful); |
||
154 | $this->assertEmpty($email->getFailedRecipients()); |
||
155 | |||
156 | /** @var TestMailer $mailer */ |
||
157 | $mailer = Injector::inst()->get(Mailer::class); |
||
158 | $sentMail = $mailer->findEmail('[email protected]'); |
||
159 | |||
160 | $this->assertTrue(is_array($sentMail)); |
||
161 | |||
162 | $this->assertEquals('[email protected]', $sentMail['To']); |
||
163 | $this->assertEquals('[email protected]', $sentMail['From']); |
||
164 | $this->assertEquals('Test send HTML', $sentMail['Subject']); |
||
165 | $this->assertEquals('Body for Test send HTML', $sentMail['Content']); |
||
166 | |||
167 | $this->assertCount(1, $sentMail['AttachedFiles']); |
||
168 | $child = reset($sentMail['AttachedFiles']); |
||
169 | $this->assertEquals('text/plain', $child['mimetype']); |
||
170 | $this->assertEquals('attachment.txt', $child['filename']); |
||
171 | $this->assertEquals('Hello, I\'m a text document.', $child['contents']); |
||
172 | } |
||
173 | |||
174 | public function testRenderedSend() |
||
175 | { |
||
176 | /** @var Email|PHPUnit_Framework_MockObject_MockObject $email */ |
||
177 | $email = $this->getMockBuilder(Email::class) |
||
178 | ->enableProxyingToOriginalMethods() |
||
179 | ->getMock(); |
||
180 | $email->setFrom('[email protected]'); |
||
181 | $email->setTo('[email protected]'); |
||
182 | $email->setData([ |
||
183 | 'EmailContent' => 'test', |
||
184 | ]); |
||
185 | $this->assertFalse($email->hasPlainPart()); |
||
186 | $this->assertEmpty($email->getBody()); |
||
187 | // these seem to fail for some reason :/ |
||
188 | //$email->expects($this->once())->method('render'); |
||
189 | //$email->expects($this->once())->method('generatePlainPartFromBody'); |
||
190 | $email->send(); |
||
191 | $this->assertTrue($email->hasPlainPart()); |
||
192 | $this->assertNotEmpty($email->getBody()); |
||
193 | } |
||
194 | |||
195 | public function testRenderedSendSubclass() |
||
196 | { |
||
197 | // Include dev theme |
||
198 | SSViewer::set_themes([ |
||
199 | 'silverstripe/framework:/tests/php/Control/Email/EmailTest', |
||
200 | '$default', |
||
201 | ]); |
||
202 | |||
203 | /** @var Email|PHPUnit_Framework_MockObject_MockObject $email */ |
||
204 | $email = $this->getMockBuilder(EmailSubClass::class) |
||
205 | ->enableProxyingToOriginalMethods() |
||
206 | ->getMock(); |
||
207 | $email->setFrom('[email protected]'); |
||
208 | $email->setTo('[email protected]'); |
||
209 | $email->setData([ |
||
210 | 'EmailContent' => 'test', |
||
211 | ]); |
||
212 | $this->assertFalse($email->hasPlainPart()); |
||
213 | $this->assertEmpty($email->getBody()); |
||
214 | $email->send(); |
||
215 | $this->assertTrue($email->hasPlainPart()); |
||
216 | $this->assertNotEmpty($email->getBody()); |
||
217 | $this->assertContains('<h1>Email Sub-class</h1>', $email->getBody()); |
||
218 | } |
||
219 | |||
220 | public function testConsturctor() |
||
221 | { |
||
222 | $email = new Email( |
||
223 | '[email protected]', |
||
224 | '[email protected]', |
||
225 | 'subject', |
||
226 | 'body', |
||
227 | '[email protected]', |
||
228 | '[email protected]', |
||
229 | '[email protected]' |
||
230 | ); |
||
231 | |||
232 | $this->assertCount(1, $email->getFrom()); |
||
233 | $this->assertContains('[email protected]', array_keys($email->getFrom())); |
||
234 | $this->assertCount(1, $email->getTo()); |
||
235 | $this->assertContains('[email protected]', array_keys($email->getTo())); |
||
236 | $this->assertEquals('subject', $email->getSubject()); |
||
237 | $this->assertEquals('body', $email->getBody()); |
||
238 | $this->assertCount(1, $email->getCC()); |
||
239 | $this->assertContains('[email protected]', array_keys($email->getCC())); |
||
240 | $this->assertCount(1, $email->getBCC()); |
||
241 | $this->assertContains('[email protected]', array_keys($email->getBCC())); |
||
242 | $this->assertEquals('[email protected]', $email->getReturnPath()); |
||
243 | } |
||
244 | |||
245 | public function testGetSwiftMessage() |
||
246 | { |
||
247 | $email = new Email( |
||
248 | '[email protected]', |
||
249 | '[email protected]', |
||
250 | 'subject', |
||
251 | 'body', |
||
252 | '[email protected]', |
||
253 | '[email protected]', |
||
254 | '[email protected]' |
||
255 | ); |
||
256 | $swiftMessage = $email->getSwiftMessage(); |
||
257 | |||
258 | $this->assertInstanceOf(Swift_Message::class, $swiftMessage); |
||
259 | |||
260 | $this->assertCount(1, $swiftMessage->getFrom()); |
||
261 | $this->assertContains('[email protected]', array_keys($swiftMessage->getFrom())); |
||
|
|||
262 | $this->assertCount(1, $swiftMessage->getTo()); |
||
263 | $this->assertContains('[email protected]', array_keys($swiftMessage->getTo())); |
||
264 | $this->assertEquals('subject', $swiftMessage->getSubject()); |
||
265 | $this->assertEquals('body', $swiftMessage->getBody()); |
||
266 | $this->assertCount(1, $swiftMessage->getCC()); |
||
267 | $this->assertContains('[email protected]', array_keys($swiftMessage->getCc())); |
||
268 | $this->assertCount(1, $swiftMessage->getBCC()); |
||
269 | $this->assertContains('[email protected]', array_keys($swiftMessage->getBcc())); |
||
270 | $this->assertEquals('[email protected]', $swiftMessage->getReturnPath()); |
||
271 | } |
||
272 | |||
273 | public function testSetSwiftMessage() |
||
274 | { |
||
275 | Email::config()->update('admin_email', '[email protected]'); |
||
276 | DBDatetime::set_mock_now('2017-01-01 07:00:00'); |
||
277 | $email = new Email(); |
||
278 | $swiftMessage = new Swift_Message(); |
||
279 | $email->setSwiftMessage($swiftMessage); |
||
280 | $dateTime = new DateTime(); |
||
281 | $dateTime->setTimestamp(DBDatetime::now()->getTimestamp()); |
||
282 | $email->getSwiftMessage()->setDate($dateTime); |
||
283 | $this->assertCount(1, $email->getFrom()); |
||
284 | $this->assertContains('[email protected]', array_keys($swiftMessage->getFrom())); |
||
285 | $this->assertEquals(strtotime('2017-01-01 07:00:00'), $swiftMessage->getDate()->getTimestamp()); |
||
286 | $this->assertEquals($swiftMessage, $email->getSwiftMessage()); |
||
287 | |||
288 | // check from field is retained |
||
289 | $swiftMessage = new Swift_Message(); |
||
290 | $swiftMessage->setFrom('[email protected]'); |
||
291 | $email->setSwiftMessage($swiftMessage); |
||
292 | $this->assertCount(1, $email->getFrom()); |
||
293 | $this->assertContains('[email protected]', array_keys($email->getFrom())); |
||
294 | } |
||
295 | |||
296 | public function testAdminEmailApplied() |
||
297 | { |
||
298 | Email::config()->update('admin_email', '[email protected]'); |
||
299 | $email = new Email(); |
||
300 | |||
301 | $this->assertCount(1, $email->getFrom()); |
||
302 | $this->assertContains('[email protected]', array_keys($email->getFrom())); |
||
303 | } |
||
304 | |||
305 | public function testGetFrom() |
||
310 | } |
||
311 | |||
312 | public function testSetFrom() |
||
313 | { |
||
314 | $email = new Email('[email protected]'); |
||
315 | $this->assertCount(1, $email->getFrom()); |
||
316 | $this->assertContains('[email protected]', array_keys($email->getFrom())); |
||
317 | $email->setFrom('[email protected]'); |
||
318 | $this->assertCount(1, $email->getFrom()); |
||
319 | $this->assertContains('[email protected]', array_keys($email->getFrom())); |
||
320 | } |
||
321 | |||
322 | public function testAddFrom() |
||
323 | { |
||
324 | $email = new Email('[email protected]'); |
||
325 | $this->assertCount(1, $email->getFrom()); |
||
326 | $this->assertContains('[email protected]', array_keys($email->getFrom())); |
||
327 | $email->addFrom('[email protected]'); |
||
328 | $this->assertCount(2, $email->getFrom()); |
||
329 | $this->assertContains('[email protected]', array_keys($email->getFrom())); |
||
330 | $this->assertContains('[email protected]', array_keys($email->getFrom())); |
||
331 | } |
||
332 | |||
333 | public function testSetGetSender() |
||
334 | { |
||
335 | $email = new Email(); |
||
336 | $this->assertEmpty($email->getSender()); |
||
337 | $email->setSender('[email protected]', 'Silver Stripe'); |
||
338 | $this->assertEquals(['[email protected]' => 'Silver Stripe'], $email->getSender()); |
||
339 | } |
||
340 | |||
341 | public function testSetGetReturnPath() |
||
342 | { |
||
343 | $email = new Email(); |
||
344 | $this->assertEmpty($email->getReturnPath()); |
||
345 | $email->setReturnPath('[email protected]'); |
||
346 | $this->assertEquals('[email protected]', $email->getReturnPath()); |
||
347 | } |
||
348 | |||
349 | public function testSetGetTo() |
||
350 | { |
||
351 | $email = new Email('[email protected]', '[email protected]'); |
||
352 | $this->assertCount(1, $email->getTo()); |
||
353 | $this->assertContains('[email protected]', array_keys($email->getTo())); |
||
354 | $email->setTo('[email protected]', 'Silver Stripe'); |
||
355 | $this->assertEquals(['[email protected]' => 'Silver Stripe'], $email->getTo()); |
||
356 | } |
||
357 | |||
358 | public function testAddTo() |
||
359 | { |
||
360 | $email = new Email('[email protected]', '[email protected]'); |
||
361 | $this->assertCount(1, $email->getTo()); |
||
362 | $this->assertContains('[email protected]', array_keys($email->getTo())); |
||
363 | $email->addTo('[email protected]'); |
||
364 | $this->assertCount(2, $email->getTo()); |
||
365 | $this->assertContains('[email protected]', array_keys($email->getTo())); |
||
366 | $this->assertContains('[email protected]', array_keys($email->getTo())); |
||
367 | } |
||
368 | |||
369 | public function testSetGetCC() |
||
370 | { |
||
371 | $email = new Email('[email protected]', '[email protected]', 'subject', 'body', '[email protected]'); |
||
372 | $this->assertCount(1, $email->getCC()); |
||
373 | $this->assertContains('[email protected]', array_keys($email->getCC())); |
||
374 | $email->setCC('[email protected]', 'Silver Stripe'); |
||
375 | $this->assertEquals(['[email protected]' => 'Silver Stripe'], $email->getCC()); |
||
376 | } |
||
377 | |||
378 | public function testAddCC() |
||
379 | { |
||
380 | $email = new Email('[email protected]', '[email protected]', 'subject', 'body', '[email protected]'); |
||
381 | $this->assertCount(1, $email->getCC()); |
||
382 | $this->assertContains('[email protected]', array_keys($email->getCC())); |
||
383 | $email->addCC('[email protected]', 'Silver Stripe'); |
||
384 | $this->assertCount(2, $email->getCC()); |
||
385 | $this->assertContains('[email protected]', array_keys($email->getCC())); |
||
386 | $this->assertContains('[email protected]', array_keys($email->getCC())); |
||
387 | } |
||
388 | |||
389 | public function testSetGetBCC() |
||
390 | { |
||
391 | $email = new Email( |
||
392 | '[email protected]', |
||
393 | '[email protected]', |
||
394 | 'subject', |
||
395 | 'body', |
||
396 | '[email protected]', |
||
397 | '[email protected]' |
||
398 | ); |
||
399 | $this->assertCount(1, $email->getBCC()); |
||
400 | $this->assertContains('[email protected]', array_keys($email->getBCC())); |
||
401 | $email->setBCC('[email protected]', 'Silver Stripe'); |
||
402 | $this->assertEquals(['[email protected]' => 'Silver Stripe'], $email->getBCC()); |
||
403 | } |
||
404 | |||
405 | public function testAddBCC() |
||
421 | } |
||
422 | |||
423 | public function testReplyTo() |
||
424 | { |
||
425 | $email = new Email(); |
||
426 | $this->assertEmpty($email->getReplyTo()); |
||
427 | $email->setReplyTo('[email protected]', 'Silver Stripe'); |
||
428 | $this->assertEquals(['[email protected]' => 'Silver Stripe'], $email->getReplyTo()); |
||
429 | $email->addReplyTo('[email protected]'); |
||
430 | $this->assertCount(2, $email->getReplyTo()); |
||
431 | $this->assertContains('[email protected]', array_keys($email->getReplyTo())); |
||
432 | $this->assertContains('[email protected]', array_keys($email->getReplyTo())); |
||
433 | } |
||
434 | |||
435 | public function testSubject() |
||
436 | { |
||
437 | $email = new Email('[email protected]', '[email protected]', 'subject'); |
||
438 | $this->assertEquals('subject', $email->getSubject()); |
||
439 | $email->setSubject('new subject'); |
||
440 | $this->assertEquals('new subject', $email->getSubject()); |
||
441 | } |
||
442 | |||
443 | public function testPriority() |
||
444 | { |
||
445 | $email = new Email(); |
||
446 | $this->assertEquals(3, $email->getPriority()); |
||
447 | $email->setPriority(5); |
||
448 | $this->assertEquals(5, $email->getPriority()); |
||
449 | } |
||
450 | |||
451 | public function testData() |
||
452 | { |
||
453 | $email = new Email(); |
||
454 | $this->assertEmpty($email->getData()); |
||
455 | $email->setData([ |
||
456 | 'Title' => 'My Title', |
||
457 | ]); |
||
458 | $this->assertCount(1, $email->getData()); |
||
459 | $this->assertEquals(['Title' => 'My Title'], $email->getData()); |
||
460 | |||
461 | $email->addData('Content', 'My content'); |
||
462 | $this->assertCount(2, $email->getData()); |
||
463 | $this->assertEquals([ |
||
464 | 'Title' => 'My Title', |
||
465 | 'Content' => 'My content', |
||
466 | ], $email->getData()); |
||
467 | $email->removeData('Title'); |
||
468 | $this->assertEquals(['Content' => 'My content'], $email->getData()); |
||
469 | } |
||
470 | |||
471 | public function testDataWithViewableData() |
||
472 | { |
||
473 | $member = new Member(); |
||
474 | $member->FirstName = 'First Name'; |
||
475 | $email = new Email(); |
||
476 | $this->assertEmpty($email->getData()); |
||
477 | $email->setData($member); |
||
478 | $this->assertEquals($member, $email->getData()); |
||
479 | $email->addData('Test', 'Test value'); |
||
480 | $this->assertEquals('Test value', $email->getData()->Test); |
||
481 | $email->removeData('Test'); |
||
482 | $this->assertNull($email->getData()->Test); |
||
483 | } |
||
484 | |||
485 | public function testBody() |
||
486 | { |
||
487 | $email = new Email(); |
||
488 | $this->assertEmpty($email->getBody()); |
||
489 | $email->setBody('<h1>Title</h1>'); |
||
490 | $this->assertEquals('<h1>Title</h1>', $email->getBody()); |
||
491 | } |
||
492 | |||
493 | public function testHTMLTemplate() |
||
494 | { |
||
495 | // Include dev theme |
||
496 | SSViewer::set_themes([ |
||
497 | 'silverstripe/framework:/tests/php/Control/Email/EmailTest', |
||
498 | '$default', |
||
499 | ]); |
||
500 | |||
501 | // Find template on disk |
||
502 | $emailTemplate = ModuleResourceLoader::singleton()->resolveResource( |
||
503 | 'silverstripe/framework:templates/SilverStripe/Control/Email/Email.ss' |
||
504 | ); |
||
505 | $subClassTemplate = ModuleResourceLoader::singleton()->resolveResource( |
||
506 | 'silverstripe/framework:tests/php/Control/Email/EmailTest/templates/' |
||
507 | . str_replace('\\', '/', EmailSubClass::class) |
||
508 | . '.ss' |
||
509 | ); |
||
510 | $this->assertTrue($emailTemplate->exists()); |
||
511 | $this->assertTrue($subClassTemplate->exists()); |
||
512 | |||
513 | // Check template is auto-found |
||
514 | $email = new Email(); |
||
515 | $this->assertEquals($emailTemplate->getPath(), $email->getHTMLTemplate()); |
||
516 | $email->setHTMLTemplate('MyTemplate'); |
||
517 | $this->assertEquals('MyTemplate', $email->getHTMLTemplate()); |
||
518 | |||
519 | // Check subclass template is found |
||
520 | $email2 = new EmailSubClass(); |
||
521 | $this->assertEquals($subClassTemplate->getPath(), $email2->getHTMLTemplate()); |
||
522 | $email->setHTMLTemplate('MyTemplate'); |
||
523 | $this->assertEquals('MyTemplate', $email->getHTMLTemplate()); |
||
524 | } |
||
525 | |||
526 | public function testPlainTemplate() |
||
527 | { |
||
528 | $email = new Email(); |
||
529 | $this->assertEmpty($email->getPlainTemplate()); |
||
530 | $email->setPlainTemplate('MyTemplate'); |
||
531 | $this->assertEquals('MyTemplate', $email->getPlainTemplate()); |
||
532 | } |
||
533 | |||
534 | public function testGetFailedRecipients() |
||
535 | { |
||
536 | $mailer = new SwiftMailer(); |
||
537 | /** @var Swift_NullTransport|PHPUnit_Framework_MockObject_MockObject $transport */ |
||
538 | $transport = $this->getMockBuilder(Swift_NullTransport::class)->getMock(); |
||
539 | $transport->expects($this->once()) |
||
540 | ->method('send') |
||
541 | ->willThrowException(new Swift_RfcComplianceException('Bad email')); |
||
542 | $mailer->setSwiftMailer(new Swift_Mailer($transport)); |
||
543 | $email = new Email(); |
||
544 | $email->setTo('[email protected]'); |
||
545 | $email->setFrom('[email protected]'); |
||
546 | $mailer->send($email); |
||
547 | $this->assertCount(1, $email->getFailedRecipients()); |
||
548 | } |
||
549 | |||
550 | public function testIsEmail() |
||
551 | { |
||
552 | $this->assertTrue((new Email)->IsEmail()); |
||
553 | } |
||
554 | |||
555 | public function testRenderAgain() |
||
556 | { |
||
557 | $email = new Email(); |
||
558 | $email->setData([ |
||
559 | 'EmailContent' => 'my content', |
||
560 | ]); |
||
561 | $email->render(); |
||
562 | $this->assertContains('my content', $email->getBody()); |
||
563 | $children = $email->getSwiftMessage()->getChildren(); |
||
564 | $this->assertCount(1, $children); |
||
565 | $plainPart = reset($children); |
||
566 | $this->assertEquals('my content', $plainPart->getBody()); |
||
567 | |||
568 | // ensure repeat renders don't add multiple plain parts |
||
569 | $email->render(); |
||
570 | $this->assertCount(1, $email->getSwiftMessage()->getChildren()); |
||
571 | } |
||
572 | |||
573 | public function testRerender() |
||
574 | { |
||
575 | $email = new Email(); |
||
576 | $email->setData([ |
||
577 | 'EmailContent' => 'my content', |
||
578 | ]); |
||
579 | $email->render(); |
||
580 | $this->assertContains('my content', $email->getBody()); |
||
581 | $children = $email->getSwiftMessage()->getChildren(); |
||
582 | $this->assertCount(1, $children); |
||
583 | $plainPart = reset($children); |
||
584 | $this->assertEquals('my content', $plainPart->getBody()); |
||
585 | |||
586 | // Ensure setting data causes a rerender |
||
587 | $email->setData([ |
||
588 | 'EmailContent' => 'your content' |
||
589 | ]); |
||
590 | $email->render(); |
||
591 | $this->assertContains('your content', $email->getBody()); |
||
592 | |||
593 | // Ensure removing data causes a rerender |
||
594 | $email->removeData('EmailContent'); |
||
595 | $email->render(); |
||
596 | $this->assertNotContains('your content', $email->getBody()); |
||
597 | |||
598 | // Ensure adding data causes a rerender |
||
599 | $email->addData([ |
||
600 | 'EmailContent' => 'their content' |
||
601 | ]); |
||
602 | $email->render(); |
||
603 | $this->assertContains('their content', $email->getBody()); |
||
604 | } |
||
605 | |||
606 | public function testRenderPlainOnly() |
||
607 | { |
||
608 | $email = new Email(); |
||
609 | $email->setData([ |
||
610 | 'EmailContent' => 'test content', |
||
611 | ]); |
||
612 | $email->render(true); |
||
613 | $this->assertEquals('text/plain', $email->getSwiftMessage()->getContentType()); |
||
614 | $this->assertEmpty($email->getSwiftMessage()->getChildren()); |
||
615 | } |
||
616 | |||
617 | public function testHasPlainPart() |
||
618 | { |
||
619 | $email = new Email(); |
||
620 | $email->setData([ |
||
621 | 'EmailContent' => 'test', |
||
622 | ]); |
||
623 | //emails are assumed to be HTML by default |
||
624 | $this->assertFalse($email->hasPlainPart()); |
||
625 | //make sure plain attachments aren't picked up as a plain part |
||
626 | $email->addAttachmentFromData('data', 'attachent.txt', 'text/plain'); |
||
627 | $this->assertFalse($email->hasPlainPart()); |
||
628 | $email->getSwiftMessage()->addPart('plain', 'text/plain'); |
||
629 | $this->assertTrue($email->hasPlainPart()); |
||
630 | } |
||
631 | |||
632 | public function testGeneratePlainPartFromBody() |
||
633 | { |
||
634 | $email = new Email(); |
||
635 | $email->setBody('<h1>Test</h1>'); |
||
636 | $this->assertEmpty($email->getSwiftMessage()->getChildren()); |
||
637 | $email->generatePlainPartFromBody(); |
||
638 | $children = $email->getSwiftMessage()->getChildren(); |
||
639 | $this->assertCount(1, $children); |
||
640 | $plainPart = reset($children); |
||
641 | $this->assertContains('Test', $plainPart->getBody()); |
||
642 | $this->assertNotContains('<h1>Test</h1>', $plainPart->getBody()); |
||
643 | } |
||
644 | |||
645 | public function testMultipleEmailSends() |
||
646 | { |
||
647 | $email = new Email(); |
||
648 | $email->setData([ |
||
649 | 'EmailContent' => 'Test', |
||
650 | ]); |
||
651 | $this->assertEmpty($email->getBody()); |
||
652 | $this->assertEmpty($email->getSwiftMessage()->getChildren()); |
||
653 | $email->send(); |
||
654 | $this->assertContains('Test', $email->getBody()); |
||
655 | $this->assertCount(1, $email->getSwiftMessage()->getChildren()); |
||
656 | $children = $email->getSwiftMessage()->getChildren(); |
||
657 | /** @var \Swift_MimePart $plainPart */ |
||
658 | $plainPart = reset($children); |
||
659 | $this->assertContains('Test', $plainPart->getBody()); |
||
660 | |||
661 | |||
662 | //send again |
||
663 | $email->send(); |
||
664 | $this->assertContains('Test', $email->getBody()); |
||
665 | $this->assertCount(1, $email->getSwiftMessage()->getChildren()); |
||
666 | $children = $email->getSwiftMessage()->getChildren(); |
||
667 | /** @var \Swift_MimePart $plainPart */ |
||
668 | $plainPart = reset($children); |
||
669 | $this->assertContains('Test', $plainPart->getBody()); |
||
670 | } |
||
671 | |||
672 | public function testGetDefaultFrom() |
||
685 | } |
||
686 | |||
687 | /** |
||
688 | * @return PHPUnit_Framework_MockObject_MockObject|Email |
||
689 | */ |
||
690 | protected function makeEmailMock($subject) |
||
705 | } |
||
706 | } |
||
707 |