DebuggerTest.php 23 KB

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