PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/Zend/Log/Writer/MailTest.php

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