1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleLog\Test; |
4
|
|
|
|
5
|
|
|
use SimpleLog\Log; |
6
|
|
|
use SimpleLog\LogStatic; |
7
|
|
|
use SimpleLog\Storage\File; |
8
|
|
|
use SimpleLog\Message\DefaultInlineMessage; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use SimpleLog\Message\DefaultMessage; |
11
|
|
|
|
12
|
|
|
class SimpleLogTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* name of test event log file |
16
|
|
|
*/ |
17
|
|
|
const NOTICE_LOG_NAME = '/notice.log'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* name of test event log file |
21
|
|
|
*/ |
22
|
|
|
const WARNING_LOG_NAME = '/warning.log'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* store generated log file path |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $logPath; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* actions launched before test starts |
33
|
|
|
*/ |
34
|
|
|
protected function setUp() |
35
|
|
|
{ |
36
|
|
|
$this->logPath = __DIR__ . '/log'; |
37
|
|
|
|
38
|
|
|
$this->tearDown(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @expectedException \Psr\Log\InvalidArgumentException |
43
|
|
|
* @expectedExceptionMessage Level not defined: incorrect |
44
|
|
|
*/ |
45
|
|
|
public function testCreateLogMessageWithIncorrectLevel() |
46
|
|
|
{ |
47
|
|
|
(new Log(['log_path' => $this->logPath])) |
48
|
|
|
->log('incorrect', 'Some log message'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @expectedException \Psr\Log\InvalidArgumentException |
53
|
|
|
* @expectedExceptionMessage Incorrect message type. Must be string, array or object with __toString method. |
54
|
|
|
*/ |
55
|
|
|
public function testCreateIncorrectLogMessage() |
56
|
|
|
{ |
57
|
|
|
(new Log(['log_path' => $this->logPath])) |
58
|
|
|
->makeLog(12312312); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* simple create log object and create log message in given directory |
63
|
|
|
*/ |
64
|
|
|
public function testCreateSimpleLogMessage() |
65
|
|
|
{ |
66
|
|
|
$log = new Log(['log_path' => $this->logPath]); |
67
|
|
|
|
68
|
|
|
$this->assertFileNotExists($this->logPath . self::NOTICE_LOG_NAME); |
69
|
|
|
|
70
|
|
|
$log->makeLog('Some log message'); |
71
|
|
|
|
72
|
|
|
$this->assertFileExists($this->logPath . self::NOTICE_LOG_NAME); |
73
|
|
|
|
74
|
|
|
$content = file_get_contents($this->logPath . self::NOTICE_LOG_NAME); |
75
|
|
|
|
76
|
|
|
//because of different time and date of creating log file, we remove first line with date |
77
|
|
|
$this->assertEquals($this->getSampleContent(), substr($content, strpos($content, "\n") +1)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testCreateSimpleLogWithOtherStorage() |
81
|
|
|
{ |
82
|
|
|
$log = new Log([ |
83
|
|
|
'storage' => new File( |
84
|
|
|
['log_path' => $this->logPath] |
85
|
|
|
) |
86
|
|
|
]); |
87
|
|
|
|
88
|
|
|
$this->assertFileNotExists($this->logPath . self::NOTICE_LOG_NAME); |
89
|
|
|
|
90
|
|
|
$log->makeLog('Some log message'); |
91
|
|
|
|
92
|
|
|
$this->assertFileExists($this->logPath . self::NOTICE_LOG_NAME); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testCreateSimpleLogWithOtherMessage() |
96
|
|
|
{ |
97
|
|
|
$log = new Log([ |
98
|
|
|
'message' => new DefaultInlineMessage, |
99
|
|
|
'log_path' => $this->logPath, |
100
|
|
|
]); |
101
|
|
|
|
102
|
|
|
$this->assertFileNotExists($this->logPath . self::NOTICE_LOG_NAME); |
103
|
|
|
|
104
|
|
|
$log->makeLog('Some log message'); |
105
|
|
|
|
106
|
|
|
$this->assertFileExists($this->logPath . self::NOTICE_LOG_NAME); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testGetLastMessage() |
110
|
|
|
{ |
111
|
|
|
$log = new Log(['log_path' => $this->logPath]); |
112
|
|
|
|
113
|
|
|
$log->makeLog('Some log message'); |
114
|
|
|
|
115
|
|
|
$content = $log->getLastMessage(); |
116
|
|
|
//because of different time and date of creating log file, we remove first line with date |
117
|
|
|
$this->assertEquals($this->getSampleContent(), substr($content, strpos($content, "\n") +1)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* simple create log object and create log message from array data in given directory |
122
|
|
|
*/ |
123
|
|
|
public function testCreateLogWithArrayMessage() |
124
|
|
|
{ |
125
|
|
|
$log = new Log(['log_path' => $this->logPath]); |
126
|
|
|
|
127
|
|
|
$this->assertFileNotExists($this->logPath . self::NOTICE_LOG_NAME); |
128
|
|
|
|
129
|
|
|
$log->makeLog( |
130
|
|
|
[ |
131
|
|
|
'message key' => 'some message', |
132
|
|
|
'another key' => 'some another message', |
133
|
|
|
'no key message', |
134
|
|
|
] |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
$this->assertFileExists($this->logPath . self::NOTICE_LOG_NAME); |
138
|
|
|
|
139
|
|
|
$content = file_get_contents($this->logPath . self::NOTICE_LOG_NAME); |
140
|
|
|
|
141
|
|
|
//because of different time and date of creating log file, we remove first line with date |
142
|
|
|
$this->assertEquals($this->getArrayMessageContent(), substr($content, strpos($content, "\n") +1)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* simple create log object and create log message from array with sub arrays data in given directory |
147
|
|
|
*/ |
148
|
|
|
public function testCreateLogWithSubArrayMessage() |
149
|
|
|
{ |
150
|
|
|
$log = new Log(['log_path' => $this->logPath]); |
151
|
|
|
|
152
|
|
|
$this->assertFileNotExists($this->logPath . self::NOTICE_LOG_NAME); |
153
|
|
|
|
154
|
|
|
$log->makeLog( |
155
|
|
|
[ |
156
|
|
|
'sub array' => [ |
157
|
|
|
'key' => 'val', |
158
|
|
|
'key 2' => 'val 2', |
159
|
|
|
], |
160
|
|
|
] |
161
|
|
|
); |
162
|
|
|
|
163
|
|
|
$this->assertFileExists($this->logPath . self::NOTICE_LOG_NAME); |
164
|
|
|
|
165
|
|
|
$content = file_get_contents($this->logPath . self::NOTICE_LOG_NAME); |
166
|
|
|
|
167
|
|
|
//because of different time and date of creating log file, we remove first line with date |
168
|
|
|
//hack with remove new lines because of differences between output and stored expectation |
169
|
|
|
$this->assertEquals( |
170
|
|
|
str_replace("\n", '', $this->getSubArrayMessageContent()), |
171
|
|
|
str_replace("\n", '', substr($content, strpos($content, "\n") +1)) |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* test setting and getting options via specified methods |
177
|
|
|
*/ |
178
|
|
|
public function testCreateLogObjectWithOtherConfig() |
179
|
|
|
{ |
180
|
|
|
$log = new Log; |
181
|
|
|
|
182
|
|
|
$this->assertFileNotExists($this->logPath . self::WARNING_LOG_NAME); |
183
|
|
|
|
184
|
|
|
$log->setOption('log_path', $this->logPath) |
185
|
|
|
->setOption('level', 'warning') |
186
|
|
|
->makeLog('Some log message'); |
187
|
|
|
|
188
|
|
|
$this->assertFileExists($this->logPath . self::WARNING_LOG_NAME); |
189
|
|
|
$this->assertEquals('warning', $log->getOption('level')); |
190
|
|
|
$this->assertEquals( |
191
|
|
|
[ |
192
|
|
|
'log_path' => $this->logPath, |
193
|
|
|
'level' => 'warning', |
194
|
|
|
'storage' => 'SimpleLog\Storage\File', |
195
|
|
|
'message' => DefaultMessage::class, |
196
|
|
|
], |
197
|
|
|
$log->getOption() |
198
|
|
|
); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* check static log interface |
203
|
|
|
*/ |
204
|
|
|
public function testCreateStaticMakeLog() |
205
|
|
|
{ |
206
|
|
|
LogStatic::setOption('log_path', $this->logPath); |
207
|
|
|
|
208
|
|
|
$this->assertFileNotExists($this->logPath . self::NOTICE_LOG_NAME); |
209
|
|
|
$this->assertEquals($this->logPath, LogStatic::getOption('log_path')); |
210
|
|
|
|
211
|
|
|
LogStatic::makeLog('Some log message'); |
212
|
|
|
|
213
|
|
|
$this->assertFileExists($this->logPath . self::NOTICE_LOG_NAME); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* check static log interface |
218
|
|
|
*/ |
219
|
|
|
public function testCreateStaticLog() |
220
|
|
|
{ |
221
|
|
|
LogStatic::setOption('log_path', $this->logPath); |
222
|
|
|
|
223
|
|
|
$this->assertFileNotExists($this->logPath . self::NOTICE_LOG_NAME); |
224
|
|
|
$this->assertEquals($this->logPath, LogStatic::getOption('log_path')); |
225
|
|
|
|
226
|
|
|
LogStatic::log('notice', 'Some log message'); |
227
|
|
|
|
228
|
|
|
$this->assertFileExists($this->logPath . self::NOTICE_LOG_NAME); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function testLogWithContext() |
232
|
|
|
{ |
233
|
|
|
$log = new Log(['log_path' => $this->logPath]); |
234
|
|
|
|
235
|
|
|
$this->assertFileNotExists($this->logPath . self::NOTICE_LOG_NAME); |
236
|
|
|
|
237
|
|
|
$log->makeLog('Some log message with {context}', ['context' => 'some value']); |
238
|
|
|
|
239
|
|
|
$this->assertFileExists($this->logPath . self::NOTICE_LOG_NAME); |
240
|
|
|
|
241
|
|
|
$content = file_get_contents($this->logPath . self::NOTICE_LOG_NAME); |
242
|
|
|
|
243
|
|
|
//because of different time and date of creating log file, we remove first line with date |
244
|
|
|
$this->assertEquals( |
245
|
|
|
$this->getSampleContentWithContext(), |
246
|
|
|
substr($content, strpos($content, "\n") +1) |
247
|
|
|
); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
protected function getSampleContent() |
251
|
|
|
{ |
252
|
|
|
return <<<EOT |
253
|
|
|
Some log message |
254
|
|
|
----------------------------------------------------------- |
255
|
|
|
|
256
|
|
|
EOT; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
protected function getSampleContentWithContext() |
260
|
|
|
{ |
261
|
|
|
return <<<EOT |
262
|
|
|
Some log message with some value |
263
|
|
|
----------------------------------------------------------- |
264
|
|
|
|
265
|
|
|
EOT; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
protected function getArrayMessageContent() |
269
|
|
|
{ |
270
|
|
|
return <<<EOT |
271
|
|
|
- message key: some message |
272
|
|
|
- another key: some another message |
273
|
|
|
- no key message |
274
|
|
|
----------------------------------------------------------- |
275
|
|
|
|
276
|
|
|
EOT; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
protected function getSubArrayMessageContent() |
280
|
|
|
{ |
281
|
|
|
return <<<EOT |
282
|
|
|
- sub array: |
283
|
|
|
- key: val |
284
|
|
|
- key 2: val 2 |
285
|
|
|
----------------------------------------------------------- |
286
|
|
|
|
287
|
|
|
EOT; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* actions launched after test was finished |
292
|
|
|
*/ |
293
|
|
|
protected function tearDown() |
294
|
|
|
{ |
295
|
|
|
if (file_exists($this->logPath . self::NOTICE_LOG_NAME)) { |
296
|
|
|
unlink($this->logPath . self::NOTICE_LOG_NAME); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
if (file_exists($this->logPath . self::WARNING_LOG_NAME)) { |
300
|
|
|
unlink($this->logPath . self::WARNING_LOG_NAME); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
if (file_exists($this->logPath)) { |
304
|
|
|
rmdir($this->logPath); |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|