PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/Utility/DebuggerTest.php

https://gitlab.com/fouzia23chowdhury/cakephpCRUD
PHP | 633 lines | 427 code | 71 blank | 135 comment | 5 complexity | 8d49b7bb56fd4e0449967280df0aaea6 MD5 | raw file
  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 CakePHP(tm) v 1.2.0.5432
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. App::uses('Debugger', 'Utility');
  16. /**
  17. * DebuggerTestCaseDebugger class
  18. *
  19. * @package Cake.Test.Case.Utility
  20. */
  21. class DebuggerTestCaseDebugger extends Debugger {
  22. }
  23. /**
  24. * DebuggerTest class
  25. *
  26. * !!! Be careful with changing code below as it may
  27. * !!! change line numbers which are used in the tests
  28. *
  29. * @package Cake.Test.Case.Utility
  30. */
  31. class DebuggerTest extends CakeTestCase {
  32. protected $_restoreError = false;
  33. /**
  34. * setUp method
  35. *
  36. * @return void
  37. */
  38. public function setUp() {
  39. parent::setUp();
  40. Configure::write('debug', 2);
  41. Configure::write('log', false);
  42. }
  43. /**
  44. * tearDown method
  45. *
  46. * @return void
  47. */
  48. public function tearDown() {
  49. parent::tearDown();
  50. Configure::write('log', true);
  51. if ($this->_restoreError) {
  52. restore_error_handler();
  53. }
  54. }
  55. /**
  56. * testDocRef method
  57. *
  58. * @return void
  59. */
  60. public function testDocRef() {
  61. ini_set('docref_root', '');
  62. $this->assertEquals(ini_get('docref_root'), '');
  63. new Debugger();
  64. $this->assertEquals(ini_get('docref_root'), 'http://php.net/');
  65. }
  66. /**
  67. * test Excerpt writing
  68. *
  69. * @return void
  70. */
  71. public function testExcerpt() {
  72. $result = Debugger::excerpt(__FILE__, __LINE__, 2);
  73. $this->assertTrue(is_array($result));
  74. $this->assertEquals(5, count($result));
  75. $this->assertRegExp('/function(.+)testExcerpt/', $result[1]);
  76. $result = Debugger::excerpt(__FILE__, 2, 2);
  77. $this->assertTrue(is_array($result));
  78. $this->assertEquals(4, count($result));
  79. $pattern = '/<code>.*?<span style\="color\: \#\d+">.*?&lt;\?php/';
  80. $this->assertRegExp($pattern, $result[0]);
  81. $result = Debugger::excerpt(__FILE__, 11, 2);
  82. $this->assertEquals(5, count($result));
  83. $pattern = '/<span style\="color\: \#\d{6}">\*<\/span>/';
  84. $this->assertRegExp($pattern, $result[0]);
  85. $return = Debugger::excerpt('[internal]', 2, 2);
  86. $this->assertTrue(empty($return));
  87. }
  88. /**
  89. * testOutput method
  90. *
  91. * @return void
  92. */
  93. public function testOutput() {
  94. set_error_handler('Debugger::showError');
  95. $this->_restoreError = true;
  96. $result = Debugger::output(false);
  97. $this->assertEquals('', $result);
  98. $out .= '';
  99. $result = Debugger::output(true);
  100. $this->assertEquals('Notice', $result[0]['error']);
  101. $this->assertRegExp('/Undefined variable\:\s+out/', $result[0]['description']);
  102. $this->assertRegExp('/DebuggerTest::testOutput/i', $result[0]['trace']);
  103. ob_start();
  104. Debugger::output('txt');
  105. $other .= '';
  106. $result = ob_get_clean();
  107. $this->assertRegExp('/Undefined variable:\s+other/', $result);
  108. $this->assertRegExp('/Context:/', $result);
  109. $this->assertRegExp('/DebuggerTest::testOutput/i', $result);
  110. ob_start();
  111. Debugger::output('html');
  112. $wrong .= '';
  113. $result = ob_get_clean();
  114. $this->assertRegExp('/<pre class="cake-error">.+<\/pre>/', $result);
  115. $this->assertRegExp('/<b>Notice<\/b>/', $result);
  116. $this->assertRegExp('/variable:\s+wrong/', $result);
  117. ob_start();
  118. Debugger::output('js');
  119. $buzz .= '';
  120. $result = explode('</a>', ob_get_clean());
  121. $this->assertTags($result[0], array(
  122. 'pre' => array('class' => 'cake-error'),
  123. 'a' => array(
  124. 'href' => "javascript:void(0);",
  125. 'onclick' => "preg:/document\.getElementById\('cakeErr[a-z0-9]+\-trace'\)\.style\.display = " .
  126. "\(document\.getElementById\('cakeErr[a-z0-9]+\-trace'\)\.style\.display == 'none'" .
  127. " \? '' \: 'none'\);/"
  128. ),
  129. 'b' => array(), 'Notice', '/b', ' (8)',
  130. ));
  131. $this->assertRegExp('/Undefined variable:\s+buzz/', $result[1]);
  132. $this->assertRegExp('/<a[^>]+>Code/', $result[1]);
  133. $this->assertRegExp('/<a[^>]+>Context/', $result[2]);
  134. $this->assertContains('$wrong = &#039;&#039;', $result[3], 'Context should be HTML escaped.');
  135. }
  136. /**
  137. * Tests that changes in output formats using Debugger::output() change the templates used.
  138. *
  139. * @return void
  140. */
  141. public function testChangeOutputFormats() {
  142. set_error_handler('Debugger::showError');
  143. $this->_restoreError = true;
  144. Debugger::output('js', array(
  145. 'traceLine' => '{:reference} - <a href="txmt://open?url=file://{:file}' .
  146. '&line={:line}">{:path}</a>, line {:line}'
  147. ));
  148. $result = Debugger::trace();
  149. $this->assertRegExp('/' . preg_quote('txmt://open?url=file://', '/') . '(\/|[A-Z]:\\\\)' . '/', $result);
  150. Debugger::output('xml', array(
  151. 'error' => '<error><code>{:code}</code><file>{:file}</file><line>{:line}</line>' .
  152. '{:description}</error>',
  153. 'context' => "<context>{:context}</context>",
  154. 'trace' => "<stack>{:trace}</stack>",
  155. ));
  156. Debugger::output('xml');
  157. ob_start();
  158. $foo .= '';
  159. $result = ob_get_clean();
  160. $data = array(
  161. 'error' => array(),
  162. 'code' => array(), '8', '/code',
  163. 'file' => array(), 'preg:/[^<]+/', '/file',
  164. 'line' => array(), '' . ((int)__LINE__ - 7), '/line',
  165. 'preg:/Undefined variable:\s+foo/',
  166. '/error'
  167. );
  168. $this->assertTags($result, $data, true);
  169. }
  170. /**
  171. * Test that outputAs works.
  172. *
  173. * @return void
  174. */
  175. public function testOutputAs() {
  176. Debugger::outputAs('html');
  177. $this->assertEquals('html', Debugger::outputAs());
  178. }
  179. /**
  180. * Test that choosing a non-existent format causes an exception
  181. *
  182. * @expectedException CakeException
  183. * @return void
  184. */
  185. public function testOutputAsException() {
  186. Debugger::outputAs('Invalid junk');
  187. }
  188. /**
  189. * Tests that changes in output formats using Debugger::output() change the templates used.
  190. *
  191. * @return void
  192. */
  193. public function testAddFormat() {
  194. set_error_handler('Debugger::showError');
  195. $this->_restoreError = true;
  196. Debugger::addFormat('js', array(
  197. 'traceLine' => '{:reference} - <a href="txmt://open?url=file://{:file}' .
  198. '&line={:line}">{:path}</a>, line {:line}'
  199. ));
  200. Debugger::outputAs('js');
  201. $result = Debugger::trace();
  202. $this->assertRegExp('/' . preg_quote('txmt://open?url=file://', '/') . '(\/|[A-Z]:\\\\)' . '/', $result);
  203. Debugger::addFormat('xml', array(
  204. 'error' => '<error><code>{:code}</code><file>{:file}</file><line>{:line}</line>' .
  205. '{:description}</error>',
  206. ));
  207. Debugger::outputAs('xml');
  208. ob_start();
  209. $foo .= '';
  210. $result = ob_get_clean();
  211. $data = array(
  212. '<error',
  213. '<code', '8', '/code',
  214. '<file', 'preg:/[^<]+/', '/file',
  215. '<line', '' . ((int)__LINE__ - 7), '/line',
  216. 'preg:/Undefined variable:\s+foo/',
  217. '/error'
  218. );
  219. $this->assertTags($result, $data, true);
  220. }
  221. /**
  222. * Test adding a format that is handled by a callback.
  223. *
  224. * @return void
  225. */
  226. public function testAddFormatCallback() {
  227. set_error_handler('Debugger::showError');
  228. $this->_restoreError = true;
  229. Debugger::addFormat('callback', array('callback' => array($this, 'customFormat')));
  230. Debugger::outputAs('callback');
  231. ob_start();
  232. $foo .= '';
  233. $result = ob_get_clean();
  234. $this->assertContains('Notice: I eated an error', $result);
  235. $this->assertContains('DebuggerTest.php', $result);
  236. }
  237. /**
  238. * Test method for testing addFormat with callbacks.
  239. *
  240. * @return void
  241. */
  242. public function customFormat($error, $strings) {
  243. return $error['error'] . ': I eated an error ' . $error['file'];
  244. }
  245. /**
  246. * testTrimPath method
  247. *
  248. * @return void
  249. */
  250. public function testTrimPath() {
  251. $this->assertEquals('APP' . DS, Debugger::trimPath(APP));
  252. $this->assertEquals('CORE', Debugger::trimPath(CAKE_CORE_INCLUDE_PATH));
  253. $this->assertEquals('ROOT', Debugger::trimPath(ROOT));
  254. $this->assertEquals('CORE' . DS . 'Cake' . DS, Debugger::trimPath(CAKE));
  255. $this->assertEquals('Some/Other/Path', Debugger::trimPath('Some/Other/Path'));
  256. }
  257. /**
  258. * testExportVar method
  259. *
  260. * @return void
  261. */
  262. public function testExportVar() {
  263. App::uses('Controller', 'Controller');
  264. $Controller = new Controller();
  265. $Controller->helpers = array('Html', 'Form');
  266. $View = new View($Controller);
  267. $View->int = 2;
  268. $View->float = 1.333;
  269. $result = Debugger::exportVar($View);
  270. $expected = <<<TEXT
  271. object(View) {
  272. Helpers => object(HelperCollection) {}
  273. Blocks => object(ViewBlock) {}
  274. plugin => null
  275. name => ''
  276. passedArgs => array()
  277. helpers => array(
  278. (int) 0 => 'Html',
  279. (int) 1 => 'Form'
  280. )
  281. viewPath => ''
  282. viewVars => array()
  283. view => null
  284. layout => 'default'
  285. layoutPath => null
  286. autoLayout => true
  287. ext => '.ctp'
  288. subDir => null
  289. theme => null
  290. cacheAction => false
  291. validationErrors => array()
  292. hasRendered => false
  293. uuids => array()
  294. request => object(CakeRequest) {}
  295. response => object(CakeResponse) {}
  296. elementCache => 'default'
  297. elementCacheSettings => array()
  298. Html => object(HtmlHelper) {}
  299. Form => object(FormHelper) {}
  300. int => (int) 2
  301. float => (float) 1.333
  302. TEXT;
  303. if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
  304. $expected .= <<<TEXT
  305. [protected] _passedVars => array(
  306. (int) 0 => 'viewVars',
  307. (int) 1 => 'autoLayout',
  308. (int) 2 => 'ext',
  309. (int) 3 => 'helpers',
  310. (int) 4 => 'view',
  311. (int) 5 => 'layout',
  312. (int) 6 => 'name',
  313. (int) 7 => 'theme',
  314. (int) 8 => 'layoutPath',
  315. (int) 9 => 'viewPath',
  316. (int) 10 => 'request',
  317. (int) 11 => 'plugin',
  318. (int) 12 => 'passedArgs',
  319. (int) 13 => 'cacheAction'
  320. )
  321. [protected] _scripts => array()
  322. [protected] _paths => array()
  323. [protected] _pathsForPlugin => array()
  324. [protected] _parents => array()
  325. [protected] _current => null
  326. [protected] _currentType => ''
  327. [protected] _stack => array()
  328. [protected] _eventManager => object(CakeEventManager) {}
  329. [protected] _eventManagerConfigured => false
  330. TEXT;
  331. }
  332. $expected .= <<<TEXT
  333. }
  334. TEXT;
  335. $this->assertTextEquals($expected, $result);
  336. $data = array(
  337. 1 => 'Index one',
  338. 5 => 'Index five'
  339. );
  340. $result = Debugger::exportVar($data);
  341. $expected = <<<TEXT
  342. array(
  343. (int) 1 => 'Index one',
  344. (int) 5 => 'Index five'
  345. )
  346. TEXT;
  347. $this->assertTextEquals($expected, $result);
  348. $data = array(
  349. 'key' => array(
  350. 'value'
  351. )
  352. );
  353. $result = Debugger::exportVar($data, 1);
  354. $expected = <<<TEXT
  355. array(
  356. 'key' => array(
  357. [maximum depth reached]
  358. )
  359. )
  360. TEXT;
  361. $this->assertTextEquals($expected, $result);
  362. $data = false;
  363. $result = Debugger::exportVar($data);
  364. $expected = <<<TEXT
  365. false
  366. TEXT;
  367. $this->assertTextEquals($expected, $result);
  368. $file = fopen('php://output', 'w');
  369. fclose($file);
  370. $result = Debugger::exportVar($file);
  371. $this->assertTextEquals('unknown', $result);
  372. }
  373. /**
  374. * Test exporting various kinds of false.
  375. *
  376. * @return void
  377. */
  378. public function testExportVarZero() {
  379. $data = array(
  380. 'nothing' => '',
  381. 'null' => null,
  382. 'false' => false,
  383. 'szero' => '0',
  384. 'zero' => 0
  385. );
  386. $result = Debugger::exportVar($data);
  387. $expected = <<<TEXT
  388. array(
  389. 'nothing' => '',
  390. 'null' => null,
  391. 'false' => false,
  392. 'szero' => '0',
  393. 'zero' => (int) 0
  394. )
  395. TEXT;
  396. $this->assertTextEquals($expected, $result);
  397. }
  398. /**
  399. * testLog method
  400. *
  401. * @return void
  402. */
  403. public function testLog() {
  404. if (file_exists(LOGS . 'debug.log')) {
  405. unlink(LOGS . 'debug.log');
  406. }
  407. CakeLog::config('file', array('engine' => 'File', 'path' => TMP . 'logs' . DS));
  408. Debugger::log('cool');
  409. $result = file_get_contents(LOGS . 'debug.log');
  410. $this->assertContains('DebuggerTest::testLog', $result);
  411. $this->assertContains("'cool'", $result);
  412. unlink(LOGS . 'debug.log');
  413. Debugger::log(array('whatever', 'here'));
  414. $result = file_get_contents(LOGS . 'debug.log');
  415. $this->assertContains('DebuggerTest::testLog', $result);
  416. $this->assertContains('[main]', $result);
  417. $this->assertContains('array', $result);
  418. $this->assertContains("'whatever',", $result);
  419. $this->assertContains("'here'", $result);
  420. }
  421. /**
  422. * test log() depth
  423. *
  424. * @return void
  425. */
  426. public function testLogDepth() {
  427. if (file_exists(LOGS . 'debug.log')) {
  428. unlink(LOGS . 'debug.log');
  429. }
  430. CakeLog::config('file', array('engine' => 'File', 'path' => TMP . 'logs' . DS));
  431. $val = array(
  432. 'test' => array('key' => 'val')
  433. );
  434. Debugger::log($val, LOG_DEBUG, 0);
  435. $result = file_get_contents(LOGS . 'debug.log');
  436. $this->assertContains('DebuggerTest::testLog', $result);
  437. $this->assertNotContains("/'val'/", $result);
  438. unlink(LOGS . 'debug.log');
  439. }
  440. /**
  441. * testDump method
  442. *
  443. * @return void
  444. */
  445. public function testDump() {
  446. $var = array('People' => array(
  447. array(
  448. 'name' => 'joeseph',
  449. 'coat' => 'technicolor',
  450. 'hair_color' => 'brown'
  451. ),
  452. array(
  453. 'name' => 'Shaft',
  454. 'coat' => 'black',
  455. 'hair' => 'black'
  456. )
  457. ));
  458. ob_start();
  459. Debugger::dump($var);
  460. $result = ob_get_clean();
  461. $open = php_sapi_name() === 'cli' ? "\n" : '<pre>';
  462. $close = php_sapi_name() === 'cli' ? "\n" : '</pre>';
  463. $expected = <<<TEXT
  464. {$open}array(
  465. 'People' => array(
  466. (int) 0 => array(
  467. 'name' => 'joeseph',
  468. 'coat' => 'technicolor',
  469. 'hair_color' => 'brown'
  470. ),
  471. (int) 1 => array(
  472. 'name' => 'Shaft',
  473. 'coat' => 'black',
  474. 'hair' => 'black'
  475. )
  476. )
  477. ){$close}
  478. TEXT;
  479. $this->assertTextEquals($expected, $result);
  480. ob_start();
  481. Debugger::dump($var, 1);
  482. $result = ob_get_clean();
  483. $open = php_sapi_name() === 'cli' ? "\n" : '<pre>';
  484. $close = php_sapi_name() === 'cli' ? "\n" : '</pre>';
  485. $expected = <<<TEXT
  486. {$open}array(
  487. 'People' => array(
  488. [maximum depth reached]
  489. )
  490. ){$close}
  491. TEXT;
  492. $this->assertTextEquals($expected, $result);
  493. }
  494. /**
  495. * test getInstance.
  496. *
  497. * @return void
  498. */
  499. public function testGetInstance() {
  500. $result = Debugger::getInstance();
  501. $this->assertInstanceOf('Debugger', $result);
  502. $result = Debugger::getInstance('DebuggerTestCaseDebugger');
  503. $this->assertInstanceOf('DebuggerTestCaseDebugger', $result);
  504. $result = Debugger::getInstance();
  505. $this->assertInstanceOf('DebuggerTestCaseDebugger', $result);
  506. $result = Debugger::getInstance('Debugger');
  507. $this->assertInstanceOf('Debugger', $result);
  508. }
  509. /**
  510. * testNoDbCredentials
  511. *
  512. * If a connection error occurs, the config variable is passed through exportVar
  513. * *** our database login credentials such that they are never visible
  514. *
  515. * @return void
  516. */
  517. public function testNoDbCredentials() {
  518. $config = array(
  519. 'datasource' => 'mysql',
  520. 'persistent' => false,
  521. 'host' => 'void.cakephp.org',
  522. 'login' => 'cakephp-user',
  523. 'password' => 'cakephp-password',
  524. 'database' => 'cakephp-database',
  525. 'prefix' => ''
  526. );
  527. $output = Debugger::exportVar($config);
  528. $expectedArray = array(
  529. 'datasource' => 'mysql',
  530. 'persistent' => false,
  531. 'host' => '*****',
  532. 'login' => '*****',
  533. 'password' => '*****',
  534. 'database' => '*****',
  535. 'prefix' => ''
  536. );
  537. $expected = Debugger::exportVar($expectedArray);
  538. $this->assertEquals($expected, $output);
  539. }
  540. /**
  541. * Test that exportVar() doesn't loop through recursive structures.
  542. *
  543. * @return void
  544. */
  545. public function testExportVarRecursion() {
  546. $output = Debugger::exportVar($GLOBALS);
  547. $this->assertContains("'GLOBALS' => [recursion]", $output);
  548. }
  549. /**
  550. * test trace exclude
  551. *
  552. * @return void
  553. */
  554. public function testTraceExclude() {
  555. $result = Debugger::trace();
  556. $this->assertRegExp('/^DebuggerTest::testTraceExclude/', $result);
  557. $result = Debugger::trace(array(
  558. 'exclude' => array('DebuggerTest::testTraceExclude')
  559. ));
  560. $this->assertNotRegExp('/^DebuggerTest::testTraceExclude/', $result);
  561. }
  562. }