PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

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