MailTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Log
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_Log_Writer_MailTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Log_Writer_MailTest::main");
  25. }
  26. /**
  27. * Test helper
  28. */
  29. require_once realpath(dirname(__FILE__) . '/../../..') . '/TestHelper.php';
  30. /** Zend_Layout */
  31. require_once 'Zend/Layout.php';
  32. /** Zend_Log */
  33. require_once 'Zend/Log.php';
  34. /** Zend_Log_Writer_Mail */
  35. require_once 'Zend/Log/Writer/Mail.php';
  36. /** Zend_Mail */
  37. require_once 'Zend/Mail.php';
  38. /** Zend_Mail_Transport_Exception */
  39. require_once 'Zend/Mail/Transport/Exception.php';
  40. /** Zend_View_Exception */
  41. require_once 'Zend/View/Exception.php';
  42. class Zend_Log_Writer_MailTest extends PHPUnit_Framework_TestCase
  43. {
  44. /**
  45. * Runs the test methods of this class.
  46. *
  47. * @return void
  48. */
  49. public static function main()
  50. {
  51. require_once "PHPUnit/TextUI/TestRunner.php";
  52. $suite = new PHPUnit_Framework_TestSuite("Zend_Log_Writer_MailTest");
  53. $result = PHPUnit_TextUI_TestRunner::run($suite);
  54. }
  55. /**
  56. * Tests normal logging, but with multiple messages for a level.
  57. *
  58. * @return void
  59. */
  60. public function testNormalLoggingMultiplePerLevel()
  61. {
  62. list(, , $log) = $this->_getSimpleLogger();
  63. $log->info('an info message');
  64. $log->info('a second info message');
  65. }
  66. /**
  67. * Tests normal logging without use of Zend_Layout.
  68. *
  69. * @return void
  70. */
  71. public function testNormalLoggingNoLayout()
  72. {
  73. list(, , $log) = $this->_getSimpleLogger();
  74. $log->info('an info message');
  75. $log->warn('a warning message');
  76. }
  77. /**
  78. * Tests normal logging with Zend_Layout usage.
  79. *
  80. * @return void
  81. */
  82. public function testNormalLoggingWithLayout()
  83. {
  84. list(, , $log) = $this->_getSimpleLogger(true);
  85. $log->info('an info message');
  86. $log->warn('a warning message');
  87. }
  88. /**
  89. * Tests normal logging with Zend_Layout and a custom formatter for it.
  90. *
  91. * @return void
  92. */
  93. public function testNormalLoggingWithLayoutAndItsFormatter()
  94. {
  95. list(, $writer, $log) = $this->_getSimpleLogger(true);
  96. // Since I'm using Zend_Layout, I should be able to set a formatter
  97. // for it.
  98. $writer->setLayoutFormatter(new Zend_Log_Formatter_Simple());
  99. // Log some messages to cover those cases.
  100. $log->info('an info message');
  101. $log->warn('a warning message');
  102. }
  103. /**
  104. * Tests normal logging with use of Zend_Layout, a custom formatter, and
  105. * subject prepend text.
  106. *
  107. * @return void
  108. */
  109. public function testNormalLoggingWithLayoutFormatterAndSubjectPrependText()
  110. {
  111. list(, $writer, $log) = $this->_getSimpleLogger(true);
  112. $writer->setLayoutFormatter(new Zend_Log_Formatter_Simple());
  113. $return = $writer->setSubjectPrependText('foo');
  114. $this->assertSame($writer, $return);
  115. // Log some messages to cover those cases.
  116. $log->info('an info message');
  117. $log->warn('a warning message');
  118. }
  119. /**
  120. * Tests setting of subject prepend text.
  121. *
  122. * @return void
  123. */
  124. public function testSetSubjectPrependTextNormal()
  125. {
  126. list($mail, $writer, $log) = $this->_getSimpleLogger();
  127. $return = $writer->setSubjectPrependText('foo');
  128. // Ensure that fluent interface is present.
  129. $this->assertSame($writer, $return);
  130. }
  131. /**
  132. * Tests that the subject prepend text can't be set if the Zend_Mail
  133. * object already has a subject line set.
  134. *
  135. * @return void
  136. */
  137. public function testSetSubjectPrependTextPreExisting()
  138. {
  139. list($mail, $writer, $log) = $this->_getSimpleLogger();
  140. // Expect a Zend_Log_Exception because the subject prepend text cannot
  141. // be set of the Zend_Mail object already has a subject line set.
  142. $this->setExpectedException('Zend_Log_Exception');
  143. // Set a subject line so the setSubjectPrependText() call triggers an
  144. // exception.
  145. $mail->setSubject('a pre-existing subject line');
  146. $writer->setSubjectPrependText('foo');
  147. }
  148. /**
  149. * Tests basic fluent interface for setting layout formatter.
  150. *
  151. * @return void
  152. */
  153. public function testSetLayoutFormatter()
  154. {
  155. list(, $writer) = $this->_getSimpleLogger(true);
  156. $return = $writer->setLayoutFormatter(new Zend_Log_Formatter_Simple());
  157. $this->assertSame($writer, $return);
  158. }
  159. /**
  160. * Tests that the layout formatter can be set and retrieved.
  161. *
  162. * @return void
  163. */
  164. public function testGetLayoutFormatter()
  165. {
  166. list(, $writer) = $this->_getSimpleLogger(true);
  167. $formatter = new Zend_Log_Formatter_Simple();
  168. // Ensure that fluent interface is present.
  169. $returnedWriter = $writer->setLayoutFormatter($formatter);
  170. $this->assertSame($writer, $returnedWriter);
  171. // Ensure that the getter returns the same formatter.
  172. $returnedFormatter = $writer->getLayoutFormatter();
  173. $this->assertSame($formatter, $returnedFormatter);
  174. }
  175. /**
  176. * Tests setting of the layout formatter when Zend_Layout is not being
  177. * used.
  178. *
  179. * @return void
  180. */
  181. public function testSetLayoutFormatterWithoutLayout()
  182. {
  183. list(, $writer) = $this->_getSimpleLogger();
  184. // If Zend_Layout is not being used, a formatter cannot be set for it.
  185. $this->setExpectedException('Zend_Log_Exception');
  186. $writer->setLayoutFormatter(new Zend_Log_Formatter_Simple());
  187. }
  188. /**
  189. * Tests destruction of the Zend_Log instance when an error message entry
  190. * is in place, but the mail can't be sent. Should result in a warning,
  191. * which we test for here.
  192. *
  193. * @return void
  194. */
  195. public function testDestructorMailError()
  196. {
  197. list($mail, $writer, $log) = $this->_getSimpleLogger(false);
  198. // Force the send() method to throw the same exception that would be
  199. // thrown if, say, the SMTP server couldn't be contacted.
  200. $mail->expects($this->any())
  201. ->method('send')
  202. ->will($this->throwException(new Zend_Mail_Transport_Exception()));
  203. // Log an error message so that there's something to send via email.
  204. $log->err('a bogus error message to force mail sending');
  205. $this->setExpectedException('PHPUnit_Framework_Error');
  206. unset($log);
  207. }
  208. /**
  209. * Tests destruction of the Zend_Log instance when an error message entry
  210. * is in place, but the layout can't be rendered. Should result in a
  211. * notice, which we test for here.
  212. *
  213. * @return void
  214. */
  215. public function testDestructorLayoutError()
  216. {
  217. list($mail, $writer, $log, $layout) = $this->_getSimpleLogger(true);
  218. // Force the render() method to throw the same exception that would
  219. // be thrown if, say, the layout template file couldn't be found.
  220. $layout->expects($this->any())
  221. ->method('render')
  222. ->will($this->throwException(new Zend_View_Exception('bogus message')));
  223. // Log an error message so that there's something to send via email.
  224. $log->err('a bogus error message to force mail sending');
  225. $this->setExpectedException('PHPUnit_Framework_Error');
  226. unset($log);
  227. }
  228. /**
  229. * Returns an array of the Zend_Mail mock object, Zend_Log_Writer_Mail
  230. * object, and Zend_Log objects.
  231. *
  232. * This is just a helper function for the various test methods above.
  233. *
  234. * @return array Numerically indexed array of Zend_Mail,
  235. * Zend_Log_Writer_Mail, Zend_Log, and Zend_Layout objects,
  236. * in that order.
  237. */
  238. protected function _getSimpleLogger($useLayout = false)
  239. {
  240. // Get a mock object for Zend_Mail so that no emails are actually
  241. // sent.
  242. $mail = $this->getMock('Zend_Mail', array('send'));
  243. // The send() method can be called any number of times.
  244. $mail->expects($this->any())
  245. ->method('send');
  246. $mail->addTo('zend_log_writer_mail_test@example.org');
  247. $mail->setFrom('zend_log_writer_mail_test@example.org');
  248. // Setup a mock object for Zend_Layout because we can't rely on any
  249. // layout files being in place.
  250. if ($useLayout) {
  251. $layout = $this->getMock('Zend_Layout', array('render'));
  252. $writer = new Zend_Log_Writer_Mail($mail, $layout);
  253. } else {
  254. $writer = new Zend_Log_Writer_Mail($mail);
  255. $layout = null;
  256. }
  257. $log = new Zend_Log();
  258. $log->addWriter($writer);
  259. return array($mail, $writer, $log, $layout);
  260. }
  261. }
  262. // Call Zend_Log_Writer_MailTest::main() if this source file is executed directly.
  263. if (PHPUnit_MAIN_METHOD == "Zend_Log_Writer_MailTest::main") {
  264. Zend_Log_Writer_MailTest::main();
  265. }