DebuggerTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP Project
  11. * @since CakePHP(tm) v 1.2.0.5432
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. App::uses('Debugger', 'Utility');
  15. /**
  16. * DebugggerTestCaseDebuggger class
  17. *
  18. * @package Cake.Test.Case.Utility
  19. */
  20. class DebuggerTestCaseDebugger extends Debugger {
  21. }
  22. /**
  23. * DebuggerTest class
  24. *
  25. * !!! Be careful with changing code below as it may
  26. * !!! change line numbers which are used in the tests
  27. *
  28. * @package Cake.Test.Case.Utility
  29. */
  30. class DebuggerTest extends CakeTestCase {
  31. protected $_restoreError = false;
  32. /**
  33. * setUp method
  34. *
  35. * @return void
  36. */
  37. public function setUp() {
  38. parent::setUp();
  39. Configure::write('debug', 2);
  40. Configure::write('log', false);
  41. }
  42. /**
  43. * tearDown method
  44. *
  45. * @return void
  46. */
  47. public function tearDown() {
  48. parent::tearDown();
  49. Configure::write('log', true);
  50. if ($this->_restoreError) {
  51. restore_error_handler();
  52. }
  53. }
  54. /**
  55. * testDocRef method
  56. *
  57. * @return void
  58. */
  59. public function testDocRef() {
  60. ini_set('docref_root', '');
  61. $this->assertEquals(ini_get('docref_root'), '');
  62. new Debugger();
  63. $this->assertEquals(ini_get('docref_root'), 'http://php.net/');
  64. }
  65. /**
  66. * test Excerpt writing
  67. *
  68. * @return void
  69. */
  70. public function testExcerpt() {
  71. $result = Debugger::excerpt(__FILE__, __LINE__, 2);
  72. $this->assertTrue(is_array($result));
  73. $this->assertEquals(5, count($result));
  74. $this->assertRegExp('/function(.+)testExcerpt/', $result[1]);
  75. $result = Debugger::excerpt(__FILE__, 2, 2);
  76. $this->assertTrue(is_array($result));
  77. $this->assertEquals(4, count($result));
  78. $pattern = '/<code>.*?<span style\="color\: \#\d+">.*?&lt;\?php/';
  79. $this->assertRegExp($pattern, $result[0]);
  80. $result = Debugger::excerpt(__FILE__, 10, 2);
  81. $this->assertEquals(5, count($result));
  82. $pattern = '/<span style\="color\: \#\d{6}">\*<\/span>/';
  83. $this->assertRegExp($pattern, $result[0]);
  84. $return = Debugger::excerpt('[internal]', 2, 2);
  85. $this->assertTrue(empty($return));
  86. }
  87. /**
  88. * testOutput method
  89. *
  90. * @return void
  91. */
  92. public function testOutput() {
  93. set_error_handler('Debugger::showError');
  94. $this->_restoreError = true;
  95. $result = Debugger::output(false);
  96. $this->assertEquals('', $result);
  97. $out .= '';
  98. $result = Debugger::output(true);
  99. $this->assertEquals('Notice', $result[0]['error']);
  100. $this->assertRegExp('/Undefined variable\:\s+out/', $result[0]['description']);
  101. $this->assertRegExp('/DebuggerTest::testOutput/i', $result[0]['trace']);
  102. ob_start();
  103. Debugger::output('txt');
  104. $other .= '';
  105. $result = ob_get_clean();
  106. $this->assertRegExp('/Undefined variable:\s+other/', $result);
  107. $this->assertRegExp('/Context:/', $result);
  108. $this->assertRegExp('/DebuggerTest::testOutput/i', $result);
  109. ob_start();
  110. Debugger::output('html');
  111. $wrong .= '';
  112. $result = ob_get_clean();
  113. $this->assertRegExp('/<pre class="cake-error">.+<\/pre>/', $result);
  114. $this->assertRegExp('/<b>Notice<\/b>/', $result);
  115. $this->assertRegExp('/variable:\s+wrong/', $result);
  116. ob_start();
  117. Debugger::output('js');
  118. $buzz .= '';
  119. $result = explode('</a>', ob_get_clean());
  120. $this->assertTags($result[0], array(
  121. 'pre' => array('class' => 'cake-error'),
  122. 'a' => array(
  123. 'href' => "javascript:void(0);",
  124. 'onclick' => "preg:/document\.getElementById\('cakeErr[a-z0-9]+\-trace'\)\.style\.display = " .
  125. "\(document\.getElementById\('cakeErr[a-z0-9]+\-trace'\)\.style\.display == 'none'" .
  126. " \? '' \: 'none'\);/"
  127. ),
  128. 'b' => array(), 'Notice', '/b', ' (8)',
  129. ));
  130. $this->assertRegExp('/Undefined variable:\s+buzz/', $result[1]);
  131. $this->assertRegExp('/<a[^>]+>Code/', $result[1]);
  132. $this->assertRegExp('/<a[^>]+>Context/', $result[2]);
  133. $this->assertContains('$wrong = &#039;&#039;', $result[3], 'Context should be HTML escaped.');
  134. }
  135. /**
  136. * Tests that changes in output formats using Debugger::output() change the templates used.
  137. *
  138. * @return void
  139. */
  140. public function testChangeOutputFormats() {
  141. set_error_handler('Debugger::showError');
  142. $this->_restoreError = true;
  143. Debugger::output('js', array(
  144. 'traceLine' => '{:reference} - <a href="txmt://open?url=file://{:file}' .
  145. '&line={:line}">{:path}</a>, line {:line}'
  146. ));
  147. $result = Debugger::trace();
  148. $this->assertRegExp('/' . preg_quote('txmt://open?url=file://', '/') . '(\/|[A-Z]:\\\\)' . '/', $result);
  149. Debugger::output('xml', array(
  150. 'error' => '<error><code>{:code}</code><file>{:file}</file><line>{:line}</line>' .
  151. '{:description}</error>',
  152. 'context' => "<context>{:context}</context>",
  153. 'trace' => "<stack>{:trace}</stack>",
  154. ));
  155. Debugger::output('xml');
  156. ob_start();
  157. $foo .= '';
  158. $result = ob_get_clean();
  159. $data = array(
  160. 'error' => array(),
  161. 'code' => array(), '8', '/code',
  162. 'file' => array(), 'preg:/[^<]+/', '/file',
  163. 'line' => array(), '' . (intval(__LINE__) - 7), '/line',
  164. 'preg:/Undefined variable:\s+foo/',
  165. '/error'
  166. );
  167. $this->assertTags($result, $data, true);
  168. }
  169. /**
  170. * Test that outputAs works.
  171. *
  172. * @return void
  173. */
  174. public function testOutputAs() {
  175. Debugger::outputAs('html');
  176. $this->assertEquals('html', Debugger::outputAs());
  177. }
  178. /**
  179. * Test that choosing a non-existent format causes an exception
  180. *
  181. * @expectedException CakeException
  182. * @return void
  183. */
  184. public function testOutputAsException() {
  185. Debugger::outputAs('Invalid junk');
  186. }
  187. /**
  188. * Tests that changes in output formats using Debugger::output() change the templates used.
  189. *
  190. * @return void
  191. */
  192. public function testAddFormat() {
  193. set_error_handler('Debugger::showError');
  194. $this->_restoreError = true;
  195. Debugger::addFormat('js', array(
  196. 'traceLine' => '{:reference} - <a href="txmt://open?url=file://{:file}' .
  197. '&line={:line}">{:path}</a>, line {:line}'
  198. ));
  199. Debugger::outputAs('js');
  200. $result = Debugger::trace();
  201. $this->assertRegExp('/' . preg_quote('txmt://open?url=file://', '/') . '(\/|[A-Z]:\\\\)' . '/', $result);
  202. Debugger::addFormat('xml', array(
  203. 'error' => '<error><code>{:code}</code><file>{:file}</file><line>{:line}</line>' .
  204. '{:description}</error>',
  205. ));
  206. Debugger::outputAs('xml');
  207. ob_start();
  208. $foo .= '';
  209. $result = ob_get_clean();
  210. $data = array(
  211. '<error',
  212. '<code', '8', '/code',
  213. '<file', 'preg:/[^<]+/', '/file',
  214. '<line', '' . (intval(__LINE__) - 7), '/line',
  215. 'preg:/Undefined variable:\s+foo/',
  216. '/error'
  217. );
  218. $this->assertTags($result, $data, true);
  219. }
  220. /**
  221. * Test adding a format that is handled by a callback.
  222. *
  223. * @return void
  224. */
  225. public function testAddFormatCallback() {
  226. set_error_handler('Debugger::showError');
  227. $this->_restoreError = true;
  228. Debugger::addFormat('callback', array('callback' => array($this, 'customFormat')));
  229. Debugger::outputAs('callback');
  230. ob_start();
  231. $foo .= '';
  232. $result = ob_get_clean();
  233. $this->assertContains('Notice: I eated an error', $result);
  234. $this->assertContains('DebuggerTest.php', $result);
  235. }
  236. /**
  237. * Test method for testing addFormat with callbacks.
  238. */
  239. public function customFormat($error, $strings) {
  240. return $error['error'] . ': I eated an error ' . $error['file'];
  241. }
  242. /**
  243. * testTrimPath method
  244. *
  245. * @return void
  246. */
  247. public function testTrimPath() {
  248. $this->assertEquals('APP' . DS, Debugger::trimPath(APP));
  249. $this->assertEquals('CORE', Debugger::trimPath(CAKE_CORE_INCLUDE_PATH));
  250. $this->assertEquals('ROOT', Debugger::trimPath(ROOT));
  251. $this->assertEquals('CORE' . DS . 'Cake' . DS, Debugger::trimPath(CAKE));
  252. $this->assertEquals('Some/Other/Path', Debugger::trimPath('Some/Other/Path'));
  253. }
  254. /**
  255. * testExportVar method
  256. *
  257. * @return void
  258. */
  259. public function testExportVar() {
  260. App::uses('Controller', 'Controller');
  261. $Controller = new Controller();
  262. $Controller->helpers = array('Html', 'Form');
  263. $View = new View($Controller);
  264. $View->int = 2;
  265. $View->float = 1.333;
  266. $result = Debugger::exportVar($View);
  267. $expected = <<<TEXT
  268. object(View) {
  269. Helpers => object(HelperCollection) {}
  270. Blocks => object(ViewBlock) {}
  271. plugin => null
  272. name => ''
  273. passedArgs => array()
  274. helpers => array(
  275. (int) 0 => 'Html',
  276. (int) 1 => 'Form'
  277. )
  278. viewPath => ''
  279. viewVars => array()
  280. view => null
  281. layout => 'default'
  282. layoutPath => null
  283. autoLayout => true
  284. ext => '.ctp'
  285. subDir => null
  286. theme => null
  287. cacheAction => false
  288. validationErrors => array()
  289. hasRendered => false
  290. uuids => array()
  291. request => object(CakeRequest) {}
  292. response => object(CakeResponse) {}
  293. elementCache => 'default'
  294. elementCacheSettings => array()
  295. int => (int) 2
  296. float => (float) 1.333
  297. TEXT;
  298. if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
  299. $expected .= <<<TEXT
  300. [protected] _passedVars => array(
  301. (int) 0 => 'viewVars',
  302. (int) 1 => 'autoLayout',
  303. (int) 2 => 'ext',
  304. (int) 3 => 'helpers',
  305. (int) 4 => 'view',
  306. (int) 5 => 'layout',
  307. (int) 6 => 'name',
  308. (int) 7 => 'theme',
  309. (int) 8 => 'layoutPath',
  310. (int) 9 => 'viewPath',
  311. (int) 10 => 'request',
  312. (int) 11 => 'plugin',
  313. (int) 12 => 'passedArgs',
  314. (int) 13 => 'cacheAction'
  315. )
  316. [protected] _scripts => array()
  317. [protected] _paths => array()
  318. [protected] _helpersLoaded => false
  319. [protected] _parents => array()
  320. [protected] _current => null
  321. [protected] _currentType => ''
  322. [protected] _stack => array()
  323. [protected] _eventManager => object(CakeEventManager) {}
  324. [protected] _eventManagerConfigured => false
  325. TEXT;
  326. }
  327. $expected .= <<<TEXT
  328. }
  329. TEXT;
  330. $this->assertTextEquals($expected, $result);
  331. $data = array(
  332. 1 => 'Index one',
  333. 5 => 'Index five'
  334. );
  335. $result = Debugger::exportVar($data);
  336. $expected = <<<TEXT
  337. array(
  338. (int) 1 => 'Index one',
  339. (int) 5 => 'Index five'
  340. )
  341. TEXT;
  342. $this->assertTextEquals($expected, $result);
  343. $data = array(
  344. 'key' => array(
  345. 'value'
  346. )
  347. );
  348. $result = Debugger::exportVar($data, 1);
  349. $expected = <<<TEXT
  350. array(
  351. 'key' => array(
  352. [maximum depth reached]
  353. )
  354. )
  355. TEXT;
  356. $this->assertTextEquals($expected, $result);
  357. $data = false;
  358. $result = Debugger::exportVar($data);
  359. $expected = <<<TEXT
  360. false
  361. TEXT;
  362. $this->assertTextEquals($expected, $result);
  363. }
  364. /**
  365. * Test exporting various kinds of false.
  366. *
  367. * @return void
  368. */
  369. public function testExportVarZero() {
  370. $data = array(
  371. 'nothing' => '',
  372. 'null' => null,
  373. 'false' => false,
  374. 'szero' => '0',
  375. 'zero' => 0
  376. );
  377. $result = Debugger::exportVar($data);
  378. $expected = <<<TEXT
  379. array(
  380. 'nothing' => '',
  381. 'null' => null,
  382. 'false' => false,
  383. 'szero' => '0',
  384. 'zero' => (int) 0
  385. )
  386. TEXT;
  387. $this->assertTextEquals($expected, $result);
  388. }
  389. /**
  390. * testLog method
  391. *
  392. * @return void
  393. */
  394. public function testLog() {
  395. if (file_exists(LOGS . 'debug.log')) {
  396. unlink(LOGS . 'debug.log');
  397. }
  398. Debugger::log('cool');
  399. $result = file_get_contents(LOGS . 'debug.log');
  400. $this->assertRegExp('/DebuggerTest\:\:testLog/i', $result);
  401. $this->assertRegExp("/'cool'/", $result);
  402. unlink(LOGS . 'debug.log');
  403. Debugger::log(array('whatever', 'here'));
  404. $result = file_get_contents(LOGS . 'debug.log');
  405. $this->assertRegExp('/DebuggerTest\:\:testLog/i', $result);
  406. $this->assertRegExp('/\[main\]/', $result);
  407. $this->assertRegExp('/array/', $result);
  408. $this->assertRegExp("/'whatever',/", $result);
  409. $this->assertRegExp("/'here'/", $result);
  410. }
  411. /**
  412. * testDump method
  413. *
  414. * @return void
  415. */
  416. public function testDump() {
  417. $var = array('People' => array(
  418. array(
  419. 'name' => 'joeseph',
  420. 'coat' => 'technicolor',
  421. 'hair_color' => 'brown'
  422. ),
  423. array(
  424. 'name' => 'Shaft',
  425. 'coat' => 'black',
  426. 'hair' => 'black'
  427. )
  428. ));
  429. ob_start();
  430. Debugger::dump($var);
  431. $result = ob_get_clean();
  432. $expected = <<<TEXT
  433. <pre>array(
  434. 'People' => array(
  435. (int) 0 => array(
  436. 'name' => 'joeseph',
  437. 'coat' => 'technicolor',
  438. 'hair_color' => 'brown'
  439. ),
  440. (int) 1 => array(
  441. 'name' => 'Shaft',
  442. 'coat' => 'black',
  443. 'hair' => 'black'
  444. )
  445. )
  446. )</pre>
  447. TEXT;
  448. $this->assertTextEquals($expected, $result);
  449. }
  450. /**
  451. * test getInstance.
  452. *
  453. * @return void
  454. */
  455. public function testGetInstance() {
  456. $result = Debugger::getInstance();
  457. $this->assertInstanceOf('Debugger', $result);
  458. $result = Debugger::getInstance('DebuggerTestCaseDebugger');
  459. $this->assertInstanceOf('DebuggerTestCaseDebugger', $result);
  460. $result = Debugger::getInstance();
  461. $this->assertInstanceOf('DebuggerTestCaseDebugger', $result);
  462. $result = Debugger::getInstance('Debugger');
  463. $this->assertInstanceOf('Debugger', $result);
  464. }
  465. /**
  466. * testNoDbCredentials
  467. *
  468. * If a connection error occurs, the config variable is passed through exportVar
  469. * *** our database login credentials such that they are never visible
  470. *
  471. * @return void
  472. */
  473. public function testNoDbCredentials() {
  474. $config = array(
  475. 'datasource' => 'mysql',
  476. 'persistent' => false,
  477. 'host' => 'void.cakephp.org',
  478. 'login' => 'cakephp-user',
  479. 'password' => 'cakephp-password',
  480. 'database' => 'cakephp-database',
  481. 'prefix' => ''
  482. );
  483. $output = Debugger::exportVar($config);
  484. $expectedArray = array(
  485. 'datasource' => 'mysql',
  486. 'persistent' => false,
  487. 'host' => '*****',
  488. 'login' => '*****',
  489. 'password' => '*****',
  490. 'database' => '*****',
  491. 'prefix' => ''
  492. );
  493. $expected = Debugger::exportVar($expectedArray);
  494. $this->assertEquals($expected, $output);
  495. }
  496. /**
  497. * Test that exportVar() doesn't loop through recursive structures.
  498. *
  499. * @return void
  500. */
  501. public function testExportVarRecursion() {
  502. $output = Debugger::exportVar($GLOBALS);
  503. $this->assertContains("'GLOBALS' => [recursion]", $output);
  504. }
  505. /**
  506. * test trace exclude
  507. *
  508. * @return void
  509. */
  510. public function testTraceExclude() {
  511. $result = Debugger::trace();
  512. $this->assertRegExp('/^DebuggerTest::testTraceExclude/', $result);
  513. $result = Debugger::trace(array(
  514. 'exclude' => array('DebuggerTest::testTraceExclude')
  515. ));
  516. $this->assertNotRegExp('/^DebuggerTest::testTraceExclude/', $result);
  517. }
  518. }