2
0

MailTest.php 9.8 KB

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