Base for a static organization website

FireCakeTest.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. /**
  3. * CakeFirePHP Test Case
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @since DebugKit 0.1
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. **/
  18. App::uses('FireCake', 'DebugKit.Lib');
  19. require_once CakePlugin::path('DebugKit') . 'Test' . DS . 'Case' . DS . 'TestFireCake.php';
  20. /**
  21. * Test Case For FireCake
  22. *
  23. * @since DebugKit 0.1
  24. */
  25. class FireCakeTestCase extends CakeTestCase {
  26. /**
  27. * setup test
  28. *
  29. * Fill FireCake with TestFireCake instance.
  30. *
  31. * @return void
  32. */
  33. public function setUp() {
  34. $this->firecake = FireCake::getInstance('TestFireCake');
  35. TestFireCake::reset();
  36. }
  37. /**
  38. * Reset the FireCake counters and headers.
  39. *
  40. * @return void
  41. */
  42. public function tearDown() {
  43. TestFireCake::reset();
  44. }
  45. /**
  46. * Test getInstance cheat.
  47. *
  48. * If this fails the rest of the test is going to fail too.
  49. *
  50. * @return void
  51. */
  52. public function testGetInstanceOverride() {
  53. $instance = FireCake::getInstance();
  54. $instance2 = FireCake::getInstance();
  55. $this->assertReference($instance, $instance2);
  56. $this->assertIsA($instance, 'FireCake');
  57. $this->assertIsA($instance, 'TestFireCake', 'Stored instance is not a copy of TestFireCake, test case is broken.');
  58. }
  59. /**
  60. * Test setOptions
  61. *
  62. * @return void
  63. */
  64. public function testSetOptions() {
  65. FireCake::setOptions(array('includeLineNumbers' => false));
  66. $this->assertEquals($this->firecake->options['includeLineNumbers'], false);
  67. }
  68. /**
  69. * Test Log()
  70. *
  71. * @return void
  72. */
  73. public function testLog() {
  74. FireCake::setOptions(array('includeLineNumbers' => false));
  75. FireCake::log('Testing');
  76. $this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-Protocol-1']));
  77. $this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-1-Plugin-1']));
  78. $this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-1-Structure-1']));
  79. $this->assertEquals($this->firecake->sentHeaders['X-Wf-1-Index'], 1);
  80. $this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-1'], '26|[{"Type":"LOG"},"Testing"]|');
  81. FireCake::log('Testing', 'log-info');
  82. $this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-2'], '45|[{"Type":"LOG","Label":"log-info"},"Testing"]|');
  83. }
  84. /**
  85. * Test info()
  86. *
  87. * @return void
  88. */
  89. public function testInfo() {
  90. FireCake::setOptions(array('includeLineNumbers' => false));
  91. FireCake::info('I have information');
  92. $this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-Protocol-1']));
  93. $this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-1-Plugin-1']));
  94. $this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-1-Structure-1']));
  95. $this->assertEquals($this->firecake->sentHeaders['X-Wf-1-Index'], 1);
  96. $this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-1'], '38|[{"Type":"INFO"},"I have information"]|');
  97. FireCake::info('I have information', 'info-label');
  98. $this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-2'], '59|[{"Type":"INFO","Label":"info-label"},"I have information"]|');
  99. }
  100. /**
  101. * Test info()
  102. *
  103. * @return void
  104. */
  105. public function testWarn() {
  106. FireCake::setOptions(array('includeLineNumbers' => false));
  107. FireCake::warn('A Warning');
  108. $this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-Protocol-1']));
  109. $this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-1-Plugin-1']));
  110. $this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-1-Structure-1']));
  111. $this->assertEquals($this->firecake->sentHeaders['X-Wf-1-Index'], 1);
  112. $this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-1'], '29|[{"Type":"WARN"},"A Warning"]|');
  113. FireCake::warn('A Warning', 'Bzzz');
  114. $this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-2'], '44|[{"Type":"WARN","Label":"Bzzz"},"A Warning"]|');
  115. }
  116. /**
  117. * Test error()
  118. *
  119. * @return void
  120. */
  121. public function testError() {
  122. FireCake::setOptions(array('includeLineNumbers' => false));
  123. FireCake::error('An error');
  124. $this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-Protocol-1']));
  125. $this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-1-Plugin-1']));
  126. $this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-1-Structure-1']));
  127. $this->assertEquals($this->firecake->sentHeaders['X-Wf-1-Index'], 1);
  128. $this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-1'], '29|[{"Type":"ERROR"},"An error"]|');
  129. FireCake::error('An error', 'wonky');
  130. $this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-2'], '45|[{"Type":"ERROR","Label":"wonky"},"An error"]|');
  131. }
  132. /**
  133. * Test dump()
  134. *
  135. * @return void
  136. */
  137. public function testDump() {
  138. FireCake::dump('mydump', array('one' => 1, 'two' => 2));
  139. $this->assertEquals($this->firecake->sentHeaders['X-Wf-1-2-1-1'], '28|{"mydump":{"one":1,"two":2}}|');
  140. $this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-1-Structure-2']));
  141. }
  142. /**
  143. * Test table() generation
  144. *
  145. * @return void
  146. */
  147. public function testTable() {
  148. $table[] = array('Col 1 Heading','Col 2 Heading');
  149. $table[] = array('Row 1 Col 1','Row 1 Col 2');
  150. $table[] = array('Row 2 Col 1','Row 2 Col 2');
  151. $table[] = array('Row 3 Col 1','Row 3 Col 2');
  152. FireCake::table('myTrace', $table);
  153. $expected = '162|[{"Type":"TABLE","Label":"myTrace"},[["Col 1 Heading","Col 2 Heading"],["Row 1 Col 1","Row 1 Col 2"],["Row 2 Col 1","Row 2 Col 2"],["Row 3 Col 1","Row 3 Col 2"]]]|';
  154. $this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-1'], $expected);
  155. }
  156. /**
  157. * TestStringEncoding
  158. *
  159. * @return void
  160. */
  161. public function testStringEncode() {
  162. $vars = array(1,2,3);
  163. $result = $this->firecake->stringEncode($vars);
  164. $this->assertEquals($result, array(1,2,3));
  165. $this->firecake->setOptions(array('maxArrayDepth' => 3));
  166. $deep = array(1 => array(2 => array(3)));
  167. $result = $this->firecake->stringEncode($deep);
  168. $this->assertEquals($result, array(1 => array(2 => '** Max Array Depth (3) **')));
  169. }
  170. /**
  171. * Test object encoding
  172. *
  173. * @return void
  174. */
  175. public function testStringEncodeObjects() {
  176. $obj = FireCake::getInstance();
  177. $result = $this->firecake->stringEncode($obj);
  178. $this->assertTrue(is_array($result));
  179. $this->assertEquals($result['_defaultOptions']['useNativeJsonEncode'], true);
  180. $this->assertEquals($result['_encodedObjects'][0], '** Recursion (TestFireCake) **');
  181. }
  182. /**
  183. * Test trace()
  184. *
  185. * @return void
  186. */
  187. public function testTrace() {
  188. FireCake::trace('myTrace');
  189. $this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-Protocol-1']));
  190. $this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-1-Plugin-1']));
  191. $this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-1-Structure-1']));
  192. $dump = $this->firecake->sentHeaders['X-Wf-1-1-1-1'];
  193. $this->assertPattern('/"Message":"myTrace"/', $dump);
  194. $this->assertPattern('/"Trace":\[/', $dump);
  195. }
  196. /**
  197. * Test enabling and disabling of FireCake output
  198. *
  199. * @return void
  200. */
  201. public function testEnableDisable() {
  202. FireCake::disable();
  203. FireCake::trace('myTrace');
  204. $this->assertTrue(empty($this->firecake->sentHeaders));
  205. FireCake::enable();
  206. FireCake::trace('myTrace');
  207. $this->assertFalse(empty($this->firecake->sentHeaders));
  208. }
  209. /**
  210. * Test correct line continuation markers on multi line headers.
  211. *
  212. * @return void
  213. */
  214. public function testMultiLineOutput() {
  215. FireCake::trace('myTrace');
  216. $this->assertGreaterThan(1, $this->firecake->sentHeaders['X-Wf-1-Index']);
  217. $header = $this->firecake->sentHeaders['X-Wf-1-1-1-1'];
  218. $this->assertEquals(substr($header, -2), '|\\');
  219. $endIndex = $this->firecake->sentHeaders['X-Wf-1-Index'];
  220. $header = $this->firecake->sentHeaders['X-Wf-1-1-1-' . $endIndex];
  221. $this->assertEquals(substr($header, -1), '|');
  222. }
  223. /**
  224. * Test inclusion of line numbers
  225. *
  226. * @return void
  227. */
  228. public function testIncludeLineNumbers() {
  229. FireCake::setOptions(array('includeLineNumbers' => true));
  230. FireCake::info('Testing');
  231. $result = $this->firecake->sentHeaders['X-Wf-1-1-1-1'];
  232. $this->assertPattern('/"File"\:".*FireCakeTest.php/', $result);
  233. $this->assertPattern('/"Line"\:\d+/', $result);
  234. }
  235. /**
  236. * Test Group messages
  237. *
  238. * @return void
  239. */
  240. public function testGroup() {
  241. FireCake::setOptions(array('includeLineNumbers' => false));
  242. FireCake::group('test');
  243. FireCake::info('my info');
  244. FireCake::groupEnd();
  245. $this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-1'], '63|[{"Collapsed":"true","Type":"GROUP_START","Label":"test"},null]|');
  246. $this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-3'], '27|[{"Type":"GROUP_END"},null]|');
  247. $this->assertEquals($this->firecake->sentHeaders['X-Wf-1-Index'], 3);
  248. }
  249. /**
  250. * Test fb() parameter parsing
  251. *
  252. * @return void
  253. */
  254. public function testFbParameterParsing() {
  255. FireCake::setOptions(array('includeLineNumbers' => false));
  256. FireCake::fb('Test');
  257. $this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-1'], '23|[{"Type":"LOG"},"Test"]|');
  258. FireCake::fb('Test', 'warn');
  259. $this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-2'], '24|[{"Type":"WARN"},"Test"]|');
  260. FireCake::fb('Test', 'Custom label', 'warn');
  261. $this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-3'], '47|[{"Type":"WARN","Label":"Custom label"},"Test"]|');
  262. $this->expectError('PHPUnit_Framework_Error');
  263. $this->assertFalse(FireCake::fb('Test', 'Custom label', 'warn', 'more parameters'));
  264. $this->assertEquals($this->firecake->sentHeaders['X-Wf-1-Index'], 3);
  265. }
  266. /**
  267. * Test defaulting to log if incorrect message type is used
  268. *
  269. * @return void
  270. */
  271. public function testIncorrectMessageType() {
  272. FireCake::setOptions(array('includeLineNumbers' => false));
  273. FireCake::fb('Hello World', 'foobared');
  274. $this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-1'], '30|[{"Type":"LOG"},"Hello World"]|');
  275. }
  276. /**
  277. * Test DetectClientExtension.
  278. *
  279. * @return void
  280. */
  281. public function testDetectClientExtension() {
  282. $back = env('HTTP_USER_AGENT');
  283. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4 FirePHP/0.2.1';
  284. $this->assertTrue(FireCake::detectClientExtension());
  285. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4 FirePHP/0.0.4';
  286. $this->assertFalse(FireCake::detectClientExtension());
  287. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4';
  288. $this->assertFalse(FireCake::detectClientExtension());
  289. $_SERVER['HTTP_USER_AGENT'] = $back;
  290. }
  291. /**
  292. * Test of Non Native JSON encoding.
  293. *
  294. * @return void
  295. */
  296. public function testNonNativeEncoding() {
  297. FireCake::setOptions(array('useNativeJsonEncode' => false));
  298. $json = FireCake::jsonEncode(array('one' => 1, 'two' => 2));
  299. $this->assertEquals($json, '{"one":1,"two":2}');
  300. $json = FireCake::jsonEncode(array(1,2,3));
  301. $this->assertEquals($json, '[1,2,3]');
  302. $json = FireCake::jsonEncode(FireCake::getInstance());
  303. $this->assertPattern('/"options"\:\{"maxObjectDepth"\:\d*,/', $json);
  304. }
  305. }