EmailTraitTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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\Email;
  18. use Cake\Mailer\TransportFactory;
  19. use Cake\TestSuite\EmailTrait;
  20. use Cake\TestSuite\TestCase;
  21. use Cake\TestSuite\TestEmailTransport;
  22. use PHPUnit\Framework\AssertionFailedError;
  23. /**
  24. * Tests EmailTrait assertions
  25. */
  26. class EmailTraitTest extends TestCase
  27. {
  28. use EmailTrait;
  29. /**
  30. * setUp
  31. *
  32. * @return void
  33. */
  34. public function setUp()
  35. {
  36. parent::setUp();
  37. Email::drop('default');
  38. Email::drop('alternate');
  39. Email::setConfig('default', [
  40. 'transport' => 'test_tools',
  41. 'from' => 'default@example.com',
  42. ]);
  43. Email::setConfig('alternate', [
  44. 'transport' => 'test_tools',
  45. 'from' => 'alternate@example.com',
  46. ]);
  47. TransportFactory::setConfig('test_tools', [
  48. 'className' => TestEmailTransport::class,
  49. ]);
  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. }
  63. /**
  64. * tests assertions against any emails that were sent
  65. *
  66. * @return void
  67. */
  68. public function testSingleAssertions()
  69. {
  70. $this->sendEmails();
  71. $this->assertMailSentFrom('default@example.com');
  72. $this->assertMailSentFrom('alternate@example.com');
  73. $this->assertMailSentTo('to@example.com');
  74. $this->assertMailSentTo('alsoto@example.com');
  75. $this->assertMailSentTo('to2@example.com');
  76. $this->assertMailContains('text');
  77. $this->assertMailContains('html');
  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. }
  83. /**
  84. * tests multiple email assertions
  85. *
  86. * @return void
  87. */
  88. public function testMultipleAssertions()
  89. {
  90. $this->assertNoMailSent();
  91. $this->sendEmails();
  92. $this->assertMailCount(2);
  93. $this->assertMailSentFromAt(0, 'default@example.com');
  94. $this->assertMailSentFromAt(1, 'alternate@example.com');
  95. $this->assertMailSentToAt(0, 'to@example.com');
  96. $this->assertMailSentToAt(1, 'to2@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 constraint failure messages
  137. *
  138. * @param string $assertion Assertion method
  139. * @param string $expectedMessage Expected failure message
  140. * @param array $params Assertion params
  141. * @dataProvider failureMessageDataProvider
  142. */
  143. public function testFailureMessages($assertion, $expectedMessage, $params)
  144. {
  145. $this->expectException(AssertionFailedError::class);
  146. $this->expectExceptionMessage($expectedMessage);
  147. call_user_func_array([$this, $assertion], $params);
  148. }
  149. /**
  150. * data provider for checking failure messages
  151. *
  152. * @return array
  153. */
  154. public function failureMessageDataProvider()
  155. {
  156. return [
  157. 'assertMailCount' => ['assertMailCount', 'Failed asserting that 2 emails were sent.', [2]],
  158. 'assertMailSentTo' => ['assertMailSentTo', 'Failed asserting that \'missing@example.com\' was sent an email.', ['missing@example.com']],
  159. 'assertMailSentToAt' => ['assertMailSentToAt', 'Failed asserting that \'missing@example.com\' was sent email #1.', [1, 'missing@example.com']],
  160. 'assertMailSentFrom' => ['assertMailSentFrom', 'Failed asserting that \'missing@example.com\' sent an email.', ['missing@example.com']],
  161. 'assertMailSentFromAt' => ['assertMailSentFromAt', 'Failed asserting that \'missing@example.com\' sent email #1.', [1, 'missing@example.com']],
  162. 'assertMailSentWith' => ['assertMailSentWith', 'Failed asserting that \'Missing\' is in an email `subject`.', ['Missing', 'subject']],
  163. 'assertMailSentWithAt' => ['assertMailSentWithAt', 'Failed asserting that \'Missing\' is in email #1 `subject`.', [1, 'Missing', 'subject']],
  164. 'assertMailContains' => ['assertMailContains', 'Failed asserting that \'Missing\' is in an email.', ['Missing']],
  165. 'assertMailContainsHtml' => ['assertMailContainsHtml', 'Failed asserting that \'Missing\' is in the html message of an email.', ['Missing']],
  166. 'assertMailContainsText' => ['assertMailContainsText', 'Failed asserting that \'Missing\' is in the text message of an email.', ['Missing']],
  167. 'assertMailContainsAt' => ['assertMailContainsAt', 'Failed asserting that \'Missing\' is in email #1.', [1, 'Missing']],
  168. 'assertMailContainsHtmlAt' => ['assertMailContainsHtmlAt', 'Failed asserting that \'Missing\' is in the html message of email #1.', [1, 'Missing']],
  169. 'assertMailContainsTextAt' => ['assertMailContainsTextAt', 'Failed asserting that \'Missing\' is in the text message of email #1.', [1, 'Missing']],
  170. ];
  171. }
  172. /**
  173. * sends some emails
  174. *
  175. * @return void
  176. */
  177. private function sendEmails()
  178. {
  179. (new Email())
  180. ->setTo(['to@example.com' => 'Foo Bar'])
  181. ->addTo('alsoto@example.com')
  182. ->setCc('cc@example.com')
  183. ->setBcc(['bcc@example.com' => 'Baz Qux'])
  184. ->setSubject('Hello world')
  185. ->setEmailFormat(Email::MESSAGE_TEXT)
  186. ->send('text');
  187. (new Email('alternate'))
  188. ->setTo('to2@example.com')
  189. ->setCc('cc2@example.com')
  190. ->setEmailFormat(Email::MESSAGE_HTML)
  191. ->send('html');
  192. }
  193. }