MailTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. * Mock Transport for Zend_Mail
  54. *
  55. * @var Zend_Mail_Transport_Abstract
  56. */
  57. protected $_transport;
  58. /**
  59. * Runs the test methods of this class.
  60. *
  61. * @return void
  62. */
  63. public static function main()
  64. {
  65. require_once "PHPUnit/TextUI/TestRunner.php";
  66. $suite = new PHPUnit_Framework_TestSuite("Zend_Log_Writer_MailTest");
  67. $result = PHPUnit_TextUI_TestRunner::run($suite);
  68. }
  69. protected function setUp()
  70. {
  71. $this->_transport = $this->getMockForAbstractClass(
  72. 'Zend_Mail_Transport_Abstract',
  73. array()
  74. );
  75. Zend_Mail::setDefaultTransport($this->_transport);
  76. }
  77. protected function tearDown()
  78. {
  79. Zend_Mail::clearDefaultTransport();
  80. }
  81. /**
  82. * Tests normal logging, but with multiple messages for a level.
  83. *
  84. * @return void
  85. */
  86. public function testNormalLoggingMultiplePerLevel()
  87. {
  88. list(, , $log) = $this->_getSimpleLogger();
  89. $log->info('an info message');
  90. $log->info('a second info message');
  91. }
  92. /**
  93. * Tests normal logging without use of Zend_Layout.
  94. *
  95. * @return void
  96. */
  97. public function testNormalLoggingNoLayout()
  98. {
  99. list(, , $log) = $this->_getSimpleLogger();
  100. $log->info('an info message');
  101. $log->warn('a warning message');
  102. }
  103. /**
  104. * Tests normal logging with Zend_Layout usage.
  105. *
  106. * @return void
  107. */
  108. public function testNormalLoggingWithLayout()
  109. {
  110. list(, , $log) = $this->_getSimpleLogger(true);
  111. $log->info('an info message');
  112. $log->warn('a warning message');
  113. }
  114. /**
  115. * Tests normal logging with Zend_Layout and a custom formatter for it.
  116. *
  117. * @return void
  118. */
  119. public function testNormalLoggingWithLayoutAndItsFormatter()
  120. {
  121. list(, $writer, $log) = $this->_getSimpleLogger(true);
  122. // Since I'm using Zend_Layout, I should be able to set a formatter
  123. // for it.
  124. $writer->setLayoutFormatter(new Zend_Log_Formatter_Simple());
  125. // Log some messages to cover those cases.
  126. $log->info('an info message');
  127. $log->warn('a warning message');
  128. }
  129. /**
  130. * Tests normal logging with use of Zend_Layout, a custom formatter, and
  131. * subject prepend text.
  132. *
  133. * @return void
  134. */
  135. public function testNormalLoggingWithLayoutFormatterAndSubjectPrependText()
  136. {
  137. list(, $writer, $log) = $this->_getSimpleLogger(true);
  138. $writer->setLayoutFormatter(new Zend_Log_Formatter_Simple());
  139. $return = $writer->setSubjectPrependText('foo');
  140. $this->assertSame($writer, $return);
  141. // Log some messages to cover those cases.
  142. $log->info('an info message');
  143. $log->warn('a warning message');
  144. }
  145. /**
  146. * Tests setting of subject prepend text.
  147. *
  148. * @return void
  149. */
  150. public function testSetSubjectPrependTextNormal()
  151. {
  152. list($mail, $writer, $log) = $this->_getSimpleLogger();
  153. $return = $writer->setSubjectPrependText('foo');
  154. // Ensure that fluent interface is present.
  155. $this->assertSame($writer, $return);
  156. }
  157. /**
  158. * Tests that the subject prepend text can't be set if the Zend_Mail
  159. * object already has a subject line set.
  160. *
  161. * @return void
  162. */
  163. public function testSetSubjectPrependTextPreExisting()
  164. {
  165. list($mail, $writer, $log) = $this->_getSimpleLogger();
  166. // Expect a Zend_Log_Exception because the subject prepend text cannot
  167. // be set of the Zend_Mail object already has a subject line set.
  168. $this->setExpectedException('Zend_Log_Exception');
  169. // Set a subject line so the setSubjectPrependText() call triggers an
  170. // exception.
  171. $mail->setSubject('a pre-existing subject line');
  172. $writer->setSubjectPrependText('foo');
  173. }
  174. /**
  175. * Tests basic fluent interface for setting layout formatter.
  176. *
  177. * @return void
  178. */
  179. public function testSetLayoutFormatter()
  180. {
  181. list(, $writer) = $this->_getSimpleLogger(true);
  182. $return = $writer->setLayoutFormatter(new Zend_Log_Formatter_Simple());
  183. $this->assertSame($writer, $return);
  184. }
  185. /**
  186. * Tests that the layout formatter can be set and retrieved.
  187. *
  188. * @return void
  189. */
  190. public function testGetLayoutFormatter()
  191. {
  192. list(, $writer) = $this->_getSimpleLogger(true);
  193. $formatter = new Zend_Log_Formatter_Simple();
  194. // Ensure that fluent interface is present.
  195. $returnedWriter = $writer->setLayoutFormatter($formatter);
  196. $this->assertSame($writer, $returnedWriter);
  197. // Ensure that the getter returns the same formatter.
  198. $returnedFormatter = $writer->getLayoutFormatter();
  199. $this->assertSame($formatter, $returnedFormatter);
  200. }
  201. /**
  202. * Tests setting of the layout formatter when Zend_Layout is not being
  203. * used.
  204. *
  205. * @return void
  206. */
  207. public function testSetLayoutFormatterWithoutLayout()
  208. {
  209. list(, $writer) = $this->_getSimpleLogger();
  210. // If Zend_Layout is not being used, a formatter cannot be set for it.
  211. $this->setExpectedException('Zend_Log_Exception');
  212. $writer->setLayoutFormatter(new Zend_Log_Formatter_Simple());
  213. }
  214. /**
  215. * Tests destruction of the Zend_Log instance when an error message entry
  216. * is in place, but the mail can't be sent. Should result in a warning,
  217. * which we test for here.
  218. *
  219. * @return void
  220. */
  221. public function testDestructorMailError()
  222. {
  223. list($mail, $writer, $log) = $this->_getSimpleLogger(false);
  224. // Force the send() method to throw the same exception that would be
  225. // thrown if, say, the SMTP server couldn't be contacted.
  226. $mail->expects($this->any())
  227. ->method('send')
  228. ->will($this->throwException(new Zend_Mail_Transport_Exception()));
  229. // Log an error message so that there's something to send via email.
  230. $log->err('a bogus error message to force mail sending');
  231. $this->setExpectedException('PHPUnit_Framework_Error');
  232. unset($log);
  233. }
  234. /**
  235. * Tests destruction of the Zend_Log instance when an error message entry
  236. * is in place, but the layout can't be rendered. Should result in a
  237. * notice, which we test for here.
  238. *
  239. * @return void
  240. */
  241. public function testDestructorLayoutError()
  242. {
  243. list($mail, $writer, $log, $layout) = $this->_getSimpleLogger(true);
  244. // Force the render() method to throw the same exception that would
  245. // be thrown if, say, the layout template file couldn't be found.
  246. $layout->expects($this->any())
  247. ->method('render')
  248. ->will($this->throwException(new Zend_View_Exception('bogus message')));
  249. // Log an error message so that there's something to send via email.
  250. $log->err('a bogus error message to force mail sending');
  251. $this->setExpectedException('PHPUnit_Framework_Error');
  252. unset($log);
  253. }
  254. /**
  255. * @group ZF-8953
  256. */
  257. public function testFluentInterface()
  258. {
  259. require_once 'Zend/Log/Formatter/Simple.php';
  260. list(, $writer) = $this->_getSimpleLogger(true);
  261. $instance = $writer->setLayoutFormatter(new Zend_Log_Formatter_Simple())
  262. ->setSubjectPrependText('subject');
  263. $this->assertTrue($instance instanceof Zend_Log_Writer_Mail);
  264. }
  265. /**
  266. * @group ZF-9990
  267. */
  268. public function testFactory()
  269. {
  270. $config = array(
  271. 'from' => array(
  272. 'email' => 'log@test.framework.zend.com'
  273. ),
  274. 'to' => 'admin@domain.com',
  275. 'subject' => '[error] exceptions on my application'
  276. );
  277. $writer = Zend_Log_Writer_Mail::factory($config);
  278. $this->assertType('Zend_Log_Writer_Mail', $writer);
  279. $writer->write($this->_getEvent());
  280. $writer->shutdown();
  281. $this->assertEquals('admin@domain.com', $this->_transport->recipients);
  282. $this->assertContains('an info message', $this->_transport->body);
  283. $this->assertContains('From: log@test.framework.zend.com', $this->_transport->header);
  284. $this->assertContains('To: admin@domain.com', $this->_transport->header);
  285. $this->assertContains('Subject: [error] exceptions on my application', $this->_transport->header);
  286. }
  287. /**
  288. * @group ZF-9990
  289. */
  290. public function testFactoryShouldSetSubjectPrependText()
  291. {
  292. $config = array(
  293. 'subjectPrependText' => '[error] exceptions on my application'
  294. );
  295. $writer = Zend_Log_Writer_Mail::factory($config);
  296. $writer->write($this->_getEvent());
  297. $writer->shutdown();
  298. $this->assertContains('Subject: [error] exceptions on my application (INFO=1)', $this->_transport->header);
  299. }
  300. /**
  301. * @group ZF-9990
  302. */
  303. public function testFactoryShouldAcceptCustomMailClass()
  304. {
  305. $this->getMock('Zend_Mail', array(), array(), 'Zend_Stub_Mail_Custom');
  306. $config = array(
  307. 'class' => 'Zend_Stub_Mail_Custom'
  308. );
  309. $writer = Zend_Log_Writer_Mail::factory($config);
  310. $this->assertType('Zend_Log_Writer_Mail', $writer);
  311. }
  312. /**
  313. * @group ZF-9990
  314. */
  315. public function testFactoryShouldSetCharsetForMail()
  316. {
  317. $config = array(
  318. 'charset' => 'UTF-8'
  319. );
  320. $writer = Zend_Log_Writer_Mail::factory($config);
  321. $writer->write($this->_getEvent());
  322. $writer->shutdown();
  323. $this->assertContains('Content-Type: text/plain; charset=UTF-8', $this->_transport->header);
  324. }
  325. /**
  326. * @group ZF-9990
  327. */
  328. public function testFactoryShouldAllowToSetMultipleRecipientsInArray()
  329. {
  330. $config = array(
  331. 'to' => array(
  332. 'John Doe' => 'admin1@domain.com',
  333. 'admin2@domain.com'
  334. ),
  335. 'cc' => array(
  336. 'bug@domain.com',
  337. 'project' => 'projectname@domain.com'
  338. )
  339. );
  340. $writer = Zend_Log_Writer_Mail::factory($config);
  341. $writer->write($this->_getEvent());
  342. $writer->shutdown();
  343. $this->assertContains('admin1@domain.com', $this->_transport->recipients);
  344. $this->assertContains('admin2@domain.com', $this->_transport->recipients);
  345. $this->assertContains('bug@domain.com', $this->_transport->recipients);
  346. $this->assertContains('projectname@domain.com', $this->_transport->recipients);
  347. $this->assertContains('To: John Doe <admin1@domain.com>', $this->_transport->header);
  348. $this->assertContains('admin2@domain.com', $this->_transport->header);
  349. $this->assertContains('Cc: bug@domain.com', $this->_transport->header);
  350. $this->assertContains('project <projectname@domain.com>', $this->_transport->header);
  351. }
  352. /**
  353. * @group ZF-9990
  354. */
  355. public function testFactoryWithLayout()
  356. {
  357. $config = array(
  358. 'layoutOptions' => array(
  359. 'layoutPath' => dirname(__FILE__) . '/_files'
  360. )
  361. );
  362. $writer = Zend_Log_Writer_Mail::factory($config);
  363. $writer->write($this->_getEvent());
  364. $writer->shutdown();
  365. $this->assertFalse(empty($this->_transport->boundary));
  366. $this->assertContains('Content-Type: multipart/', $this->_transport->header);
  367. $this->assertContains('boundary=', $this->_transport->header);
  368. $this->assertContains('Content-Type: text/plain', $this->_transport->body);
  369. $this->assertContains('Content-Type: text/html', $this->_transport->body);
  370. $this->assertContains($this->_transport->boundary, $this->_transport->body);
  371. $this->assertEquals(2, substr_count($this->_transport->body, 'an info message'));
  372. }
  373. /**
  374. * @group ZF-9990
  375. */
  376. public function testFactoryShouldSetLayoutFormatter()
  377. {
  378. $config = array(
  379. 'layoutOptions' => array(
  380. 'layoutPath' => '/path/to/layout/scripts'
  381. ),
  382. 'layoutFormatter' => 'Zend_Log_Formatter_Simple'
  383. );
  384. $writer = Zend_Log_Writer_Mail::factory($config);
  385. $this->assertType('Zend_Log_Formatter_Simple', $writer->getLayoutFormatter());
  386. }
  387. /**
  388. * @group ZF-9990
  389. */
  390. public function testFactoryWithCustomLayoutClass()
  391. {
  392. $this->getMock('Zend_Layout', null, array(), 'Zend_Stub_Layout_Custom');
  393. $config = array(
  394. 'layout' => 'Zend_Stub_Layout_Custom'
  395. );
  396. $writer = Zend_Log_Writer_Mail::factory($config);
  397. $this->assertType('Zend_Log_Writer_Mail', $writer);
  398. }
  399. /**
  400. * Returns an array of the Zend_Mail mock object, Zend_Log_Writer_Mail
  401. * object, and Zend_Log objects.
  402. *
  403. * This is just a helper function for the various test methods above.
  404. *
  405. * @return array Numerically indexed array of Zend_Mail,
  406. * Zend_Log_Writer_Mail, Zend_Log, and Zend_Layout objects,
  407. * in that order.
  408. */
  409. protected function _getSimpleLogger($useLayout = false)
  410. {
  411. // Get a mock object for Zend_Mail so that no emails are actually
  412. // sent.
  413. $mail = $this->getMock('Zend_Mail', array('send'));
  414. // The send() method can be called any number of times.
  415. $mail->expects($this->any())
  416. ->method('send');
  417. $mail->addTo('zend_log_writer_mail_test@example.org');
  418. $mail->setFrom('zend_log_writer_mail_test@example.org');
  419. // Setup a mock object for Zend_Layout because we can't rely on any
  420. // layout files being in place.
  421. if ($useLayout) {
  422. $layout = $this->getMock('Zend_Layout', array('render'));
  423. $writer = new Zend_Log_Writer_Mail($mail, $layout);
  424. } else {
  425. $writer = new Zend_Log_Writer_Mail($mail);
  426. $layout = null;
  427. }
  428. $log = new Zend_Log();
  429. $log->addWriter($writer);
  430. return array($mail, $writer, $log, $layout);
  431. }
  432. /**
  433. * Returns a sample of an event
  434. *
  435. * @return array
  436. */
  437. protected function _getEvent()
  438. {
  439. return array(
  440. 'timestamp' => date('c'),
  441. 'message' => 'an info message',
  442. 'priority' => 6,
  443. 'priorityName' => 'INFO'
  444. );
  445. }
  446. }
  447. // Call Zend_Log_Writer_MailTest::main() if this source file is executed directly.
  448. if (PHPUnit_MAIN_METHOD == "Zend_Log_Writer_MailTest::main") {
  449. Zend_Log_Writer_MailTest::main();
  450. }