MailerTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. // Copyright 2019 Hackware SpA <human@hackware.cl>
  3. // "Hackware Web Services Core" is released under the MIT License terms.
  4. namespace Hawese\Tests;
  5. use Hawese\Core\Mailer;
  6. class MailerTest extends TestCase
  7. {
  8. public function setUp(): void
  9. {
  10. parent::setUp();
  11. config([
  12. 'mail.from_address' => 'from@example.com',
  13. 'mail.from_name' => 'From Name',
  14. 'mail.reply_to_address' => 'reply@example.com',
  15. 'mail.reply_to_name' => 'Reply Name',
  16. ]);
  17. }
  18. private function mailerInstance() {
  19. $mailer = app(Mailer::class);
  20. $mailer->CharSet = Mailer::CHARSET_UTF8;
  21. $mailer->addAddress('to_mail@example.com', 'Example To');
  22. $mailer->Subject = 'Example subject';
  23. $mailer->Body = '<p>Example body</p>';
  24. $mailer->AltBody = 'Example body';
  25. return $mailer;
  26. }
  27. public function testSend()
  28. {
  29. $mailer = $this->mailerInstance();
  30. $this->assertTrue($mailer->send());
  31. $this->assertStringContainsString(
  32. 'Example body',
  33. $mailer->getSentMIMEMessage()
  34. );
  35. }
  36. public function testJsonSerialize()
  37. {
  38. $mailer = $this->mailerInstance();
  39. $this->assertJsonStringEqualsJsonString(
  40. json_encode([
  41. 'From' => $mailer->From,
  42. 'FromName' => $mailer->FromName,
  43. 'Subject' => $mailer->Subject,
  44. 'To' => [['t*****l@example.com', 'Example To']]
  45. ]),
  46. json_encode($mailer)
  47. );
  48. }
  49. // MailerServiceProvider tests
  50. public function testMailerServiceProviderSmtp()
  51. {
  52. config([
  53. 'mail.driver' => 'smtp',
  54. 'mail.host' => 'example.com',
  55. 'mail.port' => 25,
  56. 'mail.username' => 'username',
  57. 'mail.password' => 'password',
  58. 'mail.encryption' => 'ssl',
  59. ]);
  60. $mailer = $this->mailerInstance();
  61. $this->assertSame('smtp', $mailer->Mailer);
  62. $this->assertSame('example.com', $mailer->Host);
  63. $this->assertSame(25, $mailer->Port);
  64. $this->assertSame('username', $mailer->Username);
  65. $this->assertSame('password', $mailer->Password);
  66. $this->assertSame('ssl', $mailer->SMTPSecure);
  67. }
  68. public function testMailerServiceProviderSendmail()
  69. {
  70. config(['mail.driver' => 'sendmail']);
  71. $mailer = $this->mailerInstance();
  72. $this->assertSame('sendmail', $mailer->Mailer);
  73. }
  74. public function testMailerServiceProviderDefault()
  75. {
  76. config(['mail.driver' => 'anything']);
  77. $mailer = $this->mailerInstance();
  78. $this->assertSame('mail', $mailer->Mailer);
  79. }
  80. }