DebuggerTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. templatePath => null
  241. template => 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. viewVars => []
  254. Html => object(Cake\View\Helper\HtmlHelper) {}
  255. Form => object(Cake\View\Helper\FormHelper) {}
  256. int => (int) 2
  257. float => (float) 1.333
  258. [protected] _helpers => object(Cake\View\HelperRegistry) {}
  259. [protected] _ext => '.ctp'
  260. [protected] _passedVars => [
  261. (int) 0 => 'viewVars',
  262. (int) 1 => 'autoLayout',
  263. (int) 2 => 'helpers',
  264. (int) 3 => 'template',
  265. (int) 4 => 'layout',
  266. (int) 5 => 'name',
  267. (int) 6 => 'theme',
  268. (int) 7 => 'layoutPath',
  269. (int) 8 => 'templatePath',
  270. (int) 9 => 'plugin',
  271. (int) 10 => 'passedArgs'
  272. ]
  273. [protected] _paths => []
  274. [protected] _pathsForPlugin => []
  275. [protected] _parents => []
  276. [protected] _current => null
  277. [protected] _currentType => ''
  278. [protected] _stack => []
  279. [protected] _eventManager => object(Cake\Event\EventManager) {}
  280. [protected] _eventClass => '\Cake\Event\Event'
  281. [protected] _viewBuilder => null
  282. }
  283. TEXT;
  284. $this->assertTextEquals($expected, $result);
  285. $data = [
  286. 1 => 'Index one',
  287. 5 => 'Index five'
  288. ];
  289. $result = Debugger::exportVar($data);
  290. $expected = <<<TEXT
  291. [
  292. (int) 1 => 'Index one',
  293. (int) 5 => 'Index five'
  294. ]
  295. TEXT;
  296. $this->assertTextEquals($expected, $result);
  297. $data = [
  298. 'key' => [
  299. 'value'
  300. ]
  301. ];
  302. $result = Debugger::exportVar($data, 1);
  303. $expected = <<<TEXT
  304. [
  305. 'key' => [
  306. [maximum depth reached]
  307. ]
  308. ]
  309. TEXT;
  310. $this->assertTextEquals($expected, $result);
  311. $data = false;
  312. $result = Debugger::exportVar($data);
  313. $expected = <<<TEXT
  314. false
  315. TEXT;
  316. $this->assertTextEquals($expected, $result);
  317. $file = fopen('php://output', 'w');
  318. fclose($file);
  319. $result = Debugger::exportVar($file);
  320. $this->assertTextEquals('unknown', $result);
  321. }
  322. /**
  323. * Test exporting various kinds of false.
  324. *
  325. * @return void
  326. */
  327. public function testExportVarZero()
  328. {
  329. $data = [
  330. 'nothing' => '',
  331. 'null' => null,
  332. 'false' => false,
  333. 'szero' => '0',
  334. 'zero' => 0
  335. ];
  336. $result = Debugger::exportVar($data);
  337. $expected = <<<TEXT
  338. [
  339. 'nothing' => '',
  340. 'null' => null,
  341. 'false' => false,
  342. 'szero' => '0',
  343. 'zero' => (int) 0
  344. ]
  345. TEXT;
  346. $this->assertTextEquals($expected, $result);
  347. }
  348. /**
  349. * testLog method
  350. *
  351. * @return void
  352. */
  353. public function testLog()
  354. {
  355. $mock = $this->getMock('Cake\Log\Engine\BaseLog', ['log']);
  356. Log::config('test', ['engine' => $mock]);
  357. $mock->expects($this->at(0))
  358. ->method('log')
  359. ->with('debug', $this->logicalAnd(
  360. $this->stringContains('DebuggerTest::testLog'),
  361. $this->stringContains('cool')
  362. ));
  363. $mock->expects($this->at(1))
  364. ->method('log')
  365. ->with('debug', $this->logicalAnd(
  366. $this->stringContains('DebuggerTest::testLog'),
  367. $this->stringContains('[main]'),
  368. $this->stringContains("'whatever',"),
  369. $this->stringContains("'here'")
  370. ));
  371. Debugger::log('cool');
  372. Debugger::log(['whatever', 'here']);
  373. Log::drop('test');
  374. }
  375. /**
  376. * test log() depth
  377. *
  378. * @return void
  379. */
  380. public function testLogDepth()
  381. {
  382. $mock = $this->getMock('Cake\Log\Engine\BaseLog', ['log']);
  383. Log::config('test', ['engine' => $mock]);
  384. $mock->expects($this->at(0))
  385. ->method('log')
  386. ->with('debug', $this->logicalAnd(
  387. $this->stringContains('DebuggerTest::testLog'),
  388. $this->stringContains('test'),
  389. $this->logicalNot($this->stringContains('val'))
  390. ));
  391. $val = [
  392. 'test' => ['key' => 'val']
  393. ];
  394. Debugger::log($val, 'debug', 0);
  395. }
  396. /**
  397. * testDump method
  398. *
  399. * @return void
  400. */
  401. public function testDump()
  402. {
  403. $var = ['People' => [
  404. [
  405. 'name' => 'joeseph',
  406. 'coat' => 'technicolor',
  407. 'hair_color' => 'brown'
  408. ],
  409. [
  410. 'name' => 'Shaft',
  411. 'coat' => 'black',
  412. 'hair' => 'black'
  413. ]
  414. ]];
  415. ob_start();
  416. Debugger::dump($var);
  417. $result = ob_get_clean();
  418. $open = "\n";
  419. $close = "\n\n";
  420. $expected = <<<TEXT
  421. {$open}[
  422. 'People' => [
  423. (int) 0 => [
  424. 'name' => 'joeseph',
  425. 'coat' => 'technicolor',
  426. 'hair_color' => 'brown'
  427. ],
  428. (int) 1 => [
  429. 'name' => 'Shaft',
  430. 'coat' => 'black',
  431. 'hair' => 'black'
  432. ]
  433. ]
  434. ]{$close}
  435. TEXT;
  436. $this->assertTextEquals($expected, $result);
  437. ob_start();
  438. Debugger::dump($var, 1);
  439. $result = ob_get_clean();
  440. $expected = <<<TEXT
  441. {$open}[
  442. 'People' => [
  443. [maximum depth reached]
  444. ]
  445. ]{$close}
  446. TEXT;
  447. $this->assertTextEquals($expected, $result);
  448. }
  449. /**
  450. * test getInstance.
  451. *
  452. * @return void
  453. */
  454. public function testGetInstance()
  455. {
  456. $result = Debugger::getInstance();
  457. $this->assertInstanceOf('Cake\Error\Debugger', $result);
  458. $result = Debugger::getInstance(__NAMESPACE__ . '\DebuggerTestCaseDebugger');
  459. $this->assertInstanceOf(__NAMESPACE__ . '\DebuggerTestCaseDebugger', $result);
  460. $result = Debugger::getInstance();
  461. $this->assertInstanceOf(__NAMESPACE__ . '\DebuggerTestCaseDebugger', $result);
  462. $result = Debugger::getInstance('Cake\Error\Debugger');
  463. $this->assertInstanceOf('Cake\Error\Debugger', $result);
  464. }
  465. /**
  466. * Test that exportVar() doesn't loop through recursive structures.
  467. *
  468. * @return void
  469. */
  470. public function testExportVarRecursion()
  471. {
  472. $output = Debugger::exportVar($GLOBALS);
  473. $this->assertContains("'GLOBALS' => [recursion]", $output);
  474. }
  475. /**
  476. * test trace exclude
  477. *
  478. * @return void
  479. */
  480. public function testTraceExclude()
  481. {
  482. $result = Debugger::trace();
  483. $this->assertRegExp('/^Cake\\\Test\\\TestCase\\\Error\\\DebuggerTest::testTraceExclude/', $result);
  484. $result = Debugger::trace([
  485. 'exclude' => ['Cake\Test\TestCase\Error\DebuggerTest::testTraceExclude']
  486. ]);
  487. $this->assertNotRegExp('/^Cake\\\Test\\\TestCase\\\Error\\\DebuggerTest::testTraceExclude/', $result);
  488. }
  489. /**
  490. * Tests that __debugInfo is used when available
  491. *
  492. * @return void
  493. */
  494. public function testDebugInfo()
  495. {
  496. $object = new DebuggableThing();
  497. $result = Debugger::exportVar($object, 2);
  498. $expected = <<<eos
  499. object(Cake\Test\TestCase\Error\DebuggableThing) {
  500. 'foo' => 'bar',
  501. 'inner' => object(Cake\Test\TestCase\Error\DebuggableThing) {}
  502. }
  503. eos;
  504. $this->assertEquals($expected, $result);
  505. }
  506. }