DebuggerTest.php 22 KB

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