DebuggerTest.php 15 KB

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