Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11
 * @link          http://cakephp.org CakePHP Project
12
 * @since         CakePHP(tm) v 1.2.0.5432
13
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
14
 */
15
 
16
App::uses('Debugger', 'Utility');
17
 
18
/**
19
 * DebuggerTestCaseDebugger class
20
 *
21
 * @package       Cake.Test.Case.Utility
22
 */
23
class DebuggerTestCaseDebugger extends Debugger {
24
}
25
 
26
/**
27
 * DebuggerTest class
28
 *
29
 * !!! Be careful with changing code below as it may
30
 * !!! change line numbers which are used in the tests
31
 *
32
 * @package       Cake.Test.Case.Utility
33
 */
34
class DebuggerTest extends CakeTestCase {
35
 
36
	protected $_restoreError = false;
37
 
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
/**
50
 * tearDown method
51
 *
52
 * @return void
53
 */
54
	public function tearDown() {
55
		parent::tearDown();
56
		Configure::write('log', true);
57
		if ($this->_restoreError) {
58
			restore_error_handler();
59
		}
60
	}
61
 
62
/**
63
 * testDocRef method
64
 *
65
 * @return void
66
 */
67
	public function testDocRef() {
68
		ini_set('docref_root', '');
69
		$this->assertEquals(ini_get('docref_root'), '');
70
		new Debugger();
71
		$this->assertEquals(ini_get('docref_root'), 'http://php.net/');
72
	}
73
 
74
/**
75
 * test Excerpt writing
76
 *
77
 * @return void
78
 */
79
	public function testExcerpt() {
80
		$result = Debugger::excerpt(__FILE__, __LINE__, 2);
81
		$this->assertTrue(is_array($result));
82
		$this->assertEquals(5, count($result));
83
		$this->assertRegExp('/function(.+)testExcerpt/', $result[1]);
84
 
85
		$result = Debugger::excerpt(__FILE__, 2, 2);
86
		$this->assertTrue(is_array($result));
87
		$this->assertEquals(4, count($result));
88
 
89
		$pattern = '/<code>.*?<span style\="color\: \#\d+">.*?&lt;\?php/';
90
		$this->assertRegExp($pattern, $result[0]);
91
 
92
		$result = Debugger::excerpt(__FILE__, 11, 2);
93
		$this->assertEquals(5, count($result));
94
 
95
		$pattern = '/<span style\="color\: \#\d{6}">\*<\/span>/';
96
		$this->assertRegExp($pattern, $result[0]);
97
 
98
		$return = Debugger::excerpt('[internal]', 2, 2);
99
		$this->assertTrue(empty($return));
100
	}
101
 
102
/**
103
 * testOutput method
104
 *
105
 * @return void
106
 */
107
	public function testOutput() {
108
		set_error_handler('Debugger::showError');
109
		$this->_restoreError = true;
110
 
111
		$result = Debugger::output(false);
112
		$this->assertEquals('', $result);
113
		$out .= '';
114
		$result = Debugger::output(true);
115
 
116
		$this->assertEquals('Notice', $result[0]['error']);
117
		$this->assertRegExp('/Undefined variable\:\s+out/', $result[0]['description']);
118
		$this->assertRegExp('/DebuggerTest::testOutput/i', $result[0]['trace']);
119
 
120
		ob_start();
121
		Debugger::output('txt');
122
		$other .= '';
123
		$result = ob_get_clean();
124
 
125
		$this->assertRegExp('/Undefined variable:\s+other/', $result);
126
		$this->assertRegExp('/Context:/', $result);
127
		$this->assertRegExp('/DebuggerTest::testOutput/i', $result);
128
 
129
		ob_start();
130
		Debugger::output('html');
131
		$wrong .= '';
132
		$result = ob_get_clean();
133
		$this->assertRegExp('/<pre class="cake-error">.+<\/pre>/', $result);
134
		$this->assertRegExp('/<b>Notice<\/b>/', $result);
135
		$this->assertRegExp('/variable:\s+wrong/', $result);
136
 
137
		ob_start();
138
		Debugger::output('js');
139
		$buzz .= '';
140
		$result = explode('</a>', ob_get_clean());
141
		$this->assertTags($result[0], array(
142
			'pre' => array('class' => 'cake-error'),
143
			'a' => array(
144
				'href' => "javascript:void(0);",
145
				'onclick' => "preg:/document\.getElementById\('cakeErr[a-z0-9]+\-trace'\)\.style\.display = " .
146
					"\(document\.getElementById\('cakeErr[a-z0-9]+\-trace'\)\.style\.display == 'none'" .
147
					" \? '' \: 'none'\);/"
148
			),
149
			'b' => array(), 'Notice', '/b', ' (8)',
150
		));
151
 
152
		$this->assertRegExp('/Undefined variable:\s+buzz/', $result[1]);
153
		$this->assertRegExp('/<a[^>]+>Code/', $result[1]);
154
		$this->assertRegExp('/<a[^>]+>Context/', $result[2]);
155
		$this->assertContains('$wrong = &#039;&#039;', $result[3], 'Context should be HTML escaped.');
156
	}
157
 
158
/**
159
 * Tests that changes in output formats using Debugger::output() change the templates used.
160
 *
161
 * @return void
162
 */
163
	public function testChangeOutputFormats() {
164
		set_error_handler('Debugger::showError');
165
		$this->_restoreError = true;
166
 
167
		Debugger::output('js', array(
168
			'traceLine' => '{:reference} - <a href="txmt://open?url=file://{:file}' .
169
				'&line={:line}">{:path}</a>, line {:line}'
170
		));
171
		$result = Debugger::trace();
172
		$this->assertRegExp('/' . preg_quote('txmt://open?url=file://', '/') . '(\/|[A-Z]:\\\\)' . '/', $result);
173
 
174
		Debugger::output('xml', array(
175
			'error' => '<error><code>{:code}</code><file>{:file}</file><line>{:line}</line>' .
176
				'{:description}</error>',
177
			'context' => "<context>{:context}</context>",
178
			'trace' => "<stack>{:trace}</stack>",
179
		));
180
		Debugger::output('xml');
181
 
182
		ob_start();
183
		$foo .= '';
184
		$result = ob_get_clean();
185
 
186
		$data = array(
187
			'error' => array(),
188
			'code' => array(), '8', '/code',
189
			'file' => array(), 'preg:/[^<]+/', '/file',
190
			'line' => array(), '' . (intval(__LINE__) - 7), '/line',
191
			'preg:/Undefined variable:\s+foo/',
192
			'/error'
193
		);
194
		$this->assertTags($result, $data, true);
195
	}
196
 
197
/**
198
 * Test that outputAs works.
199
 *
200
 * @return void
201
 */
202
	public function testOutputAs() {
203
		Debugger::outputAs('html');
204
		$this->assertEquals('html', Debugger::outputAs());
205
	}
206
 
207
/**
208
 * Test that choosing a non-existent format causes an exception
209
 *
210
 * @expectedException CakeException
211
 * @return void
212
 */
213
	public function testOutputAsException() {
214
		Debugger::outputAs('Invalid junk');
215
	}
216
 
217
/**
218
 * Tests that changes in output formats using Debugger::output() change the templates used.
219
 *
220
 * @return void
221
 */
222
	public function testAddFormat() {
223
		set_error_handler('Debugger::showError');
224
		$this->_restoreError = true;
225
 
226
		Debugger::addFormat('js', array(
227
			'traceLine' => '{:reference} - <a href="txmt://open?url=file://{:file}' .
228
				'&line={:line}">{:path}</a>, line {:line}'
229
		));
230
		Debugger::outputAs('js');
231
 
232
		$result = Debugger::trace();
233
		$this->assertRegExp('/' . preg_quote('txmt://open?url=file://', '/') . '(\/|[A-Z]:\\\\)' . '/', $result);
234
 
235
		Debugger::addFormat('xml', array(
236
			'error' => '<error><code>{:code}</code><file>{:file}</file><line>{:line}</line>' .
237
				'{:description}</error>',
238
		));
239
		Debugger::outputAs('xml');
240
 
241
		ob_start();
242
		$foo .= '';
243
		$result = ob_get_clean();
244
 
245
		$data = array(
246
			'<error',
247
			'<code', '8', '/code',
248
			'<file', 'preg:/[^<]+/', '/file',
249
			'<line', '' . (intval(__LINE__) - 7), '/line',
250
			'preg:/Undefined variable:\s+foo/',
251
			'/error'
252
		);
253
		$this->assertTags($result, $data, true);
254
	}
255
 
256
/**
257
 * Test adding a format that is handled by a callback.
258
 *
259
 * @return void
260
 */
261
	public function testAddFormatCallback() {
262
		set_error_handler('Debugger::showError');
263
		$this->_restoreError = true;
264
 
265
		Debugger::addFormat('callback', array('callback' => array($this, 'customFormat')));
266
		Debugger::outputAs('callback');
267
 
268
		ob_start();
269
		$foo .= '';
270
		$result = ob_get_clean();
271
		$this->assertContains('Notice: I eated an error', $result);
272
		$this->assertContains('DebuggerTest.php', $result);
273
	}
274
 
275
/**
276
 * Test method for testing addFormat with callbacks.
277
 */
278
	public function customFormat($error, $strings) {
279
		return $error['error'] . ': I eated an error ' . $error['file'];
280
	}
281
 
282
/**
283
 * testTrimPath method
284
 *
285
 * @return void
286
 */
287
	public function testTrimPath() {
288
		$this->assertEquals('APP' . DS, Debugger::trimPath(APP));
289
		$this->assertEquals('CORE', Debugger::trimPath(CAKE_CORE_INCLUDE_PATH));
290
		$this->assertEquals('ROOT', Debugger::trimPath(ROOT));
291
		$this->assertEquals('CORE' . DS . 'Cake' . DS, Debugger::trimPath(CAKE));
292
		$this->assertEquals('Some/Other/Path', Debugger::trimPath('Some/Other/Path'));
293
	}
294
 
295
/**
296
 * testExportVar method
297
 *
298
 * @return void
299
 */
300
	public function testExportVar() {
301
		App::uses('Controller', 'Controller');
302
		$Controller = new Controller();
303
		$Controller->helpers = array('Html', 'Form');
304
		$View = new View($Controller);
305
		$View->int = 2;
306
		$View->float = 1.333;
307
 
308
		$result = Debugger::exportVar($View);
309
		$expected = <<<TEXT
310
object(View) {
311
	Helpers => object(HelperCollection) {}
312
	Blocks => object(ViewBlock) {}
313
	plugin => null
314
	name => ''
315
	passedArgs => array()
316
	helpers => array(
317
		(int) 0 => 'Html',
318
		(int) 1 => 'Form'
319
	)
320
	viewPath => ''
321
	viewVars => array()
322
	view => null
323
	layout => 'default'
324
	layoutPath => null
325
	autoLayout => true
326
	ext => '.ctp'
327
	subDir => null
328
	theme => null
329
	cacheAction => false
330
	validationErrors => array()
331
	hasRendered => false
332
	uuids => array()
333
	request => object(CakeRequest) {}
334
	response => object(CakeResponse) {}
335
	elementCache => 'default'
336
	elementCacheSettings => array()
337
	Html => object(HtmlHelper) {}
338
	Form => object(FormHelper) {}
339
	int => (int) 2
340
	float => (float) 1.333
341
 
342
TEXT;
343
		if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
344
			$expected .= <<<TEXT
345
	[protected] _passedVars => array(
346
		(int) 0 => 'viewVars',
347
		(int) 1 => 'autoLayout',
348
		(int) 2 => 'ext',
349
		(int) 3 => 'helpers',
350
		(int) 4 => 'view',
351
		(int) 5 => 'layout',
352
		(int) 6 => 'name',
353
		(int) 7 => 'theme',
354
		(int) 8 => 'layoutPath',
355
		(int) 9 => 'viewPath',
356
		(int) 10 => 'request',
357
		(int) 11 => 'plugin',
358
		(int) 12 => 'passedArgs',
359
		(int) 13 => 'cacheAction'
360
	)
361
	[protected] _scripts => array()
362
	[protected] _paths => array()
363
	[protected] _parents => array()
364
	[protected] _current => null
365
	[protected] _currentType => ''
366
	[protected] _stack => array()
367
	[protected] _eventManager => object(CakeEventManager) {}
368
	[protected] _eventManagerConfigured => false
369
 
370
TEXT;
371
		}
372
		$expected .= <<<TEXT
373
}
374
TEXT;
375
 
376
		$this->assertTextEquals($expected, $result);
377
 
378
		$data = array(
379
			1 => 'Index one',
380
			5 => 'Index five'
381
		);
382
		$result = Debugger::exportVar($data);
383
		$expected = <<<TEXT
384
array(
385
	(int) 1 => 'Index one',
386
	(int) 5 => 'Index five'
387
)
388
TEXT;
389
		$this->assertTextEquals($expected, $result);
390
 
391
		$data = array(
392
			'key' => array(
393
				'value'
394
			)
395
		);
396
		$result = Debugger::exportVar($data, 1);
397
		$expected = <<<TEXT
398
array(
399
	'key' => array(
400
		[maximum depth reached]
401
	)
402
)
403
TEXT;
404
		$this->assertTextEquals($expected, $result);
405
 
406
		$data = false;
407
		$result = Debugger::exportVar($data);
408
		$expected = <<<TEXT
409
false
410
TEXT;
411
		$this->assertTextEquals($expected, $result);
412
 
413
		$file = fopen('php://output', 'w');
414
		fclose($file);
415
		$result = Debugger::exportVar($file);
416
		$this->assertTextEquals('unknown', $result);
417
	}
418
 
419
/**
420
 * Test exporting various kinds of false.
421
 *
422
 * @return void
423
 */
424
	public function testExportVarZero() {
425
		$data = array(
426
			'nothing' => '',
427
			'null' => null,
428
			'false' => false,
429
			'szero' => '0',
430
			'zero' => 0
431
		);
432
		$result = Debugger::exportVar($data);
433
		$expected = <<<TEXT
434
array(
435
	'nothing' => '',
436
	'null' => null,
437
	'false' => false,
438
	'szero' => '0',
439
	'zero' => (int) 0
440
)
441
TEXT;
442
		$this->assertTextEquals($expected, $result);
443
	}
444
 
445
/**
446
 * testLog method
447
 *
448
 * @return void
449
 */
450
	public function testLog() {
451
		if (file_exists(LOGS . 'debug.log')) {
452
			unlink(LOGS . 'debug.log');
453
		}
454
		CakeLog::config('file', array('engine' => 'File', 'path' => TMP . 'logs' . DS));
455
 
456
		Debugger::log('cool');
457
		$result = file_get_contents(LOGS . 'debug.log');
458
		$this->assertRegExp('/DebuggerTest\:\:testLog/i', $result);
459
		$this->assertRegExp("/'cool'/", $result);
460
 
461
		unlink(LOGS . 'debug.log');
462
 
463
		Debugger::log(array('whatever', 'here'));
464
		$result = file_get_contents(LOGS . 'debug.log');
465
		$this->assertRegExp('/DebuggerTest\:\:testLog/i', $result);
466
		$this->assertRegExp('/\[main\]/', $result);
467
		$this->assertRegExp('/array/', $result);
468
		$this->assertRegExp("/'whatever',/", $result);
469
		$this->assertRegExp("/'here'/", $result);
470
	}
471
 
472
/**
473
 * testDump method
474
 *
475
 * @return void
476
 */
477
	public function testDump() {
478
		$var = array('People' => array(
479
			array(
480
				'name' => 'joeseph',
481
				'coat' => 'technicolor',
482
				'hair_color' => 'brown'
483
			),
484
			array(
485
				'name' => 'Shaft',
486
				'coat' => 'black',
487
				'hair' => 'black'
488
			)
489
		));
490
		ob_start();
491
		Debugger::dump($var);
492
		$result = ob_get_clean();
493
 
494
		$open = php_sapi_name() == 'cli' ? "\n" : '<pre>';
495
		$close = php_sapi_name() == 'cli' ? "\n" : '</pre>';
496
		$expected = <<<TEXT
497
{$open}array(
498
	'People' => array(
499
		(int) 0 => array(
500
			'name' => 'joeseph',
501
			'coat' => 'technicolor',
502
			'hair_color' => 'brown'
503
		),
504
		(int) 1 => array(
505
			'name' => 'Shaft',
506
			'coat' => 'black',
507
			'hair' => 'black'
508
		)
509
	)
510
){$close}
511
TEXT;
512
		$this->assertTextEquals($expected, $result);
513
	}
514
 
515
/**
516
 * test getInstance.
517
 *
518
 * @return void
519
 */
520
	public function testGetInstance() {
521
		$result = Debugger::getInstance();
522
		$this->assertInstanceOf('Debugger', $result);
523
 
524
		$result = Debugger::getInstance('DebuggerTestCaseDebugger');
525
		$this->assertInstanceOf('DebuggerTestCaseDebugger', $result);
526
 
527
		$result = Debugger::getInstance();
528
		$this->assertInstanceOf('DebuggerTestCaseDebugger', $result);
529
 
530
		$result = Debugger::getInstance('Debugger');
531
		$this->assertInstanceOf('Debugger', $result);
532
	}
533
 
534
/**
535
 * testNoDbCredentials
536
 *
537
 * If a connection error occurs, the config variable is passed through exportVar
538
 * *** our database login credentials such that they are never visible
539
 *
540
 * @return void
541
 */
542
	public function testNoDbCredentials() {
543
		$config = array(
544
			'datasource' => 'mysql',
545
			'persistent' => false,
546
			'host' => 'void.cakephp.org',
547
			'login' => 'cakephp-user',
548
			'password' => 'cakephp-password',
549
			'database' => 'cakephp-database',
550
			'prefix' => ''
551
		);
552
 
553
		$output = Debugger::exportVar($config);
554
 
555
		$expectedArray = array(
556
			'datasource' => 'mysql',
557
			'persistent' => false,
558
			'host' => '*****',
559
			'login' => '*****',
560
			'password' => '*****',
561
			'database' => '*****',
562
			'prefix' => ''
563
		);
564
		$expected = Debugger::exportVar($expectedArray);
565
 
566
		$this->assertEquals($expected, $output);
567
	}
568
 
569
/**
570
 * Test that exportVar() doesn't loop through recursive structures.
571
 *
572
 * @return void
573
 */
574
	public function testExportVarRecursion() {
575
		$output = Debugger::exportVar($GLOBALS);
576
		$this->assertContains("'GLOBALS' => [recursion]", $output);
577
	}
578
 
579
/**
580
 * test trace exclude
581
 *
582
 * @return void
583
 */
584
	public function testTraceExclude() {
585
		$result = Debugger::trace();
586
		$this->assertRegExp('/^DebuggerTest::testTraceExclude/', $result);
587
 
588
		$result = Debugger::trace(array(
589
			'exclude' => array('DebuggerTest::testTraceExclude')
590
		));
591
		$this->assertNotRegExp('/^DebuggerTest::testTraceExclude/', $result);
592
	}
593
}