DebuggerTest.php 23 KB

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