Total Complexity | 43 |
Total Lines | 656 |
Duplicated Lines | 0 % |
Changes | 1 | ||
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 |
||
23 | class EmailTest extends SapphireTest |
||
24 | { |
||
25 | |||
26 | public function testAddAttachment() |
||
41 | } |
||
42 | |||
43 | public function testAddAttachmentFromData() |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @dataProvider provideValidEmailAddresses |
||
63 | */ |
||
64 | public function testValidEmailAddress($email) |
||
65 | { |
||
66 | $this->assertTrue(Email::is_valid_address($email)); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @dataProvider provideInvalidEmailAddresses |
||
71 | */ |
||
72 | public function testInvalidEmailAddress($email) |
||
73 | { |
||
74 | $this->assertFalse(Email::is_valid_address($email)); |
||
75 | } |
||
76 | |||
77 | public function provideValidEmailAddresses() |
||
78 | { |
||
79 | return [ |
||
80 | ['[email protected]', '[email protected]'], |
||
81 | ]; |
||
82 | } |
||
83 | |||
84 | public function provideInvalidEmailAddresses() |
||
85 | { |
||
86 | return [ |
||
87 | ['foo.bar@', '@example.com', 'foo@'], |
||
88 | ]; |
||
89 | } |
||
90 | |||
91 | public function testObfuscate() |
||
92 | { |
||
93 | $emailAddress = '[email protected]'; |
||
94 | |||
95 | $direction = Email::obfuscate($emailAddress, 'direction'); |
||
96 | $visible = Email::obfuscate($emailAddress, 'visible'); |
||
97 | $hex = Email::obfuscate($emailAddress, 'hex'); |
||
98 | |||
99 | $this->assertEquals('<span class="codedirection">moc.elpmaxe@1-tset</span>', $direction); |
||
100 | $this->assertEquals('test [dash] 1 [at] example [dot] com', $visible); |
||
101 | $this->assertEquals( |
||
102 | 'test-1@example.com', |
||
103 | $hex |
||
104 | ); |
||
105 | } |
||
106 | |||
107 | public function testSendPlain() |
||
134 | } |
||
135 | |||
136 | public function testSend() |
||
137 | { |
||
138 | /** @var Email|PHPUnit_Framework_MockObject_MockObject $email */ |
||
139 | $email = $this->makeEmailMock('Test send HTML'); |
||
140 | |||
141 | // email should not call render if a body is supplied |
||
142 | $email->expects($this->never())->method('renderWith'); |
||
143 | $successful = $email->send(); |
||
144 | |||
145 | $this->assertTrue($successful); |
||
146 | $this->assertEmpty($email->getFailedRecipients()); |
||
147 | |||
148 | /** @var TestMailer $mailer */ |
||
149 | $mailer = Injector::inst()->get(Mailer::class); |
||
150 | $sentMail = $mailer->findEmail('[email protected]'); |
||
151 | |||
152 | $this->assertTrue(is_array($sentMail)); |
||
153 | |||
154 | $this->assertEquals('[email protected]', $sentMail['To']); |
||
155 | $this->assertEquals('[email protected]', $sentMail['From']); |
||
156 | $this->assertEquals('Test send HTML', $sentMail['Subject']); |
||
157 | $this->assertEquals('Body for Test send HTML', $sentMail['Content']); |
||
158 | |||
159 | $this->assertCount(1, $sentMail['AttachedFiles']); |
||
160 | $child = reset($sentMail['AttachedFiles']); |
||
161 | $this->assertEquals('text/plain', $child['mimetype']); |
||
162 | $this->assertEquals('attachment.txt', $child['filename']); |
||
163 | $this->assertEquals('Hello, I\'m a text document.', $child['contents']); |
||
164 | } |
||
165 | |||
166 | public function testRenderedSend() |
||
167 | { |
||
168 | /** @var Email|PHPUnit_Framework_MockObject_MockObject $email */ |
||
169 | $email = $this->getMockBuilder(Email::class) |
||
170 | ->enableProxyingToOriginalMethods() |
||
171 | ->getMock(); |
||
172 | $email->setFrom('[email protected]'); |
||
173 | $email->setTo('[email protected]'); |
||
174 | $email->setData([ |
||
175 | 'EmailContent' => 'test', |
||
176 | ]); |
||
177 | $this->assertFalse($email->hasPlainPart()); |
||
178 | $this->assertEmpty($email->getBody()); |
||
179 | // these seem to fail for some reason :/ |
||
180 | //$email->expects($this->once())->method('render'); |
||
181 | //$email->expects($this->once())->method('generatePlainPartFromBody'); |
||
182 | $email->send(); |
||
183 | $this->assertTrue($email->hasPlainPart()); |
||
184 | $this->assertNotEmpty($email->getBody()); |
||
185 | } |
||
186 | |||
187 | public function testRenderedSendSubclass() |
||
188 | { |
||
189 | // Include dev theme |
||
190 | SSViewer::set_themes([ |
||
191 | 'silverstripe/framework:/tests/php/Control/Email/EmailTest', |
||
192 | '$default', |
||
193 | ]); |
||
194 | |||
195 | /** @var Email|PHPUnit_Framework_MockObject_MockObject $email */ |
||
196 | $email = $this->getMockBuilder(EmailSubClass::class) |
||
197 | ->enableProxyingToOriginalMethods() |
||
198 | ->getMock(); |
||
199 | $email->setFrom('[email protected]'); |
||
200 | $email->setTo('[email protected]'); |
||
201 | $email->setData([ |
||
202 | 'EmailContent' => 'test', |
||
203 | ]); |
||
204 | $this->assertFalse($email->hasPlainPart()); |
||
205 | $this->assertEmpty($email->getBody()); |
||
206 | $email->send(); |
||
207 | $this->assertTrue($email->hasPlainPart()); |
||
208 | $this->assertNotEmpty($email->getBody()); |
||
209 | $this->assertContains('<h1>Email Sub-class</h1>', $email->getBody()); |
||
210 | } |
||
211 | |||
212 | public function testConsturctor() |
||
213 | { |
||
214 | $email = new Email( |
||
215 | '[email protected]', |
||
216 | '[email protected]', |
||
217 | 'subject', |
||
218 | 'body', |
||
219 | '[email protected]', |
||
220 | '[email protected]', |
||
221 | '[email protected]' |
||
222 | ); |
||
223 | |||
224 | $this->assertCount(1, $email->getFrom()); |
||
225 | $this->assertContains('[email protected]', array_keys($email->getFrom())); |
||
226 | $this->assertCount(1, $email->getTo()); |
||
227 | $this->assertContains('[email protected]', array_keys($email->getTo())); |
||
228 | $this->assertEquals('subject', $email->getSubject()); |
||
229 | $this->assertEquals('body', $email->getBody()); |
||
230 | $this->assertCount(1, $email->getCC()); |
||
231 | $this->assertContains('[email protected]', array_keys($email->getCC())); |
||
232 | $this->assertCount(1, $email->getBCC()); |
||
233 | $this->assertContains('[email protected]', array_keys($email->getBCC())); |
||
234 | $this->assertEquals('[email protected]', $email->getReturnPath()); |
||
235 | } |
||
236 | |||
237 | public function testGetSwiftMessage() |
||
238 | { |
||
239 | $email = new Email( |
||
240 | '[email protected]', |
||
241 | '[email protected]', |
||
242 | 'subject', |
||
243 | 'body', |
||
244 | '[email protected]', |
||
245 | '[email protected]', |
||
246 | '[email protected]' |
||
247 | ); |
||
248 | $swiftMessage = $email->getSwiftMessage(); |
||
249 | |||
250 | $this->assertInstanceOf(Swift_Message::class, $swiftMessage); |
||
251 | |||
252 | $this->assertCount(1, $swiftMessage->getFrom()); |
||
253 | $this->assertContains('[email protected]', array_keys($swiftMessage->getFrom())); |
||
|
|||
254 | $this->assertCount(1, $swiftMessage->getTo()); |
||
255 | $this->assertContains('[email protected]', array_keys($swiftMessage->getTo())); |
||
256 | $this->assertEquals('subject', $swiftMessage->getSubject()); |
||
257 | $this->assertEquals('body', $swiftMessage->getBody()); |
||
258 | $this->assertCount(1, $swiftMessage->getCC()); |
||
259 | $this->assertContains('[email protected]', array_keys($swiftMessage->getCc())); |
||
260 | $this->assertCount(1, $swiftMessage->getBCC()); |
||
261 | $this->assertContains('[email protected]', array_keys($swiftMessage->getBcc())); |
||
262 | $this->assertEquals('[email protected]', $swiftMessage->getReturnPath()); |
||
263 | } |
||
264 | |||
265 | public function testSetSwiftMessage() |
||
266 | { |
||
267 | Email::config()->update('admin_email', '[email protected]'); |
||
268 | DBDatetime::set_mock_now('2017-01-01 07:00:00'); |
||
269 | $email = new Email(); |
||
270 | $swiftMessage = new Swift_Message(); |
||
271 | $email->setSwiftMessage($swiftMessage); |
||
272 | $this->assertCount(1, $email->getFrom()); |
||
273 | $this->assertContains('[email protected]', array_keys($swiftMessage->getFrom())); |
||
274 | $this->assertEquals(strtotime('2017-01-01 07:00:00'), $swiftMessage->getDate()); |
||
275 | $this->assertEquals($swiftMessage, $email->getSwiftMessage()); |
||
276 | |||
277 | // check from field is retained |
||
278 | $swiftMessage = new Swift_Message(); |
||
279 | $swiftMessage->setFrom('[email protected]'); |
||
280 | $email->setSwiftMessage($swiftMessage); |
||
281 | $this->assertCount(1, $email->getFrom()); |
||
282 | $this->assertContains('[email protected]', array_keys($email->getFrom())); |
||
283 | } |
||
284 | |||
285 | public function testAdminEmailApplied() |
||
286 | { |
||
287 | Email::config()->update('admin_email', '[email protected]'); |
||
288 | $email = new Email(); |
||
289 | |||
290 | $this->assertCount(1, $email->getFrom()); |
||
291 | $this->assertContains('[email protected]', array_keys($email->getFrom())); |
||
292 | } |
||
293 | |||
294 | public function testGetFrom() |
||
299 | } |
||
300 | |||
301 | public function testSetFrom() |
||
302 | { |
||
303 | $email = new Email('[email protected]'); |
||
304 | $this->assertCount(1, $email->getFrom()); |
||
305 | $this->assertContains('[email protected]', array_keys($email->getFrom())); |
||
306 | $email->setFrom('[email protected]'); |
||
307 | $this->assertCount(1, $email->getFrom()); |
||
308 | $this->assertContains('[email protected]', array_keys($email->getFrom())); |
||
309 | } |
||
310 | |||
311 | public function testAddFrom() |
||
312 | { |
||
313 | $email = new Email('[email protected]'); |
||
314 | $this->assertCount(1, $email->getFrom()); |
||
315 | $this->assertContains('[email protected]', array_keys($email->getFrom())); |
||
316 | $email->addFrom('[email protected]'); |
||
317 | $this->assertCount(2, $email->getFrom()); |
||
318 | $this->assertContains('[email protected]', array_keys($email->getFrom())); |
||
319 | $this->assertContains('[email protected]', array_keys($email->getFrom())); |
||
320 | } |
||
321 | |||
322 | public function testSetGetSender() |
||
323 | { |
||
324 | $email = new Email(); |
||
325 | $this->assertEmpty($email->getSender()); |
||
326 | $email->setSender('[email protected]', 'Silver Stripe'); |
||
327 | $this->assertEquals(['[email protected]' => 'Silver Stripe'], $email->getSender()); |
||
328 | } |
||
329 | |||
330 | public function testSetGetReturnPath() |
||
331 | { |
||
332 | $email = new Email(); |
||
333 | $this->assertEmpty($email->getReturnPath()); |
||
334 | $email->setReturnPath('[email protected]'); |
||
335 | $this->assertEquals('[email protected]', $email->getReturnPath()); |
||
336 | } |
||
337 | |||
338 | public function testSetGetTo() |
||
339 | { |
||
340 | $email = new Email('[email protected]', '[email protected]'); |
||
341 | $this->assertCount(1, $email->getTo()); |
||
342 | $this->assertContains('[email protected]', array_keys($email->getTo())); |
||
343 | $email->setTo('[email protected]', 'Silver Stripe'); |
||
344 | $this->assertEquals(['[email protected]' => 'Silver Stripe'], $email->getTo()); |
||
345 | } |
||
346 | |||
347 | public function testAddTo() |
||
348 | { |
||
349 | $email = new Email('[email protected]', '[email protected]'); |
||
350 | $this->assertCount(1, $email->getTo()); |
||
351 | $this->assertContains('[email protected]', array_keys($email->getTo())); |
||
352 | $email->addTo('[email protected]'); |
||
353 | $this->assertCount(2, $email->getTo()); |
||
354 | $this->assertContains('[email protected]', array_keys($email->getTo())); |
||
355 | $this->assertContains('[email protected]', array_keys($email->getTo())); |
||
356 | } |
||
357 | |||
358 | public function testSetGetCC() |
||
359 | { |
||
360 | $email = new Email('[email protected]', '[email protected]', 'subject', 'body', '[email protected]'); |
||
361 | $this->assertCount(1, $email->getCC()); |
||
362 | $this->assertContains('[email protected]', array_keys($email->getCC())); |
||
363 | $email->setCC('[email protected]', 'Silver Stripe'); |
||
364 | $this->assertEquals(['[email protected]' => 'Silver Stripe'], $email->getCC()); |
||
365 | } |
||
366 | |||
367 | public function testAddCC() |
||
368 | { |
||
369 | $email = new Email('[email protected]', '[email protected]', 'subject', 'body', '[email protected]'); |
||
370 | $this->assertCount(1, $email->getCC()); |
||
371 | $this->assertContains('[email protected]', array_keys($email->getCC())); |
||
372 | $email->addCC('[email protected]', 'Silver Stripe'); |
||
373 | $this->assertCount(2, $email->getCC()); |
||
374 | $this->assertContains('[email protected]', array_keys($email->getCC())); |
||
375 | $this->assertContains('[email protected]', array_keys($email->getCC())); |
||
376 | } |
||
377 | |||
378 | public function testSetGetBCC() |
||
379 | { |
||
380 | $email = new Email( |
||
381 | '[email protected]', |
||
382 | '[email protected]', |
||
383 | 'subject', |
||
384 | 'body', |
||
385 | '[email protected]', |
||
386 | '[email protected]' |
||
387 | ); |
||
388 | $this->assertCount(1, $email->getBCC()); |
||
389 | $this->assertContains('[email protected]', array_keys($email->getBCC())); |
||
390 | $email->setBCC('[email protected]', 'Silver Stripe'); |
||
391 | $this->assertEquals(['[email protected]' => 'Silver Stripe'], $email->getBCC()); |
||
392 | } |
||
393 | |||
394 | public function testAddBCC() |
||
410 | } |
||
411 | |||
412 | public function testReplyTo() |
||
413 | { |
||
414 | $email = new Email(); |
||
415 | $this->assertEmpty($email->getReplyTo()); |
||
416 | $email->setReplyTo('[email protected]', 'Silver Stripe'); |
||
417 | $this->assertEquals(['[email protected]' => 'Silver Stripe'], $email->getReplyTo()); |
||
418 | $email->addReplyTo('[email protected]'); |
||
419 | $this->assertCount(2, $email->getReplyTo()); |
||
420 | $this->assertContains('[email protected]', array_keys($email->getReplyTo())); |
||
421 | $this->assertContains('[email protected]', array_keys($email->getReplyTo())); |
||
422 | } |
||
423 | |||
424 | public function testSubject() |
||
425 | { |
||
426 | $email = new Email('[email protected]', '[email protected]', 'subject'); |
||
427 | $this->assertEquals('subject', $email->getSubject()); |
||
428 | $email->setSubject('new subject'); |
||
429 | $this->assertEquals('new subject', $email->getSubject()); |
||
430 | } |
||
431 | |||
432 | public function testPriority() |
||
433 | { |
||
434 | $email = new Email(); |
||
435 | $this->assertEquals(3, $email->getPriority()); |
||
436 | $email->setPriority(5); |
||
437 | $this->assertEquals(5, $email->getPriority()); |
||
438 | } |
||
439 | |||
440 | public function testData() |
||
441 | { |
||
442 | $email = new Email(); |
||
443 | $this->assertEmpty($email->getData()); |
||
444 | $email->setData([ |
||
445 | 'Title' => 'My Title', |
||
446 | ]); |
||
447 | $this->assertCount(1, $email->getData()); |
||
448 | $this->assertEquals(['Title' => 'My Title'], $email->getData()); |
||
449 | |||
450 | $email->addData('Content', 'My content'); |
||
451 | $this->assertCount(2, $email->getData()); |
||
452 | $this->assertEquals([ |
||
453 | 'Title' => 'My Title', |
||
454 | 'Content' => 'My content', |
||
455 | ], $email->getData()); |
||
456 | $email->removeData('Title'); |
||
457 | $this->assertEquals(['Content' => 'My content'], $email->getData()); |
||
458 | } |
||
459 | |||
460 | public function testDataWithViewableData() |
||
461 | { |
||
462 | $member = new Member(); |
||
463 | $member->FirstName = 'First Name'; |
||
464 | $email = new Email(); |
||
465 | $this->assertEmpty($email->getData()); |
||
466 | $email->setData($member); |
||
467 | $this->assertEquals($member, $email->getData()); |
||
468 | $email->addData('Test', 'Test value'); |
||
469 | $this->assertEquals('Test value', $email->getData()->Test); |
||
470 | $email->removeData('Test'); |
||
471 | $this->assertNull($email->getData()->Test); |
||
472 | } |
||
473 | |||
474 | public function testBody() |
||
480 | } |
||
481 | |||
482 | public function testHTMLTemplate() |
||
483 | { |
||
484 | // Include dev theme |
||
485 | SSViewer::set_themes([ |
||
486 | 'silverstripe/framework:/tests/php/Control/Email/EmailTest', |
||
487 | '$default', |
||
513 | } |
||
514 | |||
515 | public function testPlainTemplate() |
||
516 | { |
||
517 | $email = new Email(); |
||
521 | } |
||
522 | |||
523 | public function testGetFailedRecipients() |
||
537 | } |
||
538 | |||
539 | public function testIsEmail() |
||
540 | { |
||
541 | $this->assertTrue((new Email)->IsEmail()); |
||
542 | } |
||
543 | |||
544 | public function testRenderAgain() |
||
560 | } |
||
561 | |||
562 | public function testRerender() |
||
563 | { |
||
564 | $email = new Email(); |
||
565 | $email->setData([ |
||
566 | 'EmailContent' => 'my content', |
||
567 | ]); |
||
568 | $email->render(); |
||
569 | $this->assertContains('my content', $email->getBody()); |
||
570 | $children = $email->getSwiftMessage()->getChildren(); |
||
571 | $this->assertCount(1, $children); |
||
572 | $plainPart = reset($children); |
||
573 | $this->assertEquals('my content', $plainPart->getBody()); |
||
574 | |||
575 | // Ensure setting data causes a rerender |
||
576 | $email->setData([ |
||
577 | 'EmailContent' => 'your content' |
||
578 | ]); |
||
579 | $email->render(); |
||
580 | $this->assertContains('your content', $email->getBody()); |
||
581 | |||
582 | // Ensure removing data causes a rerender |
||
583 | $email->removeData('EmailContent'); |
||
584 | $email->render(); |
||
585 | $this->assertNotContains('your content', $email->getBody()); |
||
586 | |||
587 | // Ensure adding data causes a rerender |
||
588 | $email->addData([ |
||
589 | 'EmailContent' => 'their content' |
||
590 | ]); |
||
591 | $email->render(); |
||
592 | $this->assertContains('their content', $email->getBody()); |
||
593 | } |
||
594 | |||
595 | public function testRenderPlainOnly() |
||
596 | { |
||
597 | $email = new Email(); |
||
598 | $email->setData([ |
||
599 | 'EmailContent' => 'test content', |
||
600 | ]); |
||
601 | $email->render(true); |
||
602 | $this->assertEquals('text/plain', $email->getSwiftMessage()->getContentType()); |
||
603 | $this->assertEmpty($email->getSwiftMessage()->getChildren()); |
||
604 | } |
||
605 | |||
606 | public function testHasPlainPart() |
||
607 | { |
||
608 | $email = new Email(); |
||
609 | $email->setData([ |
||
610 | 'EmailContent' => 'test', |
||
611 | ]); |
||
612 | //emails are assumed to be HTML by default |
||
613 | $this->assertFalse($email->hasPlainPart()); |
||
614 | //make sure plain attachments aren't picked up as a plain part |
||
615 | $email->addAttachmentFromData('data', 'attachent.txt', 'text/plain'); |
||
616 | $this->assertFalse($email->hasPlainPart()); |
||
617 | $email->getSwiftMessage()->addPart('plain', 'text/plain'); |
||
618 | $this->assertTrue($email->hasPlainPart()); |
||
619 | } |
||
620 | |||
621 | public function testGeneratePlainPartFromBody() |
||
622 | { |
||
623 | $email = new Email(); |
||
624 | $email->setBody('<h1>Test</h1>'); |
||
625 | $this->assertEmpty($email->getSwiftMessage()->getChildren()); |
||
626 | $email->generatePlainPartFromBody(); |
||
627 | $children = $email->getSwiftMessage()->getChildren(); |
||
628 | $this->assertCount(1, $children); |
||
629 | $plainPart = reset($children); |
||
630 | $this->assertContains('Test', $plainPart->getBody()); |
||
631 | $this->assertNotContains('<h1>Test</h1>', $plainPart->getBody()); |
||
632 | } |
||
633 | |||
634 | public function testMultipleEmailSends() |
||
635 | { |
||
636 | $email = new Email(); |
||
637 | $email->setData([ |
||
638 | 'EmailContent' => 'Test', |
||
639 | ]); |
||
640 | $this->assertEmpty($email->getBody()); |
||
641 | $this->assertEmpty($email->getSwiftMessage()->getChildren()); |
||
642 | $email->send(); |
||
643 | $this->assertContains('Test', $email->getBody()); |
||
644 | $this->assertCount(1, $email->getSwiftMessage()->getChildren()); |
||
645 | $children = $email->getSwiftMessage()->getChildren(); |
||
646 | /** @var \Swift_MimePart $plainPart */ |
||
647 | $plainPart = reset($children); |
||
648 | $this->assertContains('Test', $plainPart->getBody()); |
||
649 | |||
650 | |||
651 | //send again |
||
652 | $email->send(); |
||
653 | $this->assertContains('Test', $email->getBody()); |
||
654 | $this->assertCount(1, $email->getSwiftMessage()->getChildren()); |
||
655 | $children = $email->getSwiftMessage()->getChildren(); |
||
656 | /** @var \Swift_MimePart $plainPart */ |
||
657 | $plainPart = reset($children); |
||
658 | $this->assertContains('Test', $plainPart->getBody()); |
||
659 | } |
||
660 | |||
661 | /** |
||
662 | * @return PHPUnit_Framework_MockObject_MockObject|Email |
||
663 | */ |
||
664 | protected function makeEmailMock($subject) |
||
679 | } |
||
680 | } |
||
681 |