1 | <?php |
---|
2 | /** |
---|
3 | * DebuggerTest file |
---|
4 | * |
---|
5 | * PHP versions 4 and 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 |
---|
16 | * @subpackage cake.tests.cases.libs |
---|
17 | * @since CakePHP(tm) v 1.2.0.5432 |
---|
18 | * @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
---|
19 | */ |
---|
20 | App::import('Core', 'Debugger'); |
---|
21 | |
---|
22 | /** |
---|
23 | * DebugggerTestCaseDebuggger class |
---|
24 | * |
---|
25 | * @package cake |
---|
26 | * @subpackage cake.tests.cases.libs |
---|
27 | */ |
---|
28 | class DebuggerTestCaseDebugger extends Debugger { |
---|
29 | } |
---|
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 | /** |
---|
60 | * tearDown method |
---|
61 | * |
---|
62 | * @access public |
---|
63 | * @return void |
---|
64 | */ |
---|
65 | function tearDown() { |
---|
66 | Configure::write('log', true); |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | * testDocRef method |
---|
71 | * |
---|
72 | * @access public |
---|
73 | * @return void |
---|
74 | */ |
---|
75 | function testDocRef() { |
---|
76 | ini_set('docref_root', ''); |
---|
77 | $this->assertEqual(ini_get('docref_root'), ''); |
---|
78 | $debugger = new Debugger(); |
---|
79 | $this->assertEqual(ini_get('docref_root'), 'http://php.net/'); |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * test Excerpt writing |
---|
84 | * |
---|
85 | * @access public |
---|
86 | * @return void |
---|
87 | */ |
---|
88 | function testExcerpt() { |
---|
89 | $result = Debugger::excerpt(__FILE__, __LINE__, 2); |
---|
90 | $this->assertTrue(is_array($result)); |
---|
91 | $this->assertEqual(count($result), 5); |
---|
92 | $this->assertPattern('/function(.+)testExcerpt/', $result[1]); |
---|
93 | |
---|
94 | $result = Debugger::excerpt(__FILE__, 2, 2); |
---|
95 | $this->assertTrue(is_array($result)); |
---|
96 | $this->assertEqual(count($result), 4); |
---|
97 | |
---|
98 | $expected = '<code><span style="color: #000000"><?php'; |
---|
99 | $expected .= '</span></code>'; |
---|
100 | $this->assertEqual($result[0], $expected); |
---|
101 | |
---|
102 | $return = Debugger::excerpt('[internal]', 2, 2); |
---|
103 | $this->assertTrue(empty($return)); |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * testOutput method |
---|
108 | * |
---|
109 | * @access public |
---|
110 | * @return void |
---|
111 | */ |
---|
112 | function testOutput() { |
---|
113 | Debugger::invoke(Debugger::getInstance()); |
---|
114 | $result = Debugger::output(false); |
---|
115 | $this->assertEqual($result, ''); |
---|
116 | $out .= ''; |
---|
117 | $result = Debugger::output(true); |
---|
118 | |
---|
119 | $this->assertEqual($result[0]['error'], 'Notice'); |
---|
120 | $this->assertPattern('/Undefined variable\:\s+out/', $result[0]['description']); |
---|
121 | $this->assertPattern('/DebuggerTest::testOutput/i', $result[0]['trace']); |
---|
122 | $this->assertPattern('/SimpleInvoker::invoke/i', $result[0]['trace']); |
---|
123 | |
---|
124 | ob_start(); |
---|
125 | Debugger::output('txt'); |
---|
126 | $other .= ''; |
---|
127 | $result = ob_get_clean(); |
---|
128 | |
---|
129 | $this->assertPattern('/Undefined variable:\s+other/', $result); |
---|
130 | $this->assertPattern('/Context:/', $result); |
---|
131 | $this->assertPattern('/DebuggerTest::testOutput/i', $result); |
---|
132 | $this->assertPattern('/SimpleInvoker::invoke/i', $result); |
---|
133 | |
---|
134 | ob_start(); |
---|
135 | Debugger::output('html'); |
---|
136 | $wrong .= ''; |
---|
137 | $result = ob_get_clean(); |
---|
138 | $this->assertPattern('/<pre class="cake-debug">.+<\/pre>/', $result); |
---|
139 | $this->assertPattern('/<b>Notice<\/b>/', $result); |
---|
140 | $this->assertPattern('/variable:\s+wrong/', $result); |
---|
141 | |
---|
142 | ob_start(); |
---|
143 | Debugger::output('js'); |
---|
144 | $buzz .= ''; |
---|
145 | $result = explode('</a>', ob_get_clean()); |
---|
146 | $this->assertTags($result[0], array( |
---|
147 | 'pre' => array('class' => 'cake-debug'), |
---|
148 | 'a' => array( |
---|
149 | 'href' => "javascript:void(0);", |
---|
150 | 'onclick' => "document.getElementById('cakeErr4-trace').style.display = " . |
---|
151 | "(document.getElementById('cakeErr4-trace').style.display == 'none'" . |
---|
152 | " ? '' : 'none');" |
---|
153 | ), |
---|
154 | 'b' => array(), 'Notice', '/b', ' (8)', |
---|
155 | )); |
---|
156 | |
---|
157 | $this->assertPattern('/Undefined variable:\s+buzz/', $result[1]); |
---|
158 | $this->assertPattern('/<a[^>]+>Code/', $result[1]); |
---|
159 | $this->assertPattern('/<a[^>]+>Context/', $result[2]); |
---|
160 | set_error_handler('simpleTestErrorHandler'); |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | * Tests that changes in output formats using Debugger::output() change the templates used. |
---|
165 | * |
---|
166 | * @return void |
---|
167 | */ |
---|
168 | function testChangeOutputFormats() { |
---|
169 | Debugger::invoke(Debugger::getInstance()); |
---|
170 | Debugger::output('js', array( |
---|
171 | 'traceLine' => '{:reference} - <a href="txmt://open?url=file://{:file}' . |
---|
172 | '&line={:line}">{:path}</a>, line {:line}' |
---|
173 | )); |
---|
174 | $result = Debugger::trace(); |
---|
175 | $this->assertPattern('/' . preg_quote('txmt://open?url=file:///', '/') . '/', $result); |
---|
176 | |
---|
177 | Debugger::output('xml', array( |
---|
178 | 'error' => '<error><code>{:code}</code><file>{:file}</file><line>{:line}</line>' . |
---|
179 | '{:description}</error>', |
---|
180 | 'context' => "<context>{:context}</context>", |
---|
181 | 'trace' => "<stack>{:trace}</stack>", |
---|
182 | )); |
---|
183 | Debugger::output('xml'); |
---|
184 | |
---|
185 | ob_start(); |
---|
186 | $foo .= ''; |
---|
187 | $result = ob_get_clean(); |
---|
188 | set_error_handler('SimpleTestErrorHandler'); |
---|
189 | |
---|
190 | $data = array( |
---|
191 | 'error' => array(), |
---|
192 | 'code' => array(), '8', '/code', |
---|
193 | 'file' => array(), 'preg:/[^<]+/', '/file', |
---|
194 | 'line' => array(), '' . (intval(__LINE__) + -8), '/line', |
---|
195 | 'preg:/Undefined variable:\s+foo/', |
---|
196 | '/error' |
---|
197 | ); |
---|
198 | $this->assertTags($result, $data, true); |
---|
199 | } |
---|
200 | |
---|
201 | /** |
---|
202 | * testTrimPath method |
---|
203 | * |
---|
204 | * @access public |
---|
205 | * @return void |
---|
206 | */ |
---|
207 | function testTrimPath() { |
---|
208 | $this->assertEqual(Debugger::trimPath(APP), 'APP' . DS); |
---|
209 | $this->assertEqual(Debugger::trimPath(CAKE_CORE_INCLUDE_PATH), 'CORE'); |
---|
210 | } |
---|
211 | |
---|
212 | /** |
---|
213 | * testExportVar method |
---|
214 | * |
---|
215 | * @access public |
---|
216 | * @return void |
---|
217 | */ |
---|
218 | function testExportVar() { |
---|
219 | App::import('Controller'); |
---|
220 | $Controller = new Controller(); |
---|
221 | $Controller->helpers = array('Html', 'Form'); |
---|
222 | $View = new View($Controller); |
---|
223 | $result = Debugger::exportVar($View); |
---|
224 | $expected = 'ViewView::$base = NULL |
---|
225 | View::$here = NULL |
---|
226 | View::$plugin = NULL |
---|
227 | View::$name = "" |
---|
228 | View::$action = NULL |
---|
229 | View::$params = array |
---|
230 | View::$passedArgs = array |
---|
231 | View::$data = array |
---|
232 | View::$helpers = array |
---|
233 | View::$viewPath = "" |
---|
234 | View::$viewVars = array |
---|
235 | View::$layout = "default" |
---|
236 | View::$layoutPath = NULL |
---|
237 | View::$autoRender = true |
---|
238 | View::$autoLayout = true |
---|
239 | View::$ext = ".ctp" |
---|
240 | View::$subDir = NULL |
---|
241 | View::$theme = NULL |
---|
242 | View::$cacheAction = false |
---|
243 | View::$validationErrors = array |
---|
244 | View::$hasRendered = false |
---|
245 | View::$loaded = array |
---|
246 | View::$modelScope = false |
---|
247 | View::$model = NULL |
---|
248 | View::$association = NULL |
---|
249 | View::$field = NULL |
---|
250 | View::$fieldSuffix = NULL |
---|
251 | View::$modelId = NULL |
---|
252 | View::$uuids = array |
---|
253 | View::$output = false |
---|
254 | View::$__passedVars = array |
---|
255 | View::$__scripts = array |
---|
256 | View::$__paths = array |
---|
257 | View::$webroot = NULL'; |
---|
258 | $result = str_replace(array("\t", "\r\n", "\n"), "", strtolower($result)); |
---|
259 | $expected = str_replace(array("\t", "\r\n", "\n"), "", strtolower($expected)); |
---|
260 | $this->assertEqual($result, $expected); |
---|
261 | } |
---|
262 | |
---|
263 | /** |
---|
264 | * testLog method |
---|
265 | * |
---|
266 | * @access public |
---|
267 | * @return void |
---|
268 | */ |
---|
269 | function testLog() { |
---|
270 | if (file_exists(LOGS . 'debug.log')) { |
---|
271 | unlink(LOGS . 'debug.log'); |
---|
272 | } |
---|
273 | |
---|
274 | Debugger::log('cool'); |
---|
275 | $result = file_get_contents(LOGS . 'debug.log'); |
---|
276 | $this->assertPattern('/DebuggerTest\:\:testLog/i', $result); |
---|
277 | $this->assertPattern('/"cool"/', $result); |
---|
278 | |
---|
279 | unlink(TMP . 'logs' . DS . 'debug.log'); |
---|
280 | |
---|
281 | Debugger::log(array('whatever', 'here')); |
---|
282 | $result = file_get_contents(TMP . 'logs' . DS . 'debug.log'); |
---|
283 | $this->assertPattern('/DebuggerTest\:\:testLog/i', $result); |
---|
284 | $this->assertPattern('/\[main\]/', $result); |
---|
285 | $this->assertPattern('/array/', $result); |
---|
286 | $this->assertPattern('/"whatever",/', $result); |
---|
287 | $this->assertPattern('/"here"/', $result); |
---|
288 | } |
---|
289 | |
---|
290 | /** |
---|
291 | * testDump method |
---|
292 | * |
---|
293 | * @access public |
---|
294 | * @return void |
---|
295 | */ |
---|
296 | function testDump() { |
---|
297 | $var = array('People' => array( |
---|
298 | array( |
---|
299 | 'name' => 'joeseph', |
---|
300 | 'coat' => 'technicolor', |
---|
301 | 'hair_color' => 'brown' |
---|
302 | ), |
---|
303 | array( |
---|
304 | 'name' => 'Shaft', |
---|
305 | 'coat' => 'black', |
---|
306 | 'hair' => 'black' |
---|
307 | ) |
---|
308 | ) |
---|
309 | ); |
---|
310 | ob_start(); |
---|
311 | Debugger::dump($var); |
---|
312 | $result = ob_get_clean(); |
---|
313 | $expected = "<pre>array(\n\t\"People\" => array()\n)</pre>"; |
---|
314 | $this->assertEqual($expected, $result); |
---|
315 | } |
---|
316 | |
---|
317 | /** |
---|
318 | * test getInstance. |
---|
319 | * |
---|
320 | * @access public |
---|
321 | * @return void |
---|
322 | */ |
---|
323 | function testGetInstance() { |
---|
324 | $result =& Debugger::getInstance(); |
---|
325 | $this->assertIsA($result, 'Debugger'); |
---|
326 | |
---|
327 | $result =& Debugger::getInstance('DebuggerTestCaseDebugger'); |
---|
328 | $this->assertIsA($result, 'DebuggerTestCaseDebugger'); |
---|
329 | |
---|
330 | $result =& Debugger::getInstance(); |
---|
331 | $this->assertIsA($result, 'DebuggerTestCaseDebugger'); |
---|
332 | |
---|
333 | $result =& Debugger::getInstance('Debugger'); |
---|
334 | $this->assertIsA($result, 'Debugger'); |
---|
335 | } |
---|
336 | |
---|
337 | /** |
---|
338 | * testNoDbCredentials |
---|
339 | * |
---|
340 | * If a connection error occurs, the config variable is passed through exportVar |
---|
341 | * *** our database login credentials such that they are never visible |
---|
342 | * |
---|
343 | * @access public |
---|
344 | * @return void |
---|
345 | */ |
---|
346 | function testNoDbCredentials() { |
---|
347 | $config = array( |
---|
348 | 'driver' => 'mysql', |
---|
349 | 'persistent' => false, |
---|
350 | 'host' => 'void.cakephp.org', |
---|
351 | 'login' => 'cakephp-user', |
---|
352 | 'password' => 'cakephp-password', |
---|
353 | 'database' => 'cakephp-database', |
---|
354 | 'prefix' => '' |
---|
355 | ); |
---|
356 | |
---|
357 | $output = Debugger::exportVar($config); |
---|
358 | |
---|
359 | $expectedArray = array( |
---|
360 | 'driver' => 'mysql', |
---|
361 | 'persistent' => false, |
---|
362 | 'host' => '*****', |
---|
363 | 'login' => '*****', |
---|
364 | 'password' => '*****', |
---|
365 | 'database' => '*****', |
---|
366 | 'prefix' => '' |
---|
367 | ); |
---|
368 | $expected = Debugger::exportVar($expectedArray); |
---|
369 | |
---|
370 | $this->assertEqual($expected, $output); |
---|
371 | } |
---|
372 | } |
---|