MailerTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @since 3.1.0
  11. * @license https://opensource.org/licenses/mit-license.php MIT License
  12. */
  13. namespace Cake\Test\TestCase\Mailer;
  14. use Cake\TestSuite\TestCase;
  15. use RuntimeException;
  16. use TestApp\Mailer\TestMailer;
  17. class MailerTest extends TestCase
  18. {
  19. /**
  20. * @param array $methods
  21. * @param array $args
  22. * @return \Cake\Mailer\Email|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. public function getMockForEmail($methods = [], $args = [])
  25. {
  26. return $this->getMockBuilder('Cake\Mailer\Email')
  27. ->setMethods((array)$methods)
  28. ->setConstructorArgs((array)$args)
  29. ->getMock();
  30. }
  31. /**
  32. * @return void
  33. */
  34. public function testConstructor()
  35. {
  36. $mailer = new TestMailer();
  37. $this->assertInstanceOf('Cake\Mailer\Email', $mailer->getEmailForAssertion());
  38. }
  39. /**
  40. * @return void
  41. */
  42. public function testReset()
  43. {
  44. $mailer = new TestMailer();
  45. $email = $mailer->getEmailForAssertion();
  46. $mailer->set(['foo' => 'bar']);
  47. $this->assertNotEquals($email->getViewVars(), $mailer->reset()->getEmailForAssertion()->getViewVars());
  48. }
  49. /**
  50. * @return void
  51. */
  52. public function testGetName()
  53. {
  54. $result = (new TestMailer())->getName();
  55. $expected = 'Test';
  56. $this->assertEquals($expected, $result);
  57. }
  58. /**
  59. * @return void
  60. */
  61. public function testProxies()
  62. {
  63. $email = $this->getMockForEmail('setHeaders');
  64. $email->expects($this->once())
  65. ->method('setHeaders')
  66. ->with(['X-Something' => 'nice']);
  67. $result = (new TestMailer($email))->setHeaders(['X-Something' => 'nice']);
  68. $this->assertInstanceOf('TestApp\Mailer\TestMailer', $result);
  69. $email = $this->getMockForEmail('addHeaders');
  70. $email->expects($this->once())
  71. ->method('addHeaders')
  72. ->with(['X-Something' => 'very nice', 'X-Other' => 'cool']);
  73. $result = (new TestMailer($email))->addHeaders(['X-Something' => 'very nice', 'X-Other' => 'cool']);
  74. $this->assertInstanceOf('TestApp\Mailer\TestMailer', $result);
  75. $email = $this->getMockForEmail('setAttachments');
  76. $email->expects($this->once())
  77. ->method('setAttachments')
  78. ->with([
  79. ['file' => CAKE . 'basics.php', 'mimetype' => 'text/plain'],
  80. ]);
  81. $result = (new TestMailer($email))->setAttachments([
  82. ['file' => CAKE . 'basics.php', 'mimetype' => 'text/plain'],
  83. ]);
  84. $this->assertInstanceOf('TestApp\Mailer\TestMailer', $result);
  85. }
  86. /**
  87. * Test that get/set methods can be proxied.
  88. *
  89. * @return void
  90. */
  91. public function testGetSetProxies()
  92. {
  93. $mailer = new TestMailer();
  94. $result = $mailer
  95. ->setTo('test@example.com')
  96. ->setCc('cc@example.com');
  97. $this->assertSame($result, $mailer);
  98. $this->assertSame(['test@example.com' => 'test@example.com'], $result->getTo());
  99. $this->assertSame(['cc@example.com' => 'cc@example.com'], $result->getCc());
  100. }
  101. public function testSet()
  102. {
  103. $email = $this->getMockForEmail('setViewVars');
  104. $email->expects($this->once())
  105. ->method('setViewVars')
  106. ->with(['key' => 'value']);
  107. $result = (new TestMailer($email))->set('key', 'value');
  108. $this->assertInstanceOf('TestApp\Mailer\TestMailer', $result);
  109. $email = $this->getMockForEmail('setViewVars');
  110. $email->expects($this->once())
  111. ->method('setViewVars')
  112. ->with(['key' => 'value']);
  113. $result = (new TestMailer($email))->set(['key' => 'value']);
  114. $this->assertInstanceOf('TestApp\Mailer\TestMailer', $result);
  115. }
  116. public function testSend()
  117. {
  118. $email = $this->getMockForEmail('send');
  119. $email->expects($this->any())
  120. ->method('send')
  121. ->will($this->returnValue([]));
  122. $mailer = $this->getMockBuilder('TestApp\Mailer\TestMailer')
  123. ->setMethods(['test'])
  124. ->setConstructorArgs([$email])
  125. ->getMock();
  126. $mailer->expects($this->once())
  127. ->method('test')
  128. ->with('foo', 'bar');
  129. $mailer->send('test', ['foo', 'bar']);
  130. }
  131. public function testSendWithUnsetTemplateDefaultsToActionName()
  132. {
  133. $email = $this->getMockForEmail('send');
  134. $email->expects($this->any())
  135. ->method('send')
  136. ->will($this->returnValue([]));
  137. $mailer = $this->getMockBuilder('TestApp\Mailer\TestMailer')
  138. ->setMethods(['test'])
  139. ->setConstructorArgs([$email])
  140. ->getMock();
  141. $mailer->expects($this->once())
  142. ->method('test')
  143. ->with('foo', 'bar');
  144. $mailer->send('test', ['foo', 'bar']);
  145. $this->assertEquals($mailer->template, 'test');
  146. }
  147. /**
  148. * Test that mailers call reset() when send fails
  149. *
  150. * @return void
  151. */
  152. public function testSendFailsEmailIsReset()
  153. {
  154. $email = $this->getMockForEmail(['send', 'reset']);
  155. $email->expects($this->once())
  156. ->method('send')
  157. ->will($this->throwException(new RuntimeException('kaboom')));
  158. $mailer = $this->getMockBuilder('TestApp\Mailer\TestMailer')
  159. ->setMethods(['welcome', 'reset'])
  160. ->setConstructorArgs([$email])
  161. ->getMock();
  162. // Mailer should be reset even if sending fails.
  163. $mailer->expects($this->once())
  164. ->method('reset');
  165. try {
  166. $mailer->send('welcome', ['foo', 'bar']);
  167. $this->fail('Exception should bubble up.');
  168. } catch (RuntimeException $e) {
  169. $this->assertTrue(true, 'Exception was raised');
  170. }
  171. }
  172. /**
  173. * test that initial email instance config is restored after email is sent.
  174. *
  175. * @return void
  176. */
  177. public function testDefaultProfileRestoration()
  178. {
  179. $email = $this->getMockForEmail('send', [['template' => 'cakephp']]);
  180. $email->expects($this->any())
  181. ->method('send')
  182. ->will($this->returnValue([]));
  183. $mailer = $this->getMockBuilder('TestApp\Mailer\TestMailer')
  184. ->setMethods(['test'])
  185. ->setConstructorArgs([$email])
  186. ->getMock();
  187. $mailer->expects($this->once())
  188. ->method('test')
  189. ->with('foo', 'bar');
  190. $mailer->send('test', ['foo', 'bar']);
  191. $this->assertEquals('cakephp', $mailer->viewBuilder()->getTemplate());
  192. }
  193. /**
  194. * @return void
  195. */
  196. public function testMissingActionThrowsException()
  197. {
  198. $this->expectException(\Cake\Mailer\Exception\MissingActionException::class);
  199. $this->expectExceptionMessage('Mail TestMailer::test() could not be found, or is not accessible.');
  200. (new TestMailer())->send('test');
  201. }
  202. }