EmailTraitTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.7.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\TestSuite;
  17. use Cake\Mailer\Mailer;
  18. use Cake\Mailer\Message;
  19. use Cake\Mailer\TransportFactory;
  20. use Cake\TestSuite\Constraint\Email\MailSentFrom;
  21. use Cake\TestSuite\EmailTrait;
  22. use Cake\TestSuite\TestCase;
  23. use Cake\TestSuite\TestEmailTransport;
  24. use PHPUnit\Framework\AssertionFailedError;
  25. use PHPUnit\Framework\Constraint\LogicalNot;
  26. /**
  27. * Tests EmailTrait assertions
  28. */
  29. class EmailTraitTest extends TestCase
  30. {
  31. use EmailTrait;
  32. /**
  33. * setUp
  34. */
  35. public function setUp(): void
  36. {
  37. parent::setUp();
  38. Mailer::drop('default');
  39. Mailer::drop('alternate');
  40. Mailer::setConfig('default', [
  41. 'transport' => 'test_tools',
  42. 'from' => 'default@example.com',
  43. ]);
  44. Mailer::setConfig('alternate', [
  45. 'transport' => 'test_tools',
  46. 'from' => 'alternate@example.com',
  47. ]);
  48. TransportFactory::setConfig('test_tools', [
  49. 'className' => TestEmailTransport::class,
  50. ]);
  51. }
  52. /**
  53. * tearDown
  54. */
  55. public function tearDown(): void
  56. {
  57. parent::tearDown();
  58. Mailer::drop('default');
  59. Mailer::drop('alternate');
  60. TransportFactory::drop('test_tools');
  61. }
  62. /**
  63. * tests assertions against any emails that were sent
  64. */
  65. public function testSingleAssertions(): void
  66. {
  67. $this->sendEmails();
  68. $this->assertMailSentFrom('default@example.com');
  69. $this->assertMailSentFrom('alternate@example.com');
  70. $this->assertMailSentTo('to@example.com');
  71. $this->assertMailSentTo('alsoto@example.com');
  72. $this->assertMailSentTo('to2@example.com');
  73. $this->assertMailContains('text');
  74. $this->assertMailContains('html');
  75. $this->assertMailSubjectContains('world');
  76. $this->assertMailContainsAttachment('custom_name.php');
  77. $this->assertMailContainsAttachment('custom_name.php', ['file' => CAKE . 'basics.php']);
  78. $this->assertMailSentWith('Hello world', 'subject');
  79. $this->assertMailSentWith('cc@example.com', 'cc');
  80. $this->assertMailSentWith('bcc@example.com', 'bcc');
  81. $this->assertMailSentWith('cc2@example.com', 'cc');
  82. $this->assertMailSentWith('replyto@example.com', 'replyTo');
  83. $this->assertMailSentWith('sender@example.com', 'sender');
  84. }
  85. /**
  86. * tests multiple email assertions
  87. */
  88. public function testMultipleAssertions(): void
  89. {
  90. $this->assertNoMailSent();
  91. $this->sendEmails();
  92. $this->assertMailCount(3);
  93. $this->assertMailSentFromAt(0, 'default@example.com');
  94. $this->assertMailSentFromAt(1, 'alternate@example.com');
  95. // Confirm that "at 0" is really testing email 0, not all the emails
  96. $this->assertThat('alternate@example.com', new LogicalNot(new MailSentFrom(0)));
  97. $this->assertMailSentToAt(0, 'to@example.com');
  98. $this->assertMailSentToAt(1, 'to2@example.com');
  99. $this->assertMailSentToAt(2, 'to3@example.com');
  100. $this->assertMailContainsAt(0, 'text');
  101. $this->assertMailContainsAt(1, 'html');
  102. $this->assertMailSubjectContainsAt(0, 'world');
  103. $this->assertMailSentWithAt(0, 'Hello world', 'subject');
  104. $this->assertMailSentWithAt(0, 'replyto@example.com', 'replyTo');
  105. }
  106. /**
  107. * tests assertNoMailSent fails when no mail is sent
  108. */
  109. public function testAssertNoMailSentFailure(): void
  110. {
  111. $this->expectException(AssertionFailedError::class);
  112. $this->expectExceptionMessage('Failed asserting that no emails were sent.');
  113. $this->sendEmails();
  114. $this->assertNoMailSent();
  115. }
  116. /**
  117. * tests assertMailContainsHtml fails appropriately
  118. */
  119. public function testAssertContainsHtmlFailure(): void
  120. {
  121. $this->expectException(AssertionFailedError::class);
  122. $this->sendEmails();
  123. $this->assertMailContainsHtmlAt(0, 'text');
  124. }
  125. /**
  126. * tests assertMailContainsText fails appropriately
  127. */
  128. public function testAssertContainsTextFailure(): void
  129. {
  130. $this->expectException(AssertionFailedError::class);
  131. $this->sendEmails();
  132. $this->assertMailContainsTextAt(1, 'html');
  133. }
  134. /**
  135. * Tests asserting using RegExp characters doesn't break the assertion
  136. */
  137. public function testAssertUsingRegExpCharacters(): void
  138. {
  139. (new Mailer())
  140. ->setTo('to3@example.com')
  141. ->setCc('cc3@example.com')
  142. ->deliver('email with regexp chars $/[]');
  143. $this->assertMailContains('$/[]');
  144. }
  145. /**
  146. * tests constraint failure messages
  147. *
  148. * @param string $assertion Assertion method
  149. * @param string $expectedMessage Expected failure message
  150. * @param array $params Assertion params
  151. * @dataProvider failureMessageDataProvider
  152. */
  153. public function testFailureMessages($assertion, $expectedMessage, $params): void
  154. {
  155. $this->expectException(AssertionFailedError::class);
  156. $this->expectExceptionMessage($expectedMessage);
  157. call_user_func_array([$this, $assertion], $params);
  158. }
  159. /**
  160. * data provider for checking failure messages
  161. *
  162. * @return array
  163. */
  164. public function failureMessageDataProvider(): array
  165. {
  166. return [
  167. 'assertMailCount' => ['assertMailCount', 'Failed asserting that 2 emails were sent.', [2]],
  168. 'assertMailSentTo' => ['assertMailSentTo', 'Failed asserting that \'missing@example.com\' was sent an email.', ['missing@example.com']],
  169. 'assertMailSentToAt' => ['assertMailSentToAt', 'Failed asserting that \'missing@example.com\' was sent email #1.', [1, 'missing@example.com']],
  170. 'assertMailSentFrom' => ['assertMailSentFrom', 'Failed asserting that \'missing@example.com\' sent an email.', ['missing@example.com']],
  171. 'assertMailSentFromAt' => ['assertMailSentFromAt', 'Failed asserting that \'missing@example.com\' sent email #1.', [1, 'missing@example.com']],
  172. 'assertMailSentWith' => ['assertMailSentWith', 'Failed asserting that \'Missing\' is in an email `subject`.', ['Missing', 'subject']],
  173. 'assertMailSentWithAt' => ['assertMailSentWithAt', 'Failed asserting that \'Missing\' is in email #1 `subject`.', [1, 'Missing', 'subject']],
  174. 'assertMailContains' => ['assertMailContains', 'Failed asserting that \'Missing\' is in an email' . PHP_EOL . 'was: .', ['Missing']],
  175. 'assertMailContainsAttachment' => ['assertMailContainsAttachment', 'Failed asserting that \'no_existing_file.php\' is an attachment of an email.', ['no_existing_file.php']],
  176. 'assertMailContainsHtml' => ['assertMailContainsHtml', 'Failed asserting that \'Missing\' is in the html message of an email' . PHP_EOL . 'was: .', ['Missing']],
  177. 'assertMailContainsText' => ['assertMailContainsText', 'Failed asserting that \'Missing\' is in the text message of an email' . PHP_EOL . 'was: .', ['Missing']],
  178. 'assertMailContainsAt' => ['assertMailContainsAt', 'Failed asserting that \'Missing\' is in email #1' . PHP_EOL . 'was: .', [1, 'Missing']],
  179. 'assertMailContainsHtmlAt' => ['assertMailContainsHtmlAt', 'Failed asserting that \'Missing\' is in the html message of email #1' . PHP_EOL . 'was: .', [1, 'Missing']],
  180. 'assertMailContainsTextAt' => ['assertMailContainsTextAt', 'Failed asserting that \'Missing\' is in the text message of email #1' . PHP_EOL . 'was: .', [1, 'Missing']],
  181. 'assertMailSubjectContains' => ['assertMailSubjectContains', 'Failed asserting that \'Missing\' is in an email subject' . PHP_EOL . 'was: .', ['Missing']],
  182. 'assertMailSubjectContainsAt' => ['assertMailSubjectContainsAt', 'Failed asserting that \'Missing\' is in an email subject #1' . PHP_EOL . 'was: .', [1, 'Missing']],
  183. ];
  184. }
  185. /**
  186. * sends some emails
  187. */
  188. private function sendEmails(): void
  189. {
  190. (new Mailer())
  191. ->setSender(['sender@example.com' => 'Sender'])
  192. ->setTo(['to@example.com' => 'Foo Bar'])
  193. ->addTo('alsoto@example.com')
  194. ->setReplyTo(['replyto@example.com' => 'Reply to me'])
  195. ->setCc('cc@example.com')
  196. ->setBcc(['bcc@example.com' => 'Baz Qux'])
  197. ->setSubject('Hello world')
  198. ->setAttachments(['custom_name.php' => CAKE . 'basics.php'])
  199. ->setEmailFormat(Message::MESSAGE_TEXT)
  200. ->deliver('text');
  201. (new Mailer('alternate'))
  202. ->setTo('to2@example.com')
  203. ->setCc('cc2@example.com')
  204. ->setEmailFormat(Message::MESSAGE_HTML)
  205. ->deliver('html');
  206. (new Mailer('alternate'))
  207. ->setTo(['to3@example.com' => null])
  208. ->deliver('html');
  209. }
  210. }