pictcode / lib / Cake / Test / Case / Log / Engine / FileLogTest.php @ 635eef61
履歴 | 表示 | アノテート | ダウンロード (5.991 KB)
1 |
<?php
|
---|---|
2 |
/**
|
3 |
* FileLogTest 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('FileLog', 'Log/Engine'); |
20 |
|
21 |
/**
|
22 |
* CakeLogTest class
|
23 |
*
|
24 |
* @package Cake.Test.Case.Log.Engine
|
25 |
*/
|
26 |
class FileLogTest extends CakeTestCase { |
27 |
|
28 |
/**
|
29 |
* testLogFileWriting method
|
30 |
*
|
31 |
* @return void
|
32 |
*/
|
33 |
public function testLogFileWriting() { |
34 |
$this->_deleteLogs(LOGS); |
35 |
|
36 |
$log = new FileLog(); |
37 |
$log->write('warning', 'Test warning'); |
38 |
$this->assertTrue(file_exists(LOGS . 'error.log')); |
39 |
|
40 |
$result = file_get_contents(LOGS . 'error.log'); |
41 |
$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning/', $result); |
42 |
|
43 |
$log->write('debug', 'Test warning'); |
44 |
$this->assertTrue(file_exists(LOGS . 'debug.log')); |
45 |
|
46 |
$result = file_get_contents(LOGS . 'debug.log'); |
47 |
$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: Test warning/', $result); |
48 |
|
49 |
$log->write('random', 'Test warning'); |
50 |
$this->assertTrue(file_exists(LOGS . 'random.log')); |
51 |
|
52 |
$result = file_get_contents(LOGS . 'random.log'); |
53 |
$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Random: Test warning/', $result); |
54 |
} |
55 |
|
56 |
/**
|
57 |
* test using the path setting to write logs in other places.
|
58 |
*
|
59 |
* @return void
|
60 |
*/
|
61 |
public function testPathSetting() { |
62 |
$path = TMP . 'tests' . DS; |
63 |
$this->_deleteLogs($path); |
64 |
|
65 |
$log = new FileLog(compact('path')); |
66 |
$log->write('warning', 'Test warning'); |
67 |
$this->assertTrue(file_exists($path . 'error.log')); |
68 |
} |
69 |
|
70 |
/**
|
71 |
* test log rotation
|
72 |
*
|
73 |
* @return void
|
74 |
*/
|
75 |
public function testRotation() { |
76 |
$path = TMP . 'tests' . DS; |
77 |
$this->_deleteLogs($path); |
78 |
|
79 |
file_put_contents($path . 'error.log', "this text is under 35 bytes\n"); |
80 |
$log = new FileLog(array( |
81 |
'path' => $path, |
82 |
'size' => 35, |
83 |
'rotate' => 2 |
84 |
)); |
85 |
$log->write('warning', 'Test warning one'); |
86 |
$this->assertTrue(file_exists($path . 'error.log')); |
87 |
|
88 |
$result = file_get_contents($path . 'error.log'); |
89 |
$this->assertRegExp('/Warning: Test warning one/', $result); |
90 |
$this->assertEquals(0, count(glob($path . 'error.log.*'))); |
91 |
|
92 |
clearstatcache();
|
93 |
$log->write('warning', 'Test warning second'); |
94 |
|
95 |
$files = glob($path . 'error.log.*'); |
96 |
$this->assertEquals(1, count($files)); |
97 |
|
98 |
$result = file_get_contents($files[0]); |
99 |
$this->assertRegExp('/this text is under 35 bytes/', $result); |
100 |
$this->assertRegExp('/Warning: Test warning one/', $result); |
101 |
|
102 |
sleep(1); |
103 |
clearstatcache();
|
104 |
$log->write('warning', 'Test warning third'); |
105 |
|
106 |
$result = file_get_contents($path . 'error.log'); |
107 |
$this->assertRegExp('/Warning: Test warning third/', $result); |
108 |
|
109 |
$files = glob($path . 'error.log.*'); |
110 |
$this->assertEquals(2, count($files)); |
111 |
|
112 |
$result = file_get_contents($files[0]); |
113 |
$this->assertRegExp('/this text is under 35 bytes/', $result); |
114 |
|
115 |
$result = file_get_contents($files[1]); |
116 |
$this->assertRegExp('/Warning: Test warning second/', $result); |
117 |
|
118 |
file_put_contents($path . 'error.log.0000000000', "The oldest log file with over 35 bytes.\n"); |
119 |
|
120 |
sleep(1); |
121 |
clearstatcache();
|
122 |
$log->write('warning', 'Test warning fourth'); |
123 |
|
124 |
// rotate count reached so file count should not increase
|
125 |
$files = glob($path . 'error.log.*'); |
126 |
$this->assertEquals(2, count($files)); |
127 |
|
128 |
$result = file_get_contents($path . 'error.log'); |
129 |
$this->assertRegExp('/Warning: Test warning fourth/', $result); |
130 |
|
131 |
$result = file_get_contents(array_pop($files)); |
132 |
$this->assertRegExp('/Warning: Test warning third/', $result); |
133 |
|
134 |
$result = file_get_contents(array_pop($files)); |
135 |
$this->assertRegExp('/Warning: Test warning second/', $result); |
136 |
|
137 |
file_put_contents($path . 'debug.log', "this text is just greater than 35 bytes\n"); |
138 |
$log = new FileLog(array( |
139 |
'path' => $path, |
140 |
'size' => 35, |
141 |
'rotate' => 0 |
142 |
)); |
143 |
file_put_contents($path . 'debug.log.0000000000', "The oldest log file with over 35 bytes.\n"); |
144 |
$log->write('debug', 'Test debug'); |
145 |
$this->assertTrue(file_exists($path . 'debug.log')); |
146 |
|
147 |
$result = file_get_contents($path . 'debug.log'); |
148 |
$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: Test debug/', $result); |
149 |
$this->assertFalse(strstr($result, 'greater than 5 bytes')); |
150 |
$this->assertEquals(0, count(glob($path . 'debug.log.*'))); |
151 |
} |
152 |
|
153 |
public function testMaskSetting() { |
154 |
if (DS === '\\') { |
155 |
$this->markTestSkipped('File permission testing does not work on Windows.'); |
156 |
} |
157 |
|
158 |
$path = TMP . 'tests' . DS; |
159 |
$this->_deleteLogs($path); |
160 |
|
161 |
$log = new FileLog(array('path' => $path, 'mask' => 0666)); |
162 |
$log->write('warning', 'Test warning one'); |
163 |
$result = substr(sprintf('%o', fileperms($path . 'error.log')), -4); |
164 |
$expected = '0666'; |
165 |
$this->assertEquals($expected, $result); |
166 |
unlink($path . 'error.log'); |
167 |
|
168 |
$log = new FileLog(array('path' => $path, 'mask' => 0644)); |
169 |
$log->write('warning', 'Test warning two'); |
170 |
$result = substr(sprintf('%o', fileperms($path . 'error.log')), -4); |
171 |
$expected = '0644'; |
172 |
$this->assertEquals($expected, $result); |
173 |
unlink($path . 'error.log'); |
174 |
|
175 |
$log = new FileLog(array('path' => $path, 'mask' => 0640)); |
176 |
$log->write('warning', 'Test warning three'); |
177 |
$result = substr(sprintf('%o', fileperms($path . 'error.log')), -4); |
178 |
$expected = '0640'; |
179 |
$this->assertEquals($expected, $result); |
180 |
unlink($path . 'error.log'); |
181 |
} |
182 |
|
183 |
/**
|
184 |
* helper function to clears all log files in specified directory
|
185 |
*
|
186 |
* @return void
|
187 |
*/
|
188 |
protected function _deleteLogs($dir) { |
189 |
$files = array_merge(glob($dir . '*.log'), glob($dir . '*.log.*')); |
190 |
foreach ($files as $file) { |
191 |
unlink($file); |
192 |
} |
193 |
} |
194 |
} |