DebuggerTest.php 24 KB

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