MailTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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-2008 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. class Zend_Log_Writer_MailTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * Runs the test methods of this class.
  42. *
  43. * @return void
  44. */
  45. public static function main()
  46. {
  47. require_once "PHPUnit/TextUI/TestRunner.php";
  48. $suite = new PHPUnit_Framework_TestSuite("Zend_Log_Writer_MailTest");
  49. $result = PHPUnit_TextUI_TestRunner::run($suite);
  50. }
  51. /**
  52. * Tests normal logging, but with multiple messages for a level.
  53. *
  54. * @return void
  55. */
  56. public function testNormalLoggingMultiplePerLevel()
  57. {
  58. list(, , $log) = $this->_getSimpleLogger();
  59. $log->info('an info message');
  60. $log->info('a second info message');
  61. }
  62. /**
  63. * Tests normal logging without use of Zend_Layout.
  64. *
  65. * @return void
  66. */
  67. public function testNormalLoggingNoLayout()
  68. {
  69. list(, , $log) = $this->_getSimpleLogger();
  70. $log->info('an info message');
  71. $log->warn('a warning message');
  72. }
  73. /**
  74. * Tests normal logging with Zend_Layout usage.
  75. *
  76. * @return void
  77. */
  78. public function testNormalLoggingWithLayout()
  79. {
  80. list(, , $log) = $this->_getSimpleLogger(true);
  81. $log->info('an info message');
  82. $log->warn('a warning message');
  83. }
  84. /**
  85. * Tests normal logging with Zend_Layout and a custom formatter for it.
  86. *
  87. * @return void
  88. */
  89. public function testNormalLoggingWithLayoutAndItsFormatter()
  90. {
  91. list(, $writer, $log) = $this->_getSimpleLogger(true);
  92. // Since I'm using Zend_Layout, I should be able to set a formatter
  93. // for it.
  94. $writer->setLayoutFormatter(new Zend_Log_Formatter_Simple());
  95. // Log some messages to cover those cases.
  96. $log->info('an info message');
  97. $log->warn('a warning message');
  98. }
  99. /**
  100. * Tests normal logging with use of Zend_Layout, a custom formatter, and
  101. * subject prepend text.
  102. *
  103. * @return void
  104. */
  105. public function testNormalLoggingWithLayoutFormatterAndSubjectPrependText()
  106. {
  107. list(, $writer, $log) = $this->_getSimpleLogger(true);
  108. $writer->setLayoutFormatter(new Zend_Log_Formatter_Simple());
  109. $return = $writer->setSubjectPrependText('foo');
  110. $this->assertSame($writer, $return);
  111. // Log some messages to cover those cases.
  112. $log->info('an info message');
  113. $log->warn('a warning message');
  114. }
  115. /**
  116. * Tests setting of subject prepend text.
  117. *
  118. * @return void
  119. */
  120. public function testSetSubjectPrependTextNormal()
  121. {
  122. list($mail, $writer, $log) = $this->_getSimpleLogger();
  123. $return = $writer->setSubjectPrependText('foo');
  124. // Ensure that fluent interface is present.
  125. $this->assertSame($writer, $return);
  126. }
  127. /**
  128. * Tests that the subject prepend text can't be set if the Zend_Mail
  129. * object already has a subject line set.
  130. *
  131. * @return void
  132. */
  133. public function testSetSubjectPrependTextPreExisting()
  134. {
  135. list($mail, $writer, $log) = $this->_getSimpleLogger();
  136. // Expect a Zend_Log_Exception because the subject prepend text cannot
  137. // be set of the Zend_Mail object already has a subject line set.
  138. $this->setExpectedException('Zend_Log_Exception');
  139. // Set a subject line so the setSubjectPrependText() call triggers an
  140. // exception.
  141. $mail->setSubject('a pre-existing subject line');
  142. $writer->setSubjectPrependText('foo');
  143. }
  144. /**
  145. * Tests basic fluent interface for setting layout formatter.
  146. *
  147. * @return void
  148. */
  149. public function testSetLayoutFormatter()
  150. {
  151. list(, $writer) = $this->_getSimpleLogger(true);
  152. $return = $writer->setLayoutFormatter(new Zend_Log_Formatter_Simple());
  153. $this->assertSame($writer, $return);
  154. }
  155. /**
  156. * Tests that the layout formatter can be set and retrieved.
  157. *
  158. * @return void
  159. */
  160. public function testGetLayoutFormatter()
  161. {
  162. list(, $writer) = $this->_getSimpleLogger(true);
  163. $formatter = new Zend_Log_Formatter_Simple();
  164. // Ensure that fluent interface is present.
  165. $returnedWriter = $writer->setLayoutFormatter($formatter);
  166. $this->assertSame($writer, $returnedWriter);
  167. // Ensure that the getter returns the same formatter.
  168. $returnedFormatter = $writer->getLayoutFormatter();
  169. $this->assertSame($formatter, $returnedFormatter);
  170. }
  171. /**
  172. * Tests setting of the layout formatter when Zend_Layout is not being
  173. * used.
  174. *
  175. * @return void
  176. */
  177. public function testSetLayoutFormatterWithoutLayout()
  178. {
  179. list(, $writer) = $this->_getSimpleLogger();
  180. // If Zend_Layout is not being used, a formatter cannot be set for it.
  181. $this->setExpectedException('Zend_Log_Exception');
  182. $writer->setLayoutFormatter(new Zend_Log_Formatter_Simple());
  183. }
  184. /**
  185. * Returns an array of the Zend_Mail mock object, Zend_Log_Writer_Mail
  186. * object, and Zend_Log objects.
  187. *
  188. * This is just a helper function for the various test methods above.
  189. *
  190. * @return array Numerically indexed array of Zend_Mail,
  191. * Zend_Log_Writer_Mail, and Zend_Log objects, in that
  192. * order.
  193. */
  194. protected function _getSimpleLogger($useLayout = false)
  195. {
  196. // Get a mock object for Zend_Mail so that no emails are actually
  197. // sent.
  198. $mail = $this->getMock('Zend_Mail', array('send'));
  199. // The send() method can be called any number of times.
  200. $mail->expects($this->any())
  201. ->method('send');
  202. $mail->addTo('zend_log_writer_mail_test@example.org');
  203. $mail->setFrom('zend_log_writer_mail_test@example.org');
  204. // Setup a mock object for Zend_Layout because we can't rely on any
  205. // layout files being in place.
  206. if ($useLayout) {
  207. $layout = $this->getMock('Zend_Layout', array('render'));
  208. $writer = new Zend_Log_Writer_Mail($mail, $layout);
  209. } else {
  210. $writer = new Zend_Log_Writer_Mail($mail);
  211. }
  212. $log = new Zend_Log();
  213. $log->addWriter($writer);
  214. return array($mail, $writer, $log);
  215. }
  216. }
  217. // Call Zend_Log_Writer_MailTest::main() if this source file is executed directly.
  218. if (PHPUnit_MAIN_METHOD == "Zend_Log_Writer_MailTest::main") {
  219. Zend_Log_Writer_MailTest::main();
  220. }