DebuggerTest.php 23 KB

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