DebuggerTest.php 22 KB

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