PageRenderTime 13ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Zend/Log/Writer/FirebugTest.php

https://bitbucket.org/ksekar/campus
PHP | 421 lines | 263 code | 89 blank | 69 comment | 13 complexity | 698a10809597cce89a8ed446453724f9 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: FirebugTest.php 24594 2012-01-05 21:27:01Z matthew $
 21 */
 22
 23if (!defined('PHPUnit_MAIN_METHOD')) {
 24    define('PHPUnit_MAIN_METHOD', 'Zend_Log_Writer_FirebugTest::main');
 25}
 26
 27/** Zend_Log */
 28require_once 'Zend/Log.php';
 29
 30/** Zend_Log_Writer_Firebug */
 31require_once 'Zend/Log/Writer/Firebug.php';
 32
 33/** Zend_Log_Formatter_Firebug */
 34require_once 'Zend/Log/Formatter/Firebug.php';
 35
 36/** Zend_Wildfire_Channel_HttpHeaders */
 37require_once 'Zend/Wildfire/Channel/HttpHeaders.php';
 38
 39/** Zend_Wildfire_Plugin_FirePhp */
 40require_once 'Zend/Wildfire/Plugin/FirePhp.php';
 41
 42/** Zend_Controller_Request_Http */
 43require_once 'Zend/Controller/Request/Http.php';
 44
 45/** Zend_Controller_Response_Http */
 46require_once 'Zend/Controller/Response/Http.php';
 47
 48/**
 49 * @category   Zend
 50 * @package    Zend_Log
 51 * @subpackage UnitTests
 52 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 53 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 54 * @group      Zend_Log
 55 */
 56class Zend_Log_Writer_FirebugTest extends PHPUnit_Framework_TestCase
 57{
 58    protected $_controller = null;
 59    protected $_request = null;
 60    protected $_response = null;
 61    protected $_writer = null;
 62    protected $_logger = null;
 63
 64    /**
 65     * Runs the test methods of this class.
 66     *
 67     * @access public
 68     * @static
 69     */
 70    public static function main()
 71    {
 72        $suite  = new PHPUnit_Framework_TestSuite(__CLASS__);
 73        $result = PHPUnit_TextUI_TestRunner::run($suite);
 74    }
 75
 76    public function setUp()
 77    {
 78        date_default_timezone_set('America/Los_Angeles');
 79
 80        // Reset front controller to reset registered plugins and
 81        // registered request/response objects
 82        Zend_Controller_Front::getInstance()->resetInstance();
 83
 84        $this->_request = new Zend_Log_Writer_FirebugTest_Request();
 85        $this->_response = new Zend_Log_Writer_FirebugTest_Response();
 86
 87        $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
 88        $channel->setRequest($this->_request);
 89        $channel->setResponse($this->_response);
 90
 91        $this->_writer = new Zend_Log_Writer_Firebug();
 92
 93        // Explicitly enable writer as it is disabled by default
 94        // when running from the command line.
 95        $this->_writer->setEnabled(true);
 96
 97        $this->_logger = new Zend_Log($this->_writer);
 98
 99        Zend_Wildfire_Plugin_FirePhp::getInstance()->setOption('includeLineNumbers', false);
100    }
101
102    public function tearDown()
103    {
104        Zend_Wildfire_Channel_HttpHeaders::destroyInstance();
105        Zend_Wildfire_Plugin_FirePhp::destroyInstance();
106    }
107
108
109    /**
110     * Test for ZF-3960
111     *
112     * Zend_Log_Writer_Firebug should be automatically disabled when
113     * run from the command line
114     */
115    public function testZf3960()
116    {
117        Zend_Wildfire_Channel_HttpHeaders::destroyInstance();
118        Zend_Wildfire_Plugin_FirePhp::destroyInstance();
119
120        $log = new Zend_Log();
121        $writerFirebug = new Zend_Log_Writer_Firebug();
122        $log->addWriter($writerFirebug);
123        $log->log('hi', 2);
124    }
125
126    /**
127     * @group ZF-4952
128     */
129    public function testSetFormatter()
130    {
131        $firephp = Zend_Wildfire_Plugin_FirePhp::getInstance();
132        $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
133        $protocol = $channel->getProtocol(Zend_Wildfire_Plugin_FirePhp::PROTOCOL_URI);
134
135        $this->_logger->log('Test Message 1', Zend_Log::INFO);
136
137        $formatter = new Zend_Log_Writer_FirebugTest_Formatter();
138        $this->_writer->setFormatter($formatter);
139
140        $this->_logger->setEventItem('testLabel','Test Label');
141
142        $this->_logger->log('Test Message 2', Zend_Log::INFO);
143
144        $messages = $protocol->getMessages();
145
146        $message = $messages[Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE]
147                            [Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI]
148                            [0];
149
150        $this->assertEquals($message,
151                            '[{"Type":"INFO"},"Test Message 1"]');
152
153        $message = $messages[Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE]
154                            [Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI]
155                            [1];
156
157        $this->assertEquals($message,
158                            '[{"Type":"INFO"},"Test Label : Test Message 2"]');
159    }
160
161    /**
162     * @group ZF-4952
163     */
164    public function testEventItemLabel()
165    {
166        $firephp = Zend_Wildfire_Plugin_FirePhp::getInstance();
167        $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
168        $protocol = $channel->getProtocol(Zend_Wildfire_Plugin_FirePhp::PROTOCOL_URI);
169
170
171        $this->_logger->log('Test Message 1', Zend_Log::INFO);
172
173        $this->_logger->setEventItem('firebugLabel','Test Label');
174
175        $this->_logger->log('Test Message 2', Zend_Log::INFO);
176
177        $messages = $protocol->getMessages();
178
179        $message = $messages[Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE]
180                            [Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI]
181                            [0];
182
183        $this->assertEquals($message,
184                            '[{"Type":"INFO"},"Test Message 1"]');
185
186        $message = $messages[Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE]
187                            [Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI]
188                            [1];
189
190        $this->assertEquals($message,
191                            '[{"Type":"INFO","Label":"Test Label"},"Test Message 2"]');
192    }
193
194    public function testLogStyling()
195    {
196        $this->assertEquals($this->_writer->getDefaultPriorityStyle(),
197                            Zend_Wildfire_Plugin_FirePhp::LOG);
198        $this->assertEquals($this->_writer->setDefaultPriorityStyle(Zend_Wildfire_Plugin_FirePhp::WARN),
199                            Zend_Wildfire_Plugin_FirePhp::LOG);
200        $this->assertEquals($this->_writer->getDefaultPriorityStyle(),
201                            Zend_Wildfire_Plugin_FirePhp::WARN);
202
203        $this->assertEquals($this->_writer->getPriorityStyle(9),
204                            false);
205        $this->assertEquals($this->_writer->setPriorityStyle(9,Zend_Wildfire_Plugin_FirePhp::WARN),
206                            true);
207        $this->assertEquals($this->_writer->getPriorityStyle(9),
208                            Zend_Wildfire_Plugin_FirePhp::WARN);
209        $this->assertEquals($this->_writer->setPriorityStyle(9,Zend_Wildfire_Plugin_FirePhp::LOG),
210                            Zend_Wildfire_Plugin_FirePhp::WARN);
211    }
212
213    public function testBasicLogging()
214    {
215        $message = 'This is a log message!';
216
217        $this->_logger->log($message, Zend_Log::INFO);
218
219        Zend_Wildfire_Channel_HttpHeaders::getInstance()->flush();
220
221        $headers = array();
222        $headers['X-Wf-Protocol-1'] = 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2';
223        $headers['X-Wf-1-Structure-1'] = 'http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1';
224        $headers['X-Wf-1-Plugin-1'] = 'http://meta.firephp.org/Wildfire/Plugin/ZendFramework/FirePHP/1.6.2';
225        $headers['X-Wf-1-1-1-1'] = '42|[{"Type":"INFO"},"This is a log message!"]|';
226
227        $this->assertTrue($this->_response->verifyHeaders($headers));
228    }
229
230
231    /**
232     * @group ZF-4934
233     */
234    public function testAdvancedLogging()
235    {
236        Zend_Wildfire_Plugin_FirePhp::getInstance()->setOption('maxTraceDepth',0);
237
238        $message = 'This is a log message!';
239        $label = 'Test Label';
240        $table = array('Summary line for the table',
241                       array(
242                           array('Column 1', 'Column 2'),
243                           array('Row 1 c 1',' Row 1 c 2'),
244                           array('Row 2 c 1',' Row 2 c 2')
245                       )
246                      );
247
248
249        $this->_logger->addPriority('TRACE', 8);
250        $this->_logger->addPriority('TABLE', 9);
251        $this->_writer->setPriorityStyle(8, 'TRACE');
252        $this->_writer->setPriorityStyle(9, 'TABLE');
253
254        $this->_logger->trace($message);
255        $this->_logger->table($table);
256
257        try {
258          throw new Exception('Test Exception');
259        } catch (Exception $e) {
260          $this->_logger->err($e);
261        }
262
263        try {
264            Zend_Wildfire_Plugin_FirePhp::send($message, $label, 'UNKNOWN');
265            $this->fail('Should not be able to log with undefined log style');
266        } catch (Exception $e) {
267            // success
268        }
269
270        $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
271        $protocol = $channel->getProtocol(Zend_Wildfire_Plugin_FirePhp::PROTOCOL_URI);
272
273        $messages = array(Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE=>
274                          array(Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI=>
275                                array(1=>'[{"Type":"TABLE"},["Summary line for the table",[["Column 1","Column 2"],["Row 1 c 1"," Row 1 c 2"],["Row 2 c 1"," Row 2 c 2"]]]]')));
276
277        $qued_messages = $protocol->getMessages();
278
279        unset($qued_messages[Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE][Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI][0]);
280        unset($qued_messages[Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE][Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI][2]);
281
282        $this->assertEquals(serialize($qued_messages),
283                            serialize($messages));
284    }
285
286    public function testFactory()
287    {
288        $cfg = array('log' => array('memory' => array(
289            'writerName' => "Firebug"
290        )));
291
292        $logger = Zend_Log::factory($cfg['log']);
293        $this->assertTrue($logger instanceof Zend_Log);
294    }
295
296    /**
297     * @group ZF-10537
298     */
299    public function testFileLineOffsets()
300    {
301        $firephp = Zend_Wildfire_Plugin_FirePhp::getInstance();
302        $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
303        $protocol = $channel->getProtocol(Zend_Wildfire_Plugin_FirePhp::PROTOCOL_URI);
304        $firephp->setOption('includeLineNumbers', true);
305        $firephp->setOption('maxTraceDepth', 0);
306
307        $lines = array();
308        // NOTE: Do NOT separate the following pairs otherwise the line numbers will not match for the test
309
310        // Message number: 1
311        $lines[] = __LINE__+1;
312        $this->_logger->log('Hello World', Zend_Log::INFO);
313
314        // Message number: 2
315        $this->_logger->addPriority('TRACE', 8);
316        $this->_writer->setPriorityStyle(8, 'TRACE');
317        $lines[] = __LINE__+1;
318        $this->_logger->trace('Trace to here');
319
320        // Message number: 3
321        $this->_logger->addPriority('TABLE', 9);
322        $this->_writer->setPriorityStyle(9, 'TABLE');
323        $table = array('Summary line for the table',
324                       array(
325                           array('Column 1', 'Column 2'),
326                           array('Row 1 c 1',' Row 1 c 2'),
327                           array('Row 2 c 1',' Row 2 c 2')
328                       )
329                      );
330        $lines[] = __LINE__+1;
331        $this->_logger->table($table);
332
333        // Message number: 4
334        $lines[] = __LINE__+1;
335        $this->_logger->info('Hello World');
336
337        $messages = $protocol->getMessages();
338        $messages = $messages[Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE][Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI];
339
340        for( $i=0 ; $i<sizeof($messages) ; $i++ ) {
341            if(!preg_match_all('/FirebugTest\.php","Line":' . $lines[$i] . '/', $messages[$i], $m)) {
342                $this->fail("File and line does not match for message number: " . ($i+1));
343            }
344
345        }
346    }
347}
348
349class Zend_Log_Writer_FirebugTest_Formatter extends Zend_Log_Formatter_Firebug
350{
351    public function format($event)
352    {
353        return $event['testLabel'].' : '.$event['message'];
354    }
355}
356
357
358class Zend_Log_Writer_FirebugTest_Request extends Zend_Controller_Request_Http
359{
360    public function getHeader($header)
361    {
362        if ($header == 'User-Agent') {
363            return 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14 FirePHP/0.1.0';
364        }
365    }
366}
367
368
369class Zend_Log_Writer_FirebugTest_Response extends Zend_Controller_Response_Http
370{
371
372    public function canSendHeaders($throw = false)
373    {
374        return true;
375    }
376
377    public function verifyHeaders($headers)
378    {
379
380        $response_headers = $this->getHeaders();
381        if (!$response_headers) {
382            return false;
383        }
384
385        $keys1 = array_keys($headers);
386        sort($keys1);
387        $keys1 = serialize($keys1);
388
389        $keys2 = array();
390        foreach ($response_headers as $header ) {
391            $keys2[] = $header['name'];
392        }
393        sort($keys2);
394        $keys2 = serialize($keys2);
395
396        if ($keys1 != $keys2) {
397            return false;
398        }
399
400        $values1 = array_values($headers);
401        sort($values1);
402        $values1 = serialize($values1);
403
404        $values2 = array();
405        foreach ($response_headers as $header ) {
406            $values2[] = $header['value'];
407        }
408        sort($values2);
409        $values2 = serialize($values2);
410
411        if ($values1 != $values2) {
412            return false;
413        }
414
415        return true;
416    }
417}
418
419if (PHPUnit_MAIN_METHOD == 'Zend_Log_Writer_FirebugTest::main') {
420    Zend_Log_Writer_FirebugTest::main();
421}