DebuggerTest.php 14 KB

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