DebuggerTest.php 22 KB

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