WildfireTest.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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_Wildfire
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 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. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Wildfire_WildfireTest::main');
  24. }
  25. require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
  26. /** Zend_Wildfire_Channel_HttpHeaders */
  27. require_once 'Zend/Wildfire/Channel/HttpHeaders.php';
  28. /** Zend_Wildfire_Plugin_FirePhp */
  29. require_once 'Zend/Wildfire/Plugin/FirePhp.php';
  30. /** Zend_Wildfire_Plugin_FirePhp_Message */
  31. require_once 'Zend/Wildfire/Plugin/FirePhp/Message.php';
  32. /** Zend_Wildfire_Plugin_FirePhp_TableMessage */
  33. require_once 'Zend/Wildfire/Plugin/FirePhp/TableMessage.php';
  34. /** Zend_Controller_Request_HttpTestCase */
  35. require_once 'Zend/Controller/Request/HttpTestCase.php';
  36. /** Zend_Controller_Response_Http */
  37. require_once 'Zend/Controller/Response/HttpTestCase.php';
  38. /** Zend_Controller_Front **/
  39. require_once 'Zend/Controller/Front.php';
  40. /** Zend_Json */
  41. require_once 'Zend/Json.php';
  42. /** Zend_Json_Encoder */
  43. require_once 'Zend/Json/Encoder.php';
  44. /**
  45. * @category Zend
  46. * @package Zend_Wildfire
  47. * @subpackage UnitTests
  48. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  49. * @license http://framework.zend.com/license/new-bsd New BSD License
  50. */
  51. class Zend_Wildfire_WildfireTest extends PHPUnit_Framework_TestCase
  52. {
  53. protected $_controller = null;
  54. protected $_request = null;
  55. protected $_response = null;
  56. /**
  57. * Runs the test methods of this class.
  58. *
  59. * @access public
  60. * @static
  61. */
  62. public static function main()
  63. {
  64. $suite = new PHPUnit_Framework_TestSuite("Zend_Wildfire_WildfireTest");
  65. $result = PHPUnit_TextUI_TestRunner::run($suite);
  66. }
  67. public function setUp()
  68. {
  69. date_default_timezone_set('America/Los_Angeles');
  70. Zend_Wildfire_Channel_HttpHeaders::destroyInstance();
  71. Zend_Wildfire_Plugin_FirePhp::destroyInstance();
  72. }
  73. public function tearDown()
  74. {
  75. Zend_Controller_Front::getInstance()->resetInstance();
  76. Zend_Wildfire_Channel_HttpHeaders::destroyInstance();
  77. Zend_Wildfire_Plugin_FirePhp::destroyInstance();
  78. }
  79. protected function _setupWithFrontController()
  80. {
  81. $this->_request = new Zend_Wildfire_WildfireTest_Request();
  82. $this->_response = new Zend_Wildfire_WildfireTest_Response();
  83. $this->_controller = Zend_Controller_Front::getInstance();
  84. $this->_controller->resetInstance();
  85. $this->_controller->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files')
  86. ->setRequest($this->_request)
  87. ->setResponse($this->_response)
  88. ->setParam('noErrorHandler', true)
  89. ->setParam('noViewRenderer', true)
  90. ->throwExceptions(false);
  91. Zend_Wildfire_Plugin_FirePhp::getInstance()->setOption('includeLineNumbers', false);
  92. $this->_request->setUserAgentExtensionEnabled(true);
  93. }
  94. protected function _setupWithoutFrontController($ModifyOptions=true)
  95. {
  96. $this->_request = new Zend_Wildfire_WildfireTest_Request();
  97. $this->_response = new Zend_Wildfire_WildfireTest_Response();
  98. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  99. $channel->setRequest($this->_request);
  100. $channel->setResponse($this->_response);
  101. if ($ModifyOptions) {
  102. Zend_Wildfire_Plugin_FirePhp::getInstance()->setOption('includeLineNumbers', false);
  103. }
  104. $this->_request->setUserAgentExtensionEnabled(true);
  105. }
  106. public function testNoResponseObject()
  107. {
  108. Zend_Wildfire_Plugin_FirePhp::getInstance()->setEnabled(false);
  109. Zend_Wildfire_Plugin_FirePhp::send('Hello World');
  110. Zend_Wildfire_Plugin_FirePhp::getInstance()->setEnabled(true);
  111. try {
  112. Zend_Wildfire_Plugin_FirePhp::send('Hello World');
  113. $this->fail('Should throw a response object not initialized error');
  114. } catch (Exception $e) {
  115. // success
  116. }
  117. }
  118. public function testIsReady1()
  119. {
  120. $this->_setupWithFrontController();
  121. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  122. $this->assertTrue($channel->isReady(true));
  123. $this->_request->setUserAgentExtensionEnabled(false);
  124. $this->assertFalse($channel->isReady(true));
  125. }
  126. public function testIsReady2()
  127. {
  128. $this->_setupWithoutFrontController();
  129. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  130. $this->assertTrue($channel->isReady());
  131. $this->_request->setUserAgentExtensionEnabled(false);
  132. $this->assertFalse($channel->isReady());
  133. }
  134. public function testFirePhpPluginInstanciation()
  135. {
  136. $this->_setupWithoutFrontController();
  137. try {
  138. Zend_Wildfire_Plugin_FirePhp::getInstance();
  139. Zend_Wildfire_Plugin_FirePhp::init(null);
  140. $this->fail('Should not be able to re-initialize');
  141. } catch (Exception $e) {
  142. // success
  143. }
  144. }
  145. public function testFirePhpPluginEnablement()
  146. {
  147. $this->_setupWithoutFrontController();
  148. $firephp = Zend_Wildfire_Plugin_FirePhp::getInstance();
  149. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  150. $protocol = $channel->getProtocol(Zend_Wildfire_Plugin_FirePhp::PROTOCOL_URI);
  151. $this->assertFalse($protocol->getMessages());
  152. $this->assertTrue($firephp->getEnabled());
  153. $this->assertTrue($firephp->send('Hello World'));
  154. $messages = array(Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE=>
  155. array(Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI=>
  156. array('[{"Type":"LOG"},"Hello World"]')));
  157. $this->assertEquals(serialize($protocol->getMessages()),
  158. serialize($messages));
  159. $this->assertTrue($firephp->setEnabled(false));
  160. $this->assertFalse($firephp->send('Hello World'));
  161. $this->assertFalse($protocol->getMessages());
  162. }
  163. public function testSetControllerPluginStackIndex1()
  164. {
  165. $this->_setupWithoutFrontController();
  166. $controller = Zend_Controller_Front::getInstance();
  167. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  168. $firephp = Zend_Wildfire_Plugin_FirePhp::getInstance();
  169. $firephp->send('Hello World');
  170. $plugins = $controller->getPlugins();
  171. $this->assertEquals($plugins[999],$channel);
  172. }
  173. public function testSetControllerPluginStackIndex2()
  174. {
  175. $this->_setupWithoutFrontController(false);
  176. $controller = Zend_Controller_Front::getInstance();
  177. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  178. Zend_Wildfire_Channel_HttpHeaders::setControllerPluginStackIndex(99);
  179. $firephp = Zend_Wildfire_Plugin_FirePhp::getInstance();
  180. $firephp->send('Hello World');
  181. $plugins = $controller->getPlugins();
  182. $this->assertEquals($plugins[99],$channel);
  183. }
  184. public function testBasicLogging1()
  185. {
  186. $this->_setupWithoutFrontController();
  187. $firephp = Zend_Wildfire_Plugin_FirePhp::getInstance();
  188. $message = 'This is a log message!';
  189. $firephp->send($message);
  190. Zend_Wildfire_Channel_HttpHeaders::getInstance()->flush();
  191. $headers = array();
  192. $headers['X-Wf-Protocol-1'] = 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2';
  193. $headers['X-Wf-1-Structure-1'] = 'http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1';
  194. $headers['X-Wf-1-Plugin-1'] = 'http://meta.firephp.org/Wildfire/Plugin/ZendFramework/FirePHP/1.6.2';
  195. $headers['X-Wf-1-1-1-1'] = '41|[{"Type":"LOG"},"This is a log message!"]|';
  196. $this->assertTrue($this->_response->verifyHeaders($headers));
  197. }
  198. public function testBasicLogging2()
  199. {
  200. $this->_setupWithFrontController();
  201. $firephp = Zend_Wildfire_Plugin_FirePhp::getInstance();
  202. $message = 'This is a log message!';
  203. $firephp->send($message);
  204. $this->_controller->dispatch();
  205. $headers = array();
  206. $headers['X-Wf-Protocol-1'] = 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2';
  207. $headers['X-Wf-1-Structure-1'] = 'http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1';
  208. $headers['X-Wf-1-Plugin-1'] = 'http://meta.firephp.org/Wildfire/Plugin/ZendFramework/FirePHP/1.6.2';
  209. $headers['X-Wf-1-1-1-1'] = '41|[{"Type":"LOG"},"This is a log message!"]|';
  210. $this->assertTrue($this->_response->verifyHeaders($headers));
  211. }
  212. /**
  213. * At this point UTF-8 characters are not supported as Zend_Json_Decoder
  214. * does not support them.
  215. * Once ZF-4054 is resolved this test must be updated.
  216. */
  217. public function testUTF8Logging()
  218. {
  219. $this->_setupWithFrontController();
  220. $firephp = Zend_Wildfire_Plugin_FirePhp::getInstance();
  221. $encodedMessage = $message = 'Отладочный';
  222. if (function_exists('json_encode') && Zend_Json::$useBuiltinEncoderDecoder !== true) {
  223. $encodedMessage = '\u041e\u0442\u043b\u0430\u0434\u043e\u0447\u043d\u044b\u0439';
  224. }
  225. $firephp->send($message);
  226. $this->_controller->dispatch();
  227. $headers = array();
  228. $headers['X-Wf-Protocol-1'] = 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2';
  229. $headers['X-Wf-1-Structure-1'] = 'http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1';
  230. $headers['X-Wf-1-Plugin-1'] = 'http://meta.firephp.org/Wildfire/Plugin/ZendFramework/FirePHP/1.6.2';
  231. $headers['X-Wf-1-1-1-1'] = (strlen($encodedMessage)+19).'|[{"Type":"LOG"},"'.$encodedMessage.'"]|';
  232. $this->assertTrue($this->_response->verifyHeaders($headers));
  233. }
  234. public function testCyclicalObjectLogging()
  235. {
  236. $this->_setupWithFrontController();
  237. $firephp = Zend_Wildfire_Plugin_FirePhp::getInstance();
  238. $obj1 = new Zend_Wildfire_WildfireTest_JsonEncodingTestClass();
  239. $obj2 = new Zend_Wildfire_WildfireTest_JsonEncodingTestClass();
  240. $obj1->child = $obj2;
  241. $obj2->child = $obj1;
  242. $firephp->send($obj1);
  243. $this->_controller->dispatch();
  244. $headers = array();
  245. $headers['X-Wf-Protocol-1'] = 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2';
  246. $headers['X-Wf-1-Structure-1'] = 'http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1';
  247. $headers['X-Wf-1-Plugin-1'] = 'http://meta.firephp.org/Wildfire/Plugin/ZendFramework/FirePHP/1.6.2';
  248. $headers['X-Wf-1-1-1-1'] = '257|[{"Type":"LOG"},{"__className":"Zend_Wildfire_WildfireTest_JsonEncodingTestClass","undeclared:child":{"__className":"Zend_Wildfire_WildfireTest_JsonEncodingTestClass","undeclared:child":"** Recursion (Zend_Wildfire_WildfireTest_JsonEncodingTestClass) **"}}]|';
  249. $this->assertTrue($this->_response->verifyHeaders($headers));
  250. }
  251. /**
  252. * @group ZF-4927
  253. */
  254. public function testAdvancedLogging()
  255. {
  256. Zend_Wildfire_Plugin_FirePhp::getInstance()->setOption('maxTraceDepth',0);
  257. $this->_setupWithoutFrontController();
  258. $firephp = Zend_Wildfire_Plugin_FirePhp::getInstance();
  259. $message = 'This is a log message!';
  260. $label = 'Test Label';
  261. $table = array('Summary line for the table',
  262. array(
  263. array('Column 1', 'Column 2'),
  264. array('Row 1 c 1',' Row 1 c 2'),
  265. array('Row 2 c 1',' Row 2 c 2')
  266. )
  267. );
  268. $firephp->send($message, null, 'TRACE');
  269. $firephp->send($table, null, 'TABLE');
  270. Zend_Wildfire_Plugin_FirePhp::send($message, $label);
  271. Zend_Wildfire_Plugin_FirePhp::send($message, $label, Zend_Wildfire_Plugin_FirePhp::DUMP);
  272. try {
  273. throw new Exception('Test Exception');
  274. } catch (Exception $e) {
  275. Zend_Wildfire_Plugin_FirePhp::send($e);
  276. }
  277. try {
  278. Zend_Wildfire_Plugin_FirePhp::send($message, $label, 'UNKNOWN');
  279. $this->fail('Should not be able to log with undefined log style');
  280. } catch (Exception $e) {
  281. // success
  282. }
  283. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  284. $protocol = $channel->getProtocol(Zend_Wildfire_Plugin_FirePhp::PROTOCOL_URI);
  285. $messages = array(Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE=>
  286. array(Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI=>
  287. 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"]]]]',
  288. 2=>'[{"Type":"LOG","Label":"Test Label"},"This is a log message!"]')),
  289. Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_DUMP=>
  290. array(Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI=>
  291. array('{"Test Label":"This is a log message!"}')));
  292. $qued_messages = $protocol->getMessages();
  293. unset($qued_messages[Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE][Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI][0]);
  294. unset($qued_messages[Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE][Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI][3]);
  295. $this->assertEquals(serialize($qued_messages),
  296. serialize($messages));
  297. }
  298. public function testMessage()
  299. {
  300. $this->_setupWithoutFrontController();
  301. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  302. $protocol = $channel->getProtocol(Zend_Wildfire_Plugin_FirePhp::PROTOCOL_URI);
  303. $message = new Zend_Wildfire_Plugin_FirePhp_Message(Zend_Wildfire_Plugin_FirePhp::LOG, 'Message 1');
  304. $this->assertEquals($message->getStyle(), Zend_Wildfire_Plugin_FirePhp::LOG);
  305. $message->setStyle(Zend_Wildfire_Plugin_FirePhp::INFO);
  306. $this->assertEquals($message->getStyle(), Zend_Wildfire_Plugin_FirePhp::INFO);
  307. $this->assertNull($message->getLabel());
  308. $message->setLabel('Label 1');
  309. $this->assertEquals($message->getLabel(), 'Label 1');
  310. $this->assertFalse($message->getDestroy());
  311. $message->setDestroy(true);
  312. $this->assertTrue($message->getDestroy());
  313. $this->assertEquals($message->getMessage(), 'Message 1');
  314. $message->setMessage('Message 2');
  315. $this->assertEquals($message->getMessage(), 'Message 2');
  316. $message->setDestroy(true);
  317. $this->assertfalse(Zend_Wildfire_Plugin_FirePhp::send($message));
  318. $message->setDestroy(false);
  319. $this->assertTrue(Zend_Wildfire_Plugin_FirePhp::send($message));
  320. Zend_Wildfire_Channel_HttpHeaders::getInstance()->flush();
  321. $messages = $protocol->getMessages();
  322. $this->assertEquals($messages[Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE]
  323. [Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI][0],
  324. '[{"Type":"INFO","Label":"Label 1"},"Message 2"]');
  325. }
  326. /**
  327. * @group ZF-5742
  328. */
  329. public function testTableMessage()
  330. {
  331. $table = new Zend_Wildfire_Plugin_FirePhp_TableMessage('TestMessage');
  332. $this->assertEquals($table->getLabel(), 'TestMessage');
  333. try {
  334. $table->getLastRow();
  335. $this->fail('Should throw exception when no rows exist');
  336. } catch (Exception $e) {
  337. // success
  338. }
  339. $row = array('col1','col2');
  340. $this->assertEquals($table->getRowCount(), 0);
  341. try {
  342. $table->getRowAt(1);
  343. $this->fail('Should throw exception as no rows present');
  344. } catch (Exception $e) {
  345. // success
  346. }
  347. try {
  348. $table->setRowAt(1,array());
  349. $this->fail('Should throw exception as no rows present');
  350. } catch (Exception $e) {
  351. // success
  352. }
  353. $table->addRow($row);
  354. $this->assertEquals($table->getMessage(), array($row));
  355. $this->assertEquals($table->getLastRow(), $row);
  356. $this->assertEquals($table->getRowCount(), 1);
  357. $table->addRow($row);
  358. $this->assertEquals($table->getMessage(), array($row,
  359. $row));
  360. $this->assertEquals($table->getRowCount(), 2);
  361. $this->assertEquals($table->getLastRow(), $row);
  362. try {
  363. $table->getRowAt(2);
  364. $this->fail('Should throw exception as index is out of bounds');
  365. } catch (Exception $e) {
  366. // success
  367. }
  368. try {
  369. $table->setRowAt(2,array());
  370. $this->fail('Should throw exception as index is out of bounds');
  371. } catch (Exception $e) {
  372. // success
  373. }
  374. $rowOld = $table->getRowAt(1);
  375. $this->assertEquals($rowOld, $row);
  376. $rowOld[1] = 'Column2';
  377. $table->setRowAt(1, $rowOld);
  378. $this->assertEquals($table->getRowAt(1), $rowOld);
  379. $this->assertEquals($table->getLastRow(), $rowOld);
  380. $this->assertEquals($table->getMessage(), array($row,
  381. $rowOld));
  382. $header = array('hc1', 'hc2');
  383. $table->setHeader($header);
  384. $this->assertEquals($table->getMessage(), array($header,
  385. $row,
  386. $rowOld));
  387. }
  388. public function testMessageGroups()
  389. {
  390. $this->_setupWithFrontController();
  391. Zend_Wildfire_Plugin_FirePhp::group('Test Group');
  392. Zend_Wildfire_Plugin_FirePhp::send('Test Message');
  393. Zend_Wildfire_Plugin_FirePhp::groupEnd();
  394. $this->_controller->dispatch();
  395. $headers = array();
  396. $headers['X-Wf-Protocol-1'] = 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2';
  397. $headers['X-Wf-1-Structure-1'] = 'http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1';
  398. $headers['X-Wf-1-Plugin-1'] = 'http://meta.firephp.org/Wildfire/Plugin/ZendFramework/FirePHP/1.6.2';
  399. $headers['X-Wf-1-1-1-1'] = '50|[{"Type":"GROUP_START","Label":"Test Group"},null]|';
  400. $headers['X-Wf-1-1-1-2'] = '31|[{"Type":"LOG"},"Test Message"]|';
  401. $headers['X-Wf-1-1-1-3'] = '27|[{"Type":"GROUP_END"},null]|';
  402. $this->assertTrue($this->_response->verifyHeaders($headers));
  403. }
  404. public function testMessageComparison()
  405. {
  406. $label = 'Message';
  407. $message1 = new Zend_Wildfire_Plugin_FirePhp_Message(Zend_Wildfire_Plugin_FirePhp::LOG, $label);
  408. $message2 = new Zend_Wildfire_Plugin_FirePhp_Message(Zend_Wildfire_Plugin_FirePhp::LOG, $label);
  409. $this->assertNotEquals($message1,$message2);
  410. }
  411. public function testBufferedMessage1()
  412. {
  413. $this->_setupWithoutFrontController();
  414. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  415. $protocol = $channel->getProtocol(Zend_Wildfire_Plugin_FirePhp::PROTOCOL_URI);
  416. $message = new Zend_Wildfire_Plugin_FirePhp_Message(Zend_Wildfire_Plugin_FirePhp::LOG, 'Message 1');
  417. $this->assertFalse($message->setBuffered(true));
  418. Zend_Wildfire_Plugin_FirePhp::send($message);
  419. $this->assertFalse($protocol->getMessages());
  420. $message->setMessage('Message 2');
  421. Zend_Wildfire_Channel_HttpHeaders::getInstance()->flush();
  422. $messages = $protocol->getMessages();
  423. $this->assertEquals($messages[Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE]
  424. [Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI][0],
  425. '[{"Type":"LOG"},"Message 2"]');
  426. }
  427. public function testBufferedMessage2()
  428. {
  429. $this->_setupWithFrontController();
  430. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  431. $protocol = $channel->getProtocol(Zend_Wildfire_Plugin_FirePhp::PROTOCOL_URI);
  432. $message = new Zend_Wildfire_Plugin_FirePhp_Message(Zend_Wildfire_Plugin_FirePhp::LOG, 'Message 1');
  433. $this->assertFalse($message->setBuffered(true));
  434. Zend_Wildfire_Plugin_FirePhp::send($message);
  435. $this->assertFalse($protocol->getMessages());
  436. $message->setMessage('Message 2');
  437. $this->_controller->dispatch();
  438. $messages = $protocol->getMessages();
  439. $this->assertEquals($messages[Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE]
  440. [Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI][0],
  441. '[{"Type":"LOG"},"Message 2"]');
  442. }
  443. public function testDestroyedBufferedMessage()
  444. {
  445. $this->_setupWithoutFrontController();
  446. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  447. $protocol = $channel->getProtocol(Zend_Wildfire_Plugin_FirePhp::PROTOCOL_URI);
  448. $message = new Zend_Wildfire_Plugin_FirePhp_Message(Zend_Wildfire_Plugin_FirePhp::LOG, 'Message 1');
  449. $message->setBuffered(true);
  450. Zend_Wildfire_Plugin_FirePhp::send($message);
  451. $this->assertEquals($message->getStyle(), Zend_Wildfire_Plugin_FirePhp::LOG);
  452. $message->setStyle(Zend_Wildfire_Plugin_FirePhp::INFO);
  453. $this->assertEquals($message->getStyle(), Zend_Wildfire_Plugin_FirePhp::INFO);
  454. $message->setDestroy(true);
  455. Zend_Wildfire_Channel_HttpHeaders::getInstance()->flush();
  456. $this->assertFalse($protocol->getMessages());
  457. }
  458. public function testChannelInstanciation()
  459. {
  460. $this->_setupWithoutFrontController();
  461. try {
  462. Zend_Wildfire_Channel_HttpHeaders::getInstance();
  463. Zend_Wildfire_Channel_HttpHeaders::init(null);
  464. $this->fail('Should not be able to re-initialize');
  465. } catch (Exception $e) {
  466. // success
  467. }
  468. }
  469. public function testChannelFlush()
  470. {
  471. $this->_setupWithoutFrontController(false);
  472. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  473. $this->assertFalse($channel->flush(), 'Nothing to flush - No messages');
  474. Zend_Wildfire_Plugin_FirePhp::send('Hello World');
  475. $this->assertTrue($channel->flush(), 'One message to flush');
  476. $this->_request->setUserAgentExtensionEnabled(false);
  477. $this->assertFalse($channel->flush(), 'Nothing to flush - Extension not in UserAgent');
  478. }
  479. public function testFirePhpPluginSubclass()
  480. {
  481. $firephp = Zend_Wildfire_Plugin_FirePhp::init('Zend_Wildfire_WildfireTest_FirePhpPlugin');
  482. $this->assertEquals(get_class($firephp),
  483. 'Zend_Wildfire_WildfireTest_FirePhpPlugin');
  484. Zend_Wildfire_Plugin_FirePhp::destroyInstance();
  485. try {
  486. Zend_Wildfire_Plugin_FirePhp::init('Zend_Wildfire_WildfireTest_Request');
  487. $this->fail('Should not be able to initialize');
  488. } catch (Exception $e) {
  489. // success
  490. }
  491. $this->assertNull(Zend_Wildfire_Plugin_FirePhp::getInstance(true));
  492. try {
  493. Zend_Wildfire_Plugin_FirePhp::init(array());
  494. $this->fail('Should not be able to initialize');
  495. } catch (Exception $e) {
  496. // success
  497. }
  498. $this->assertNull(Zend_Wildfire_Plugin_FirePhp::getInstance(true));
  499. }
  500. public function testHttpHeadersChannelSubclass()
  501. {
  502. $firephp = Zend_Wildfire_Channel_HttpHeaders::init('Zend_Wildfire_WildfireTest_HttpHeadersChannel');
  503. $this->assertEquals(get_class($firephp),
  504. 'Zend_Wildfire_WildfireTest_HttpHeadersChannel');
  505. Zend_Wildfire_Channel_HttpHeaders::destroyInstance();
  506. try {
  507. Zend_Wildfire_Channel_HttpHeaders::init('Zend_Wildfire_WildfireTest_Request');
  508. $this->fail('Should not be able to initialize');
  509. } catch (Exception $e) {
  510. // success
  511. }
  512. $this->assertNull(Zend_Wildfire_Channel_HttpHeaders::getInstance(true));
  513. try {
  514. Zend_Wildfire_Channel_HttpHeaders::init(array());
  515. $this->fail('Should not be able to initialize');
  516. } catch (Exception $e) {
  517. // success
  518. }
  519. $this->assertNull(Zend_Wildfire_Channel_HttpHeaders::getInstance(true));
  520. }
  521. /**
  522. * @group ZF-4843
  523. */
  524. public function testResourceLogging()
  525. {
  526. $this->_setupWithoutFrontController();
  527. $firephp = Zend_Wildfire_Plugin_FirePhp::getInstance();
  528. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  529. $protocol = $channel->getProtocol(Zend_Wildfire_Plugin_FirePhp::PROTOCOL_URI);
  530. $firephp->send(array('file'=>tmpfile()));
  531. $messages = $protocol->getMessages();
  532. $message = $messages[Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE]
  533. [Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI]
  534. [0];
  535. $this->assertEquals(substr($message,0,41)
  536. , '[{"Type":"LOG"},{"file":"** Resource id #');
  537. }
  538. /**
  539. * @group ZF-4863
  540. */
  541. public function testLargeMessages()
  542. {
  543. $this->_setupWithoutFrontController();
  544. $firephp = Zend_Wildfire_Plugin_FirePhp::getInstance();
  545. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  546. $protocol = $channel->getProtocol(Zend_Wildfire_Plugin_FirePhp::PROTOCOL_URI);
  547. $data = array();
  548. for ($i=0 ; $i < 400 ; $i++) {
  549. $data[] = 'Test Data '.$i;
  550. }
  551. $firephp->send($data);
  552. Zend_Wildfire_Channel_HttpHeaders::getInstance()->flush();
  553. $messages = $this->_response->getHeadersForTesting();
  554. $this->assertTrue(substr($messages[3]['value'],0,10)=='6308|[{"Ty'
  555. && substr($messages[3]['value'],-8,8)==',"Test|\\'
  556. && substr($messages[4]['value'],0,10)=='| Data 318'
  557. && substr($messages[4]['value'],-7,7)=='399"]]|');
  558. }
  559. /**
  560. * @group ZF-5540
  561. */
  562. public function testOptions()
  563. {
  564. $firephp = Zend_Wildfire_Plugin_FirePhp::getInstance();
  565. $_options = array(
  566. 'traceOffset' => 1, /* The offset in the trace which identifies the source of the message */
  567. 'maxTraceDepth' => 99, /* Maximum depth for stack traces */
  568. 'maxObjectDepth' => 10, /* The maximum depth to traverse objects when encoding */
  569. 'maxArrayDepth' => 20, /* The maximum depth to traverse nested arrays when encoding */
  570. 'includeLineNumbers' => true /* Whether to include line and file info for each message */
  571. );
  572. $this->assertEquals($firephp->getOptions(), $_options, 'Ensure defaults stay the same');
  573. $this->assertEquals($firephp->setOption('includeLineNumbers',false),
  574. $_options['includeLineNumbers'],
  575. 'Should return old value');
  576. $this->assertEquals($firephp->getOption('includeLineNumbers'),
  577. false,
  578. 'Should contain new value');
  579. }
  580. /**
  581. * @group ZF-5540
  582. */
  583. public function testObjectFilter()
  584. {
  585. $this->_setupWithoutFrontController();
  586. $firephp = Zend_Wildfire_Plugin_FirePhp::getInstance();
  587. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  588. $protocol = $channel->getProtocol(Zend_Wildfire_Plugin_FirePhp::PROTOCOL_URI);
  589. $obj = new Zend_Wildfire_WildfireTest_TestObject1();
  590. $firephp->send($obj);
  591. $firephp->setObjectFilter('Zend_Wildfire_WildfireTest_TestObject1',array('value', 'protectedStatic'));
  592. $firephp->send($obj);
  593. $messages = $protocol->getMessages();
  594. $message = $messages[Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE]
  595. [Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI]
  596. [0];
  597. if (version_compare(phpversion(), '5.3' , '<')) {
  598. $this->assertEquals($message,
  599. '[{"Type":"LOG"},{"__className":"Zend_Wildfire_WildfireTest_TestObject1","public:name":"Name","public:value":"Value","protected:static:protectedStatic":"** Need PHP 5.3 to get value **"}]');
  600. } else
  601. if (version_compare(phpversion(), '5.3' , '>=')) {
  602. $this->assertEquals($message,
  603. '[{"Type":"LOG"},{"__className":"Zend_Wildfire_WildfireTest_TestObject1","public:name":"Name","public:value":"Value","protected:static:protectedStatic":"ProtectedStatic"}]');
  604. }
  605. $message = $messages[Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE]
  606. [Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI]
  607. [1];
  608. $this->assertEquals($message,
  609. '[{"Type":"LOG"},{"__className":"Zend_Wildfire_WildfireTest_TestObject1","public:name":"Name","public:value":"** Excluded by Filter **","protected:static:protectedStatic":"** Excluded by Filter **"}]');
  610. }
  611. public function testObjectMembers()
  612. {
  613. $this->_setupWithoutFrontController();
  614. $firephp = Zend_Wildfire_Plugin_FirePhp::getInstance();
  615. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  616. $protocol = $channel->getProtocol(Zend_Wildfire_Plugin_FirePhp::PROTOCOL_URI);
  617. $obj = new Zend_Wildfire_WildfireTest_TestObject2();
  618. $firephp->send($obj);
  619. $messages = $protocol->getMessages();
  620. $message = $messages[Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE]
  621. [Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI]
  622. [0];
  623. if (version_compare(phpversion(), '5.3' , '<')) {
  624. $this->assertEquals($message,
  625. '[{"Type":"LOG"},{"__className":"Zend_Wildfire_WildfireTest_TestObject2","public:public":"Public","private:private":"Private","protected:protected":"Protected","public:static:static":"Static","private:static:staticPrivate":"** Need PHP 5.3 to get value **","protected:static:staticProtected":"** Need PHP 5.3 to get value **"}]');
  626. } else
  627. if (version_compare(phpversion(), '5.3' , '>=')) {
  628. $this->assertEquals($message,
  629. '[{"Type":"LOG"},{"__className":"Zend_Wildfire_WildfireTest_TestObject2","public:public":"Public","private:private":"Private","protected:protected":"Protected","public:static:static":"Static","private:static:staticPrivate":"StaticPrivate","protected:static:staticProtected":"StaticProtected"}]');
  630. }
  631. }
  632. /**
  633. * @group ZF-5540
  634. */
  635. public function testMaxObjectArrayDepth()
  636. {
  637. $this->_setupWithoutFrontController();
  638. $firephp = Zend_Wildfire_Plugin_FirePhp::getInstance();
  639. $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  640. $protocol = $channel->getProtocol(Zend_Wildfire_Plugin_FirePhp::PROTOCOL_URI);
  641. $firephp->setOption('maxObjectDepth',2);
  642. $firephp->setOption('maxArrayDepth',1);
  643. $obj = new Zend_Wildfire_WildfireTest_TestObject3();
  644. $obj->testArray = array('val1',array('val2',array('Hello World')));
  645. $obj->child = clone $obj;
  646. $obj->child->child = clone $obj;
  647. $firephp->send($obj);
  648. $table = array();
  649. $table[] = array('Col1', 'Col2');
  650. $table[] = array($obj, $obj);
  651. $firephp->send($table, 'Label', Zend_Wildfire_Plugin_FirePhp::TABLE);
  652. $messages = $protocol->getMessages();
  653. $message = $messages[Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE]
  654. [Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI]
  655. [0];
  656. $this->assertEquals($message,
  657. '[{"Type":"LOG"},{"__className":"Zend_Wildfire_WildfireTest_TestObject3","public:name":"Name","public:value":"Value","undeclared:testArray":["val1","** Max Array Depth (1) **"],"undeclared:child":{"__className":"Zend_Wildfire_WildfireTest_TestObject3","public:name":"Name","public:value":"Value","undeclared:testArray":["val1","** Max Array Depth (1) **"],"undeclared:child":"** Max Object Depth (2) **"}}]');
  658. $message = $messages[Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE]
  659. [Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI]
  660. [1];
  661. $this->assertEquals($message,
  662. '[{"Type":"TABLE","Label":"Label"},[["Col1","Col2"],[{"__className":"Zend_Wildfire_WildfireTest_TestObject3","public:name":"Name","public:value":"Value","undeclared:testArray":["val1","** Max Array Depth (1) **"],"undeclared:child":{"__className":"Zend_Wildfire_WildfireTest_TestObject3","public:name":"Name","public:value":"Value","undeclared:testArray":["val1","** Max Array Depth (1) **"],"undeclared:child":"** Max Object Depth (2) **"}},{"__className":"Zend_Wildfire_WildfireTest_TestObject3","public:name":"Name","public:value":"Value","undeclared:testArray":["val1","** Max Array Depth (1) **"],"undeclared:child":{"__className":"Zend_Wildfire_WildfireTest_TestObject3","public:name":"Name","public:value":"Value","undeclared:testArray":["val1","** Max Array Depth (1) **"],"undeclared:child":"** Max Object Depth (2) **"}}]]]');
  663. }
  664. }
  665. class Zend_Wildfire_WildfireTest_TestObject1
  666. {
  667. var $name = 'Name';
  668. var $value = 'Value';
  669. protected static $protectedStatic = 'ProtectedStatic';
  670. }
  671. class Zend_Wildfire_WildfireTest_TestObject2
  672. {
  673. var $public = 'Public';
  674. private $private = 'Private';
  675. protected $protected = 'Protected';
  676. static $static = 'Static';
  677. static private $staticPrivate = 'StaticPrivate';
  678. static protected $staticProtected = 'StaticProtected';
  679. }
  680. class Zend_Wildfire_WildfireTest_TestObject3
  681. {
  682. var $name = 'Name';
  683. var $value = 'Value';
  684. }
  685. class Zend_Wildfire_WildfireTest_JsonEncodingTestClass
  686. {
  687. }
  688. class Zend_Wildfire_WildfireTest_FirePhpPlugin extends Zend_Wildfire_Plugin_FirePhp
  689. {
  690. }
  691. class Zend_Wildfire_WildfireTest_HttpHeadersChannel extends Zend_Wildfire_Channel_HttpHeaders
  692. {
  693. }
  694. class Zend_Wildfire_WildfireTest_Request extends Zend_Controller_Request_HttpTestCase
  695. {
  696. protected $_enabled = false;
  697. public function setUserAgentExtensionEnabled($enabled) {
  698. $this->_enabled = $enabled;
  699. }
  700. public function getHeader($header, $default = null)
  701. {
  702. if ($header == 'User-Agent') {
  703. if ($this->_enabled) {
  704. 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';
  705. } else {
  706. return 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14';
  707. }
  708. }
  709. }
  710. }
  711. class Zend_Wildfire_WildfireTest_Response extends Zend_Controller_Response_HttpTestCase
  712. {
  713. public function getHeadersForTesting()
  714. {
  715. return $this->getHeaders();
  716. }
  717. public function verifyHeaders($headers)
  718. {
  719. $response_headers = $this->getHeaders();
  720. if (!$response_headers) {
  721. return false;
  722. }
  723. $keys1 = array_keys($headers);
  724. sort($keys1);
  725. $keys1 = serialize($keys1);
  726. $keys2 = array();
  727. foreach ($response_headers as $header ) {
  728. $keys2[] = $header['name'];
  729. }
  730. sort($keys2);
  731. $keys2 = serialize($keys2);
  732. if ($keys1 != $keys2) {
  733. return false;
  734. }
  735. $values1 = array_values($headers);
  736. sort($values1);
  737. $values1 = serialize($values1);
  738. $values2 = array();
  739. foreach ($response_headers as $header ) {
  740. $values2[] = $header['value'];
  741. }
  742. sort($values2);
  743. $values2 = serialize($values2);
  744. if ($values1 != $values2) {
  745. return false;
  746. }
  747. return true;
  748. }
  749. }
  750. if (PHPUnit_MAIN_METHOD == 'Zend_Wildfire_WildfireTest::main') {
  751. Zend_Wildfire_WildfireTest::main();
  752. }