WildfireTest.php 38 KB

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