Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 1
<?php
2
/**
3
 * CakeFirePHP Test Case
4
 *
5
 * PHP 5
6
 *
7
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8
 * Copyright (c) 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 (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
14
 * @link          http://cakephp.org CakePHP(tm) Project
15
 * @since         DebugKit 0.1
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 **/
18
 
19
App::uses('FireCake', 'DebugKit.Lib');
20
require_once CakePlugin::path('DebugKit') . 'Test' . DS . 'Case' . DS . 'TestFireCake.php';
21
 
22
/**
23
 * Test Case For FireCake
24
 *
25
 * @since         DebugKit 0.1
26
 */
27
class FireCakeTestCase extends CakeTestCase {
28
 
29
/**
30
 * setup test
31
 *
32
 * Fill FireCake with TestFireCake instance.
33
 *
34
 * @return void
35
 */
36
	public function setUp() {
37
		$this->firecake = FireCake::getInstance('TestFireCake');
38
		TestFireCake::reset();
39
	}
40
 
41
/**
42
 * Reset the FireCake counters and headers.
43
 *
44
 * @return void
45
 */
46
	public function tearDown() {
47
		TestFireCake::reset();
48
	}
49
 
50
/**
51
 * Test getInstance cheat.
52
 *
53
 * If this fails the rest of the test is going to fail too.
54
 *
55
 * @return void
56
 */
57
	public function testGetInstanceOverride() {
58
		$instance = FireCake::getInstance();
59
		$instance2 = FireCake::getInstance();
60
		$this->assertReference($instance, $instance2);
61
		$this->assertIsA($instance, 'FireCake');
62
		$this->assertIsA($instance, 'TestFireCake', 'Stored instance is not a copy of TestFireCake, test case is broken.');
63
	}
64
 
65
/**
66
 * Test setOptions
67
 *
68
 * @return void
69
 */
70
	public function testSetOptions() {
71
		FireCake::setOptions(array('includeLineNumbers' => false));
72
		$this->assertEquals($this->firecake->options['includeLineNumbers'], false);
73
	}
74
 
75
/**
76
 * Test Log()
77
 *
78
 * @return void
79
 */
80
	public function testLog() {
81
		FireCake::setOptions(array('includeLineNumbers' => false));
82
		FireCake::log('Testing');
83
		$this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-Protocol-1']));
84
		$this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-1-Plugin-1']));
85
		$this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-1-Structure-1']));
86
		$this->assertEquals($this->firecake->sentHeaders['X-Wf-1-Index'], 1);
87
		$this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-1'], '26|[{"Type":"LOG"},"Testing"]|');
88
 
89
		FireCake::log('Testing', 'log-info');
90
		$this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-2'], '45|[{"Type":"LOG","Label":"log-info"},"Testing"]|');
91
	}
92
 
93
/**
94
 * Test info()
95
 *
96
 * @return void
97
 */
98
	public function testInfo() {
99
		FireCake::setOptions(array('includeLineNumbers' => false));
100
		FireCake::info('I have information');
101
		$this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-Protocol-1']));
102
		$this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-1-Plugin-1']));
103
		$this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-1-Structure-1']));
104
		$this->assertEquals($this->firecake->sentHeaders['X-Wf-1-Index'], 1);
105
		$this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-1'], '38|[{"Type":"INFO"},"I have information"]|');
106
 
107
		FireCake::info('I have information', 'info-label');
108
		$this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-2'], '59|[{"Type":"INFO","Label":"info-label"},"I have information"]|');
109
	}
110
 
111
/**
112
 * Test info()
113
 *
114
 * @return void
115
 */
116
	public function testWarn() {
117
		FireCake::setOptions(array('includeLineNumbers' => false));
118
		FireCake::warn('A Warning');
119
		$this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-Protocol-1']));
120
		$this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-1-Plugin-1']));
121
		$this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-1-Structure-1']));
122
		$this->assertEquals($this->firecake->sentHeaders['X-Wf-1-Index'], 1);
123
		$this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-1'], '29|[{"Type":"WARN"},"A Warning"]|');
124
 
125
		FireCake::warn('A Warning', 'Bzzz');
126
		$this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-2'], '44|[{"Type":"WARN","Label":"Bzzz"},"A Warning"]|');
127
	}
128
 
129
/**
130
 * Test error()
131
 *
132
 * @return void
133
 */
134
	public function testError() {
135
		FireCake::setOptions(array('includeLineNumbers' => false));
136
		FireCake::error('An error');
137
		$this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-Protocol-1']));
138
		$this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-1-Plugin-1']));
139
		$this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-1-Structure-1']));
140
		$this->assertEquals($this->firecake->sentHeaders['X-Wf-1-Index'], 1);
141
		$this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-1'], '29|[{"Type":"ERROR"},"An error"]|');
142
 
143
		FireCake::error('An error', 'wonky');
144
		$this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-2'], '45|[{"Type":"ERROR","Label":"wonky"},"An error"]|');
145
	}
146
 
147
/**
148
 * Test dump()
149
 *
150
 * @return void
151
 */
152
	public function testDump() {
153
		FireCake::dump('mydump', array('one' => 1, 'two' => 2));
154
		$this->assertEquals($this->firecake->sentHeaders['X-Wf-1-2-1-1'], '28|{"mydump":{"one":1,"two":2}}|');
155
		$this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-1-Structure-2']));
156
	}
157
 
158
/**
159
 * Test table() generation
160
 *
161
 * @return void
162
 */
163
	public function testTable() {
164
		$table[] = array('Col 1 Heading','Col 2 Heading');
165
		$table[] = array('Row 1 Col 1','Row 1 Col 2');
166
		$table[] = array('Row 2 Col 1','Row 2 Col 2');
167
		$table[] = array('Row 3 Col 1','Row 3 Col 2');
168
		FireCake::table('myTrace', $table);
169
		$expected = '162|[{"Type":"TABLE","Label":"myTrace"},[["Col 1 Heading","Col 2 Heading"],["Row 1 Col 1","Row 1 Col 2"],["Row 2 Col 1","Row 2 Col 2"],["Row 3 Col 1","Row 3 Col 2"]]]|';
170
		$this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-1'], $expected);
171
	}
172
 
173
/**
174
 * TestStringEncoding
175
 *
176
 * @return void
177
 */
178
	public function testStringEncode() {
179
		$vars = array(1,2,3);
180
		$result = $this->firecake->stringEncode($vars);
181
		$this->assertEquals($result, array(1,2,3));
182
 
183
		$this->firecake->setOptions(array('maxArrayDepth' => 3));
184
		$deep = array(1 => array(2 => array(3)));
185
		$result = $this->firecake->stringEncode($deep);
186
		$this->assertEquals($result, array(1 => array(2 => '** Max Array Depth (3) **')));
187
	}
188
 
189
/**
190
 * Test object encoding
191
 *
192
 * @return void
193
 */
194
	public function testStringEncodeObjects() {
195
		$obj = FireCake::getInstance();
196
		$result = $this->firecake->stringEncode($obj);
197
 
198
		$this->assertTrue(is_array($result));
199
		$this->assertEquals($result['_defaultOptions']['useNativeJsonEncode'], true);
200
		$this->assertEquals($result['_encodedObjects'][0], '** Recursion (TestFireCake) **');
201
	}
202
 
203
/**
204
 * Test trace()
205
 *
206
 * @return void
207
 */
208
	public function testTrace() {
209
		FireCake::trace('myTrace');
210
		$this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-Protocol-1']));
211
		$this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-1-Plugin-1']));
212
		$this->assertTrue(isset($this->firecake->sentHeaders['X-Wf-1-Structure-1']));
213
		$dump = $this->firecake->sentHeaders['X-Wf-1-1-1-1'];
214
		$this->assertPattern('/"Message":"myTrace"/', $dump);
215
		$this->assertPattern('/"Trace":\[/', $dump);
216
	}
217
 
218
/**
219
 * Test enabling and disabling of FireCake output
220
 *
221
 * @return void
222
 */
223
	public function testEnableDisable() {
224
		FireCake::disable();
225
		FireCake::trace('myTrace');
226
		$this->assertTrue(empty($this->firecake->sentHeaders));
227
 
228
		FireCake::enable();
229
		FireCake::trace('myTrace');
230
		$this->assertFalse(empty($this->firecake->sentHeaders));
231
	}
232
 
233
/**
234
 * Test correct line continuation markers on multi line headers.
235
 *
236
 * @return void
237
 */
238
	public function testMultiLineOutput() {
239
		FireCake::trace('myTrace');
240
		$this->assertGreaterThan(1, $this->firecake->sentHeaders['X-Wf-1-Index']);
241
		$header = $this->firecake->sentHeaders['X-Wf-1-1-1-1'];
242
		$this->assertEquals(substr($header, -2), '|\\');
243
 
244
		$endIndex = $this->firecake->sentHeaders['X-Wf-1-Index'];
245
		$header = $this->firecake->sentHeaders['X-Wf-1-1-1-' . $endIndex];
246
		$this->assertEquals(substr($header, -1), '|');
247
	}
248
 
249
/**
250
 * Test inclusion of line numbers
251
 *
252
 * @return void
253
 */
254
	public function testIncludeLineNumbers() {
255
		FireCake::setOptions(array('includeLineNumbers' => true));
256
		FireCake::info('Testing');
257
		$result = $this->firecake->sentHeaders['X-Wf-1-1-1-1'];
258
		$this->assertPattern('/"File"\:".*FireCakeTest.php/', $result);
259
		$this->assertPattern('/"Line"\:\d+/', $result);
260
	}
261
 
262
/**
263
 * Test Group messages
264
 *
265
 * @return void
266
 */
267
	public function testGroup() {
268
		FireCake::setOptions(array('includeLineNumbers' => false));
269
		FireCake::group('test');
270
		FireCake::info('my info');
271
		FireCake::groupEnd();
272
		$this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-1'], '63|[{"Collapsed":"true","Type":"GROUP_START","Label":"test"},null]|');
273
		$this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-3'], '27|[{"Type":"GROUP_END"},null]|');
274
		$this->assertEquals($this->firecake->sentHeaders['X-Wf-1-Index'], 3);
275
	}
276
 
277
/**
278
 * Test fb() parameter parsing
279
 *
280
 * @return void
281
 */
282
	public function testFbParameterParsing() {
283
		FireCake::setOptions(array('includeLineNumbers' => false));
284
		FireCake::fb('Test');
285
		$this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-1'], '23|[{"Type":"LOG"},"Test"]|');
286
 
287
		FireCake::fb('Test', 'warn');
288
		$this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-2'], '24|[{"Type":"WARN"},"Test"]|');
289
 
290
		FireCake::fb('Test', 'Custom label', 'warn');
291
		$this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-3'], '47|[{"Type":"WARN","Label":"Custom label"},"Test"]|');
292
 
293
		$this->expectError('PHPUnit_Framework_Error');
294
		$this->assertFalse(FireCake::fb('Test', 'Custom label', 'warn', 'more parameters'));
295
 
296
		$this->assertEquals($this->firecake->sentHeaders['X-Wf-1-Index'], 3);
297
	}
298
 
299
/**
300
 * Test defaulting to log if incorrect message type is used
301
 *
302
 * @return void
303
 */
304
	public function testIncorrectMessageType() {
305
		FireCake::setOptions(array('includeLineNumbers' => false));
306
		FireCake::fb('Hello World', 'foobared');
307
		$this->assertEquals($this->firecake->sentHeaders['X-Wf-1-1-1-1'], '30|[{"Type":"LOG"},"Hello World"]|');
308
	}
309
 
310
/**
311
 * Test DetectClientExtension.
312
 *
313
 * @return void
314
 */
315
	public function testDetectClientExtension() {
316
		$back = env('HTTP_USER_AGENT');
317
		$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4 FirePHP/0.2.1';
318
		$this->assertTrue(FireCake::detectClientExtension());
319
 
320
		$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4 FirePHP/0.0.4';
321
		$this->assertFalse(FireCake::detectClientExtension());
322
 
323
		$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4';
324
		$this->assertFalse(FireCake::detectClientExtension());
325
		$_SERVER['HTTP_USER_AGENT'] = $back;
326
	}
327
 
328
/**
329
 * Test of Non Native JSON encoding.
330
 *
331
 * @return void
332
 */
333
	public function testNonNativeEncoding() {
334
		FireCake::setOptions(array('useNativeJsonEncode' => false));
335
		$json = FireCake::jsonEncode(array('one' => 1, 'two' => 2));
336
		$this->assertEquals($json, '{"one":1,"two":2}');
337
 
338
		$json = FireCake::jsonEncode(array(1,2,3));
339
		$this->assertEquals($json, '[1,2,3]');
340
 
341
		$json = FireCake::jsonEncode(FireCake::getInstance());
342
		$this->assertPattern('/"options"\:\{"maxObjectDepth"\:\d*,/', $json);
343
	}
344
 
345
}