|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: lenovo |
|
5
|
|
|
* Date: 6/18/2018 |
|
6
|
|
|
* Time: 3:23 PM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace TimSDK\Foundation\Log; |
|
10
|
|
|
|
|
11
|
|
|
use Monolog\Logger; |
|
12
|
|
|
use TimSDK\Tests\TestCase; |
|
13
|
|
|
use TimSDK\Foundation\Config; |
|
14
|
|
|
use Monolog\Handler\ErrorLogHandler; |
|
15
|
|
|
use TimSDK\Container\ServiceContainer; |
|
16
|
|
|
|
|
17
|
|
|
class LogManagerTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
public function testStack() |
|
20
|
|
|
{ |
|
21
|
|
|
$app = new ServiceContainer([], [ |
|
22
|
|
|
'config' => new Config([ |
|
23
|
|
|
'log' => [ |
|
24
|
|
|
'default' => 'errorlog', |
|
25
|
|
|
'channels' => [ |
|
26
|
|
|
'stack' => [ |
|
27
|
|
|
'driver' => 'stack', |
|
28
|
|
|
'channels' => ['errorlog', 'single'], |
|
29
|
|
|
], |
|
30
|
|
|
'errorlog' => [ |
|
31
|
|
|
'driver' => 'errorlog', |
|
32
|
|
|
'type' => ErrorLogHandler::OPERATING_SYSTEM, |
|
33
|
|
|
'level' => 'debug', |
|
34
|
|
|
], |
|
35
|
|
|
'single' => [ |
|
36
|
|
|
'driver' => 'single', |
|
37
|
|
|
'path' => __DIR__ . '/logs/tim-sdk.log', |
|
38
|
|
|
'level' => 'debug', |
|
39
|
|
|
], |
|
40
|
|
|
], |
|
41
|
|
|
] |
|
42
|
|
|
]) |
|
43
|
|
|
]); |
|
44
|
|
|
|
|
45
|
|
|
$log = new LogManager($app); |
|
46
|
|
|
$this->assertInstanceOf(ErrorLogHandler::class, $log->stack(['errorlog', 'single'])->getHandlers()[0]); |
|
47
|
|
|
$this->assertInstanceOf(ErrorLogHandler::class, $log->channel('stack')->getHandlers()[0]); |
|
|
|
|
|
|
48
|
|
|
$this->assertInstanceOf(ErrorLogHandler::class, $log->driver('stack')->getHandlers()[0]); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function testResolveUndefinedDriver() |
|
52
|
|
|
{ |
|
53
|
|
|
$app = new ServiceContainer([]); |
|
54
|
|
|
$log = \Mockery::mock(LogManager::class.'[createEmergencyLogger]', [$app])->shouldAllowMockingProtectedMethods(); |
|
55
|
|
|
|
|
56
|
|
|
$emergencyLogger = \Mockery::mock(Logger::class); |
|
57
|
|
|
$log->shouldReceive('createEmergencyLogger')->andReturn($emergencyLogger)->once(); |
|
58
|
|
|
$emergencyLogger->shouldReceive('emergency') |
|
|
|
|
|
|
59
|
|
|
->with('Unable to create configured logger. Using emergency logger.', \Mockery::on(function ($data) { |
|
60
|
|
|
$this->assertArrayHasKey('exception', $data); |
|
61
|
|
|
$this->assertInstanceOf(\InvalidArgumentException::class, $data['exception']); |
|
62
|
|
|
$this->assertSame('Log [bad-name] is not defined.', $data['exception']->getMessage()); |
|
63
|
|
|
|
|
64
|
|
|
return true; |
|
65
|
|
|
})); |
|
66
|
|
|
$log->driver('bad-name'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testResolveCustomCreator() |
|
70
|
|
|
{ |
|
71
|
|
|
$app = new ServiceContainer([], [ |
|
72
|
|
|
'config' => new Config([ |
|
73
|
|
|
'log' => [ |
|
74
|
|
|
'channels' => [ |
|
75
|
|
|
'custom' => [ |
|
76
|
|
|
'driver' => 'mylog', |
|
77
|
|
|
'key' => 'value', |
|
78
|
|
|
'level' => 'debug', |
|
79
|
|
|
], |
|
80
|
|
|
], |
|
81
|
|
|
], |
|
82
|
|
|
]), |
|
83
|
|
|
]); |
|
84
|
|
|
|
|
85
|
|
|
$log = new LogManager($app); |
|
86
|
|
|
$log->extend('mylog', function () { |
|
87
|
|
|
return 'mylog'; |
|
88
|
|
|
}); |
|
89
|
|
|
|
|
90
|
|
|
$this->assertSame('mylog', $log->driver('custom')); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function testUnsupportedDriver() |
|
94
|
|
|
{ |
|
95
|
|
|
$app = new ServiceContainer([], [ |
|
96
|
|
|
'config' => new Config([ |
|
97
|
|
|
'log' => [ |
|
98
|
|
|
'channels' => [ |
|
99
|
|
|
'custom' => [ |
|
100
|
|
|
'driver' => 'abcde', |
|
101
|
|
|
'key' => 'value', |
|
102
|
|
|
'level' => 'debug', |
|
103
|
|
|
], |
|
104
|
|
|
], |
|
105
|
|
|
], |
|
106
|
|
|
]), |
|
107
|
|
|
]); |
|
108
|
|
|
|
|
109
|
|
|
$log = \Mockery::mock(LogManager::class.'[createEmergencyLogger]', [$app])->shouldAllowMockingProtectedMethods(); |
|
110
|
|
|
$emergencyLogger = \Mockery::mock(Logger::class); |
|
111
|
|
|
$log->shouldReceive('createEmergencyLogger')->andReturn($emergencyLogger)->once(); |
|
112
|
|
|
$emergencyLogger->shouldReceive('emergency') |
|
|
|
|
|
|
113
|
|
|
->with('Unable to create configured logger. Using emergency logger.', \Mockery::on(function ($data) { |
|
114
|
|
|
$this->assertArrayHasKey('exception', $data); |
|
115
|
|
|
$this->assertInstanceOf(\InvalidArgumentException::class, $data['exception']); |
|
116
|
|
|
$this->assertSame('Driver [abcde] is not supported.', $data['exception']->getMessage()); |
|
117
|
|
|
|
|
118
|
|
|
return true; |
|
119
|
|
|
}))->once(); |
|
120
|
|
|
$log->driver('custom'); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function testLogAgencyMethod() |
|
124
|
|
|
{ |
|
125
|
|
|
$app = new ServiceContainer([], [ |
|
126
|
|
|
'config' => new Config([ |
|
127
|
|
|
'log' => [ |
|
128
|
|
|
'default' => 'single', |
|
129
|
|
|
'channels' => [ |
|
130
|
|
|
'single' => [ |
|
131
|
|
|
'driver' => 'single', |
|
132
|
|
|
], |
|
133
|
|
|
], |
|
134
|
|
|
], |
|
135
|
|
|
]), |
|
136
|
|
|
]); |
|
137
|
|
|
$log = \Mockery::mock(LogManager::class.'[createSingleDriver]', [$app])->shouldAllowMockingProtectedMethods(); |
|
138
|
|
|
|
|
139
|
|
|
$logger = \Mockery::mock(Logger::class); |
|
140
|
|
|
$log->shouldReceive('createSingleDriver')->andReturn($logger)->once(); |
|
141
|
|
|
$logger->shouldReceive('log')->with('debug', 'log message', [])->once(); |
|
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
$log->log('debug', 'log message'); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function testAgencyMethods() |
|
147
|
|
|
{ |
|
148
|
|
|
$app = new ServiceContainer([], [ |
|
149
|
|
|
'config' => new Config([ |
|
150
|
|
|
'log' => [ |
|
151
|
|
|
'default' => 'single', |
|
152
|
|
|
'channels' => [ |
|
153
|
|
|
'single' => [ |
|
154
|
|
|
'driver' => 'single', |
|
155
|
|
|
], |
|
156
|
|
|
], |
|
157
|
|
|
], |
|
158
|
|
|
]), |
|
159
|
|
|
]); |
|
160
|
|
|
$log = \Mockery::mock(LogManager::class.'[createSingleDriver]', [$app])->shouldAllowMockingProtectedMethods(); |
|
161
|
|
|
|
|
162
|
|
|
$logger = \Mockery::mock(Logger::class); |
|
163
|
|
|
|
|
164
|
|
|
$log->shouldReceive('createSingleDriver')->andReturn($logger)->once(); |
|
165
|
|
|
$logger->shouldReceive('log')->withAnyArgs()->once(); |
|
|
|
|
|
|
166
|
|
|
$logger->shouldReceive('emergency')->with('emergency message', [])->once(); |
|
167
|
|
|
$logger->shouldReceive('alert')->with('alert message', [])->once(); |
|
168
|
|
|
$logger->shouldReceive('critical')->with('critical message', [])->once(); |
|
169
|
|
|
$logger->shouldReceive('error')->with('error message', [])->once(); |
|
170
|
|
|
$logger->shouldReceive('warning')->with('warning message', [])->once(); |
|
171
|
|
|
$logger->shouldReceive('notice')->with('notice message', [])->once(); |
|
172
|
|
|
$logger->shouldReceive('info')->with('info message', [])->once(); |
|
173
|
|
|
$logger->shouldReceive('debug')->with('debug message', [])->once(); |
|
174
|
|
|
|
|
175
|
|
|
$log->log('debug', 'log message'); |
|
176
|
|
|
$log->emergency('emergency message'); |
|
177
|
|
|
$log->alert('alert message'); |
|
178
|
|
|
$log->critical('critical message'); |
|
179
|
|
|
$log->error('error message'); |
|
180
|
|
|
$log->warning('warning message'); |
|
181
|
|
|
$log->notice('notice message'); |
|
182
|
|
|
$log->info('info message'); |
|
183
|
|
|
$log->debug('debug message'); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function testSetDefaultDriver() |
|
187
|
|
|
{ |
|
188
|
|
|
$app = new ServiceContainer([], [ |
|
189
|
|
|
'config' => new Config([ |
|
190
|
|
|
'log' => [ |
|
191
|
|
|
'channels' => [ |
|
192
|
|
|
'single' => [ |
|
193
|
|
|
'driver' => 'single', |
|
194
|
|
|
], |
|
195
|
|
|
], |
|
196
|
|
|
], |
|
197
|
|
|
]), |
|
198
|
|
|
]); |
|
199
|
|
|
$log = \Mockery::mock(LogManager::class.'[createSingleDriver]', [$app])->shouldAllowMockingProtectedMethods(); |
|
200
|
|
|
|
|
201
|
|
|
$logger = \Mockery::mock(Logger::class); |
|
202
|
|
|
|
|
203
|
|
|
$this->assertNull($log->getDefaultDriver()); |
|
204
|
|
|
|
|
205
|
|
|
$log->setDefaultDriver('single'); |
|
206
|
|
|
|
|
207
|
|
|
$log->shouldReceive('createSingleDriver')->andReturn($logger)->once(); |
|
208
|
|
|
$logger->shouldReceive('log')->withAnyArgs()->once(); |
|
|
|
|
|
|
209
|
|
|
$logger->shouldReceive('debug')->with('debug message', [])->once(); |
|
210
|
|
|
|
|
211
|
|
|
$log->debug('debug message'); |
|
212
|
|
|
|
|
213
|
|
|
$this->assertSame('single', $log->getDefaultDriver()); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @expectedException \InvalidArgumentException |
|
218
|
|
|
* @expectedExceptionMessage Invalid log file path. |
|
219
|
|
|
*/ |
|
220
|
|
|
public function testInvalidSingleLogPath() |
|
221
|
|
|
{ |
|
222
|
|
|
$app = new ServiceContainer([]); |
|
223
|
|
|
|
|
224
|
|
|
$log = \Mockery::mock(LogManager::class, [$app]) |
|
225
|
|
|
->shouldAllowMockingProtectedMethods() |
|
226
|
|
|
->shouldDeferMissing(); |
|
227
|
|
|
|
|
228
|
|
|
$log->createSingleDriver([ |
|
229
|
|
|
'path' => null, |
|
230
|
|
|
]); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @expectedException \InvalidArgumentException |
|
235
|
|
|
* @expectedExceptionMessage Invalid log file path. |
|
236
|
|
|
*/ |
|
237
|
|
|
public function testInvalidDailyLogPath() |
|
238
|
|
|
{ |
|
239
|
|
|
$app = new ServiceContainer([]); |
|
240
|
|
|
|
|
241
|
|
|
$log = \Mockery::mock(LogManager::class, [$app]) |
|
242
|
|
|
->shouldAllowMockingProtectedMethods() |
|
243
|
|
|
->shouldDeferMissing(); |
|
244
|
|
|
|
|
245
|
|
|
$log->createDailyDriver([ |
|
246
|
|
|
'path' => null, |
|
247
|
|
|
]); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
public function testDriverCreators() |
|
251
|
|
|
{ |
|
252
|
|
|
$app = new ServiceContainer([], [ |
|
253
|
|
|
'config' => new Config([ |
|
254
|
|
|
'log' => [ |
|
255
|
|
|
'channels' => [ |
|
256
|
|
|
'single' => [ |
|
257
|
|
|
'driver' => 'single', |
|
258
|
|
|
'path' => '/path/to/file.log' |
|
259
|
|
|
], |
|
260
|
|
|
], |
|
261
|
|
|
], |
|
262
|
|
|
]), |
|
263
|
|
|
]); |
|
264
|
|
|
$log = \Mockery::mock(LogManager::class, [$app]) |
|
265
|
|
|
->shouldAllowMockingProtectedMethods() |
|
266
|
|
|
->shouldDeferMissing(); |
|
267
|
|
|
|
|
268
|
|
|
$this->assertInstanceOf(Logger::class, $log->createStackDriver(['channels' => ['single']])); |
|
269
|
|
|
$this->assertInstanceOf(Logger::class, $log->createSlackDriver(['url' => 'https://baidu.com'])); |
|
270
|
|
|
$this->assertInstanceOf(Logger::class, $log->createDailyDriver(['path' => '/path/to/file.log'])); |
|
271
|
|
|
$this->assertInstanceOf(Logger::class, $log->createSyslogDriver([])); |
|
272
|
|
|
$this->assertInstanceOf(Logger::class, $log->createErrorlogDriver([])); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* @expectedException \InvalidArgumentException |
|
277
|
|
|
* @expectedExceptionMessage Invalid log level. |
|
278
|
|
|
*/ |
|
279
|
|
|
public function testInvalidLevel() |
|
280
|
|
|
{ |
|
281
|
|
|
$app = new ServiceContainer([]); |
|
282
|
|
|
$log = \Mockery::mock(LogManager::class, [$app]) |
|
283
|
|
|
->shouldAllowMockingProtectedMethods() |
|
284
|
|
|
->shouldDeferMissing(); |
|
285
|
|
|
|
|
286
|
|
|
$log->level([ |
|
287
|
|
|
'level' => 'undefined', |
|
288
|
|
|
]); |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
public function testCall() |
|
292
|
|
|
{ |
|
293
|
|
|
$app = new ServiceContainer([]); |
|
294
|
|
|
$log = new LogManager($app); |
|
295
|
|
|
$this->assertInternalType('array', $log->getHandlers()); |
|
|
|
|
|
|
296
|
|
|
} |
|
297
|
|
|
} |
|
298
|
|
|
|