Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * ConsoleLogTest 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.Engine
15
 * @since         CakePHP(tm) v 1.3
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('ConsoleLog', 'Log/Engine');
20
 
21
/**
22
 * Class TestConsoleLog
23
 *
24
 * @package       Cake.Test.Case.Log.Engine
25
 */
26
class TestConsoleLog extends ConsoleLog {
27
 
28
}
29
 
30
/**
31
 * Class TestCakeLog
32
 *
33
 * @package       Cake.Test.Case.Log.Engine
34
 */
35
class TestCakeLog extends CakeLog {
36
 
37
	public static function replace($key, &$engine) {
38
		self::$_Collection->{$key} = $engine;
39
	}
40
 
41
}
42
 
43
/**
44
 * ConsoleLogTest class
45
 *
46
 * @package       Cake.Test.Case.Log.Engine
47
 */
48
class ConsoleLogTest extends CakeTestCase {
49
 
50
	public function setUp() {
51
		parent::setUp();
52
		CakeLog::config('debug', array(
53
			'engine' => 'File',
54
			'types' => array('notice', 'info', 'debug'),
55
			'file' => 'debug',
56
		));
57
		CakeLog::config('error', array(
58
			'engine' => 'File',
59
			'types' => array('error', 'warning'),
60
			'file' => 'error',
61
		));
62
	}
63
 
64
	public function tearDown() {
65
		parent::tearDown();
66
		if (file_exists(LOGS . 'error.log')) {
67
			unlink(LOGS . 'error.log');
68
		}
69
		if (file_exists(LOGS . 'debug.log')) {
70
			unlink(LOGS . 'debug.log');
71
		}
72
	}
73
 
74
/**
75
 * Test writing to ConsoleOutput
76
 */
77
	public function testConsoleOutputWrites() {
78
		TestCakeLog::config('test_console_log', array(
79
			'engine' => 'TestConsole',
80
			));
81
 
82
		$mock = $this->getMock('TestConsoleLog', array('write'), array(
83
			array('types' => 'error'),
84
			));
85
		TestCakeLog::replace('test_console_log', $mock);
86
 
87
		$message = 'Test error message';
88
		$mock->expects($this->once())
89
			->method('write');
90
		TestCakeLog::write(LOG_ERR, $message);
91
	}
92
 
93
/**
94
 * Test logging to both ConsoleLog and FileLog
95
 */
96
	public function testCombinedLogWriting() {
97
		TestCakeLog::config('test_console_log', array(
98
			'engine' => 'TestConsole',
99
			));
100
		$mock = $this->getMock('TestConsoleLog', array('write'), array(
101
			array('types' => 'error'),
102
			));
103
		TestCakeLog::replace('test_console_log', $mock);
104
 
105
		// log to both file and console
106
		$message = 'Test error message';
107
		$mock->expects($this->once())
108
			->method('write');
109
		TestCakeLog::write(LOG_ERR, $message);
110
		$this->assertTrue(file_exists(LOGS . 'error.log'), 'error.log missing');
111
		$logOutput = file_get_contents(LOGS . 'error.log');
112
		$this->assertContains($message, $logOutput);
113
 
114
		// TestConsoleLog is only interested in `error` type
115
		$message = 'Test info message';
116
		$mock->expects($this->never())
117
			->method('write');
118
		TestCakeLog::write(LOG_INFO, $message);
119
 
120
		// checks that output is correctly written in the correct logfile
121
		$this->assertTrue(file_exists(LOGS . 'error.log'), 'error.log missing');
122
		$this->assertTrue(file_exists(LOGS . 'debug.log'), 'debug.log missing');
123
		$logOutput = file_get_contents(LOGS . 'error.log');
124
		$this->assertNotContains($message, $logOutput);
125
		$logOutput = file_get_contents(LOGS . 'debug.log');
126
		$this->assertContains($message, $logOutput);
127
	}
128
 
129
/**
130
 * test default value of stream 'outputAs'
131
 */
132
	public function testDefaultOutputAs() {
133
		TestCakeLog::config('test_console_log', array(
134
			'engine' => 'TestConsole',
135
			));
136
		if (DS === '\\' && !(bool)env('ANSICON')) {
137
			$expected = ConsoleOutput::PLAIN;
138
		} else {
139
			$expected = ConsoleOutput::COLOR;
140
		}
141
		$stream = TestCakeLog::stream('test_console_log');
142
		$config = $stream->config();
143
		$this->assertEquals($expected, $config['outputAs']);
144
	}
145
 
146
}