DebuggerTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Error;
  16. use Cake\Controller\Controller;
  17. use Cake\Core\Configure;
  18. use Cake\Error\Debugger;
  19. use Cake\Log\Log;
  20. use Cake\TestSuite\TestCase;
  21. use Cake\View\View;
  22. /**
  23. * DebuggerTestCaseDebugger class
  24. *
  25. */
  26. class DebuggerTestCaseDebugger extends Debugger
  27. {
  28. }
  29. class DebuggableThing
  30. {
  31. public function __debugInfo()
  32. {
  33. return ['foo' => 'bar', 'inner' => new self()];
  34. }
  35. }
  36. /**
  37. * DebuggerTest class
  38. *
  39. * !!! Be careful with changing code below as it may
  40. * !!! change line numbers which are used in the tests
  41. *
  42. */
  43. class DebuggerTest extends TestCase
  44. {
  45. protected $_restoreError = false;
  46. /**
  47. * setUp method
  48. *
  49. * @return void
  50. */
  51. public function setUp()
  52. {
  53. parent::setUp();
  54. Configure::write('debug', true);
  55. Log::drop('stderr');
  56. Log::drop('stdout');
  57. }
  58. /**
  59. * tearDown method
  60. *
  61. * @return void
  62. */
  63. public function tearDown()
  64. {
  65. parent::tearDown();
  66. if ($this->_restoreError) {
  67. restore_error_handler();
  68. }
  69. }
  70. /**
  71. * testDocRef method
  72. *
  73. * @return void
  74. */
  75. public function testDocRef()
  76. {
  77. $this->skipIf(
  78. defined('HHVM_VERSION'),
  79. 'HHVM does not output doc references'
  80. );
  81. ini_set('docref_root', '');
  82. $this->assertEquals(ini_get('docref_root'), '');
  83. new Debugger();
  84. $this->assertEquals(ini_get('docref_root'), 'http://php.net/');
  85. }
  86. /**
  87. * test Excerpt writing
  88. *
  89. * @return void
  90. */
  91. public function testExcerpt()
  92. {
  93. $result = Debugger::excerpt(__FILE__, __LINE__ - 1, 2);
  94. $this->assertTrue(is_array($result));
  95. $this->assertCount(5, $result);
  96. $this->assertRegExp('/function(.+)testExcerpt/', $result[1]);
  97. $result = Debugger::excerpt(__FILE__, 2, 2);
  98. $this->assertTrue(is_array($result));
  99. $this->assertCount(4, $result);
  100. $this->skipIf(defined('HHVM_VERSION'), 'HHVM does not highlight php code');
  101. $pattern = '/<code>.*?<span style\="color\: \#\d+">.*?&lt;\?php/';
  102. $this->assertRegExp($pattern, $result[0]);
  103. $result = Debugger::excerpt(__FILE__, 11, 2);
  104. $this->assertCount(5, $result);
  105. $pattern = '/<span style\="color\: \#\d{6}">\*<\/span>/';
  106. $this->assertRegExp($pattern, $result[0]);
  107. $return = Debugger::excerpt('[internal]', 2, 2);
  108. $this->assertTrue(empty($return));
  109. $result = Debugger::excerpt(__FILE__, __LINE__, 5);
  110. $this->assertCount(11, $result);
  111. $this->assertContains('Debugger', $result[5]);
  112. $this->assertContains('excerpt', $result[5]);
  113. $this->assertContains('__FILE__', $result[5]);
  114. }
  115. /**
  116. * Test that outputAs works.
  117. *
  118. * @return void
  119. */
  120. public function testOutputAs()
  121. {
  122. Debugger::outputAs('html');
  123. $this->assertEquals('html', Debugger::outputAs());
  124. }
  125. /**
  126. * Test that choosing a non-existent format causes an exception
  127. *
  128. * @expectedException \InvalidArgumentException
  129. * @return void
  130. */
  131. public function testOutputAsException()
  132. {
  133. Debugger::outputAs('Invalid junk');
  134. }
  135. /**
  136. * Tests that changes in output formats using Debugger::output() change the templates used.
  137. *
  138. * @return void
  139. */
  140. public function testAddFormat()
  141. {
  142. Debugger::addFormat('js', [
  143. 'traceLine' => '{:reference} - <a href="txmt://open?url=file://{:file}' .
  144. '&line={:line}">{:path}</a>, line {:line}'
  145. ]);
  146. Debugger::outputAs('js');
  147. $result = Debugger::trace();
  148. $this->assertRegExp('/' . preg_quote('txmt://open?url=file://', '/') . '(\/|[A-Z]:\\\\)' . '/', $result);
  149. Debugger::addFormat('xml', [
  150. 'error' => '<error><code>{:code}</code><file>{:file}</file><line>{:line}</line>' .
  151. '{:description}</error>',
  152. ]);
  153. Debugger::outputAs('xml');
  154. ob_start();
  155. $debugger = Debugger::getInstance();
  156. $debugger->outputError([
  157. 'level' => E_NOTICE,
  158. 'code' => E_NOTICE,
  159. 'file' => __FILE__,
  160. 'line' => __LINE__,
  161. 'description' => 'Undefined variable: foo',
  162. ]);
  163. $result = ob_get_clean();
  164. $expected = [
  165. '<error',
  166. '<code', '8', '/code',
  167. '<file', 'preg:/[^<]+/', '/file',
  168. '<line', '' . ((int)__LINE__ - 9), '/line',
  169. 'preg:/Undefined variable:\s+foo/',
  170. '/error'
  171. ];
  172. $this->assertHtml($expected, $result, true);
  173. }
  174. /**
  175. * Test adding a format that is handled by a callback.
  176. *
  177. * @return void
  178. */
  179. public function testAddFormatCallback()
  180. {
  181. Debugger::addFormat('callback', ['callback' => [$this, 'customFormat']]);
  182. Debugger::outputAs('callback');
  183. ob_start();
  184. $debugger = Debugger::getInstance();
  185. $debugger->outputError([
  186. 'error' => 'Notice',
  187. 'code' => E_NOTICE,
  188. 'level' => E_NOTICE,
  189. 'description' => 'Undefined variable $foo',
  190. 'file' => __FILE__,
  191. 'line' => __LINE__,
  192. ]);
  193. $result = ob_get_clean();
  194. $this->assertContains('Notice: I eated an error', $result);
  195. $this->assertContains('DebuggerTest.php', $result);
  196. }
  197. /**
  198. * Test method for testing addFormat with callbacks.
  199. *
  200. * @return void
  201. */
  202. public function customFormat($error, $strings)
  203. {
  204. echo $error['error'] . ': I eated an error ' . $error['file'];
  205. }
  206. /**
  207. * testTrimPath method
  208. *
  209. * @return void
  210. */
  211. public function testTrimPath()
  212. {
  213. $this->assertEquals('APP/', Debugger::trimPath(APP));
  214. $this->assertEquals('CORE' . DS . 'src' . DS, Debugger::trimPath(CAKE));
  215. $this->assertEquals('Some/Other/Path', Debugger::trimPath('Some/Other/Path'));
  216. }
  217. /**
  218. * testExportVar method
  219. *
  220. * @return void
  221. */
  222. public function testExportVar()
  223. {
  224. $Controller = new Controller();
  225. $Controller->helpers = ['Html', 'Form'];
  226. $View = $Controller->createView();
  227. $View->int = 2;
  228. $View->float = 1.333;
  229. $result = Debugger::exportVar($View);
  230. $expected = <<<TEXT
  231. object(Cake\View\View) {
  232. Blocks => object(Cake\View\ViewBlock) {}
  233. plugin => null
  234. name => ''
  235. passedArgs => []
  236. helpers => [
  237. (int) 0 => 'Html',
  238. (int) 1 => 'Form'
  239. ]
  240. viewPath => null
  241. view => null
  242. layout => 'default'
  243. layoutPath => null
  244. autoLayout => true
  245. subDir => null
  246. theme => null
  247. hasRendered => false
  248. uuids => []
  249. request => object(Cake\Network\Request) {}
  250. response => object(Cake\Network\Response) {}
  251. elementCache => 'default'
  252. viewClass => null
  253. _view => null
  254. viewVars => []
  255. Html => object(Cake\View\Helper\HtmlHelper) {}
  256. Form => object(Cake\View\Helper\FormHelper) {}
  257. int => (int) 2
  258. float => (float) 1.333
  259. [protected] _helpers => object(Cake\View\HelperRegistry) {}
  260. [protected] _ext => '.ctp'
  261. [protected] _passedVars => [
  262. (int) 0 => 'viewVars',
  263. (int) 1 => 'autoLayout',
  264. (int) 2 => 'helpers',
  265. (int) 3 => 'view',
  266. (int) 4 => 'layout',
  267. (int) 5 => 'name',
  268. (int) 6 => 'theme',
  269. (int) 7 => 'layoutPath',
  270. (int) 8 => 'viewPath',
  271. (int) 9 => 'plugin',
  272. (int) 10 => 'passedArgs'
  273. ]
  274. [protected] _paths => []
  275. [protected] _pathsForPlugin => []
  276. [protected] _parents => []
  277. [protected] _current => null
  278. [protected] _currentType => ''
  279. [protected] _stack => []
  280. [protected] _eventManager => object(Cake\Event\EventManager) {}
  281. [protected] _eventClass => '\Cake\Event\Event'
  282. [protected] _viewBuilder => null
  283. }
  284. TEXT;
  285. $this->assertTextEquals($expected, $result);
  286. $data = [
  287. 1 => 'Index one',
  288. 5 => 'Index five'
  289. ];
  290. $result = Debugger::exportVar($data);
  291. $expected = <<<TEXT
  292. [
  293. (int) 1 => 'Index one',
  294. (int) 5 => 'Index five'
  295. ]
  296. TEXT;
  297. $this->assertTextEquals($expected, $result);
  298. $data = [
  299. 'key' => [
  300. 'value'
  301. ]
  302. ];
  303. $result = Debugger::exportVar($data, 1);
  304. $expected = <<<TEXT
  305. [
  306. 'key' => [
  307. [maximum depth reached]
  308. ]
  309. ]
  310. TEXT;
  311. $this->assertTextEquals($expected, $result);
  312. $data = false;
  313. $result = Debugger::exportVar($data);
  314. $expected = <<<TEXT
  315. false
  316. TEXT;
  317. $this->assertTextEquals($expected, $result);
  318. $file = fopen('php://output', 'w');
  319. fclose($file);
  320. $result = Debugger::exportVar($file);
  321. $this->assertTextEquals('unknown', $result);
  322. }
  323. /**
  324. * Test exporting various kinds of false.
  325. *
  326. * @return void
  327. */
  328. public function testExportVarZero()
  329. {
  330. $data = [
  331. 'nothing' => '',
  332. 'null' => null,
  333. 'false' => false,
  334. 'szero' => '0',
  335. 'zero' => 0
  336. ];
  337. $result = Debugger::exportVar($data);
  338. $expected = <<<TEXT
  339. [
  340. 'nothing' => '',
  341. 'null' => null,
  342. 'false' => false,
  343. 'szero' => '0',
  344. 'zero' => (int) 0
  345. ]
  346. TEXT;
  347. $this->assertTextEquals($expected, $result);
  348. }
  349. /**
  350. * testLog method
  351. *
  352. * @return void
  353. */
  354. public function testLog()
  355. {
  356. $mock = $this->getMock('Cake\Log\Engine\BaseLog', ['log']);
  357. Log::config('test', ['engine' => $mock]);
  358. $mock->expects($this->at(0))
  359. ->method('log')
  360. ->with('debug', $this->logicalAnd(
  361. $this->stringContains('DebuggerTest::testLog'),
  362. $this->stringContains('cool')
  363. ));
  364. $mock->expects($this->at(1))
  365. ->method('log')
  366. ->with('debug', $this->logicalAnd(
  367. $this->stringContains('DebuggerTest::testLog'),
  368. $this->stringContains('[main]'),
  369. $this->stringContains("'whatever',"),
  370. $this->stringContains("'here'")
  371. ));
  372. Debugger::log('cool');
  373. Debugger::log(['whatever', 'here']);
  374. Log::drop('test');
  375. }
  376. /**
  377. * test log() depth
  378. *
  379. * @return void
  380. */
  381. public function testLogDepth()
  382. {
  383. $mock = $this->getMock('Cake\Log\Engine\BaseLog', ['log']);
  384. Log::config('test', ['engine' => $mock]);
  385. $mock->expects($this->at(0))
  386. ->method('log')
  387. ->with('debug', $this->logicalAnd(
  388. $this->stringContains('DebuggerTest::testLog'),
  389. $this->stringContains('test'),
  390. $this->logicalNot($this->stringContains('val'))
  391. ));
  392. $val = [
  393. 'test' => ['key' => 'val']
  394. ];
  395. Debugger::log($val, 'debug', 0);
  396. }
  397. /**
  398. * testDump method
  399. *
  400. * @return void
  401. */
  402. public function testDump()
  403. {
  404. $var = ['People' => [
  405. [
  406. 'name' => 'joeseph',
  407. 'coat' => 'technicolor',
  408. 'hair_color' => 'brown'
  409. ],
  410. [
  411. 'name' => 'Shaft',
  412. 'coat' => 'black',
  413. 'hair' => 'black'
  414. ]
  415. ]];
  416. ob_start();
  417. Debugger::dump($var);
  418. $result = ob_get_clean();
  419. $open = "\n";
  420. $close = "\n\n";
  421. $expected = <<<TEXT
  422. {$open}[
  423. 'People' => [
  424. (int) 0 => [
  425. 'name' => 'joeseph',
  426. 'coat' => 'technicolor',
  427. 'hair_color' => 'brown'
  428. ],
  429. (int) 1 => [
  430. 'name' => 'Shaft',
  431. 'coat' => 'black',
  432. 'hair' => 'black'
  433. ]
  434. ]
  435. ]{$close}
  436. TEXT;
  437. $this->assertTextEquals($expected, $result);
  438. ob_start();
  439. Debugger::dump($var, 1);
  440. $result = ob_get_clean();
  441. $expected = <<<TEXT
  442. {$open}[
  443. 'People' => [
  444. [maximum depth reached]
  445. ]
  446. ]{$close}
  447. TEXT;
  448. $this->assertTextEquals($expected, $result);
  449. }
  450. /**
  451. * test getInstance.
  452. *
  453. * @return void
  454. */
  455. public function testGetInstance()
  456. {
  457. $result = Debugger::getInstance();
  458. $this->assertInstanceOf('Cake\Error\Debugger', $result);
  459. $result = Debugger::getInstance(__NAMESPACE__ . '\DebuggerTestCaseDebugger');
  460. $this->assertInstanceOf(__NAMESPACE__ . '\DebuggerTestCaseDebugger', $result);
  461. $result = Debugger::getInstance();
  462. $this->assertInstanceOf(__NAMESPACE__ . '\DebuggerTestCaseDebugger', $result);
  463. $result = Debugger::getInstance('Cake\Error\Debugger');
  464. $this->assertInstanceOf('Cake\Error\Debugger', $result);
  465. }
  466. /**
  467. * Test that exportVar() doesn't loop through recursive structures.
  468. *
  469. * @return void
  470. */
  471. public function testExportVarRecursion()
  472. {
  473. $output = Debugger::exportVar($GLOBALS);
  474. $this->assertContains("'GLOBALS' => [recursion]", $output);
  475. }
  476. /**
  477. * test trace exclude
  478. *
  479. * @return void
  480. */
  481. public function testTraceExclude()
  482. {
  483. $result = Debugger::trace();
  484. $this->assertRegExp('/^Cake\\\Test\\\TestCase\\\Error\\\DebuggerTest::testTraceExclude/', $result);
  485. $result = Debugger::trace([
  486. 'exclude' => ['Cake\Test\TestCase\Error\DebuggerTest::testTraceExclude']
  487. ]);
  488. $this->assertNotRegExp('/^Cake\\\Test\\\TestCase\\\Error\\\DebuggerTest::testTraceExclude/', $result);
  489. }
  490. /**
  491. * Tests that __debugInfo is used when available
  492. *
  493. * @return void
  494. */
  495. public function testDebugInfo()
  496. {
  497. $object = new DebuggableThing();
  498. $result = Debugger::exportVar($object, 2);
  499. $expected = <<<eos
  500. object(Cake\Test\TestCase\Error\DebuggableThing) {
  501. 'foo' => 'bar',
  502. 'inner' => object(Cake\Test\TestCase\Error\DebuggableThing) {}
  503. }
  504. eos;
  505. $this->assertEquals($expected, $result);
  506. }
  507. }