PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/cake/tests/cases/libs/debugger.test.php

https://github.com/msadouni/cakephp2x
PHP | 339 lines | 208 code | 30 blank | 101 comment | 5 complexity | 70e1fda6f35edd5ddca71f06658285d3 MD5 | raw file
  1. <?php
  2. /**
  3. * DebuggerTest file
  4. *
  5. * Long description for file
  6. *
  7. * PHP Version 5.x
  8. *
  9. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  10. * Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The Open Group Test Suite License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  17. * @package cake
  18. * @subpackage cake.tests.cases.libs
  19. * @since CakePHP(tm) v 1.2.0.5432
  20. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  21. */
  22. App::import('Core', 'Debugger');
  23. /**
  24. * DebugggerTestCaseDebuggger class
  25. *
  26. * @package cake
  27. * @subpackage cake.tests.cases.libs
  28. */
  29. class DebuggerTestCaseDebugger extends Debugger {
  30. }
  31. /**
  32. * DebuggerTest class
  33. *
  34. * @package cake
  35. * @subpackage cake.tests.cases.libs
  36. */
  37. class DebuggerTest extends CakeTestCase {
  38. // !!!
  39. // !!! Be careful with changing code below as it may
  40. // !!! change line numbers which are used in the tests
  41. // !!!
  42. /**
  43. * setUp method
  44. *
  45. * @access public
  46. * @return void
  47. */
  48. function setUp() {
  49. Configure::write('log', false);
  50. if (!defined('SIMPLETESTVENDORPATH')) {
  51. if (file_exists(APP . DS . 'vendors' . DS . 'simpletest' . DS . 'reporter.php')) {
  52. define('SIMPLETESTVENDORPATH', 'APP' . DS . 'vendors');
  53. } else {
  54. define('SIMPLETESTVENDORPATH', 'CORE' . DS . 'vendors');
  55. }
  56. }
  57. }
  58. /**
  59. * tearDown method
  60. *
  61. * @access public
  62. * @return void
  63. */
  64. function tearDown() {
  65. Configure::write('log', true);
  66. }
  67. /**
  68. * testDocRef method
  69. *
  70. * @access public
  71. * @return void
  72. */
  73. function testDocRef() {
  74. ini_set('docref_root', '');
  75. $this->assertEqual(ini_get('docref_root'), '');
  76. $debugger = new Debugger();
  77. $this->assertEqual(ini_get('docref_root'), 'http://php.net/');
  78. }
  79. /**
  80. * test Excerpt writing
  81. *
  82. * @access public
  83. * @return void
  84. */
  85. function testExcerpt() {
  86. $result = Debugger::excerpt(__FILE__, __LINE__, 2);
  87. $this->assertTrue(is_array($result));
  88. $this->assertEqual(count($result), 5);
  89. $this->assertPattern('/function(.+)testExcerpt/', $result[1]);
  90. $result = Debugger::excerpt(__FILE__, 2, 2);
  91. $this->assertTrue(is_array($result));
  92. $this->assertEqual(count($result), 4);
  93. $expected = '<code><span style="color: #000000">&lt;?php';
  94. $expected .= '</span></code>';
  95. $this->assertEqual($result[0], $expected);
  96. $return = Debugger::excerpt('[internal]', 2, 2);
  97. $this->assertTrue(empty($return));
  98. }
  99. /**
  100. * testOutput method
  101. *
  102. * @access public
  103. * @return void
  104. */
  105. function testOutput() {
  106. Debugger::invoke(Debugger::getInstance());
  107. $result = Debugger::output(false);
  108. $this->assertEqual($result, '');
  109. $out .= '';
  110. $result = Debugger::output(true);
  111. $this->assertEqual($result[0]['error'], 'Notice');
  112. $this->assertEqual($result[0]['description'], 'Undefined variable: out');
  113. $this->assertPattern('/DebuggerTest::testOutput/', $result[0]['trace']);
  114. $this->assertPattern('/SimpleInvoker::invoke/', $result[0]['trace']);
  115. ob_start();
  116. Debugger::output('txt');
  117. $other .= '';
  118. $result = ob_get_clean();
  119. $this->assertPattern('/Undefined variable: other/', $result);
  120. $this->assertPattern('/Context:/', $result);
  121. $this->assertPattern('/DebuggerTest::testOutput/', $result);
  122. $this->assertPattern('/SimpleInvoker::invoke/', $result);
  123. ob_start();
  124. Debugger::output('html');
  125. $wrong .= '';
  126. $result = ob_get_clean();
  127. $this->assertPattern('/<pre class="cake-debug">.+<\/pre>/', $result);
  128. $this->assertPattern('/<b>Notice<\/b>/', $result);
  129. $this->assertPattern('/variable: wrong/', $result);
  130. ob_start();
  131. Debugger::output('js');
  132. $buzz .= '';
  133. $result = explode('</a>', ob_get_clean());
  134. $this->assertTags($result[0], array(
  135. 'pre' => array('class' => 'cake-debug'),
  136. 'a' => array(
  137. 'href' => "javascript:void(0);",
  138. 'onclick' => "document.getElementById('cakeErr4-trace').style.display = " .
  139. "(document.getElementById('cakeErr4-trace').style.display == 'none'" .
  140. " ? '' : 'none');"
  141. ),
  142. 'b' => array(), 'Notice', '/b', ' (8)',
  143. ));
  144. $this->assertPattern('/Undefined variable: buzz/', $result[1]);
  145. $this->assertPattern('/<a[^>]+>Code/', $result[1]);
  146. $this->assertPattern('/<a[^>]+>Context/', $result[2]);
  147. set_error_handler('simpleTestErrorHandler');
  148. }
  149. /**
  150. * Tests that changes in output formats using Debugger::output() change the templates used.
  151. *
  152. * @return void
  153. */
  154. function testChangeOutputFormats() {
  155. Debugger::invoke(Debugger::getInstance());
  156. Debugger::output('js', array(
  157. 'traceLine' => '{:reference} - <a href="txmt://open?url=file://{:file}' .
  158. '&line={:line}">{:path}</a>, line {:line}'
  159. ));
  160. $result = Debugger::trace();
  161. $this->assertPattern('/' . preg_quote('txmt://open?url=file:///', '/') . '/', $result);
  162. Debugger::output('xml', array(
  163. 'error' => '<error><code>{:code}</code><file>{:file}</file><line>{:line}</line>' .
  164. '{:description}</error>',
  165. 'context' => "<context>{:context}</context>",
  166. 'trace' => "<stack>{:trace}</stack>",
  167. ));
  168. Debugger::output('xml');
  169. ob_start();
  170. $foo .= '';
  171. $result = ob_get_clean();
  172. set_error_handler('simpleTestErrorHandler');
  173. $data = array(
  174. 'error' => array(),
  175. 'code' => array(), '8', '/code',
  176. 'file' => array(), 'preg:/[^<]+/', '/file',
  177. 'line' => array(), '' . (intval(__LINE__) + -8), '/line',
  178. 'Undefined variable: foo',
  179. '/error'
  180. );
  181. $this->assertTags($result, $data, true);
  182. }
  183. /**
  184. * testTrimPath method
  185. *
  186. * @access public
  187. * @return void
  188. */
  189. function testTrimPath() {
  190. $this->assertEqual(Debugger::trimPath(APP), 'APP' . DS);
  191. $this->assertEqual(Debugger::trimPath(CAKE_CORE_INCLUDE_PATH), 'CORE');
  192. }
  193. /**
  194. * testExportVar method
  195. *
  196. * @access public
  197. * @return void
  198. */
  199. function testExportVar() {
  200. App::import('AppController');
  201. $Controller = new AppController();
  202. $Controller->helpers = array('Html', 'Form');
  203. $View = new View($Controller);
  204. $result = Debugger::exportVar($View);
  205. $expected = 'ViewView::$base = NULL
  206. View::$here = NULL
  207. View::$plugin = NULL
  208. View::$name = ""
  209. View::$action = NULL
  210. View::$params = array
  211. View::$passedArgs = array
  212. View::$data = array
  213. View::$helpers = array
  214. View::$viewPath = ""
  215. View::$viewVars = array
  216. View::$layout = "default"
  217. View::$layoutPath = NULL
  218. View::$autoRender = true
  219. View::$autoLayout = true
  220. View::$ext = ".ctp"
  221. View::$subDir = NULL
  222. View::$theme = NULL
  223. View::$cacheAction = false
  224. View::$validationErrors = array
  225. View::$hasRendered = false
  226. View::$loaded = array
  227. View::$modelScope = false
  228. View::$model = NULL
  229. View::$association = NULL
  230. View::$field = NULL
  231. View::$fieldSuffix = NULL
  232. View::$modelId = NULL
  233. View::$uuids = array
  234. View::$output = false
  235. View::$__passedVars = array
  236. View::$__scripts = array
  237. View::$__paths = array
  238. View::$webroot = NULL';
  239. $result = str_replace(array("\t", "\r\n", "\n"), "", $result);
  240. $expected = str_replace(array("\t", "\r\n", "\n"), "", $expected);
  241. $this->assertEqual($result, $expected);
  242. }
  243. /**
  244. * testLog method
  245. *
  246. * @access public
  247. * @return void
  248. */
  249. function testLog() {
  250. if (file_exists(LOGS . 'debug.log')) {
  251. unlink(LOGS . 'debug.log');
  252. }
  253. Debugger::log('cool');
  254. $result = file_get_contents(LOGS . 'debug.log');
  255. $this->assertPattern('/DebuggerTest\:\:testLog/', $result);
  256. $this->assertPattern('/"cool"/', $result);
  257. unlink(TMP . 'logs' . DS . 'debug.log');
  258. Debugger::log(array('whatever', 'here'));
  259. $result = file_get_contents(TMP . 'logs' . DS . 'debug.log');
  260. $this->assertPattern('/DebuggerTest\:\:testLog/', $result);
  261. $this->assertPattern('/\[main\]/', $result);
  262. $this->assertPattern('/array/', $result);
  263. $this->assertPattern('/"whatever",/', $result);
  264. $this->assertPattern('/"here"/', $result);
  265. }
  266. /**
  267. * testDump method
  268. *
  269. * @access public
  270. * @return void
  271. */
  272. function testDump() {
  273. $var = array('People' => array(
  274. array(
  275. 'name' => 'joeseph',
  276. 'coat' => 'technicolor',
  277. 'hair_color' => 'brown'
  278. ),
  279. array(
  280. 'name' => 'Shaft',
  281. 'coat' => 'black',
  282. 'hair' => 'black'
  283. )
  284. )
  285. );
  286. ob_start();
  287. Debugger::dump($var);
  288. $result = ob_get_clean();
  289. $expected = "<pre>array(\n\t\"People\" => array()\n)</pre>";
  290. $this->assertEqual($expected, $result);
  291. }
  292. /**
  293. * test getInstance.
  294. *
  295. * @access public
  296. * @return void
  297. */
  298. function testGetInstance() {
  299. $result = Debugger::getInstance();
  300. $this->assertIsA($result, 'Debugger');
  301. $result = Debugger::getInstance('DebuggerTestCaseDebugger');
  302. $this->assertIsA($result, 'DebuggerTestCaseDebugger');
  303. $result = Debugger::getInstance();
  304. $this->assertIsA($result, 'DebuggerTestCaseDebugger');
  305. $result = Debugger::getInstance('Debugger');
  306. $this->assertIsA($result, 'Debugger');
  307. }
  308. }
  309. ?>