DebuggerTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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. Blocks => object(Cake\View\ViewBlock) {}
  320. plugin => null
  321. name => ''
  322. passedArgs => []
  323. helpers => [
  324. (int) 0 => 'Html',
  325. (int) 1 => 'Form'
  326. ]
  327. templatePath => null
  328. template => null
  329. layout => 'default'
  330. layoutPath => null
  331. autoLayout => true
  332. subDir => null
  333. theme => null
  334. hasRendered => false
  335. uuids => []
  336. request => object(Cake\Http\ServerRequest) {}
  337. response => object(Cake\Http\Response) {}
  338. elementCache => 'default'
  339. viewClass => null
  340. viewVars => []
  341. Html => object(Cake\View\Helper\HtmlHelper) {}
  342. Form => object(Cake\View\Helper\FormHelper) {}
  343. int => (int) 2
  344. float => (float) 1.333
  345. string => ' '
  346. [protected] _helpers => object(Cake\View\HelperRegistry) {}
  347. [protected] _ext => '.ctp'
  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] _eventManager => object(Cake\Event\EventManager) {}
  368. [protected] _eventClass => '\Cake\Event\Event'
  369. [protected] _viewBuilder => null
  370. }
  371. TEXT;
  372. $this->assertTextEquals($expected, $result);
  373. $data = [
  374. 1 => 'Index one',
  375. 5 => 'Index five'
  376. ];
  377. $result = Debugger::exportVar($data);
  378. $expected = <<<TEXT
  379. [
  380. (int) 1 => 'Index one',
  381. (int) 5 => 'Index five'
  382. ]
  383. TEXT;
  384. $this->assertTextEquals($expected, $result);
  385. $data = [
  386. 'key' => [
  387. 'value'
  388. ]
  389. ];
  390. $result = Debugger::exportVar($data, 1);
  391. $expected = <<<TEXT
  392. [
  393. 'key' => [
  394. [maximum depth reached]
  395. ]
  396. ]
  397. TEXT;
  398. $this->assertTextEquals($expected, $result);
  399. $data = false;
  400. $result = Debugger::exportVar($data);
  401. $expected = <<<TEXT
  402. false
  403. TEXT;
  404. $this->assertTextEquals($expected, $result);
  405. $file = fopen('php://output', 'w');
  406. fclose($file);
  407. $result = Debugger::exportVar($file);
  408. $this->assertTextEquals('unknown', $result);
  409. }
  410. /**
  411. * Test exporting various kinds of false.
  412. *
  413. * @return void
  414. */
  415. public function testExportVarZero()
  416. {
  417. $data = [
  418. 'nothing' => '',
  419. 'null' => null,
  420. 'false' => false,
  421. 'szero' => '0',
  422. 'zero' => 0
  423. ];
  424. $result = Debugger::exportVar($data);
  425. $expected = <<<TEXT
  426. [
  427. 'nothing' => '',
  428. 'null' => null,
  429. 'false' => false,
  430. 'szero' => '0',
  431. 'zero' => (int) 0
  432. ]
  433. TEXT;
  434. $this->assertTextEquals($expected, $result);
  435. }
  436. /**
  437. * testLog method
  438. *
  439. * @return void
  440. */
  441. public function testLog()
  442. {
  443. $mock = $this->getMockBuilder('Cake\Log\Engine\BaseLog')
  444. ->setMethods(['log'])
  445. ->getMock();
  446. Log::setConfig('test', ['engine' => $mock]);
  447. $mock->expects($this->at(0))
  448. ->method('log')
  449. ->with('debug', $this->logicalAnd(
  450. $this->stringContains('DebuggerTest::testLog'),
  451. $this->stringContains('cool')
  452. ));
  453. $mock->expects($this->at(1))
  454. ->method('log')
  455. ->with('debug', $this->logicalAnd(
  456. $this->stringContains('DebuggerTest::testLog'),
  457. $this->stringContains('[main]'),
  458. $this->stringContains("'whatever',"),
  459. $this->stringContains("'here'")
  460. ));
  461. Debugger::log('cool');
  462. Debugger::log(['whatever', 'here']);
  463. Log::drop('test');
  464. }
  465. /**
  466. * test log() depth
  467. *
  468. * @return void
  469. */
  470. public function testLogDepth()
  471. {
  472. $mock = $this->getMockBuilder('Cake\Log\Engine\BaseLog')
  473. ->setMethods(['log'])
  474. ->getMock();
  475. Log::setConfig('test', ['engine' => $mock]);
  476. $mock->expects($this->at(0))
  477. ->method('log')
  478. ->with('debug', $this->logicalAnd(
  479. $this->stringContains('DebuggerTest::testLog'),
  480. $this->stringContains('test'),
  481. $this->logicalNot($this->stringContains('val'))
  482. ));
  483. $val = [
  484. 'test' => ['key' => 'val']
  485. ];
  486. Debugger::log($val, 'debug', 0);
  487. }
  488. /**
  489. * testDump method
  490. *
  491. * @return void
  492. */
  493. public function testDump()
  494. {
  495. $var = ['People' => [
  496. [
  497. 'name' => 'joeseph',
  498. 'coat' => 'technicolor',
  499. 'hair_color' => 'brown'
  500. ],
  501. [
  502. 'name' => 'Shaft',
  503. 'coat' => 'black',
  504. 'hair' => 'black'
  505. ]
  506. ]];
  507. ob_start();
  508. Debugger::dump($var);
  509. $result = ob_get_clean();
  510. $open = "\n";
  511. $close = "\n\n";
  512. $expected = <<<TEXT
  513. {$open}[
  514. 'People' => [
  515. (int) 0 => [
  516. 'name' => 'joeseph',
  517. 'coat' => 'technicolor',
  518. 'hair_color' => 'brown'
  519. ],
  520. (int) 1 => [
  521. 'name' => 'Shaft',
  522. 'coat' => 'black',
  523. 'hair' => 'black'
  524. ]
  525. ]
  526. ]{$close}
  527. TEXT;
  528. $this->assertTextEquals($expected, $result);
  529. ob_start();
  530. Debugger::dump($var, 1);
  531. $result = ob_get_clean();
  532. $expected = <<<TEXT
  533. {$open}[
  534. 'People' => [
  535. [maximum depth reached]
  536. ]
  537. ]{$close}
  538. TEXT;
  539. $this->assertTextEquals($expected, $result);
  540. }
  541. /**
  542. * test getInstance.
  543. *
  544. * @return void
  545. */
  546. public function testGetInstance()
  547. {
  548. $result = Debugger::getInstance();
  549. $this->assertInstanceOf('Cake\Error\Debugger', $result);
  550. $result = Debugger::getInstance(__NAMESPACE__ . '\DebuggerTestCaseDebugger');
  551. $this->assertInstanceOf(__NAMESPACE__ . '\DebuggerTestCaseDebugger', $result);
  552. $result = Debugger::getInstance();
  553. $this->assertInstanceOf(__NAMESPACE__ . '\DebuggerTestCaseDebugger', $result);
  554. $result = Debugger::getInstance('Cake\Error\Debugger');
  555. $this->assertInstanceOf('Cake\Error\Debugger', $result);
  556. }
  557. /**
  558. * Test that exportVar() doesn't loop through recursive structures.
  559. *
  560. * @return void
  561. */
  562. public function testExportVarRecursion()
  563. {
  564. $output = Debugger::exportVar($GLOBALS);
  565. $this->assertContains("'GLOBALS' => [recursion]", $output);
  566. }
  567. /**
  568. * test trace exclude
  569. *
  570. * @return void
  571. */
  572. public function testTraceExclude()
  573. {
  574. $result = Debugger::trace();
  575. $this->assertRegExp('/^Cake\\\Test\\\TestCase\\\Error\\\DebuggerTest::testTraceExclude/', $result);
  576. $result = Debugger::trace([
  577. 'exclude' => ['Cake\Test\TestCase\Error\DebuggerTest::testTraceExclude']
  578. ]);
  579. $this->assertNotRegExp('/^Cake\\\Test\\\TestCase\\\Error\\\DebuggerTest::testTraceExclude/', $result);
  580. }
  581. /**
  582. * Tests that __debugInfo is used when available
  583. *
  584. * @return void
  585. */
  586. public function testDebugInfo()
  587. {
  588. $object = new DebuggableThing();
  589. $result = Debugger::exportVar($object, 2);
  590. $expected = <<<eos
  591. object(Cake\Test\TestCase\Error\DebuggableThing) {
  592. 'foo' => 'bar',
  593. 'inner' => object(Cake\Test\TestCase\Error\DebuggableThing) {}
  594. }
  595. eos;
  596. $this->assertEquals($expected, $result);
  597. }
  598. /**
  599. * Tests reading the output mask settings.
  600. *
  601. * @return void
  602. */
  603. public function testSetOutputMask()
  604. {
  605. Debugger::setOutputMask(['password' => '[**********]']);
  606. $this->assertEquals(['password' => '[**********]'], Debugger::outputMask());
  607. Debugger::setOutputMask(['serial' => 'XXXXXX']);
  608. $this->assertEquals(['password' => '[**********]', 'serial' => 'XXXXXX'], Debugger::outputMask());
  609. Debugger::setOutputMask([], false);
  610. $this->assertEquals([], Debugger::outputMask());
  611. }
  612. /**
  613. * Tests the masking of an array key.
  614. *
  615. * @return void
  616. */
  617. public function testMaskArray()
  618. {
  619. Debugger::setOutputMask(['password' => '[**********]']);
  620. $result = Debugger::exportVar(['password' => 'pass1234']);
  621. $expected = "['password'=>[**********]]";
  622. $this->assertEquals($expected, preg_replace('/\s+/', '', $result));
  623. }
  624. /**
  625. * Tests the masking of an array key.
  626. *
  627. * @return void
  628. */
  629. public function testMaskObject()
  630. {
  631. Debugger::setOutputMask(['password' => '[**********]']);
  632. $object = new SecurityThing();
  633. $result = Debugger::exportVar($object);
  634. $expected = 'object(Cake\\Test\\TestCase\\Error\\SecurityThing){password=>[**********]}';
  635. $this->assertEquals($expected, preg_replace('/\s+/', '', $result));
  636. }
  637. /**
  638. * test testPrintVar()
  639. *
  640. * @return void
  641. */
  642. public function testPrintVar()
  643. {
  644. ob_start();
  645. Debugger::printVar('this-is-a-test', ['file' => __FILE__, 'line' => __LINE__], false);
  646. $result = ob_get_clean();
  647. $expectedText = <<<EXPECTED
  648. %s (line %d)
  649. ########## DEBUG ##########
  650. 'this-is-a-test'
  651. ###########################
  652. EXPECTED;
  653. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  654. $this->assertEquals($expected, $result);
  655. ob_start();
  656. $value = '<div>this-is-a-test</div>';
  657. Debugger::printVar($value, ['file' => __FILE__, 'line' => __LINE__], true);
  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. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  668. $this->assertEquals($expected, $result);
  669. ob_start();
  670. Debugger::printVar('<div>this-is-a-test</div>', ['file' => __FILE__, 'line' => __LINE__], true);
  671. $result = ob_get_clean();
  672. $expected = <<<EXPECTED
  673. <div class="cake-debug-output">
  674. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  675. <pre class="cake-debug">
  676. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  677. </pre>
  678. </div>
  679. EXPECTED;
  680. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  681. $this->assertEquals($expected, $result);
  682. ob_start();
  683. Debugger::printVar('<div>this-is-a-test</div>', [], true);
  684. $result = ob_get_clean();
  685. $expected = <<<EXPECTED
  686. <div class="cake-debug-output">
  687. <pre class="cake-debug">
  688. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  689. </pre>
  690. </div>
  691. EXPECTED;
  692. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  693. $this->assertEquals($expected, $result);
  694. ob_start();
  695. Debugger::printVar('<div>this-is-a-test</div>', ['file' => __FILE__, 'line' => __LINE__]);
  696. $result = ob_get_clean();
  697. $expectedHtml = <<<EXPECTED
  698. <div class="cake-debug-output">
  699. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  700. <pre class="cake-debug">
  701. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  702. </pre>
  703. </div>
  704. EXPECTED;
  705. $expectedText = <<<EXPECTED
  706. %s (line %d)
  707. ########## DEBUG ##########
  708. '<div>this-is-a-test</div>'
  709. ###########################
  710. EXPECTED;
  711. if ((PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg')) {
  712. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
  713. } else {
  714. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
  715. }
  716. $this->assertEquals($expected, $result);
  717. ob_start();
  718. Debugger::printVar('<div>this-is-a-test</div>');
  719. $result = ob_get_clean();
  720. $expectedHtml = <<<EXPECTED
  721. <div class="cake-debug-output">
  722. <pre class="cake-debug">
  723. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  724. </pre>
  725. </div>
  726. EXPECTED;
  727. $expectedText = <<<EXPECTED
  728. ########## DEBUG ##########
  729. '<div>this-is-a-test</div>'
  730. ###########################
  731. EXPECTED;
  732. if ((PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg')) {
  733. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
  734. } else {
  735. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
  736. }
  737. $this->assertEquals($expected, $result);
  738. ob_start();
  739. Debugger::printVar('<div>this-is-a-test</div>', ['file' => __FILE__, 'line' => __LINE__], false);
  740. $result = ob_get_clean();
  741. $expected = <<<EXPECTED
  742. %s (line %d)
  743. ########## DEBUG ##########
  744. '<div>this-is-a-test</div>'
  745. ###########################
  746. EXPECTED;
  747. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  748. $this->assertEquals($expected, $result);
  749. ob_start();
  750. Debugger::printVar('<div>this-is-a-test</div>', ['file' => __FILE__, 'line' => __LINE__], false);
  751. $result = ob_get_clean();
  752. $expected = <<<EXPECTED
  753. %s (line %d)
  754. ########## DEBUG ##########
  755. '<div>this-is-a-test</div>'
  756. ###########################
  757. EXPECTED;
  758. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  759. $this->assertEquals($expected, $result);
  760. ob_start();
  761. Debugger::printVar('<div>this-is-a-test</div>', [], false);
  762. $result = ob_get_clean();
  763. $expected = <<<EXPECTED
  764. ########## DEBUG ##########
  765. '<div>this-is-a-test</div>'
  766. ###########################
  767. EXPECTED;
  768. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  769. $this->assertEquals($expected, $result);
  770. ob_start();
  771. Debugger::printVar(false, [], false);
  772. $result = ob_get_clean();
  773. $expected = <<<EXPECTED
  774. ########## DEBUG ##########
  775. false
  776. ###########################
  777. EXPECTED;
  778. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  779. $this->assertEquals($expected, $result);
  780. }
  781. }