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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
class LoggerTest extends PHPUnit_Framework_TestCase
{
private $client;
public function setup()
{
$this->client = new Google_Client();
}
/**
* @dataProvider logLevelsProvider
*/
public function testPsrMethods($key)
{
$message = 'This is my log message';
$context = array('some'=>'context');
$logger = $this->getLogger('log');
$logger->expects($this->once())
->method('log')
->with($key, $message, $context);
call_user_func(array($logger, $key), $message, $context);
}
/**
* @dataProvider invalidLevelsProvider
* @expectedException Google_Logger_Exception
* @expectedExceptionMessage Unknown LogLevel
*/
public function testSetLabelWithBadValue($level)
{
$this->getLogger()->setLevel($level);
}
/**
* @dataProvider invalidLevelsProvider
* @expectedException Google_Logger_Exception
* @expectedExceptionMessage Unknown LogLevel
*/
public function testSetLabelWithBadValueFromConfig($level)
{
$this->client->setClassConfig('Google_Logger_Abstract', 'level', $level);
$this->getLogger();
}
/**
* @dataProvider filterProvider
*/
public function testShouldHandle($setLevel, $handleLevel, $expected)
{
$logger = $this->getLogger();
$logger->setLevel($setLevel);
$this->assertEquals($expected, $logger->shouldHandle($handleLevel));
}
/**
* @dataProvider filterProvider
*/
public function testShouldHandleFromConfig($config, $handleLevel, $expected)
{
$this->client->setClassConfig('Google_Logger_Abstract', 'level', $config);
$logger = $this->getLogger();
$this->assertEquals($expected, $logger->shouldHandle($handleLevel));
}
/**
* @dataProvider filterProvider
*/
public function testShouldWrite($setLevel, $logLevel, $expected)
{
$logger = $this->getLogger();
$logger->expects($expected ? $this->once() : $this->never())
->method('write');
$logger->setLevel($setLevel);
$logger->log($logLevel, 'This is my log message');
}
/**
* @dataProvider filterProvider
*/
public function testShouldWriteFromConfig($config, $logLevel, $expected)
{
$this->client->setClassConfig('Google_Logger_Abstract', 'level', $config);
$logger = $this->getLogger();
$logger->expects($expected ? $this->once() : $this->never())
->method('write');
$logger->log($logLevel, 'This is my log message');
}
/**
* @dataProvider formattingProvider
*/
public function testMessageFormatting(
$format,
$date_format,
$newlines,
$message,
$context,
$expected
) {
$this->client->setClassConfig(
'Google_Logger_Abstract',
'log_format',
$format
);
$this->client->setClassConfig(
'Google_Logger_Abstract',
'date_format',
$date_format
);
$this->client->setClassConfig(
'Google_Logger_Abstract',
'allow_newlines',
$newlines
);
$logger = $this->getLogger();
$logger->expects($this->once())
->method('write')
->with($expected);
$logger->log('debug', $message, $context);
}
public function testNullLoggerNeverWrites()
{
$logger = $this->getLogger('write', 'Google_Logger_Null');
$logger->expects($this->never())
->method('write');
$logger->log(
'emergency',
'Should not be written',
array('same' => 'for this')
);
}
public function testNullLoggerNeverHandles()
{
$logger = $this->getLogger('write', 'Google_Logger_Null');
$this->assertFalse($logger->shouldHandle('emergency'));
$this->assertFalse($logger->shouldHandle(600));
}
public function testPsrNeverWrites()
{
$logger = $this->getLogger('write', 'Google_Logger_Psr');
$logger->expects($this->never())
->method('write');
$logger->log(
'emergency',
'Should not be written',
array('same' => 'for this')
);
$logger->setLogger($this->getLogger());
$logger->log(
'emergency',
'Should not be written',
array('same' => 'for this')
);
}
public function testPsrNeverShouldHandleWhenNoLoggerSet()
{
$logger = $this->getLogger(null, 'Google_Logger_Psr');
$this->assertFalse($logger->shouldHandle('emergency'));
$this->assertFalse($logger->shouldHandle(600));
}
public function testPsrShouldHandleWhenLoggerSet()
{
$logger = $this->getLogger(null, 'Google_Logger_Psr');
$logger->setLogger($this->getLogger());
$this->assertTrue($logger->shouldHandle('emergency'));
$this->assertTrue($logger->shouldHandle(600));
}
/**
* @dataProvider logLevelsProvider
*/
public function testPsrDelegates($key)
{
$message = 'This is my log message';
$context = array('some'=>'context');
$delegate = $this->getLogger('log');
$delegate->expects($this->once())
->method('log')
->with($key, $message, $context);
$logger = $this->getLogger(null, 'Google_Logger_Psr');
$logger->setLogger($delegate);
call_user_func(array($logger, $key), $message, $context);
}
/**
* @expectedException Google_Logger_Exception
* @expectedExceptionMessage File logger requires a filename or a valid file pointer
*/
public function testLoggerWithBadFileType()
{
$this->client->setClassConfig('Google_Logger_File', 'file', false);
$logger = $this->getLogger(null, 'Google_Logger_File');
}
/**
* @expectedException Google_Logger_Exception
* @expectedExceptionMessage Logger Error
*/
public function testLoggerWithBadFileValue()
{
$this->client->setClassConfig('Google_Logger_File', 'file', 'not://exist');
$logger = $this->getLogger(null, 'Google_Logger_File');
$logger->log('emergency', 'will fail');
}
/**
* @expectedException Google_Logger_Exception
* @expectedExceptionMessage File pointer is no longer available
*/
public function testLoggerWithClosedFileReference()
{
$fp = fopen('php://memory', 'r+');
$this->client->setClassConfig('Google_Logger_File', 'file', $fp);
$logger = $this->getLogger(null, 'Google_Logger_File');
fclose($fp);
$logger->log('emergency', 'will fail');
}
public function testLoggerWithFileReference()
{
$fp = fopen('php://memory', 'r+');
$this->client->setClassConfig('Google_Logger_File', 'file', $fp);
$this->client->setClassConfig(
'Google_Logger_Abstract',
'log_format',
"%level% - %message%\n"
);
$logger = $this->getLogger(null, 'Google_Logger_File');
$logger->log('emergency', 'test one');
$logger->log('alert', 'test two');
$logger->log(500, 'test three');
rewind($fp);
$this->assertEquals(
"EMERGENCY - test one\nALERT - test two\nCRITICAL - test three\n",
stream_get_contents($fp)
);
fclose($fp);
}
public function testLoggerWithFile()
{
$this->expectOutputString(
"EMERGENCY - test one\nALERT - test two\nCRITICAL - test three\n"
);
$this->client->setClassConfig(
'Google_Logger_File',
'file',
'php://output'
);
$this->client->setClassConfig(
'Google_Logger_Abstract',
'log_format',
"%level% - %message%\n"
);
$logger = $this->getLogger(null, 'Google_Logger_File');
$logger->log('emergency', 'test one');
$logger->log('alert', 'test two');
$logger->log(500, 'test three');
}
public function formattingProvider()
{
return array(
'no interpolation' => array(
'this is my format',
'd/M/Y:H:i:s O',
false,
'you wont see this',
array('or' => 'this'),
'this is my format',
),
'only message interpolation' => array(
'my format: %message%',
'd/M/Y:H:i:s O',
false,
'you will see this',
array('but not' => 'this'),
'my format: you will see this',
),
'message and level interpolation' => array(
'%level% - my format: %message%',
'd/M/Y:H:i:s O',
false,
'you will see this',
array('but not' => 'this'),
'DEBUG - my format: you will see this',
),
'message, level, datetime interpolation' => array(
'[%datetime%] %level% - my format: %message%',
'\T\I\M\E',
false,
'you will see this',
array('but not' => 'this'),
'[TIME] DEBUG - my format: you will see this',
),
'message, level, datetime, context interpolation' => array(
'[%datetime%] %level% - my format: %message%: %context%',
'\T\I\M\E',
false,
'you will see this',
array('and' => 'this'),
'[TIME] DEBUG - my format: you will see this: {"and":"this"}',
),
'reverse JSON in context' => array(
'%context%',
'd/M/Y:H:i:s O',
false,
'you will not see this',
array('reverse' => json_encode(array('this' => 'is cool'))),
'{"reverse":{"this":"is cool"}}',
),
'remove newlines in message' => array(
'%message%',
'd/M/Y:H:i:s O',
false,
"This \n\n\r\n is \r my \r\n newlines \r\r\r\n\n message",
array('you wont' => 'see this'),
'This is my newlines message',
),
'allow newlines in message' => array(
'%message%',
'd/M/Y:H:i:s O',
true,
"This \n\n\r\n is \r my \r\n newlines \r\r\r\n\n message",
array('you wont' => 'see this'),
"This \n\n\r\n is \r my \r\n newlines \r\r\r\n\n message",
),
'allow newlines in JSON' => array(
'%context%',
'd/M/Y:H:i:s O',
true,
"wont see this",
array('you will' => 'see this'),
version_compare(PHP_VERSION, '5.4.0', '>=') ?
"{\n \"you will\": \"see this\"\n}" :
'{"you will":"see this"}'
),
);
}
public function filterProvider()
{
return array(
array('debug', 'debug', true),
array(100, 'debug', true),
array('info', 'debug', false),
array('info', 'notice', true),
array('notice', 'notice', true),
array(250, 'notice', true),
array('notice', 'debug', false),
array(250, 'alert', true),
array('error', 'error', true),
array('error', 'warning', false),
array('error', 'critical', true),
array(600, 'alert', false),
array(600, 'critical', false),
array(600, 'emergency', true),
array('emergency', 'emergency', true),
);
}
public function invalidLevelsProvider()
{
return array(
array('100'),
array('DEBUG'),
array(100.0),
array(''),
array(0),
);
}
public function logLevelsProvider()
{
return array(
array('emergency', 600),
array('alert', 550),
array('critical', 500),
array('error', 400),
array('warning', 300),
array('notice', 250),
array('info', 200),
array('debug', 100),
);
}
private function getLogger($methods = null, $type = 'Google_Logger_Abstract')
{
return $this->getMockBuilder($type)
->setMethods((array) $methods)
->setConstructorArgs(array($this->client))
->getMockForAbstractClass();
}
}
|