DebuggerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <?php
  2. /**
  3. * DebuggerTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP Project
  15. * @package Cake.Test.Case.Utility
  16. * @since CakePHP(tm) v 1.2.0.5432
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Debugger', 'Utility');
  20. /**
  21. * DebugggerTestCaseDebuggger class
  22. *
  23. * @package Cake.Test.Case.Utility
  24. */
  25. class DebuggerTestCaseDebugger extends Debugger {
  26. }
  27. /**
  28. * DebuggerTest class
  29. *
  30. * @package Cake.Test.Case.Utility
  31. */
  32. class DebuggerTest extends CakeTestCase {
  33. // !!!
  34. // !!! Be careful with changing code below as it may
  35. // !!! change line numbers which are used in the tests
  36. // !!!
  37. protected $_restoreError = false;
  38. /**
  39. * setUp method
  40. *
  41. * @return void
  42. */
  43. public function setUp() {
  44. parent::setup();
  45. Configure::write('debug', 2);
  46. Configure::write('log', false);
  47. }
  48. /**
  49. * tearDown method
  50. *
  51. * @return void
  52. */
  53. public function tearDown() {
  54. parent::teardown();
  55. Configure::write('log', true);
  56. if ($this->_restoreError) {
  57. restore_error_handler();
  58. }
  59. }
  60. /**
  61. * testDocRef method
  62. *
  63. * @return void
  64. */
  65. public function testDocRef() {
  66. ini_set('docref_root', '');
  67. $this->assertEqual(ini_get('docref_root'), '');
  68. $debugger = new Debugger();
  69. $this->assertEqual(ini_get('docref_root'), 'http://php.net/');
  70. }
  71. /**
  72. * test Excerpt writing
  73. *
  74. * @return void
  75. */
  76. public function testExcerpt() {
  77. $result = Debugger::excerpt(__FILE__, __LINE__, 2);
  78. $this->assertTrue(is_array($result));
  79. $this->assertEqual(count($result), 5);
  80. $this->assertPattern('/function(.+)testExcerpt/', $result[1]);
  81. $result = Debugger::excerpt(__FILE__, 2, 2);
  82. $this->assertTrue(is_array($result));
  83. $this->assertEqual(count($result), 4);
  84. $expected = '<code><span style="color: #000000">&lt;?php';
  85. $expected .= '</span></code>';
  86. $this->assertEqual($result[0], $expected);
  87. $return = Debugger::excerpt('[internal]', 2, 2);
  88. $this->assertTrue(empty($return));
  89. }
  90. /**
  91. * testOutput method
  92. *
  93. * @return void
  94. */
  95. public function testOutput() {
  96. set_error_handler('Debugger::showError');
  97. $this->_restoreError = true;
  98. $result = Debugger::output(false);
  99. $this->assertEqual($result, '');
  100. $out .= '';
  101. $result = Debugger::output(true);
  102. $this->assertEqual($result[0]['error'], 'Notice');
  103. $this->assertPattern('/Undefined variable\:\s+out/', $result[0]['description']);
  104. $this->assertPattern('/DebuggerTest::testOutput/i', $result[0]['trace']);
  105. ob_start();
  106. Debugger::output('txt');
  107. $other .= '';
  108. $result = ob_get_clean();
  109. $this->assertPattern('/Undefined variable:\s+other/', $result);
  110. $this->assertPattern('/Context:/', $result);
  111. $this->assertPattern('/DebuggerTest::testOutput/i', $result);
  112. ob_start();
  113. Debugger::output('html');
  114. $wrong .= '';
  115. $result = ob_get_clean();
  116. $this->assertPattern('/<pre class="cake-error">.+<\/pre>/', $result);
  117. $this->assertPattern('/<b>Notice<\/b>/', $result);
  118. $this->assertPattern('/variable:\s+wrong/', $result);
  119. ob_start();
  120. Debugger::output('js');
  121. $buzz .= '';
  122. $result = explode('</a>', ob_get_clean());
  123. $this->assertTags($result[0], array(
  124. 'pre' => array('class' => 'cake-error'),
  125. 'a' => array(
  126. 'href' => "javascript:void(0);",
  127. 'onclick' => "preg:/document\.getElementById\('cakeErr[a-z0-9]+\-trace'\)\.style\.display = " .
  128. "\(document\.getElementById\('cakeErr[a-z0-9]+\-trace'\)\.style\.display == 'none'" .
  129. " \? '' \: 'none'\);/"
  130. ),
  131. 'b' => array(), 'Notice', '/b', ' (8)',
  132. ));
  133. $this->assertPattern('/Undefined variable:\s+buzz/', $result[1]);
  134. $this->assertPattern('/<a[^>]+>Code/', $result[1]);
  135. $this->assertPattern('/<a[^>]+>Context/', $result[2]);
  136. }
  137. /**
  138. * Tests that changes in output formats using Debugger::output() change the templates used.
  139. *
  140. * @return void
  141. */
  142. public function testChangeOutputFormats() {
  143. set_error_handler('Debugger::showError');
  144. $this->_restoreError = true;
  145. Debugger::output('js', array(
  146. 'traceLine' => '{:reference} - <a href="txmt://open?url=file://{:file}' .
  147. '&line={:line}">{:path}</a>, line {:line}'
  148. ));
  149. $result = Debugger::trace();
  150. $this->assertPattern('/' . preg_quote('txmt://open?url=file://', '/') . '(\/|[A-Z]:\\\\)' . '/', $result);
  151. Debugger::output('xml', array(
  152. 'error' => '<error><code>{:code}</code><file>{:file}</file><line>{:line}</line>' .
  153. '{:description}</error>',
  154. 'context' => "<context>{:context}</context>",
  155. 'trace' => "<stack>{:trace}</stack>",
  156. ));
  157. Debugger::output('xml');
  158. ob_start();
  159. $foo .= '';
  160. $result = ob_get_clean();
  161. $data = array(
  162. 'error' => array(),
  163. 'code' => array(), '8', '/code',
  164. 'file' => array(), 'preg:/[^<]+/', '/file',
  165. 'line' => array(), '' . (intval(__LINE__) - 7), '/line',
  166. 'preg:/Undefined variable:\s+foo/',
  167. '/error'
  168. );
  169. $this->assertTags($result, $data, true);
  170. }
  171. /**
  172. * Test that outputAs works.
  173. *
  174. * @return void
  175. */
  176. public function testOutputAs() {
  177. Debugger::outputAs('html');
  178. $this->assertEquals('html', Debugger::outputAs());
  179. }
  180. /**
  181. * Test that choosing a non-existant format causes an exception
  182. *
  183. * @expectedException CakeException
  184. * @return void
  185. */
  186. public function testOutputAsException() {
  187. Debugger::outputAs('Invalid junk');
  188. }
  189. /**
  190. * Tests that changes in output formats using Debugger::output() change the templates used.
  191. *
  192. * @return void
  193. */
  194. public function testAddFormat() {
  195. set_error_handler('Debugger::showError');
  196. $this->_restoreError = true;
  197. Debugger::addFormat('js', array(
  198. 'traceLine' => '{:reference} - <a href="txmt://open?url=file://{:file}' .
  199. '&line={:line}">{:path}</a>, line {:line}'
  200. ));
  201. Debugger::outputAs('js');
  202. $result = Debugger::trace();
  203. $this->assertPattern('/' . preg_quote('txmt://open?url=file://', '/') . '(\/|[A-Z]:\\\\)' . '/', $result);
  204. Debugger::addFormat('xml', array(
  205. 'error' => '<error><code>{:code}</code><file>{:file}</file><line>{:line}</line>' .
  206. '{:description}</error>',
  207. ));
  208. Debugger::outputAs('xml');
  209. ob_start();
  210. $foo .= '';
  211. $result = ob_get_clean();
  212. $data = array(
  213. '<error',
  214. '<code', '8', '/code',
  215. '<file', 'preg:/[^<]+/', '/file',
  216. '<line', '' . (intval(__LINE__) - 7), '/line',
  217. 'preg:/Undefined variable:\s+foo/',
  218. '/error'
  219. );
  220. $this->assertTags($result, $data, true);
  221. }
  222. /**
  223. * Test adding a format that is handled by a callback.
  224. *
  225. * @return void
  226. */
  227. public function testAddFormatCallback() {
  228. set_error_handler('Debugger::showError');
  229. $this->_restoreError = true;
  230. Debugger::addFormat('callback', array('callback' => array($this, 'customFormat')));
  231. Debugger::outputAs('callback');
  232. ob_start();
  233. $foo .= '';
  234. $result = ob_get_clean();
  235. $this->assertEquals('Notice: I eated an error CORE/Cake/Test/Case/Utility/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['path'];
  242. }
  243. /**
  244. * testTrimPath method
  245. *
  246. * @return void
  247. */
  248. public function testTrimPath() {
  249. $this->assertEqual(Debugger::trimPath(APP), 'APP' . DS);
  250. $this->assertEqual(Debugger::trimPath(CAKE_CORE_INCLUDE_PATH), 'CORE');
  251. }
  252. /**
  253. * testExportVar method
  254. *
  255. * @return void
  256. */
  257. public function testExportVar() {
  258. App::uses('Controller', 'Controller');
  259. $Controller = new Controller();
  260. $Controller->helpers = array('Html', 'Form');
  261. $View = new View($Controller);
  262. $result = Debugger::exportVar($View);
  263. $expected = 'View
  264. View::$Helpers = HelperCollection object
  265. View::$plugin = NULL
  266. View::$name = ""
  267. View::$passedArgs = array
  268. View::$helpers = array
  269. View::$viewPath = ""
  270. View::$viewVars = array
  271. View::$view = NULL
  272. View::$layout = "default"
  273. View::$layoutPath = NULL
  274. View::$autoLayout = true
  275. View::$ext = ".ctp"
  276. View::$subDir = NULL
  277. View::$theme = NULL
  278. View::$cacheAction = false
  279. View::$validationErrors = array
  280. View::$hasRendered = false
  281. View::$uuids = array
  282. View::$output = false
  283. View::$request = NULL
  284. View::$elementCache = "default"';
  285. $result = str_replace(array("\t", "\r\n", "\n"), "", $result);
  286. $expected = str_replace(array("\t", "\r\n", "\n"), "", $expected);
  287. $this->assertEqual($expected, $result);
  288. }
  289. /**
  290. * testLog method
  291. *
  292. * @return void
  293. */
  294. public function testLog() {
  295. if (file_exists(LOGS . 'debug.log')) {
  296. unlink(LOGS . 'debug.log');
  297. }
  298. Debugger::log('cool');
  299. $result = file_get_contents(LOGS . 'debug.log');
  300. $this->assertPattern('/DebuggerTest\:\:testLog/i', $result);
  301. $this->assertPattern('/"cool"/', $result);
  302. unlink(TMP . 'logs' . DS . 'debug.log');
  303. Debugger::log(array('whatever', 'here'));
  304. $result = file_get_contents(TMP . 'logs' . DS . 'debug.log');
  305. $this->assertPattern('/DebuggerTest\:\:testLog/i', $result);
  306. $this->assertPattern('/\[main\]/', $result);
  307. $this->assertPattern('/array/', $result);
  308. $this->assertPattern('/"whatever",/', $result);
  309. $this->assertPattern('/"here"/', $result);
  310. }
  311. /**
  312. * testDump method
  313. *
  314. * @return void
  315. */
  316. public function testDump() {
  317. $var = array('People' => array(
  318. array(
  319. 'name' => 'joeseph',
  320. 'coat' => 'technicolor',
  321. 'hair_color' => 'brown'
  322. ),
  323. array(
  324. 'name' => 'Shaft',
  325. 'coat' => 'black',
  326. 'hair' => 'black'
  327. )
  328. )
  329. );
  330. ob_start();
  331. Debugger::dump($var);
  332. $result = ob_get_clean();
  333. $expected = "<pre>array(\n\t\"People\" => array()\n)</pre>";
  334. $this->assertEqual($expected, $result);
  335. }
  336. /**
  337. * test getInstance.
  338. *
  339. * @return void
  340. */
  341. public function testGetInstance() {
  342. $result = Debugger::getInstance();
  343. $this->assertIsA($result, 'Debugger');
  344. $result = Debugger::getInstance('DebuggerTestCaseDebugger');
  345. $this->assertIsA($result, 'DebuggerTestCaseDebugger');
  346. $result = Debugger::getInstance();
  347. $this->assertIsA($result, 'DebuggerTestCaseDebugger');
  348. $result = Debugger::getInstance('Debugger');
  349. $this->assertIsA($result, 'Debugger');
  350. }
  351. /**
  352. * testNoDbCredentials
  353. *
  354. * If a connection error occurs, the config variable is passed through exportVar
  355. * *** our database login credentials such that they are never visible
  356. *
  357. * @return void
  358. */
  359. function testNoDbCredentials() {
  360. $config = array(
  361. 'driver' => 'mysql',
  362. 'persistent' => false,
  363. 'host' => 'void.cakephp.org',
  364. 'login' => 'cakephp-user',
  365. 'password' => 'cakephp-password',
  366. 'database' => 'cakephp-database',
  367. 'prefix' => ''
  368. );
  369. $output = Debugger::exportVar($config);
  370. $expectedArray = array(
  371. 'driver' => 'mysql',
  372. 'persistent' => false,
  373. 'host' => '*****',
  374. 'login' => '*****',
  375. 'password' => '*****',
  376. 'database' => '*****',
  377. 'prefix' => ''
  378. );
  379. $expected = Debugger::exportVar($expectedArray);
  380. $this->assertEqual($expected, $output);
  381. }
  382. }