/
FileLogTest.php
195 lines (159 loc) · 5.95 KB
/
FileLogTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* FileLogTest file
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @since 1.3.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Log\Engine;
use Cake\Log\Engine\FileLog;
use Cake\TestSuite\TestCase;
/**
* FileLogTest class
*
*/
class FileLogTest extends TestCase {
/**
* testLogFileWriting method
*
* @return void
*/
public function testLogFileWriting() {
$this->_deleteLogs(LOGS);
$log = new FileLog();
$log->log('warning', 'Test warning');
$this->assertTrue(file_exists(LOGS . 'error.log'));
$result = file_get_contents(LOGS . 'error.log');
$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning/', $result);
$log->log('debug', 'Test warning');
$this->assertTrue(file_exists(LOGS . 'debug.log'));
$result = file_get_contents(LOGS . 'debug.log');
$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: Test warning/', $result);
$log->log('random', 'Test warning');
$this->assertTrue(file_exists(LOGS . 'random.log'));
$result = file_get_contents(LOGS . 'random.log');
$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Random: Test warning/', $result);
}
/**
* test using the path setting to log logs in other places.
*
* @return void
*/
public function testPathSetting() {
$path = TMP . 'tests' . DS;
$this->_deleteLogs($path);
$log = new FileLog(compact('path'));
$log->log('warning', 'Test warning');
$this->assertTrue(file_exists($path . 'error.log'));
}
/**
* test log rotation
*
* @return void
*/
public function testRotation() {
$path = TMP . 'tests' . DS;
$this->_deleteLogs($path);
file_put_contents($path . 'error.log', "this text is under 35 bytes\n");
$log = new FileLog(array(
'path' => $path,
'size' => 35,
'rotate' => 2
));
$log->log('warning', 'Test warning one');
$this->assertTrue(file_exists($path . 'error.log'));
$result = file_get_contents($path . 'error.log');
$this->assertRegExp('/Warning: Test warning one/', $result);
$this->assertEquals(0, count(glob($path . 'error.log.*')));
clearstatcache();
$log->log('warning', 'Test warning second');
$files = glob($path . 'error.log.*');
$this->assertEquals(1, count($files));
$result = file_get_contents($files[0]);
$this->assertRegExp('/this text is under 35 bytes/', $result);
$this->assertRegExp('/Warning: Test warning one/', $result);
sleep(1);
clearstatcache();
$log->log('warning', 'Test warning third');
$result = file_get_contents($path . 'error.log');
$this->assertRegExp('/Warning: Test warning third/', $result);
$files = glob($path . 'error.log.*');
$this->assertEquals(2, count($files));
$result = file_get_contents($files[0]);
$this->assertRegExp('/this text is under 35 bytes/', $result);
$result = file_get_contents($files[1]);
$this->assertRegExp('/Warning: Test warning second/', $result);
file_put_contents($path . 'error.log.0000000000', "The oldest log file with over 35 bytes.\n");
sleep(1);
clearstatcache();
$log->log('warning', 'Test warning fourth');
// rotate count reached so file count should not increase
$files = glob($path . 'error.log.*');
$this->assertEquals(2, count($files));
$result = file_get_contents($path . 'error.log');
$this->assertRegExp('/Warning: Test warning fourth/', $result);
$result = file_get_contents(array_pop($files));
$this->assertRegExp('/Warning: Test warning third/', $result);
$result = file_get_contents(array_pop($files));
$this->assertRegExp('/Warning: Test warning second/', $result);
file_put_contents($path . 'debug.log', "this text is just greater than 35 bytes\n");
$log = new FileLog(array(
'path' => $path,
'size' => 35,
'rotate' => 0
));
file_put_contents($path . 'debug.log.0000000000', "The oldest log file with over 35 bytes.\n");
$log->log('debug', 'Test debug');
$this->assertTrue(file_exists($path . 'debug.log'));
$result = file_get_contents($path . 'debug.log');
$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: Test debug/', $result);
$this->assertFalse(strstr($result, 'greater than 5 bytes'));
$this->assertEquals(0, count(glob($path . 'debug.log.*')));
}
public function testMaskSetting() {
if (DS === '\\') {
$this->markTestSkipped('File permission testing does not work on Windows.');
}
$path = TMP . 'tests' . DS;
$this->_deleteLogs($path);
$log = new FileLog(array('path' => $path, 'mask' => 0666));
$log->log('warning', 'Test warning one');
$result = substr(sprintf('%o', fileperms($path . 'error.log')), -4);
$expected = '0666';
$this->assertEquals($expected, $result);
unlink($path . 'error.log');
$log = new FileLog(array('path' => $path, 'mask' => 0644));
$log->log('warning', 'Test warning two');
$result = substr(sprintf('%o', fileperms($path . 'error.log')), -4);
$expected = '0644';
$this->assertEquals($expected, $result);
unlink($path . 'error.log');
$log = new FileLog(array('path' => $path, 'mask' => 0640));
$log->log('warning', 'Test warning three');
$result = substr(sprintf('%o', fileperms($path . 'error.log')), -4);
$expected = '0640';
$this->assertEquals($expected, $result);
unlink($path . 'error.log');
}
/**
* helper function to clears all log files in specified directory
*
* @param string $dir
* @return void
*/
protected function _deleteLogs($dir) {
$files = array_merge(glob($dir . '*.log'), glob($dir . '*.log.*'));
foreach ($files as $file) {
unlink($file);
}
}
}