EmailTraitTest.php 7.8 KB

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