WildfireTest.php 36 KB

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