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