DebuggerTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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. class SecurityThing
  35. {
  36. public $password = 'pass1234';
  37. }
  38. /**
  39. * DebuggerTest class
  40. *
  41. * !!! Be careful with changing code below as it may
  42. * !!! change line numbers which are used in the tests
  43. */
  44. class DebuggerTest extends TestCase
  45. {
  46. protected $_restoreError = false;
  47. /**
  48. * setUp method
  49. *
  50. * @return void
  51. */
  52. public function setUp()
  53. {
  54. parent::setUp();
  55. Configure::write('debug', true);
  56. Log::drop('stderr');
  57. Log::drop('stdout');
  58. }
  59. /**
  60. * tearDown method
  61. *
  62. * @return void
  63. */
  64. public function tearDown()
  65. {
  66. parent::tearDown();
  67. if ($this->_restoreError) {
  68. restore_error_handler();
  69. }
  70. }
  71. /**
  72. * testDocRef method
  73. *
  74. * @return void
  75. */
  76. public function testDocRef()
  77. {
  78. $this->skipIf(
  79. defined('HHVM_VERSION'),
  80. 'HHVM does not output doc references'
  81. );
  82. ini_set('docref_root', '');
  83. $this->assertEquals(ini_get('docref_root'), '');
  84. new Debugger();
  85. $this->assertEquals(ini_get('docref_root'), 'http://php.net/');
  86. }
  87. /**
  88. * test Excerpt writing
  89. *
  90. * @return void
  91. */
  92. public function testExcerpt()
  93. {
  94. $result = Debugger::excerpt(__FILE__, __LINE__ - 1, 2);
  95. $this->assertTrue(is_array($result));
  96. $this->assertCount(5, $result);
  97. $this->assertRegExp('/function(.+)testExcerpt/', $result[1]);
  98. $result = Debugger::excerpt(__FILE__, 2, 2);
  99. $this->assertTrue(is_array($result));
  100. $this->assertCount(4, $result);
  101. $this->skipIf(defined('HHVM_VERSION'), 'HHVM does not highlight php code');
  102. $pattern = '/<code>.*?<span style\="color\: \#\d+">.*?&lt;\?php/';
  103. $this->assertRegExp($pattern, $result[0]);
  104. $result = Debugger::excerpt(__FILE__, 11, 2);
  105. $this->assertCount(5, $result);
  106. $pattern = '/<span style\="color\: \#\d{6}">\*<\/span>/';
  107. $this->assertRegExp($pattern, $result[0]);
  108. $return = Debugger::excerpt('[internal]', 2, 2);
  109. $this->assertTrue(empty($return));
  110. $result = Debugger::excerpt(__FILE__, __LINE__, 5);
  111. $this->assertCount(11, $result);
  112. $this->assertContains('Debugger', $result[5]);
  113. $this->assertContains('excerpt', $result[5]);
  114. $this->assertContains('__FILE__', $result[5]);
  115. }
  116. /**
  117. * Test that outputAs works.
  118. *
  119. * @return void
  120. */
  121. public function testOutputAs()
  122. {
  123. Debugger::outputAs('html');
  124. $this->assertEquals('html', Debugger::outputAs());
  125. }
  126. /**
  127. * Test that choosing a non-existent format causes an exception
  128. *
  129. * @expectedException \InvalidArgumentException
  130. * @return void
  131. */
  132. public function testOutputAsException()
  133. {
  134. Debugger::outputAs('Invalid junk');
  135. }
  136. /**
  137. * Test outputError with description encoding
  138. *
  139. * @return void
  140. */
  141. public function testOutputErrorDescriptionEncoding()
  142. {
  143. Debugger::outputAs('html');
  144. ob_start();
  145. $debugger = Debugger::getInstance();
  146. $debugger->outputError([
  147. 'error' => 'Notice',
  148. 'code' => E_NOTICE,
  149. 'level' => E_NOTICE,
  150. 'description' => 'Undefined index <script>alert(1)</script>',
  151. 'file' => __FILE__,
  152. 'line' => __LINE__,
  153. ]);
  154. $result = ob_get_clean();
  155. $this->assertContains('&lt;script&gt;', $result);
  156. $this->assertNotContains('<script>', $result);
  157. }
  158. /**
  159. * Tests that changes in output formats using Debugger::output() change the templates used.
  160. *
  161. * @return void
  162. */
  163. public function testAddFormat()
  164. {
  165. Debugger::addFormat('js', [
  166. 'traceLine' => '{:reference} - <a href="txmt://open?url=file://{:file}' .
  167. '&line={:line}">{:path}</a>, line {:line}'
  168. ]);
  169. Debugger::outputAs('js');
  170. $result = Debugger::trace();
  171. $this->assertRegExp('/' . preg_quote('txmt://open?url=file://', '/') . '(\/|[A-Z]:\\\\)' . '/', $result);
  172. Debugger::addFormat('xml', [
  173. 'error' => '<error><code>{:code}</code><file>{:file}</file><line>{:line}</line>' .
  174. '{:description}</error>',
  175. ]);
  176. Debugger::outputAs('xml');
  177. ob_start();
  178. $debugger = Debugger::getInstance();
  179. $debugger->outputError([
  180. 'level' => E_NOTICE,
  181. 'code' => E_NOTICE,
  182. 'file' => __FILE__,
  183. 'line' => __LINE__,
  184. 'description' => 'Undefined variable: foo',
  185. ]);
  186. $result = ob_get_clean();
  187. $expected = [
  188. '<error',
  189. '<code', '8', '/code',
  190. '<file', 'preg:/[^<]+/', '/file',
  191. '<line', '' . ((int)__LINE__ - 9), '/line',
  192. 'preg:/Undefined variable:\s+foo/',
  193. '/error'
  194. ];
  195. $this->assertHtml($expected, $result, true);
  196. }
  197. /**
  198. * Test adding a format that is handled by a callback.
  199. *
  200. * @return void
  201. */
  202. public function testAddFormatCallback()
  203. {
  204. Debugger::addFormat('callback', ['callback' => [$this, 'customFormat']]);
  205. Debugger::outputAs('callback');
  206. ob_start();
  207. $debugger = Debugger::getInstance();
  208. $debugger->outputError([
  209. 'error' => 'Notice',
  210. 'code' => E_NOTICE,
  211. 'level' => E_NOTICE,
  212. 'description' => 'Undefined variable $foo',
  213. 'file' => __FILE__,
  214. 'line' => __LINE__,
  215. ]);
  216. $result = ob_get_clean();
  217. $this->assertContains('Notice: I eated an error', $result);
  218. $this->assertContains('DebuggerTest.php', $result);
  219. }
  220. /**
  221. * Test method for testing addFormat with callbacks.
  222. *
  223. * @return void
  224. */
  225. public function customFormat($error, $strings)
  226. {
  227. echo $error['error'] . ': I eated an error ' . $error['file'];
  228. }
  229. /**
  230. * testTrimPath method
  231. *
  232. * @return void
  233. */
  234. public function testTrimPath()
  235. {
  236. $this->assertEquals('APP/', Debugger::trimPath(APP));
  237. $this->assertEquals('CORE' . DS . 'src' . DS, Debugger::trimPath(CAKE));
  238. $this->assertEquals('Some/Other/Path', Debugger::trimPath('Some/Other/Path'));
  239. }
  240. /**
  241. * testExportVar method
  242. *
  243. * @return void
  244. */
  245. public function testExportVar()
  246. {
  247. $Controller = new Controller();
  248. $Controller->helpers = ['Html', 'Form'];
  249. $View = $Controller->createView();
  250. $View->int = 2;
  251. $View->float = 1.333;
  252. $result = Debugger::exportVar($View);
  253. $expected = <<<TEXT
  254. object(Cake\View\View) {
  255. Blocks => object(Cake\View\ViewBlock) {}
  256. plugin => null
  257. name => ''
  258. passedArgs => []
  259. helpers => [
  260. (int) 0 => 'Html',
  261. (int) 1 => 'Form'
  262. ]
  263. templatePath => null
  264. template => null
  265. layout => 'default'
  266. layoutPath => null
  267. autoLayout => true
  268. subDir => null
  269. theme => null
  270. hasRendered => false
  271. uuids => []
  272. request => object(Cake\Http\ServerRequest) {}
  273. response => object(Cake\Http\Response) {}
  274. elementCache => 'default'
  275. viewClass => null
  276. viewVars => []
  277. Html => object(Cake\View\Helper\HtmlHelper) {}
  278. Form => object(Cake\View\Helper\FormHelper) {}
  279. int => (int) 2
  280. float => (float) 1.333
  281. [protected] _helpers => object(Cake\View\HelperRegistry) {}
  282. [protected] _ext => '.ctp'
  283. [protected] _passedVars => [
  284. (int) 0 => 'viewVars',
  285. (int) 1 => 'autoLayout',
  286. (int) 2 => 'helpers',
  287. (int) 3 => 'template',
  288. (int) 4 => 'layout',
  289. (int) 5 => 'name',
  290. (int) 6 => 'theme',
  291. (int) 7 => 'layoutPath',
  292. (int) 8 => 'templatePath',
  293. (int) 9 => 'plugin',
  294. (int) 10 => 'passedArgs'
  295. ]
  296. [protected] _paths => []
  297. [protected] _pathsForPlugin => []
  298. [protected] _parents => []
  299. [protected] _current => null
  300. [protected] _currentType => ''
  301. [protected] _stack => []
  302. [protected] _eventManager => object(Cake\Event\EventManager) {}
  303. [protected] _eventClass => '\Cake\Event\Event'
  304. [protected] _viewBuilder => null
  305. }
  306. TEXT;
  307. $this->assertTextEquals($expected, $result);
  308. $data = [
  309. 1 => 'Index one',
  310. 5 => 'Index five'
  311. ];
  312. $result = Debugger::exportVar($data);
  313. $expected = <<<TEXT
  314. [
  315. (int) 1 => 'Index one',
  316. (int) 5 => 'Index five'
  317. ]
  318. TEXT;
  319. $this->assertTextEquals($expected, $result);
  320. $data = [
  321. 'key' => [
  322. 'value'
  323. ]
  324. ];
  325. $result = Debugger::exportVar($data, 1);
  326. $expected = <<<TEXT
  327. [
  328. 'key' => [
  329. [maximum depth reached]
  330. ]
  331. ]
  332. TEXT;
  333. $this->assertTextEquals($expected, $result);
  334. $data = false;
  335. $result = Debugger::exportVar($data);
  336. $expected = <<<TEXT
  337. false
  338. TEXT;
  339. $this->assertTextEquals($expected, $result);
  340. $file = fopen('php://output', 'w');
  341. fclose($file);
  342. $result = Debugger::exportVar($file);
  343. $this->assertTextEquals('unknown', $result);
  344. }
  345. /**
  346. * Test exporting various kinds of false.
  347. *
  348. * @return void
  349. */
  350. public function testExportVarZero()
  351. {
  352. $data = [
  353. 'nothing' => '',
  354. 'null' => null,
  355. 'false' => false,
  356. 'szero' => '0',
  357. 'zero' => 0
  358. ];
  359. $result = Debugger::exportVar($data);
  360. $expected = <<<TEXT
  361. [
  362. 'nothing' => '',
  363. 'null' => null,
  364. 'false' => false,
  365. 'szero' => '0',
  366. 'zero' => (int) 0
  367. ]
  368. TEXT;
  369. $this->assertTextEquals($expected, $result);
  370. }
  371. /**
  372. * testLog method
  373. *
  374. * @return void
  375. */
  376. public function testLog()
  377. {
  378. $mock = $this->getMockBuilder('Cake\Log\Engine\BaseLog')
  379. ->setMethods(['log'])
  380. ->getMock();
  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('cool')
  387. ));
  388. $mock->expects($this->at(1))
  389. ->method('log')
  390. ->with('debug', $this->logicalAnd(
  391. $this->stringContains('DebuggerTest::testLog'),
  392. $this->stringContains('[main]'),
  393. $this->stringContains("'whatever',"),
  394. $this->stringContains("'here'")
  395. ));
  396. Debugger::log('cool');
  397. Debugger::log(['whatever', 'here']);
  398. Log::drop('test');
  399. }
  400. /**
  401. * test log() depth
  402. *
  403. * @return void
  404. */
  405. public function testLogDepth()
  406. {
  407. $mock = $this->getMockBuilder('Cake\Log\Engine\BaseLog')
  408. ->setMethods(['log'])
  409. ->getMock();
  410. Log::config('test', ['engine' => $mock]);
  411. $mock->expects($this->at(0))
  412. ->method('log')
  413. ->with('debug', $this->logicalAnd(
  414. $this->stringContains('DebuggerTest::testLog'),
  415. $this->stringContains('test'),
  416. $this->logicalNot($this->stringContains('val'))
  417. ));
  418. $val = [
  419. 'test' => ['key' => 'val']
  420. ];
  421. Debugger::log($val, 'debug', 0);
  422. }
  423. /**
  424. * testDump method
  425. *
  426. * @return void
  427. */
  428. public function testDump()
  429. {
  430. $var = ['People' => [
  431. [
  432. 'name' => 'joeseph',
  433. 'coat' => 'technicolor',
  434. 'hair_color' => 'brown'
  435. ],
  436. [
  437. 'name' => 'Shaft',
  438. 'coat' => 'black',
  439. 'hair' => 'black'
  440. ]
  441. ]];
  442. ob_start();
  443. Debugger::dump($var);
  444. $result = ob_get_clean();
  445. $open = "\n";
  446. $close = "\n\n";
  447. $expected = <<<TEXT
  448. {$open}[
  449. 'People' => [
  450. (int) 0 => [
  451. 'name' => 'joeseph',
  452. 'coat' => 'technicolor',
  453. 'hair_color' => 'brown'
  454. ],
  455. (int) 1 => [
  456. 'name' => 'Shaft',
  457. 'coat' => 'black',
  458. 'hair' => 'black'
  459. ]
  460. ]
  461. ]{$close}
  462. TEXT;
  463. $this->assertTextEquals($expected, $result);
  464. ob_start();
  465. Debugger::dump($var, 1);
  466. $result = ob_get_clean();
  467. $expected = <<<TEXT
  468. {$open}[
  469. 'People' => [
  470. [maximum depth reached]
  471. ]
  472. ]{$close}
  473. TEXT;
  474. $this->assertTextEquals($expected, $result);
  475. }
  476. /**
  477. * test getInstance.
  478. *
  479. * @return void
  480. */
  481. public function testGetInstance()
  482. {
  483. $result = Debugger::getInstance();
  484. $this->assertInstanceOf('Cake\Error\Debugger', $result);
  485. $result = Debugger::getInstance(__NAMESPACE__ . '\DebuggerTestCaseDebugger');
  486. $this->assertInstanceOf(__NAMESPACE__ . '\DebuggerTestCaseDebugger', $result);
  487. $result = Debugger::getInstance();
  488. $this->assertInstanceOf(__NAMESPACE__ . '\DebuggerTestCaseDebugger', $result);
  489. $result = Debugger::getInstance('Cake\Error\Debugger');
  490. $this->assertInstanceOf('Cake\Error\Debugger', $result);
  491. }
  492. /**
  493. * Test that exportVar() doesn't loop through recursive structures.
  494. *
  495. * @return void
  496. */
  497. public function testExportVarRecursion()
  498. {
  499. $output = Debugger::exportVar($GLOBALS);
  500. $this->assertContains("'GLOBALS' => [recursion]", $output);
  501. }
  502. /**
  503. * test trace exclude
  504. *
  505. * @return void
  506. */
  507. public function testTraceExclude()
  508. {
  509. $result = Debugger::trace();
  510. $this->assertRegExp('/^Cake\\\Test\\\TestCase\\\Error\\\DebuggerTest::testTraceExclude/', $result);
  511. $result = Debugger::trace([
  512. 'exclude' => ['Cake\Test\TestCase\Error\DebuggerTest::testTraceExclude']
  513. ]);
  514. $this->assertNotRegExp('/^Cake\\\Test\\\TestCase\\\Error\\\DebuggerTest::testTraceExclude/', $result);
  515. }
  516. /**
  517. * Tests that __debugInfo is used when available
  518. *
  519. * @return void
  520. */
  521. public function testDebugInfo()
  522. {
  523. $object = new DebuggableThing();
  524. $result = Debugger::exportVar($object, 2);
  525. $expected = <<<eos
  526. object(Cake\Test\TestCase\Error\DebuggableThing) {
  527. 'foo' => 'bar',
  528. 'inner' => object(Cake\Test\TestCase\Error\DebuggableThing) {}
  529. }
  530. eos;
  531. $this->assertEquals($expected, $result);
  532. }
  533. /**
  534. * Tests reading the output mask settings.
  535. *
  536. * @return void
  537. */
  538. public function testSetOutputMask()
  539. {
  540. Debugger::setOutputMask(['password' => '[**********]']);
  541. $this->assertEquals(['password' => '[**********]'], Debugger::outputMask());
  542. Debugger::setOutputMask(['serial' => 'XXXXXX']);
  543. $this->assertEquals(['password' => '[**********]', 'serial' => 'XXXXXX'], Debugger::outputMask());
  544. Debugger::setOutputMask([], false);
  545. $this->assertEquals([], Debugger::outputMask());
  546. }
  547. /**
  548. * Tests the masking of an array key.
  549. *
  550. * @return void
  551. */
  552. public function testMaskArray()
  553. {
  554. Debugger::setOutputMask(['password' => '[**********]']);
  555. $result = Debugger::exportVar(['password' => 'pass1234']);
  556. $expected = "['password'=>[**********]]";
  557. $this->assertEquals($expected, preg_replace('/\s+/', '', $result));
  558. }
  559. /**
  560. * Tests the masking of an array key.
  561. *
  562. * @return void
  563. */
  564. public function testMaskObject()
  565. {
  566. Debugger::setOutputMask(['password' => '[**********]']);
  567. $object = new SecurityThing();
  568. $result = Debugger::exportVar($object);
  569. $expected = "object(Cake\\Test\\TestCase\\Error\\SecurityThing){password=>[**********]}";
  570. $this->assertEquals($expected, preg_replace('/\s+/', '', $result));
  571. }
  572. /**
  573. * test testPrintVar()
  574. *
  575. * @return void
  576. */
  577. public function testPrintVar()
  578. {
  579. ob_start();
  580. Debugger::printVar('this-is-a-test', ['file' => __FILE__, 'line' => __LINE__], false);
  581. $result = ob_get_clean();
  582. $expectedText = <<<EXPECTED
  583. %s (line %d)
  584. ########## DEBUG ##########
  585. 'this-is-a-test'
  586. ###########################
  587. EXPECTED;
  588. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  589. $this->assertEquals($expected, $result);
  590. ob_start();
  591. $value = '<div>this-is-a-test</div>';
  592. Debugger::printVar($value, ['file' => __FILE__, 'line' => __LINE__], true);
  593. $result = ob_get_clean();
  594. $expectedHtml = <<<EXPECTED
  595. <div class="cake-debug-output">
  596. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  597. <pre class="cake-debug">
  598. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  599. </pre>
  600. </div>
  601. EXPECTED;
  602. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  603. $this->assertEquals($expected, $result);
  604. ob_start();
  605. Debugger::printVar('<div>this-is-a-test</div>', ['file' => __FILE__, 'line' => __LINE__], true);
  606. $result = ob_get_clean();
  607. $expected = <<<EXPECTED
  608. <div class="cake-debug-output">
  609. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  610. <pre class="cake-debug">
  611. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  612. </pre>
  613. </div>
  614. EXPECTED;
  615. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  616. $this->assertEquals($expected, $result);
  617. ob_start();
  618. Debugger::printVar('<div>this-is-a-test</div>', [], true);
  619. $result = ob_get_clean();
  620. $expected = <<<EXPECTED
  621. <div class="cake-debug-output">
  622. <pre class="cake-debug">
  623. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  624. </pre>
  625. </div>
  626. EXPECTED;
  627. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  628. $this->assertEquals($expected, $result);
  629. ob_start();
  630. Debugger::printVar('<div>this-is-a-test</div>', ['file' => __FILE__, 'line' => __LINE__]);
  631. $result = ob_get_clean();
  632. $expectedHtml = <<<EXPECTED
  633. <div class="cake-debug-output">
  634. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  635. <pre class="cake-debug">
  636. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  637. </pre>
  638. </div>
  639. EXPECTED;
  640. $expectedText = <<<EXPECTED
  641. %s (line %d)
  642. ########## DEBUG ##########
  643. '<div>this-is-a-test</div>'
  644. ###########################
  645. EXPECTED;
  646. if ((PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg')) {
  647. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
  648. } else {
  649. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
  650. }
  651. $this->assertEquals($expected, $result);
  652. ob_start();
  653. Debugger::printVar('<div>this-is-a-test</div>');
  654. $result = ob_get_clean();
  655. $expectedHtml = <<<EXPECTED
  656. <div class="cake-debug-output">
  657. <pre class="cake-debug">
  658. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  659. </pre>
  660. </div>
  661. EXPECTED;
  662. $expectedText = <<<EXPECTED
  663. ########## DEBUG ##########
  664. '<div>this-is-a-test</div>'
  665. ###########################
  666. EXPECTED;
  667. if ((PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg')) {
  668. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
  669. } else {
  670. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
  671. }
  672. $this->assertEquals($expected, $result);
  673. ob_start();
  674. Debugger::printVar('<div>this-is-a-test</div>', ['file' => __FILE__, 'line' => __LINE__], false);
  675. $result = ob_get_clean();
  676. $expected = <<<EXPECTED
  677. %s (line %d)
  678. ########## DEBUG ##########
  679. '<div>this-is-a-test</div>'
  680. ###########################
  681. EXPECTED;
  682. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  683. $this->assertEquals($expected, $result);
  684. ob_start();
  685. Debugger::printVar('<div>this-is-a-test</div>', ['file' => __FILE__, 'line' => __LINE__], false);
  686. $result = ob_get_clean();
  687. $expected = <<<EXPECTED
  688. %s (line %d)
  689. ########## DEBUG ##########
  690. '<div>this-is-a-test</div>'
  691. ###########################
  692. EXPECTED;
  693. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  694. $this->assertEquals($expected, $result);
  695. ob_start();
  696. Debugger::printVar('<div>this-is-a-test</div>', [], false);
  697. $result = ob_get_clean();
  698. $expected = <<<EXPECTED
  699. ########## DEBUG ##########
  700. '<div>this-is-a-test</div>'
  701. ###########################
  702. EXPECTED;
  703. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  704. $this->assertEquals($expected, $result);
  705. ob_start();
  706. Debugger::printVar(false, [], false);
  707. $result = ob_get_clean();
  708. $expected = <<<EXPECTED
  709. ########## DEBUG ##########
  710. false
  711. ###########################
  712. EXPECTED;
  713. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  714. $this->assertEquals($expected, $result);
  715. }
  716. }