Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * CakeLogTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.Log
15
 * @since         CakePHP(tm) v 1.2.0.5432
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('CakeLog', 'Log');
20
App::uses('FileLog', 'Log/Engine');
21
 
22
/**
23
 * CakeLogTest class
24
 *
25
 * @package       Cake.Test.Case.Log
26
 */
27
class CakeLogTest extends CakeTestCase {
28
 
29
/**
30
 * Start test callback, clears all streams enabled.
31
 *
32
 * @return void
33
 */
34
	public function setUp() {
35
		parent::setUp();
36
		$streams = CakeLog::configured();
37
		foreach ($streams as $stream) {
38
			CakeLog::drop($stream);
39
		}
40
	}
41
 
42
/**
43
 * test importing loggers from app/libs and plugins.
44
 *
45
 * @return void
46
 */
47
	public function testImportingLoggers() {
48
		App::build(array(
49
			'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
50
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
51
		), App::RESET);
52
		CakePlugin::load('TestPlugin');
53
 
54
		$result = CakeLog::config('libtest', array(
55
			'engine' => 'TestAppLog'
56
		));
57
		$this->assertTrue($result);
58
		$this->assertEquals(CakeLog::configured(), array('libtest'));
59
 
60
		$result = CakeLog::config('plugintest', array(
61
			'engine' => 'TestPlugin.TestPluginLog'
62
		));
63
		$this->assertTrue($result);
64
		$this->assertEquals(CakeLog::configured(), array('libtest', 'plugintest'));
65
 
66
		CakeLog::write(LOG_INFO, 'TestPluginLog is not a BaseLog descendant');
67
 
68
		App::build();
69
		CakePlugin::unload();
70
	}
71
 
72
/**
73
 * test all the errors from failed logger imports
74
 *
75
 * @expectedException CakeLogException
76
 * @return void
77
 */
78
	public function testImportingLoggerFailure() {
79
		CakeLog::config('fail', array());
80
	}
81
 
82
/**
83
 * test config() with valid key name
84
 *
85
 * @return void
86
 */
87
	public function testValidKeyName() {
88
		CakeLog::config('valid', array('engine' => 'File'));
89
		$stream = CakeLog::stream('valid');
90
		$this->assertInstanceOf('FileLog', $stream);
91
		CakeLog::drop('valid');
92
	}
93
 
94
/**
95
 * test config() with valid key name including the deprecated Log suffix
96
 *
97
 * @return void
98
 */
99
	public function testValidKeyNameLogSuffix() {
100
		CakeLog::config('valid', array('engine' => 'FileLog'));
101
		$stream = CakeLog::stream('valid');
102
		$this->assertInstanceOf('FileLog', $stream);
103
		CakeLog::drop('valid');
104
	}
105
 
106
/**
107
 * test config() with invalid key name
108
 *
109
 * @expectedException CakeLogException
110
 * @return void
111
 */
112
	public function testInvalidKeyName() {
113
		CakeLog::config('1nv', array('engine' => 'File'));
114
	}
115
 
116
/**
117
 * test that loggers have to implement the correct interface.
118
 *
119
 * @expectedException CakeLogException
120
 * @return void
121
 */
122
	public function testNotImplementingInterface() {
123
		CakeLog::config('fail', array('engine' => 'stdClass'));
124
	}
125
 
126
/**
127
 * Test that CakeLog autoconfigures itself to use a FileLogger with the LOGS dir.
128
 * When no streams are there.
129
 *
130
 * @return void
131
 */
132
	public function testAutoConfig() {
133
		if (file_exists(LOGS . 'error.log')) {
134
			unlink(LOGS . 'error.log');
135
		}
136
		CakeLog::write(LOG_WARNING, 'Test warning');
137
		$this->assertTrue(file_exists(LOGS . 'error.log'));
138
 
139
		$result = CakeLog::configured();
140
		$this->assertEquals(array('default'), $result);
141
 
142
		$testMessage = 'custom message';
143
		CakeLog::write('custom', $testMessage);
144
		$content = file_get_contents(LOGS . 'custom.log');
145
		$this->assertContains($testMessage, $content);
146
		unlink(LOGS . 'error.log');
147
		unlink(LOGS . 'custom.log');
148
	}
149
 
150
/**
151
 * test configuring log streams
152
 *
153
 * @return void
154
 */
155
	public function testConfig() {
156
		CakeLog::config('file', array(
157
			'engine' => 'File',
158
			'path' => LOGS
159
		));
160
		$result = CakeLog::configured();
161
		$this->assertEquals(array('file'), $result);
162
 
163
		if (file_exists(LOGS . 'error.log')) {
164
			unlink(LOGS . 'error.log');
165
		}
166
		CakeLog::write(LOG_WARNING, 'Test warning');
167
		$this->assertTrue(file_exists(LOGS . 'error.log'));
168
 
169
		$result = file_get_contents(LOGS . 'error.log');
170
		$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning/', $result);
171
		unlink(LOGS . 'error.log');
172
	}
173
 
174
/**
175
 * explicit tests for drop()
176
 *
177
 * @return void
178
 */
179
	public function testDrop() {
180
		CakeLog::config('file', array(
181
			'engine' => 'File',
182
			'path' => LOGS
183
		));
184
		$result = CakeLog::configured();
185
		$this->assertEquals(array('file'), $result);
186
 
187
		CakeLog::drop('file');
188
		$result = CakeLog::configured();
189
		$this->assertSame(array(), $result);
190
	}
191
 
192
/**
193
 * testLogFileWriting method
194
 *
195
 * @return void
196
 */
197
	public function testLogFileWriting() {
198
		if (file_exists(LOGS . 'error.log')) {
199
			unlink(LOGS . 'error.log');
200
		}
201
		$result = CakeLog::write(LOG_WARNING, 'Test warning');
202
		$this->assertTrue($result);
203
		$this->assertTrue(file_exists(LOGS . 'error.log'));
204
		unlink(LOGS . 'error.log');
205
 
206
		CakeLog::write(LOG_WARNING, 'Test warning 1');
207
		CakeLog::write(LOG_WARNING, 'Test warning 2');
208
		$result = file_get_contents(LOGS . 'error.log');
209
		$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 1/', $result);
210
		$this->assertRegExp('/2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 2$/', $result);
211
		unlink(LOGS . 'error.log');
212
	}
213
 
214
/**
215
 * test selective logging by level/type
216
 *
217
 * @return void
218
 */
219
	public function testSelectiveLoggingByLevel() {
220
		if (file_exists(LOGS . 'spam.log')) {
221
			unlink(LOGS . 'spam.log');
222
		}
223
		if (file_exists(LOGS . 'eggs.log')) {
224
			unlink(LOGS . 'eggs.log');
225
		}
226
		CakeLog::config('spam', array(
227
			'engine' => 'File',
228
			'types' => 'debug',
229
			'file' => 'spam',
230
		));
231
		CakeLog::config('eggs', array(
232
			'engine' => 'File',
233
			'types' => array('eggs', 'debug', 'error', 'warning'),
234
			'file' => 'eggs',
235
		));
236
 
237
		$testMessage = 'selective logging';
238
		CakeLog::write(LOG_WARNING, $testMessage);
239
 
240
		$this->assertTrue(file_exists(LOGS . 'eggs.log'));
241
		$this->assertFalse(file_exists(LOGS . 'spam.log'));
242
 
243
		CakeLog::write(LOG_DEBUG, $testMessage);
244
		$this->assertTrue(file_exists(LOGS . 'spam.log'));
245
 
246
		$contents = file_get_contents(LOGS . 'spam.log');
247
		$this->assertContains('Debug: ' . $testMessage, $contents);
248
		$contents = file_get_contents(LOGS . 'eggs.log');
249
		$this->assertContains('Debug: ' . $testMessage, $contents);
250
 
251
		if (file_exists(LOGS . 'spam.log')) {
252
			unlink(LOGS . 'spam.log');
253
		}
254
		if (file_exists(LOGS . 'eggs.log')) {
255
			unlink(LOGS . 'eggs.log');
256
		}
257
	}
258
 
259
/**
260
 * test enable
261
 *
262
 * @expectedException CakeLogException
263
 */
264
	public function testStreamEnable() {
265
		CakeLog::config('spam', array(
266
			'engine' => 'File',
267
			'file' => 'spam',
268
			));
269
		$this->assertTrue(CakeLog::enabled('spam'));
270
		CakeLog::drop('spam');
271
		CakeLog::enable('bogus_stream');
272
	}
273
 
274
/**
275
 * test disable
276
 *
277
 * @expectedException CakeLogException
278
 */
279
	public function testStreamDisable() {
280
		CakeLog::config('spam', array(
281
			'engine' => 'File',
282
			'file' => 'spam',
283
			));
284
		$this->assertTrue(CakeLog::enabled('spam'));
285
		CakeLog::disable('spam');
286
		$this->assertFalse(CakeLog::enabled('spam'));
287
		CakeLog::drop('spam');
288
		CakeLog::enable('bogus_stream');
289
	}
290
 
291
/**
292
 * test enabled() invalid stream
293
 *
294
 * @expectedException CakeLogException
295
 */
296
	public function testStreamEnabledInvalid() {
297
		CakeLog::enabled('bogus_stream');
298
	}
299
 
300
/**
301
 * test disable invalid stream
302
 *
303
 * @expectedException CakeLogException
304
 */
305
	public function testStreamDisableInvalid() {
306
		CakeLog::disable('bogus_stream');
307
	}
308
 
309
	protected function _resetLogConfig() {
310
		CakeLog::config('debug', array(
311
			'engine' => 'File',
312
			'types' => array('notice', 'info', 'debug'),
313
			'file' => 'debug',
314
		));
315
		CakeLog::config('error', array(
316
			'engine' => 'File',
317
			'types' => array('warning', 'error', 'critical', 'alert', 'emergency'),
318
			'file' => 'error',
319
		));
320
	}
321
 
322
	protected function _deleteLogs() {
323
		if (file_exists(LOGS . 'shops.log')) {
324
			unlink(LOGS . 'shops.log');
325
		}
326
		if (file_exists(LOGS . 'error.log')) {
327
			unlink(LOGS . 'error.log');
328
		}
329
		if (file_exists(LOGS . 'debug.log')) {
330
			unlink(LOGS . 'debug.log');
331
		}
332
		if (file_exists(LOGS . 'bogus.log')) {
333
			unlink(LOGS . 'bogus.log');
334
		}
335
		if (file_exists(LOGS . 'spam.log')) {
336
			unlink(LOGS . 'spam.log');
337
		}
338
		if (file_exists(LOGS . 'eggs.log')) {
339
			unlink(LOGS . 'eggs.log');
340
		}
341
	}
342
 
343
/**
344
 * test backward compatible scoped logging
345
 *
346
 * @return void
347
 */
348
	public function testScopedLoggingBC() {
349
		$this->_resetLogConfig();
350
 
351
		CakeLog::config('shops', array(
352
			'engine' => 'File',
353
			'types' => array('info', 'notice', 'warning'),
354
			'scopes' => array('transactions', 'orders'),
355
			'file' => 'shops',
356
		));
357
		$this->_deleteLogs();
358
 
359
		CakeLog::write('info', 'info message');
360
		$this->assertFalse(file_exists(LOGS . 'error.log'));
361
		$this->assertTrue(file_exists(LOGS . 'debug.log'));
362
 
363
		$this->_deleteLogs();
364
 
365
		CakeLog::write('transactions', 'transaction message');
366
		$this->assertTrue(file_exists(LOGS . 'shops.log'));
367
		$this->assertFalse(file_exists(LOGS . 'transactions.log'));
368
		$this->assertFalse(file_exists(LOGS . 'error.log'));
369
		$this->assertFalse(file_exists(LOGS . 'debug.log'));
370
 
371
		$this->_deleteLogs();
372
 
373
		CakeLog::write('error', 'error message');
374
		$this->assertTrue(file_exists(LOGS . 'error.log'));
375
		$this->assertFalse(file_exists(LOGS . 'debug.log'));
376
		$this->assertFalse(file_exists(LOGS . 'shops.log'));
377
 
378
		$this->_deleteLogs();
379
 
380
		CakeLog::write('orders', 'order message');
381
		$this->assertFalse(file_exists(LOGS . 'error.log'));
382
		$this->assertFalse(file_exists(LOGS . 'debug.log'));
383
		$this->assertFalse(file_exists(LOGS . 'orders.log'));
384
		$this->assertTrue(file_exists(LOGS . 'shops.log'));
385
 
386
		$this->_deleteLogs();
387
 
388
		CakeLog::write('warning', 'warning message');
389
		$this->assertTrue(file_exists(LOGS . 'error.log'));
390
		$this->assertFalse(file_exists(LOGS . 'debug.log'));
391
 
392
		$this->_deleteLogs();
393
 
394
		CakeLog::drop('shops');
395
	}
396
 
397
/**
398
 * Test that scopes are exclusive and don't bleed.
399
 *
400
 * @return void
401
 */
402
	public function testScopedLoggingExclusive() {
403
		$this->_deleteLogs();
404
 
405
		CakeLog::config('shops', array(
406
			'engine' => 'File',
407
			'types' => array('info', 'notice', 'warning'),
408
			'scopes' => array('transactions', 'orders'),
409
			'file' => 'shops.log',
410
		));
411
		CakeLog::config('eggs', array(
412
			'engine' => 'File',
413
			'types' => array('info', 'notice', 'warning'),
414
			'scopes' => array('eggs'),
415
			'file' => 'eggs.log',
416
		));
417
 
418
		CakeLog::write('info', 'transactions message', 'transactions');
419
		$this->assertFalse(file_exists(LOGS . 'eggs.log'));
420
		$this->assertTrue(file_exists(LOGS . 'shops.log'));
421
 
422
		$this->_deleteLogs();
423
 
424
		CakeLog::write('info', 'eggs message', 'eggs');
425
		$this->assertTrue(file_exists(LOGS . 'eggs.log'));
426
		$this->assertFalse(file_exists(LOGS . 'shops.log'));
427
	}
428
 
429
/**
430
 * test scoped logging
431
 *
432
 * @return void
433
 */
434
	public function testScopedLogging() {
435
		$this->_resetLogConfig();
436
		$this->_deleteLogs();
437
 
438
		CakeLog::config('string-scope', array(
439
			'engine' => 'File',
440
			'types' => array('info', 'notice', 'warning'),
441
			'scopes' => 'string-scope',
442
			'file' => 'string-scope.log'
443
		));
444
		CakeLog::write('info', 'info message', 'string-scope');
445
		$this->assertTrue(file_exists(LOGS . 'string-scope.log'));
446
 
447
		CakeLog::drop('string-scope');
448
 
449
		CakeLog::config('shops', array(
450
			'engine' => 'File',
451
			'types' => array('info', 'notice', 'warning'),
452
			'scopes' => array('transactions', 'orders'),
453
			'file' => 'shops.log',
454
		));
455
 
456
		CakeLog::write('info', 'info message', 'transactions');
457
		$this->assertFalse(file_exists(LOGS . 'error.log'));
458
		$this->assertTrue(file_exists(LOGS . 'shops.log'));
459
		$this->assertTrue(file_exists(LOGS . 'debug.log'));
460
 
461
		$this->_deleteLogs();
462
 
463
		CakeLog::write('transactions', 'transaction message', 'orders');
464
		$this->assertTrue(file_exists(LOGS . 'shops.log'));
465
		$this->assertFalse(file_exists(LOGS . 'transactions.log'));
466
		$this->assertFalse(file_exists(LOGS . 'error.log'));
467
		$this->assertFalse(file_exists(LOGS . 'debug.log'));
468
 
469
		$this->_deleteLogs();
470
 
471
		CakeLog::write('error', 'error message', 'orders');
472
		$this->assertTrue(file_exists(LOGS . 'error.log'));
473
		$this->assertFalse(file_exists(LOGS . 'debug.log'));
474
		$this->assertFalse(file_exists(LOGS . 'shops.log'));
475
 
476
		$this->_deleteLogs();
477
 
478
		CakeLog::write('orders', 'order message', 'transactions');
479
		$this->assertFalse(file_exists(LOGS . 'error.log'));
480
		$this->assertFalse(file_exists(LOGS . 'debug.log'));
481
		$this->assertFalse(file_exists(LOGS . 'orders.log'));
482
		$this->assertTrue(file_exists(LOGS . 'shops.log'));
483
 
484
		$this->_deleteLogs();
485
 
486
		CakeLog::write('warning', 'warning message', 'orders');
487
		$this->assertTrue(file_exists(LOGS . 'error.log'));
488
		$this->assertTrue(file_exists(LOGS . 'shops.log'));
489
		$this->assertFalse(file_exists(LOGS . 'debug.log'));
490
 
491
		$this->_deleteLogs();
492
 
493
		CakeLog::drop('shops');
494
	}
495
 
496
/**
497
 * test bogus type and scope
498
 *
499
 */
500
	public function testBogusTypeAndScope() {
501
		$this->_resetLogConfig();
502
		$this->_deleteLogs();
503
 
504
		CakeLog::write('bogus', 'bogus message');
505
		$this->assertTrue(file_exists(LOGS . 'bogus.log'));
506
		$this->assertFalse(file_exists(LOGS . 'error.log'));
507
		$this->assertFalse(file_exists(LOGS . 'debug.log'));
508
		$this->_deleteLogs();
509
 
510
		CakeLog::write('bogus', 'bogus message', 'bogus');
511
		$this->assertTrue(file_exists(LOGS . 'bogus.log'));
512
		$this->assertFalse(file_exists(LOGS . 'error.log'));
513
		$this->assertFalse(file_exists(LOGS . 'debug.log'));
514
		$this->_deleteLogs();
515
 
516
		CakeLog::write('error', 'bogus message', 'bogus');
517
		$this->assertFalse(file_exists(LOGS . 'bogus.log'));
518
		$this->assertTrue(file_exists(LOGS . 'error.log'));
519
		$this->assertFalse(file_exists(LOGS . 'debug.log'));
520
		$this->_deleteLogs();
521
	}
522
 
523
/**
524
 * test scoped logging with convenience methods
525
 */
526
	public function testConvenienceScopedLogging() {
527
		if (file_exists(LOGS . 'shops.log')) {
528
			unlink(LOGS . 'shops.log');
529
		}
530
		if (file_exists(LOGS . 'error.log')) {
531
			unlink(LOGS . 'error.log');
532
		}
533
		if (file_exists(LOGS . 'debug.log')) {
534
			unlink(LOGS . 'debug.log');
535
		}
536
 
537
		$this->_resetLogConfig();
538
		CakeLog::config('shops', array(
539
			'engine' => 'File',
540
			'types' => array('info', 'debug', 'notice', 'warning'),
541
			'scopes' => array('transactions', 'orders'),
542
			'file' => 'shops',
543
		));
544
 
545
		CakeLog::info('info message', 'transactions');
546
		$this->assertFalse(file_exists(LOGS . 'error.log'));
547
		$this->assertTrue(file_exists(LOGS . 'shops.log'));
548
		$this->assertTrue(file_exists(LOGS . 'debug.log'));
549
 
550
		$this->_deleteLogs();
551
 
552
		CakeLog::error('error message', 'orders');
553
		$this->assertTrue(file_exists(LOGS . 'error.log'));
554
		$this->assertFalse(file_exists(LOGS . 'debug.log'));
555
		$this->assertFalse(file_exists(LOGS . 'shops.log'));
556
 
557
		$this->_deleteLogs();
558
 
559
		CakeLog::warning('warning message', 'orders');
560
		$this->assertTrue(file_exists(LOGS . 'error.log'));
561
		$this->assertTrue(file_exists(LOGS . 'shops.log'));
562
		$this->assertFalse(file_exists(LOGS . 'debug.log'));
563
 
564
		$this->_deleteLogs();
565
 
566
		CakeLog::drop('shops');
567
	}
568
 
569
/**
570
 * test convenience methods
571
 */
572
	public function testConvenienceMethods() {
573
		$this->_deleteLogs();
574
 
575
		CakeLog::config('debug', array(
576
			'engine' => 'File',
577
			'types' => array('notice', 'info', 'debug'),
578
			'file' => 'debug',
579
		));
580
		CakeLog::config('error', array(
581
			'engine' => 'File',
582
			'types' => array('emergency', 'alert', 'critical', 'error', 'warning'),
583
			'file' => 'error',
584
		));
585
 
586
		$testMessage = 'emergency message';
587
		CakeLog::emergency($testMessage);
588
		$contents = file_get_contents(LOGS . 'error.log');
589
		$this->assertRegExp('/(Emergency|Critical): ' . $testMessage . '/', $contents);
590
		$this->assertFalse(file_exists(LOGS . 'debug.log'));
591
		$this->_deleteLogs();
592
 
593
		$testMessage = 'alert message';
594
		CakeLog::alert($testMessage);
595
		$contents = file_get_contents(LOGS . 'error.log');
596
		$this->assertRegExp('/(Alert|Critical): ' . $testMessage . '/', $contents);
597
		$this->assertFalse(file_exists(LOGS . 'debug.log'));
598
		$this->_deleteLogs();
599
 
600
		$testMessage = 'critical message';
601
		CakeLog::critical($testMessage);
602
		$contents = file_get_contents(LOGS . 'error.log');
603
		$this->assertContains('Critical: ' . $testMessage, $contents);
604
		$this->assertFalse(file_exists(LOGS . 'debug.log'));
605
		$this->_deleteLogs();
606
 
607
		$testMessage = 'error message';
608
		CakeLog::error($testMessage);
609
		$contents = file_get_contents(LOGS . 'error.log');
610
		$this->assertContains('Error: ' . $testMessage, $contents);
611
		$this->assertFalse(file_exists(LOGS . 'debug.log'));
612
		$this->_deleteLogs();
613
 
614
		$testMessage = 'warning message';
615
		CakeLog::warning($testMessage);
616
		$contents = file_get_contents(LOGS . 'error.log');
617
		$this->assertContains('Warning: ' . $testMessage, $contents);
618
		$this->assertFalse(file_exists(LOGS . 'debug.log'));
619
		$this->_deleteLogs();
620
 
621
		$testMessage = 'notice message';
622
		CakeLog::notice($testMessage);
623
		$contents = file_get_contents(LOGS . 'debug.log');
624
		$this->assertRegExp('/(Notice|Debug): ' . $testMessage . '/', $contents);
625
		$this->assertFalse(file_exists(LOGS . 'error.log'));
626
		$this->_deleteLogs();
627
 
628
		$testMessage = 'info message';
629
		CakeLog::info($testMessage);
630
		$contents = file_get_contents(LOGS . 'debug.log');
631
		$this->assertRegExp('/(Info|Debug): ' . $testMessage . '/', $contents);
632
		$this->assertFalse(file_exists(LOGS . 'error.log'));
633
		$this->_deleteLogs();
634
 
635
		$testMessage = 'debug message';
636
		CakeLog::debug($testMessage);
637
		$contents = file_get_contents(LOGS . 'debug.log');
638
		$this->assertContains('Debug: ' . $testMessage, $contents);
639
		$this->assertFalse(file_exists(LOGS . 'error.log'));
640
		$this->_deleteLogs();
641
	}
642
 
643
/**
644
 * test levels customization
645
 */
646
	public function testLevelCustomization() {
647
		$this->skipIf(DIRECTORY_SEPARATOR === '\\', 'Log level tests not supported on Windows.');
648
 
649
		$levels = CakeLog::defaultLevels();
650
		$this->assertNotEmpty($levels);
651
		$result = array_keys($levels);
652
		$this->assertEquals(array(0, 1, 2, 3, 4, 5, 6, 7), $result);
653
 
654
		$levels = CakeLog::levels(array('foo', 'bar'));
655
		CakeLog::defaultLevels();
656
		$this->assertEquals('foo', $levels[8]);
657
		$this->assertEquals('bar', $levels[9]);
658
 
659
		$levels = CakeLog::levels(array(11 => 'spam', 'bar' => 'eggs'));
660
		CakeLog::defaultLevels();
661
		$this->assertEquals('spam', $levels[8]);
662
		$this->assertEquals('eggs', $levels[9]);
663
 
664
		$levels = CakeLog::levels(array(11 => 'spam', 'bar' => 'eggs'), false);
665
		CakeLog::defaultLevels();
666
		$this->assertEquals(array('spam', 'eggs'), $levels);
667
 
668
		$levels = CakeLog::levels(array('ham', 9 => 'spam', '12' => 'fam'), false);
669
		CakeLog::defaultLevels();
670
		$this->assertEquals(array('ham', 'spam', 'fam'), $levels);
671
	}
672
 
673
/**
674
 * Test writing log files with custom levels
675
 */
676
	public function testCustomLevelWrites() {
677
		$this->_deleteLogs();
678
		$this->_resetLogConfig();
679
 
680
		CakeLog::levels(array('spam', 'eggs'));
681
 
682
		$testMessage = 'error message';
683
		CakeLog::write('error', $testMessage);
684
		CakeLog::defaultLevels();
685
		$this->assertTrue(file_exists(LOGS . 'error.log'));
686
		$contents = file_get_contents(LOGS . 'error.log');
687
		$this->assertContains('Error: ' . $testMessage, $contents);
688
 
689
		CakeLog::config('spam', array(
690
			'engine' => 'File',
691
			'file' => 'spam.log',
692
			'types' => 'spam',
693
			));
694
		CakeLog::config('eggs', array(
695
			'engine' => 'File',
696
			'file' => 'eggs.log',
697
			'types' => array('spam', 'eggs'),
698
			));
699
 
700
		$testMessage = 'spam message';
701
		CakeLog::write('spam', $testMessage);
702
		CakeLog::defaultLevels();
703
		$this->assertTrue(file_exists(LOGS . 'spam.log'));
704
		$this->assertTrue(file_exists(LOGS . 'eggs.log'));
705
		$contents = file_get_contents(LOGS . 'spam.log');
706
		$this->assertContains('Spam: ' . $testMessage, $contents);
707
 
708
		$testMessage = 'egg message';
709
		CakeLog::write('eggs', $testMessage);
710
		CakeLog::defaultLevels();
711
		$contents = file_get_contents(LOGS . 'spam.log');
712
		$this->assertNotContains('Eggs: ' . $testMessage, $contents);
713
		$contents = file_get_contents(LOGS . 'eggs.log');
714
		$this->assertContains('Eggs: ' . $testMessage, $contents);
715
 
716
		CakeLog::drop('spam');
717
		CakeLog::drop('eggs');
718
 
719
		$this->_deleteLogs();
720
	}
721
 
722
}