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. /**
  22. * DebuggerTestCaseDebugger class
  23. */
  24. class DebuggerTestCaseDebugger extends Debugger
  25. {
  26. }
  27. class DebuggableThing
  28. {
  29. public function __debugInfo()
  30. {
  31. return ['foo' => 'bar', 'inner' => new self()];
  32. }
  33. }
  34. /**
  35. * DebuggerTest class
  36. *
  37. * !!! Be careful with changing code below as it may
  38. * !!! change line numbers which are used in the tests
  39. */
  40. class DebuggerTest extends TestCase
  41. {
  42. protected $_restoreError = false;
  43. /**
  44. * setUp method
  45. *
  46. * @return void
  47. */
  48. public function setUp()
  49. {
  50. parent::setUp();
  51. Configure::write('debug', true);
  52. Log::drop('stderr');
  53. Log::drop('stdout');
  54. }
  55. /**
  56. * tearDown method
  57. *
  58. * @return void
  59. */
  60. public function tearDown()
  61. {
  62. parent::tearDown();
  63. if ($this->_restoreError) {
  64. restore_error_handler();
  65. }
  66. }
  67. /**
  68. * testDocRef method
  69. *
  70. * @return void
  71. */
  72. public function testDocRef()
  73. {
  74. $this->skipIf(
  75. defined('HHVM_VERSION'),
  76. 'HHVM does not output doc references'
  77. );
  78. ini_set('docref_root', '');
  79. $this->assertEquals(ini_get('docref_root'), '');
  80. new Debugger();
  81. $this->assertEquals(ini_get('docref_root'), 'http://php.net/');
  82. }
  83. /**
  84. * test Excerpt writing
  85. *
  86. * @return void
  87. */
  88. public function testExcerpt()
  89. {
  90. $result = Debugger::excerpt(__FILE__, __LINE__ - 1, 2);
  91. $this->assertTrue(is_array($result));
  92. $this->assertCount(5, $result);
  93. $this->assertRegExp('/function(.+)testExcerpt/', $result[1]);
  94. $result = Debugger::excerpt(__FILE__, 2, 2);
  95. $this->assertTrue(is_array($result));
  96. $this->assertCount(4, $result);
  97. $this->skipIf(defined('HHVM_VERSION'), 'HHVM does not highlight php code');
  98. $pattern = '/<code>.*?<span style\="color\: \#\d+">.*?&lt;\?php/';
  99. $this->assertRegExp($pattern, $result[0]);
  100. $result = Debugger::excerpt(__FILE__, 11, 2);
  101. $this->assertCount(5, $result);
  102. $pattern = '/<span style\="color\: \#\d{6}">\*<\/span>/';
  103. $this->assertRegExp($pattern, $result[0]);
  104. $return = Debugger::excerpt('[internal]', 2, 2);
  105. $this->assertTrue(empty($return));
  106. $result = Debugger::excerpt(__FILE__, __LINE__, 5);
  107. $this->assertCount(11, $result);
  108. $this->assertContains('Debugger', $result[5]);
  109. $this->assertContains('excerpt', $result[5]);
  110. $this->assertContains('__FILE__', $result[5]);
  111. }
  112. /**
  113. * Test that outputAs works.
  114. *
  115. * @return void
  116. */
  117. public function testOutputAs()
  118. {
  119. Debugger::outputAs('html');
  120. $this->assertEquals('html', Debugger::outputAs());
  121. }
  122. /**
  123. * Test that choosing a non-existent format causes an exception
  124. *
  125. * @expectedException \InvalidArgumentException
  126. * @return void
  127. */
  128. public function testOutputAsException()
  129. {
  130. Debugger::outputAs('Invalid junk');
  131. }
  132. /**
  133. * Tests that changes in output formats using Debugger::output() change the templates used.
  134. *
  135. * @return void
  136. */
  137. public function testAddFormat()
  138. {
  139. Debugger::addFormat('js', [
  140. 'traceLine' => '{:reference} - <a href="txmt://open?url=file://{:file}' .
  141. '&line={:line}">{:path}</a>, line {:line}'
  142. ]);
  143. Debugger::outputAs('js');
  144. $result = Debugger::trace();
  145. $this->assertRegExp('/' . preg_quote('txmt://open?url=file://', '/') . '(\/|[A-Z]:\\\\)' . '/', $result);
  146. Debugger::addFormat('xml', [
  147. 'error' => '<error><code>{:code}</code><file>{:file}</file><line>{:line}</line>' .
  148. '{:description}</error>',
  149. ]);
  150. Debugger::outputAs('xml');
  151. ob_start();
  152. $debugger = Debugger::getInstance();
  153. $debugger->outputError([
  154. 'level' => E_NOTICE,
  155. 'code' => E_NOTICE,
  156. 'file' => __FILE__,
  157. 'line' => __LINE__,
  158. 'description' => 'Undefined variable: foo',
  159. ]);
  160. $result = ob_get_clean();
  161. $expected = [
  162. '<error',
  163. '<code', '8', '/code',
  164. '<file', 'preg:/[^<]+/', '/file',
  165. '<line', '' . ((int)__LINE__ - 9), '/line',
  166. 'preg:/Undefined variable:\s+foo/',
  167. '/error'
  168. ];
  169. $this->assertHtml($expected, $result, true);
  170. }
  171. /**
  172. * Test adding a format that is handled by a callback.
  173. *
  174. * @return void
  175. */
  176. public function testAddFormatCallback()
  177. {
  178. Debugger::addFormat('callback', ['callback' => [$this, 'customFormat']]);
  179. Debugger::outputAs('callback');
  180. ob_start();
  181. $debugger = Debugger::getInstance();
  182. $debugger->outputError([
  183. 'error' => 'Notice',
  184. 'code' => E_NOTICE,
  185. 'level' => E_NOTICE,
  186. 'description' => 'Undefined variable $foo',
  187. 'file' => __FILE__,
  188. 'line' => __LINE__,
  189. ]);
  190. $result = ob_get_clean();
  191. $this->assertContains('Notice: I eated an error', $result);
  192. $this->assertContains('DebuggerTest.php', $result);
  193. }
  194. /**
  195. * Test method for testing addFormat with callbacks.
  196. *
  197. * @return void
  198. */
  199. public function customFormat($error, $strings)
  200. {
  201. echo $error['error'] . ': I eated an error ' . $error['file'];
  202. }
  203. /**
  204. * testTrimPath method
  205. *
  206. * @return void
  207. */
  208. public function testTrimPath()
  209. {
  210. $this->assertEquals('APP/', Debugger::trimPath(APP));
  211. $this->assertEquals('CORE' . DS . 'src' . DS, Debugger::trimPath(CAKE));
  212. $this->assertEquals('Some/Other/Path', Debugger::trimPath('Some/Other/Path'));
  213. }
  214. /**
  215. * testExportVar method
  216. *
  217. * @return void
  218. */
  219. public function testExportVar()
  220. {
  221. $Controller = new Controller();
  222. $Controller->helpers = ['Html', 'Form'];
  223. $View = $Controller->createView();
  224. $View->int = 2;
  225. $View->float = 1.333;
  226. $result = Debugger::exportVar($View);
  227. $expected = <<<TEXT
  228. object(Cake\View\View) {
  229. Blocks => object(Cake\View\ViewBlock) {}
  230. plugin => null
  231. name => ''
  232. passedArgs => []
  233. helpers => [
  234. (int) 0 => 'Html',
  235. (int) 1 => 'Form'
  236. ]
  237. templatePath => null
  238. template => null
  239. layout => 'default'
  240. layoutPath => null
  241. autoLayout => true
  242. subDir => null
  243. theme => null
  244. hasRendered => false
  245. uuids => []
  246. request => object(Cake\Network\Request) {}
  247. response => object(Cake\Network\Response) {}
  248. elementCache => 'default'
  249. viewClass => null
  250. viewVars => []
  251. Html => object(Cake\View\Helper\HtmlHelper) {}
  252. Form => object(Cake\View\Helper\FormHelper) {}
  253. int => (int) 2
  254. float => (float) 1.333
  255. [protected] _helpers => object(Cake\View\HelperRegistry) {}
  256. [protected] _ext => '.ctp'
  257. [protected] _passedVars => [
  258. (int) 0 => 'viewVars',
  259. (int) 1 => 'autoLayout',
  260. (int) 2 => 'helpers',
  261. (int) 3 => 'template',
  262. (int) 4 => 'layout',
  263. (int) 5 => 'name',
  264. (int) 6 => 'theme',
  265. (int) 7 => 'layoutPath',
  266. (int) 8 => 'templatePath',
  267. (int) 9 => 'plugin',
  268. (int) 10 => 'passedArgs'
  269. ]
  270. [protected] _paths => []
  271. [protected] _pathsForPlugin => []
  272. [protected] _parents => []
  273. [protected] _current => null
  274. [protected] _currentType => ''
  275. [protected] _stack => []
  276. [protected] _eventManager => object(Cake\Event\EventManager) {}
  277. [protected] _eventClass => '\Cake\Event\Event'
  278. [protected] _viewBuilder => null
  279. }
  280. TEXT;
  281. $this->assertTextEquals($expected, $result);
  282. $data = [
  283. 1 => 'Index one',
  284. 5 => 'Index five'
  285. ];
  286. $result = Debugger::exportVar($data);
  287. $expected = <<<TEXT
  288. [
  289. (int) 1 => 'Index one',
  290. (int) 5 => 'Index five'
  291. ]
  292. TEXT;
  293. $this->assertTextEquals($expected, $result);
  294. $data = [
  295. 'key' => [
  296. 'value'
  297. ]
  298. ];
  299. $result = Debugger::exportVar($data, 1);
  300. $expected = <<<TEXT
  301. [
  302. 'key' => [
  303. [maximum depth reached]
  304. ]
  305. ]
  306. TEXT;
  307. $this->assertTextEquals($expected, $result);
  308. $data = false;
  309. $result = Debugger::exportVar($data);
  310. $expected = <<<TEXT
  311. false
  312. TEXT;
  313. $this->assertTextEquals($expected, $result);
  314. $file = fopen('php://output', 'w');
  315. fclose($file);
  316. $result = Debugger::exportVar($file);
  317. $this->assertTextEquals('unknown', $result);
  318. }
  319. /**
  320. * Test exporting various kinds of false.
  321. *
  322. * @return void
  323. */
  324. public function testExportVarZero()
  325. {
  326. $data = [
  327. 'nothing' => '',
  328. 'null' => null,
  329. 'false' => false,
  330. 'szero' => '0',
  331. 'zero' => 0
  332. ];
  333. $result = Debugger::exportVar($data);
  334. $expected = <<<TEXT
  335. [
  336. 'nothing' => '',
  337. 'null' => null,
  338. 'false' => false,
  339. 'szero' => '0',
  340. 'zero' => (int) 0
  341. ]
  342. TEXT;
  343. $this->assertTextEquals($expected, $result);
  344. }
  345. /**
  346. * testLog method
  347. *
  348. * @return void
  349. */
  350. public function testLog()
  351. {
  352. $mock = $this->getMockBuilder('Cake\Log\Engine\BaseLog')
  353. ->setMethods(['log'])
  354. ->getMock();
  355. Log::config('test', ['engine' => $mock]);
  356. $mock->expects($this->at(0))
  357. ->method('log')
  358. ->with('debug', $this->logicalAnd(
  359. $this->stringContains('DebuggerTest::testLog'),
  360. $this->stringContains('cool')
  361. ));
  362. $mock->expects($this->at(1))
  363. ->method('log')
  364. ->with('debug', $this->logicalAnd(
  365. $this->stringContains('DebuggerTest::testLog'),
  366. $this->stringContains('[main]'),
  367. $this->stringContains("'whatever',"),
  368. $this->stringContains("'here'")
  369. ));
  370. Debugger::log('cool');
  371. Debugger::log(['whatever', 'here']);
  372. Log::drop('test');
  373. }
  374. /**
  375. * test log() depth
  376. *
  377. * @return void
  378. */
  379. public function testLogDepth()
  380. {
  381. $mock = $this->getMockBuilder('Cake\Log\Engine\BaseLog')
  382. ->setMethods(['log'])
  383. ->getMock();
  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. }