MailTest.php 16 KB

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