DebuggerTest.php 22 KB

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