DebuggerTest.php 23 KB

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