MailerTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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->getMock('Cake\Mailer\Email', (array)$methods, (array)$args);
  21. }
  22. public function testConstructor()
  23. {
  24. $mailer = new TestMailer();
  25. $this->assertInstanceOf('Cake\Mailer\Email', $mailer->getEmailForAssertion());
  26. }
  27. public function testReset()
  28. {
  29. $mailer = new TestMailer();
  30. $email = $mailer->getEmailForAssertion();
  31. $mailer->set(['foo' => 'bar']);
  32. $this->assertNotEquals($email->viewVars(), $mailer->reset()->getEmailForAssertion()->viewVars());
  33. }
  34. public function testGetName()
  35. {
  36. $result = (new TestMailer())->getName();
  37. $expected = 'Test';
  38. $this->assertEquals($expected, $result);
  39. }
  40. public function testLayout()
  41. {
  42. $result = (new TestMailer())->layout('foo');
  43. $this->assertInstanceOf('TestApp\Mailer\TestMailer', $result);
  44. $this->assertEquals('foo', $result->viewBuilder()->layout());
  45. }
  46. public function testProxies()
  47. {
  48. $email = $this->getMockForEmail('setHeaders');
  49. $email->expects($this->once())
  50. ->method('setHeaders')
  51. ->with(['X-Something' => 'nice']);
  52. $result = (new TestMailer($email))->setHeaders(['X-Something' => 'nice']);
  53. $this->assertInstanceOf('TestApp\Mailer\TestMailer', $result);
  54. $email = $this->getMockForEmail('addHeaders');
  55. $email->expects($this->once())
  56. ->method('addHeaders')
  57. ->with(['X-Something' => 'very nice', 'X-Other' => 'cool']);
  58. $result = (new TestMailer($email))->addHeaders(['X-Something' => 'very nice', 'X-Other' => 'cool']);
  59. $this->assertInstanceOf('TestApp\Mailer\TestMailer', $result);
  60. $email = $this->getMockForEmail('attachments');
  61. $email->expects($this->once())
  62. ->method('attachments')
  63. ->with([
  64. ['file' => CAKE . 'basics.php', 'mimetype' => 'text/plain']
  65. ]);
  66. $result = (new TestMailer($email))->attachments([
  67. ['file' => CAKE . 'basics.php', 'mimetype' => 'text/plain']
  68. ]);
  69. $this->assertInstanceOf('TestApp\Mailer\TestMailer', $result);
  70. }
  71. public function testSet()
  72. {
  73. $email = $this->getMockForEmail('viewVars');
  74. $email->expects($this->once())
  75. ->method('viewVars')
  76. ->with(['key' => 'value']);
  77. $result = (new TestMailer($email))->set('key', 'value');
  78. $this->assertInstanceOf('TestApp\Mailer\TestMailer', $result);
  79. $email = $this->getMockForEmail('viewVars');
  80. $email->expects($this->once())
  81. ->method('viewVars')
  82. ->with(['key' => 'value']);
  83. $result = (new TestMailer($email))->set(['key' => 'value']);
  84. $this->assertInstanceOf('TestApp\Mailer\TestMailer', $result);
  85. }
  86. public function testSend()
  87. {
  88. $email = $this->getMockForEmail('send');
  89. $email->expects($this->any())
  90. ->method('send')
  91. ->will($this->returnValue([]));
  92. $mailer = $this->getMock('TestApp\Mailer\TestMailer', ['test'], [$email]);
  93. $mailer->expects($this->once())
  94. ->method('test')
  95. ->with('foo', 'bar');
  96. $mailer->template('foobar');
  97. $mailer->send('test', ['foo', 'bar']);
  98. $this->assertEquals($mailer->template, 'foobar');
  99. }
  100. public function testSendWithUnsetTemplateDefaultsToActionName()
  101. {
  102. $email = $this->getMockForEmail('send');
  103. $email->expects($this->any())
  104. ->method('send')
  105. ->will($this->returnValue([]));
  106. $mailer = $this->getMock('TestApp\Mailer\TestMailer', ['test'], [$email]);
  107. $mailer->expects($this->once())
  108. ->method('test')
  109. ->with('foo', 'bar');
  110. $mailer->send('test', ['foo', 'bar']);
  111. $this->assertEquals($mailer->template, 'test');
  112. }
  113. /**
  114. * test that initial email instance config is restored after email is sent.
  115. *
  116. * @return [type]
  117. */
  118. public function testDefaultProfileRestoration()
  119. {
  120. $email = $this->getMockForEmail('send', [['template' => 'cakephp']]);
  121. $email->expects($this->any())
  122. ->method('send')
  123. ->will($this->returnValue([]));
  124. $mailer = $this->getMock('TestApp\Mailer\TestMailer', ['test'], [$email]);
  125. $mailer->expects($this->once())
  126. ->method('test')
  127. ->with('foo', 'bar');
  128. $mailer->template('test');
  129. $mailer->send('test', ['foo', 'bar']);
  130. $this->assertEquals($mailer->template, 'test');
  131. $this->assertEquals('cakephp', $mailer->viewBuilder()->template());
  132. }
  133. /**
  134. * @expectedException Cake\Mailer\Exception\MissingActionException
  135. * @expectedExceptionMessage Mail TestMailer::test() could not be found, or is not accessible.
  136. */
  137. public function testMissingActionThrowsException()
  138. {
  139. (new TestMailer())->send('test');
  140. }
  141. }