MailerTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  10. * @since 3.1.0
  11. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  12. */
  13. namespace Cake\Test\TestCase\Mailer;
  14. use Cake\TestSuite\TestCase;
  15. use TestApp\Mailer\TestMailer;
  16. class MailerTest extends TestCase
  17. {
  18. public function getMockForEmail($methods = [], $args = [])
  19. {
  20. return $this->getMockBuilder('Cake\Mailer\Email')
  21. ->setMethods((array)$methods)
  22. ->setConstructorArgs((array)$args)
  23. ->getMock();
  24. }
  25. public function testConstructor()
  26. {
  27. $mailer = new TestMailer();
  28. $this->assertInstanceOf('Cake\Mailer\Email', $mailer->getEmailForAssertion());
  29. }
  30. public function testReset()
  31. {
  32. $mailer = new TestMailer();
  33. $email = $mailer->getEmailForAssertion();
  34. $mailer->set(['foo' => 'bar']);
  35. $this->assertNotEquals($email->viewVars(), $mailer->reset()->getEmailForAssertion()->viewVars());
  36. }
  37. public function testGetName()
  38. {
  39. $result = (new TestMailer())->getName();
  40. $expected = 'Test';
  41. $this->assertEquals($expected, $result);
  42. }
  43. public function testLayout()
  44. {
  45. $result = (new TestMailer())->layout('foo');
  46. $this->assertInstanceOf('TestApp\Mailer\TestMailer', $result);
  47. $this->assertEquals('foo', $result->viewBuilder()->layout());
  48. }
  49. public function testProxies()
  50. {
  51. $email = $this->getMockForEmail('setHeaders');
  52. $email->expects($this->once())
  53. ->method('setHeaders')
  54. ->with(['X-Something' => 'nice']);
  55. $result = (new TestMailer($email))->setHeaders(['X-Something' => 'nice']);
  56. $this->assertInstanceOf('TestApp\Mailer\TestMailer', $result);
  57. $email = $this->getMockForEmail('addHeaders');
  58. $email->expects($this->once())
  59. ->method('addHeaders')
  60. ->with(['X-Something' => 'very nice', 'X-Other' => 'cool']);
  61. $result = (new TestMailer($email))->addHeaders(['X-Something' => 'very nice', 'X-Other' => 'cool']);
  62. $this->assertInstanceOf('TestApp\Mailer\TestMailer', $result);
  63. $email = $this->getMockForEmail('attachments');
  64. $email->expects($this->once())
  65. ->method('attachments')
  66. ->with([
  67. ['file' => CAKE . 'basics.php', 'mimetype' => 'text/plain']
  68. ]);
  69. $result = (new TestMailer($email))->attachments([
  70. ['file' => CAKE . 'basics.php', 'mimetype' => 'text/plain']
  71. ]);
  72. $this->assertInstanceOf('TestApp\Mailer\TestMailer', $result);
  73. }
  74. public function testSet()
  75. {
  76. $email = $this->getMockForEmail('viewVars');
  77. $email->expects($this->once())
  78. ->method('viewVars')
  79. ->with(['key' => 'value']);
  80. $result = (new TestMailer($email))->set('key', 'value');
  81. $this->assertInstanceOf('TestApp\Mailer\TestMailer', $result);
  82. $email = $this->getMockForEmail('viewVars');
  83. $email->expects($this->once())
  84. ->method('viewVars')
  85. ->with(['key' => 'value']);
  86. $result = (new TestMailer($email))->set(['key' => 'value']);
  87. $this->assertInstanceOf('TestApp\Mailer\TestMailer', $result);
  88. }
  89. public function testSend()
  90. {
  91. $email = $this->getMockForEmail('send');
  92. $email->expects($this->any())
  93. ->method('send')
  94. ->will($this->returnValue([]));
  95. $mailer = $this->getMockBuilder('TestApp\Mailer\TestMailer')
  96. ->setMethods(['test'])
  97. ->setConstructorArgs([$email])
  98. ->getMock();
  99. $mailer->expects($this->once())
  100. ->method('test')
  101. ->with('foo', 'bar');
  102. $mailer->template('foobar');
  103. $mailer->send('test', ['foo', 'bar']);
  104. $this->assertEquals($mailer->template, 'foobar');
  105. }
  106. public function testSendWithUnsetTemplateDefaultsToActionName()
  107. {
  108. $email = $this->getMockForEmail('send');
  109. $email->expects($this->any())
  110. ->method('send')
  111. ->will($this->returnValue([]));
  112. $mailer = $this->getMockBuilder('TestApp\Mailer\TestMailer')
  113. ->setMethods(['test'])
  114. ->setConstructorArgs([$email])
  115. ->getMock();
  116. $mailer->expects($this->once())
  117. ->method('test')
  118. ->with('foo', 'bar');
  119. $mailer->send('test', ['foo', 'bar']);
  120. $this->assertEquals($mailer->template, 'test');
  121. }
  122. /**
  123. * test that initial email instance config is restored after email is sent.
  124. *
  125. * @return [type]
  126. */
  127. public function testDefaultProfileRestoration()
  128. {
  129. $email = $this->getMockForEmail('send', [['template' => 'cakephp']]);
  130. $email->expects($this->any())
  131. ->method('send')
  132. ->will($this->returnValue([]));
  133. $mailer = $this->getMockBuilder('TestApp\Mailer\TestMailer')
  134. ->setMethods(['test'])
  135. ->setConstructorArgs([$email])
  136. ->getMock();
  137. $mailer->expects($this->once())
  138. ->method('test')
  139. ->with('foo', 'bar');
  140. $mailer->template('test');
  141. $mailer->send('test', ['foo', 'bar']);
  142. $this->assertEquals($mailer->template, 'test');
  143. $this->assertEquals('cakephp', $mailer->viewBuilder()->template());
  144. }
  145. /**
  146. * @expectedException Cake\Mailer\Exception\MissingActionException
  147. * @expectedExceptionMessage Mail TestMailer::test() could not be found, or is not accessible.
  148. */
  149. public function testMissingActionThrowsException()
  150. {
  151. (new TestMailer())->send('test');
  152. }
  153. }