DebuggerTest.php 14 KB

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