PageRenderTime 38ms CodeModel.GetById 13ms app.highlight 17ms RepoModel.GetById 2ms app.codeStats 0ms

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