PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/udeshika/fake_twitter
PHP | 447 lines | 272 code | 52 blank | 123 comment | 3 complexity | 62f485e2cf8f592f900041ef0a7a9752 MD5 | raw file
  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->assertEquals(ini_get('docref_root'), '');
  68. $debugger = new Debugger();
  69. $this->assertEquals(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->assertEquals(count($result), 5);
  80. $this->assertRegExp('/function(.+)testExcerpt/', $result[1]);
  81. $result = Debugger::excerpt(__FILE__, 2, 2);
  82. $this->assertTrue(is_array($result));
  83. $this->assertEquals(count($result), 4);
  84. $pattern = '/<code><span style\="color\: \#\d+">.*?&lt;\?php/';
  85. $this->assertRegExp($pattern, $result[0]);
  86. $return = Debugger::excerpt('[internal]', 2, 2);
  87. $this->assertTrue(empty($return));
  88. }
  89. /**
  90. * testOutput method
  91. *
  92. * @return void
  93. */
  94. public function testOutput() {
  95. set_error_handler('Debugger::showError');
  96. $this->_restoreError = true;
  97. $result = Debugger::output(false);
  98. $this->assertEquals($result, '');
  99. $out .= '';
  100. $result = Debugger::output(true);
  101. $this->assertEquals($result[0]['error'], 'Notice');
  102. $this->assertRegExp('/Undefined variable\:\s+out/', $result[0]['description']);
  103. $this->assertRegExp('/DebuggerTest::testOutput/i', $result[0]['trace']);
  104. ob_start();
  105. Debugger::output('txt');
  106. $other .= '';
  107. $result = ob_get_clean();
  108. $this->assertRegExp('/Undefined variable:\s+other/', $result);
  109. $this->assertRegExp('/Context:/', $result);
  110. $this->assertRegExp('/DebuggerTest::testOutput/i', $result);
  111. ob_start();
  112. Debugger::output('html');
  113. $wrong .= '';
  114. $result = ob_get_clean();
  115. $this->assertRegExp('/<pre class="cake-error">.+<\/pre>/', $result);
  116. $this->assertRegExp('/<b>Notice<\/b>/', $result);
  117. $this->assertRegExp('/variable:\s+wrong/', $result);
  118. ob_start();
  119. Debugger::output('js');
  120. $buzz .= '';
  121. $result = explode('</a>', ob_get_clean());
  122. $this->assertTags($result[0], array(
  123. 'pre' => array('class' => 'cake-error'),
  124. 'a' => array(
  125. 'href' => "javascript:void(0);",
  126. 'onclick' => "preg:/document\.getElementById\('cakeErr[a-z0-9]+\-trace'\)\.style\.display = " .
  127. "\(document\.getElementById\('cakeErr[a-z0-9]+\-trace'\)\.style\.display == 'none'" .
  128. " \? '' \: 'none'\);/"
  129. ),
  130. 'b' => array(), 'Notice', '/b', ' (8)',
  131. ));
  132. $this->assertRegExp('/Undefined variable:\s+buzz/', $result[1]);
  133. $this->assertRegExp('/<a[^>]+>Code/', $result[1]);
  134. $this->assertRegExp('/<a[^>]+>Context/', $result[2]);
  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-existant 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['path'];
  242. }
  243. /**
  244. * testTrimPath method
  245. *
  246. * @return void
  247. */
  248. public function testTrimPath() {
  249. $this->assertEquals(Debugger::trimPath(APP), 'APP' . DS);
  250. $this->assertEquals(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->assertEquals($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->assertRegExp('/DebuggerTest\:\:testLog/i', $result);
  301. $this->assertRegExp('/"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->assertRegExp('/DebuggerTest\:\:testLog/i', $result);
  306. $this->assertRegExp('/\[main\]/', $result);
  307. $this->assertRegExp('/array/', $result);
  308. $this->assertRegExp('/"whatever",/', $result);
  309. $this->assertRegExp('/"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->assertEquals($expected, $result);
  335. }
  336. /**
  337. * test getInstance.
  338. *
  339. * @return void
  340. */
  341. public function testGetInstance() {
  342. $result = Debugger::getInstance();
  343. $this->assertInstanceOf('Debugger', $result);
  344. $result = Debugger::getInstance('DebuggerTestCaseDebugger');
  345. $this->assertInstanceOf('DebuggerTestCaseDebugger', $result);
  346. $result = Debugger::getInstance();
  347. $this->assertInstanceOf('DebuggerTestCaseDebugger', $result);
  348. $result = Debugger::getInstance('Debugger');
  349. $this->assertInstanceOf('Debugger', $result);
  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. public function testNoDbCredentials() {
  360. $config = array(
  361. 'datasource' => '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. 'datasource' => 'mysql',
  372. 'persistent' => false,
  373. 'host' => '*****',
  374. 'login' => '*****',
  375. 'password' => '*****',
  376. 'database' => '*****',
  377. 'prefix' => ''
  378. );
  379. $expected = Debugger::exportVar($expectedArray);
  380. $this->assertEquals($expected, $output);
  381. }
  382. /**
  383. * test trace exclude
  384. *
  385. * @return void
  386. */
  387. public function testTraceExclude() {
  388. $result = Debugger::trace();
  389. $this->assertRegExp('/^DebuggerTest::testTraceExclude/', $result);
  390. $result = Debugger::trace(array(
  391. 'exclude' => array('DebuggerTest::testTraceExclude')
  392. ));
  393. $this->assertNotRegExp('/^DebuggerTest::testTraceExclude/', $result);
  394. }
  395. }