MailerTest.php 6.9 KB

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