DebuggerTest.php 14 KB

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