EmailTraitTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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\TestSuite\TestCase;
  18. use Cake\TestSuite\TestEmailTransport;
  19. use PHPUnit\Framework\AssertionFailedError;
  20. /**
  21. * Tests EmailTrait assertions
  22. */
  23. class EmailTraitTest extends TestCase
  24. {
  25. /**
  26. * setUp
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. Email::drop('default');
  34. Email::drop('alternate');
  35. Email::setConfig('default', [
  36. 'transport' => 'test_tools',
  37. 'from' => 'default@example.com',
  38. ]);
  39. Email::setConfig('alternate', [
  40. 'transport' => 'test_tools',
  41. 'from' => 'alternate@example.com',
  42. ]);
  43. Email::setConfigTransport('test_tools', [
  44. 'className' => TestEmailTransport::class
  45. ]);
  46. }
  47. /**
  48. * tearDown
  49. *
  50. * @return void
  51. */
  52. public function tearDown()
  53. {
  54. parent::tearDown();
  55. Email::drop('default');
  56. Email::drop('alternate');
  57. Email::dropTransport('test_tools');
  58. }
  59. /**
  60. * tests assertions against any emails that were sent
  61. *
  62. * @return void
  63. */
  64. public function testSingleAssertions()
  65. {
  66. $this->sendEmails();
  67. $this->assertMailSentFrom('default@example.com');
  68. $this->assertMailSentFrom('alternate@example.com');
  69. $this->assertMailSentTo('to@example.com');
  70. $this->assertMailSentTo('alsoto@example.com');
  71. $this->assertMailSentTo('to2@example.com');
  72. $this->assertMailContains('text');
  73. $this->assertMailContains('html');
  74. $this->assertMailSentWith('Hello world', 'subject');
  75. $this->assertMailSentWith('cc@example.com', 'cc');
  76. $this->assertMailSentWith('bcc@example.com', 'bcc');
  77. $this->assertMailSentWith('cc2@example.com', 'cc');
  78. }
  79. /**
  80. * tests multiple email assertions
  81. *
  82. * @return void
  83. */
  84. public function testMultipleAssertions()
  85. {
  86. $this->assertNoMailSent();
  87. $this->sendEmails();
  88. $this->assertMailCount(2);
  89. $this->assertMailSentFromAt(0, 'default@example.com');
  90. $this->assertMailSentFromAt(1, 'alternate@example.com');
  91. $this->assertMailSentToAt(0, 'to@example.com');
  92. $this->assertMailSentToAt(1, 'to2@example.com');
  93. $this->assertMailContainsAt(0, 'text');
  94. $this->assertMailContainsAt(1, 'html');
  95. $this->assertMailSentWithAt(0, 'Hello world', 'subject');
  96. }
  97. /**
  98. * tests assertNoMailSent fails when no mail is sent
  99. *
  100. * @return void
  101. */
  102. public function testAssertNoMailSentFailure()
  103. {
  104. $this->expectException(AssertionFailedError::class);
  105. $this->expectExceptionMessage('Failed asserting that no emails were sent.');
  106. $this->sendEmails();
  107. $this->assertNoMailSent();
  108. }
  109. /**
  110. * tests assertMailContainsHtml fails appropriately
  111. *
  112. * @return void
  113. */
  114. public function testAssertContainsHtmlFailure()
  115. {
  116. $this->expectException(AssertionFailedError::class);
  117. $this->sendEmails();
  118. $this->assertMailContainsHtmlAt(0, 'text');
  119. }
  120. /**
  121. * tests assertMailContainsText fails appropriately
  122. *
  123. * @return void
  124. */
  125. public function testAssertContainsTextFailure()
  126. {
  127. $this->expectException(AssertionFailedError::class);
  128. $this->sendEmails();
  129. $this->assertMailContainsTextAt(1, 'html');
  130. }
  131. /**
  132. * tests constraint failure messages
  133. *
  134. * @param string $assertion Assertion method
  135. * @param string $expectedMessage Expected failure message
  136. * @param array $params Assertion params
  137. * @dataProvider failureMessageDataProvider
  138. */
  139. public function testFailureMessages($assertion, $expectedMessage, $params)
  140. {
  141. $this->expectException(AssertionFailedError::class);
  142. $this->expectExceptionMessage($expectedMessage);
  143. call_user_func_array([$this, $assertion], $params);
  144. }
  145. /**
  146. * data provider for checking failure messages
  147. *
  148. * @return array
  149. */
  150. public function failureMessageDataProvider()
  151. {
  152. return [
  153. 'assertMailCount' => ['assertMailCount', 'Failed asserting that 2 emails were sent.', [2]],
  154. 'assertMailSentTo' => ['assertMailSentTo', 'Failed asserting that \'missing@example.com\' was sent an email.', ['missing@example.com']],
  155. 'assertMailSentToAt' => ['assertMailSentToAt', 'Failed asserting that \'missing@example.com\' was sent email #1.', [1, 'missing@example.com']],
  156. 'assertMailSentFrom' => ['assertMailSentFrom', 'Failed asserting that \'missing@example.com\' sent an email.', ['missing@example.com']],
  157. 'assertMailSentFromAt' => ['assertMailSentFromAt', 'Failed asserting that \'missing@example.com\' sent email #1.', [1, 'missing@example.com']],
  158. 'assertMailSentWith' => ['assertMailSentWith', 'Failed asserting that \'Missing\' is in an email `subject`.', ['Missing', 'subject']],
  159. 'assertMailSentWithAt' => ['assertMailSentWithAt', 'Failed asserting that \'Missing\' is in email #1 `subject`.', [1, 'Missing', 'subject']],
  160. 'assertMailContains' => ['assertMailContains', 'Failed asserting that \'Missing\' is in an email.', ['Missing']],
  161. 'assertMailContainsHtml' => ['assertMailContainsHtml', 'Failed asserting that \'Missing\' is in the html message of an email.', ['Missing']],
  162. 'assertMailContainsText' => ['assertMailContainsText', 'Failed asserting that \'Missing\' is in the text message of an email.', ['Missing']],
  163. 'assertMailContainsAt' => ['assertMailContainsAt', 'Failed asserting that \'Missing\' is in email #1.', [1, 'Missing']],
  164. 'assertMailContainsHtmlAt' => ['assertMailContainsHtmlAt', 'Failed asserting that \'Missing\' is in the html message of email #1.', [1, 'Missing']],
  165. 'assertMailContainsTextAt' => ['assertMailContainsTextAt', 'Failed asserting that \'Missing\' is in the text message of email #1.', [1, 'Missing']],
  166. ];
  167. }
  168. /**
  169. * sends some emails
  170. *
  171. * @return void
  172. */
  173. private function sendEmails()
  174. {
  175. (new Email())
  176. ->setTo(['to@example.com' => 'Foo Bar'])
  177. ->addTo('alsoto@example.com')
  178. ->setCc('cc@example.com')
  179. ->setBcc(['bcc@example.com' => 'Baz Qux'])
  180. ->setSubject('Hello world')
  181. ->setEmailFormat(Email::MESSAGE_TEXT)
  182. ->send('text');
  183. (new Email('alternate'))
  184. ->setTo('to2@example.com')
  185. ->setCc('cc2@example.com')
  186. ->setEmailFormat(Email::MESSAGE_HTML)
  187. ->send('html');
  188. }
  189. }