PageRenderTime 66ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/LeThanhDat/firstdummyapp
PHP | 582 lines | 391 code | 63 blank | 128 comment | 4 complexity | cfd88e418098818d8afcf8f8ddb24b60 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 MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. App::uses('Debugger', 'Utility');
  16. /**
  17. * DebugggerTestCaseDebuggger 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(), '' . (intval(__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', '' . (intval(__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. public function customFormat($error, $strings) {
  241. return $error['error'] . ': I eated an error ' . $error['file'];
  242. }
  243. /**
  244. * testTrimPath method
  245. *
  246. * @return void
  247. */
  248. public function testTrimPath() {
  249. $this->assertEquals('APP' . DS, Debugger::trimPath(APP));
  250. $this->assertEquals('CORE', Debugger::trimPath(CAKE_CORE_INCLUDE_PATH));
  251. $this->assertEquals('ROOT', Debugger::trimPath(ROOT));
  252. $this->assertEquals('CORE' . DS . 'Cake' . DS, Debugger::trimPath(CAKE));
  253. $this->assertEquals('Some/Other/Path', Debugger::trimPath('Some/Other/Path'));
  254. }
  255. /**
  256. * testExportVar method
  257. *
  258. * @return void
  259. */
  260. public function testExportVar() {
  261. App::uses('Controller', 'Controller');
  262. $Controller = new Controller();
  263. $Controller->helpers = array('Html', 'Form');
  264. $View = new View($Controller);
  265. $View->int = 2;
  266. $View->float = 1.333;
  267. $result = Debugger::exportVar($View);
  268. $expected = <<<TEXT
  269. object(View) {
  270. Helpers => object(HelperCollection) {}
  271. Blocks => object(ViewBlock) {}
  272. plugin => null
  273. name => ''
  274. passedArgs => array()
  275. helpers => array(
  276. (int) 0 => 'Html',
  277. (int) 1 => 'Form'
  278. )
  279. viewPath => ''
  280. viewVars => array()
  281. view => null
  282. layout => 'default'
  283. layoutPath => null
  284. autoLayout => true
  285. ext => '.ctp'
  286. subDir => null
  287. theme => null
  288. cacheAction => false
  289. validationErrors => array()
  290. hasRendered => false
  291. uuids => array()
  292. request => object(CakeRequest) {}
  293. response => object(CakeResponse) {}
  294. elementCache => 'default'
  295. elementCacheSettings => array()
  296. int => (int) 2
  297. float => (float) 1.333
  298. TEXT;
  299. if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
  300. $expected .= <<<TEXT
  301. [protected] _passedVars => array(
  302. (int) 0 => 'viewVars',
  303. (int) 1 => 'autoLayout',
  304. (int) 2 => 'ext',
  305. (int) 3 => 'helpers',
  306. (int) 4 => 'view',
  307. (int) 5 => 'layout',
  308. (int) 6 => 'name',
  309. (int) 7 => 'theme',
  310. (int) 8 => 'layoutPath',
  311. (int) 9 => 'viewPath',
  312. (int) 10 => 'request',
  313. (int) 11 => 'plugin',
  314. (int) 12 => 'passedArgs',
  315. (int) 13 => 'cacheAction'
  316. )
  317. [protected] _scripts => array()
  318. [protected] _paths => array()
  319. [protected] _helpersLoaded => false
  320. [protected] _parents => array()
  321. [protected] _current => null
  322. [protected] _currentType => ''
  323. [protected] _stack => array()
  324. [protected] _eventManager => object(CakeEventManager) {}
  325. [protected] _eventManagerConfigured => false
  326. TEXT;
  327. }
  328. $expected .= <<<TEXT
  329. }
  330. TEXT;
  331. $this->assertTextEquals($expected, $result);
  332. $data = array(
  333. 1 => 'Index one',
  334. 5 => 'Index five'
  335. );
  336. $result = Debugger::exportVar($data);
  337. $expected = <<<TEXT
  338. array(
  339. (int) 1 => 'Index one',
  340. (int) 5 => 'Index five'
  341. )
  342. TEXT;
  343. $this->assertTextEquals($expected, $result);
  344. $data = array(
  345. 'key' => array(
  346. 'value'
  347. )
  348. );
  349. $result = Debugger::exportVar($data, 1);
  350. $expected = <<<TEXT
  351. array(
  352. 'key' => array(
  353. [maximum depth reached]
  354. )
  355. )
  356. TEXT;
  357. $this->assertTextEquals($expected, $result);
  358. $data = false;
  359. $result = Debugger::exportVar($data);
  360. $expected = <<<TEXT
  361. false
  362. TEXT;
  363. $this->assertTextEquals($expected, $result);
  364. }
  365. /**
  366. * Test exporting various kinds of false.
  367. *
  368. * @return void
  369. */
  370. public function testExportVarZero() {
  371. $data = array(
  372. 'nothing' => '',
  373. 'null' => null,
  374. 'false' => false,
  375. 'szero' => '0',
  376. 'zero' => 0
  377. );
  378. $result = Debugger::exportVar($data);
  379. $expected = <<<TEXT
  380. array(
  381. 'nothing' => '',
  382. 'null' => null,
  383. 'false' => false,
  384. 'szero' => '0',
  385. 'zero' => (int) 0
  386. )
  387. TEXT;
  388. $this->assertTextEquals($expected, $result);
  389. }
  390. /**
  391. * testLog method
  392. *
  393. * @return void
  394. */
  395. public function testLog() {
  396. if (file_exists(LOGS . 'debug.log')) {
  397. unlink(LOGS . 'debug.log');
  398. }
  399. Debugger::log('cool');
  400. $result = file_get_contents(LOGS . 'debug.log');
  401. $this->assertRegExp('/DebuggerTest\:\:testLog/i', $result);
  402. $this->assertRegExp("/'cool'/", $result);
  403. unlink(LOGS . 'debug.log');
  404. Debugger::log(array('whatever', 'here'));
  405. $result = file_get_contents(LOGS . 'debug.log');
  406. $this->assertRegExp('/DebuggerTest\:\:testLog/i', $result);
  407. $this->assertRegExp('/\[main\]/', $result);
  408. $this->assertRegExp('/array/', $result);
  409. $this->assertRegExp("/'whatever',/", $result);
  410. $this->assertRegExp("/'here'/", $result);
  411. }
  412. /**
  413. * testDump method
  414. *
  415. * @return void
  416. */
  417. public function testDump() {
  418. $var = array('People' => array(
  419. array(
  420. 'name' => 'joeseph',
  421. 'coat' => 'technicolor',
  422. 'hair_color' => 'brown'
  423. ),
  424. array(
  425. 'name' => 'Shaft',
  426. 'coat' => 'black',
  427. 'hair' => 'black'
  428. )
  429. ));
  430. ob_start();
  431. Debugger::dump($var);
  432. $result = ob_get_clean();
  433. $expected = <<<TEXT
  434. <pre>array(
  435. 'People' => array(
  436. (int) 0 => array(
  437. 'name' => 'joeseph',
  438. 'coat' => 'technicolor',
  439. 'hair_color' => 'brown'
  440. ),
  441. (int) 1 => array(
  442. 'name' => 'Shaft',
  443. 'coat' => 'black',
  444. 'hair' => 'black'
  445. )
  446. )
  447. )</pre>
  448. TEXT;
  449. $this->assertTextEquals($expected, $result);
  450. }
  451. /**
  452. * test getInstance.
  453. *
  454. * @return void
  455. */
  456. public function testGetInstance() {
  457. $result = Debugger::getInstance();
  458. $this->assertInstanceOf('Debugger', $result);
  459. $result = Debugger::getInstance('DebuggerTestCaseDebugger');
  460. $this->assertInstanceOf('DebuggerTestCaseDebugger', $result);
  461. $result = Debugger::getInstance();
  462. $this->assertInstanceOf('DebuggerTestCaseDebugger', $result);
  463. $result = Debugger::getInstance('Debugger');
  464. $this->assertInstanceOf('Debugger', $result);
  465. }
  466. /**
  467. * testNoDbCredentials
  468. *
  469. * If a connection error occurs, the config variable is passed through exportVar
  470. * *** our database login credentials such that they are never visible
  471. *
  472. * @return void
  473. */
  474. public function testNoDbCredentials() {
  475. $config = array(
  476. 'datasource' => 'mysql',
  477. 'persistent' => false,
  478. 'host' => 'void.cakephp.org',
  479. 'login' => 'cakephp-user',
  480. 'password' => 'cakephp-password',
  481. 'database' => 'cakephp-database',
  482. 'prefix' => ''
  483. );
  484. $output = Debugger::exportVar($config);
  485. $expectedArray = array(
  486. 'datasource' => 'mysql',
  487. 'persistent' => false,
  488. 'host' => '*****',
  489. 'login' => '*****',
  490. 'password' => '*****',
  491. 'database' => '*****',
  492. 'prefix' => ''
  493. );
  494. $expected = Debugger::exportVar($expectedArray);
  495. $this->assertEquals($expected, $output);
  496. }
  497. /**
  498. * Test that exportVar() doesn't loop through recursive structures.
  499. *
  500. * @return void
  501. */
  502. public function testExportVarRecursion() {
  503. $output = Debugger::exportVar($GLOBALS);
  504. $this->assertContains("'GLOBALS' => [recursion]", $output);
  505. }
  506. /**
  507. * test trace exclude
  508. *
  509. * @return void
  510. */
  511. public function testTraceExclude() {
  512. $result = Debugger::trace();
  513. $this->assertRegExp('/^DebuggerTest::testTraceExclude/', $result);
  514. $result = Debugger::trace(array(
  515. 'exclude' => array('DebuggerTest::testTraceExclude')
  516. ));
  517. $this->assertNotRegExp('/^DebuggerTest::testTraceExclude/', $result);
  518. }
  519. }