EmailTraitTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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\Constraint\Email\MailSentFrom;
  19. use Cake\TestSuite\EmailTrait;
  20. use Cake\TestSuite\TestCase;
  21. use Cake\TestSuite\TestEmailTransport;
  22. use PHPUnit\Framework\AssertionFailedError;
  23. use PHPUnit\Framework\Constraint\LogicalNot;
  24. /**
  25. * Tests EmailTrait assertions
  26. */
  27. class EmailTraitTest extends TestCase
  28. {
  29. use EmailTrait;
  30. /**
  31. * setUp
  32. *
  33. * @return void
  34. */
  35. public function setUp()
  36. {
  37. parent::setUp();
  38. Email::drop('default');
  39. Email::drop('alternate');
  40. Email::setConfig('default', [
  41. 'transport' => 'test_tools',
  42. 'from' => ['default@example.com' => 'Default Name'],
  43. ]);
  44. Email::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. * @return void
  56. */
  57. public function tearDown()
  58. {
  59. parent::tearDown();
  60. Email::drop('default');
  61. Email::drop('alternate');
  62. TransportFactory::drop('test_tools');
  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' => 'Default Name']);
  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->assertMailContainsAttachment('custom_name.php');
  80. $this->assertMailContainsAttachment('custom_name.php', ['file' => CAKE . 'basics.php']);
  81. $this->assertMailSentWith('Hello world', 'subject');
  82. $this->assertMailSentWith('cc@example.com', 'cc');
  83. $this->assertMailSentWith('bcc@example.com', 'bcc');
  84. $this->assertMailSentWith('cc2@example.com', 'cc');
  85. $this->assertMailSentWith('default', 'template');
  86. $this->assertMailSentWith('default', 'layout');
  87. }
  88. /**
  89. * tests multiple email assertions
  90. *
  91. * @return void
  92. */
  93. public function testMultipleAssertions()
  94. {
  95. $this->assertNoMailSent();
  96. $this->sendEmails();
  97. $this->assertMailCount(3);
  98. $this->assertMailSentFromAt(0, 'default@example.com');
  99. $this->assertMailSentFromAt(1, 'alternate@example.com');
  100. $this->assertMailSentToAt(0, 'to@example.com');
  101. $this->assertMailSentToAt(1, 'to2@example.com');
  102. $this->assertMailSentToAt(2, 'to3@example.com');
  103. $this->assertMailContainsAt(0, 'text');
  104. $this->assertMailContainsAt(1, 'html');
  105. $this->assertMailSentWithAt(0, 'Hello world', 'subject');
  106. }
  107. /**
  108. * confirm that "at 0" is really testing email 0, not all the emails
  109. *
  110. * @return void
  111. */
  112. public function testAt0()
  113. {
  114. $this->skipIf(!class_exists('PHPUnit\Framework\Constraint\LogicalNot'), 'LogicalNot class not supported on PHP5.6');
  115. $this->assertNoMailSent();
  116. $this->sendEmails();
  117. $this->assertMailCount(3);
  118. $this->assertThat('alternate@example.com', new LogicalNot(new MailSentFrom(0)));
  119. }
  120. /**
  121. * tests assertNoMailSent fails when no mail is sent
  122. *
  123. * @return void
  124. */
  125. public function testAssertNoMailSentFailure()
  126. {
  127. $this->expectException(AssertionFailedError::class);
  128. $this->expectExceptionMessage('Failed asserting that no emails were sent.');
  129. $this->sendEmails();
  130. $this->assertNoMailSent();
  131. }
  132. /**
  133. * tests assertMailContainsHtml fails appropriately
  134. *
  135. * @return void
  136. */
  137. public function testAssertContainsHtmlFailure()
  138. {
  139. $this->expectException(AssertionFailedError::class);
  140. $this->sendEmails();
  141. $this->assertMailContainsHtmlAt(0, 'text');
  142. }
  143. /**
  144. * tests assertMailContainsText fails appropriately
  145. *
  146. * @return void
  147. */
  148. public function testAssertContainsTextFailure()
  149. {
  150. $this->expectException(AssertionFailedError::class);
  151. $this->sendEmails();
  152. $this->assertMailContainsTextAt(1, 'html');
  153. }
  154. /**
  155. * Tests asserting using RegExp characters doesn't break the assertion
  156. *
  157. * @return void
  158. */
  159. public function testAssertUsingRegExpCharacters()
  160. {
  161. (new Email())
  162. ->setTo('to3@example.com')
  163. ->setCc('cc3@example.com')
  164. ->send('email with regexp chars $/[]');
  165. $this->assertMailContains('$/[]');
  166. }
  167. /**
  168. * tests constraint failure messages
  169. *
  170. * @param string $assertion Assertion method
  171. * @param string $expectedMessage Expected failure message
  172. * @param array $params Assertion params
  173. * @dataProvider failureMessageDataProvider
  174. */
  175. public function testFailureMessages($assertion, $expectedMessage, $params)
  176. {
  177. $this->expectException(AssertionFailedError::class);
  178. $this->expectExceptionMessage($expectedMessage);
  179. call_user_func_array([$this, $assertion], $params);
  180. }
  181. /**
  182. * data provider for checking failure messages
  183. *
  184. * @return array
  185. */
  186. public function failureMessageDataProvider()
  187. {
  188. return [
  189. 'assertMailCount' => ['assertMailCount', 'Failed asserting that 2 emails were sent.', [2]],
  190. 'assertMailSentTo' => ['assertMailSentTo', 'Failed asserting that \'missing@example.com\' was sent an email.', ['missing@example.com']],
  191. 'assertMailSentToAt' => ['assertMailSentToAt', 'Failed asserting that \'missing@example.com\' was sent email #1.', [1, 'missing@example.com']],
  192. 'assertMailSentFrom' => ['assertMailSentFrom', 'Failed asserting that \'missing@example.com\' sent an email.', ['missing@example.com']],
  193. 'assertMailSentFromAt' => ['assertMailSentFromAt', 'Failed asserting that \'missing@example.com\' sent email #1.', [1, 'missing@example.com']],
  194. 'assertMailSentWith' => ['assertMailSentWith', 'Failed asserting that \'Missing\' is in an email `subject`.', ['Missing', 'subject']],
  195. 'assertMailSentWithAt' => ['assertMailSentWithAt', 'Failed asserting that \'Missing\' is in email #1 `subject`.', [1, 'Missing', 'subject']],
  196. 'assertMailContains' => ['assertMailContains', 'Failed asserting that \'Missing\' is in an email.', ['Missing']],
  197. 'assertMailContainsAttachment' => ['assertMailContainsAttachment', 'Failed asserting that \'no_existing_file.php\' is an attachment of an email.', ['no_existing_file.php']],
  198. 'assertMailContainsHtml' => ['assertMailContainsHtml', 'Failed asserting that \'Missing\' is in the html message of an email.', ['Missing']],
  199. 'assertMailContainsText' => ['assertMailContainsText', 'Failed asserting that \'Missing\' is in the text message of an email.', ['Missing']],
  200. 'assertMailContainsAt' => ['assertMailContainsAt', 'Failed asserting that \'Missing\' is in email #1.', [1, 'Missing']],
  201. 'assertMailContainsHtmlAt' => ['assertMailContainsHtmlAt', 'Failed asserting that \'Missing\' is in the html message of email #1.', [1, 'Missing']],
  202. 'assertMailContainsTextAt' => ['assertMailContainsTextAt', 'Failed asserting that \'Missing\' is in the text message of email #1.', [1, 'Missing']],
  203. ];
  204. }
  205. /**
  206. * sends some emails
  207. *
  208. * @return void
  209. */
  210. private function sendEmails()
  211. {
  212. (new Email())
  213. ->setTo(['to@example.com' => 'Foo Bar'])
  214. ->addTo('alsoto@example.com')
  215. ->setCc('cc@example.com')
  216. ->setBcc(['bcc@example.com' => 'Baz Qux'])
  217. ->setSubject('Hello world')
  218. ->setAttachments(['custom_name.php' => CAKE . 'basics.php'])
  219. ->setEmailFormat(Email::MESSAGE_TEXT)
  220. ->send('text');
  221. $email = (new Email('alternate'))
  222. ->setTo('to2@example.com')
  223. ->setCc('cc2@example.com')
  224. ->setEmailFormat(Email::MESSAGE_HTML);
  225. $email->viewBuilder()
  226. ->setTemplate('default')
  227. ->setLayout('default');
  228. $email->send('html');
  229. (new Email('alternate'))
  230. ->setTo(['to3@example.com' => null])
  231. ->send('html');
  232. }
  233. }