WildfireTest.php 37 KB

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