EmailTraitTest.php 7.4 KB

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