| 16591 |
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 |
static::$_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 |
* @return void
|
|
|
78 |
*/
|
|
|
79 |
public function testConsoleOutputWrites() {
|
|
|
80 |
TestCakeLog::config('test_console_log', array(
|
|
|
81 |
'engine' => 'TestConsole',
|
|
|
82 |
));
|
|
|
83 |
|
|
|
84 |
$mock = $this->getMock('TestConsoleLog', array('write'), array(
|
|
|
85 |
array('types' => 'error'),
|
|
|
86 |
));
|
|
|
87 |
TestCakeLog::replace('test_console_log', $mock);
|
|
|
88 |
|
|
|
89 |
$message = 'Test error message';
|
|
|
90 |
$mock->expects($this->once())
|
|
|
91 |
->method('write');
|
|
|
92 |
TestCakeLog::write(LOG_ERR, $message);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Test logging to both ConsoleLog and FileLog
|
|
|
97 |
*
|
|
|
98 |
* @return void
|
|
|
99 |
*/
|
|
|
100 |
public function testCombinedLogWriting() {
|
|
|
101 |
TestCakeLog::config('test_console_log', array(
|
|
|
102 |
'engine' => 'TestConsole',
|
|
|
103 |
));
|
|
|
104 |
$mock = $this->getMock('TestConsoleLog', array('write'), array(
|
|
|
105 |
array('types' => 'error'),
|
|
|
106 |
));
|
|
|
107 |
TestCakeLog::replace('test_console_log', $mock);
|
|
|
108 |
|
|
|
109 |
// log to both file and console
|
|
|
110 |
$message = 'Test error message';
|
|
|
111 |
$mock->expects($this->once())
|
|
|
112 |
->method('write');
|
|
|
113 |
TestCakeLog::write(LOG_ERR, $message);
|
|
|
114 |
$this->assertTrue(file_exists(LOGS . 'error.log'), 'error.log missing');
|
|
|
115 |
$logOutput = file_get_contents(LOGS . 'error.log');
|
|
|
116 |
$this->assertContains($message, $logOutput);
|
|
|
117 |
|
|
|
118 |
// TestConsoleLog is only interested in `error` type
|
|
|
119 |
$message = 'Test info message';
|
|
|
120 |
$mock->expects($this->never())
|
|
|
121 |
->method('write');
|
|
|
122 |
TestCakeLog::write(LOG_INFO, $message);
|
|
|
123 |
|
|
|
124 |
// checks that output is correctly written in the correct logfile
|
|
|
125 |
$this->assertTrue(file_exists(LOGS . 'error.log'), 'error.log missing');
|
|
|
126 |
$this->assertTrue(file_exists(LOGS . 'debug.log'), 'debug.log missing');
|
|
|
127 |
$logOutput = file_get_contents(LOGS . 'error.log');
|
|
|
128 |
$this->assertNotContains($message, $logOutput);
|
|
|
129 |
$logOutput = file_get_contents(LOGS . 'debug.log');
|
|
|
130 |
$this->assertContains($message, $logOutput);
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
/**
|
|
|
134 |
* test default value of stream 'outputAs'
|
|
|
135 |
*
|
|
|
136 |
* @return void
|
|
|
137 |
*/
|
|
|
138 |
public function testDefaultOutputAs() {
|
|
|
139 |
TestCakeLog::config('test_console_log', array(
|
|
|
140 |
'engine' => 'TestConsole',
|
|
|
141 |
));
|
|
|
142 |
if ((DS === '\\' && !(bool)env('ANSICON') && env('ConEmuANSI') !== 'ON') ||
|
|
|
143 |
(function_exists('posix_isatty') && !posix_isatty(null))
|
|
|
144 |
) {
|
|
|
145 |
$expected = ConsoleOutput::PLAIN;
|
|
|
146 |
} else {
|
|
|
147 |
$expected = ConsoleOutput::COLOR;
|
|
|
148 |
}
|
|
|
149 |
$stream = TestCakeLog::stream('test_console_log');
|
|
|
150 |
$config = $stream->config();
|
|
|
151 |
$this->assertEquals($expected, $config['outputAs']);
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
}
|