-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathVerbosityTest.php
More file actions
639 lines (577 loc) · 22.6 KB
/
VerbosityTest.php
File metadata and controls
639 lines (577 loc) · 22.6 KB
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
<?php
namespace Rollbar;
use Psr\Log\LogLevel;
use Rollbar\Payload\Level;
use Rollbar\Payload\Payload;
use Rollbar\Payload\Data;
use Rollbar\TestHelpers\ArrayLogger;
/**
* \Rollbar\VerboseTest tests the verbosity of the SDK.
*
* This test doesn't focus on testing one particular class.
* Instead it tests `verbose` functionality across multiple
* classes.
*
* The log mocking is achieved by mocking out the `handle`
* method of the log handler used in the `verbose_logger`.
*
* @package Rollbar
* @author Artur Moczulski <[email protected]>
* @author Rollbar, Inc.
*/
class VerbosityTest extends BaseRollbarTest
{
private ArrayLogger|null $verboseLogger = null;
/**
* Prepare session
*
* @return void
*/
public function setUp(): void
{
$_SESSION = array();
$this->verboseLogger = new ArrayLogger();
parent::setUp();
}
/**
* Clean up Rollbar and the verbose logger handler mock for the next test
*
* @return void
*/
public function tearDown(): void
{
$this->verboseLogger = null;
Rollbar::destroy();
parent::tearDown();
}
/**
* Test verbosity of \Rollbar\RollbarLogger::log with `enabled` == true.
*
* @return void
*/
public function testRollbarLoggerEnabled(): void
{
Rollbar::init([
'access_token' => $this->getTestAccessToken(),
'environment' => 'testing-php',
"enabled" => true,
'verbose_logger' => $this->verboseLogger,
]);
Rollbar::log(Level::WARNING, "Testing PHP Notifier");
$this->assertVerboseLogsConsecutive(
['level' => LogLevel::INFO, 'messageRegEx' => 'Attempting to log: \[warning\] Testing PHP Notifier'],
['level' => LogLevel::INFO, 'messageRegEx' => 'Occurrence'],
);
}
/**
* Test verbosity of \Rollbar\RollbarLogger::log with `enabled` == false.
*
* @return void
*/
public function testRollbarLoggerDisabled(): void
{
Rollbar::init([
'access_token' => $this->getTestAccessToken(),
'environment' => 'testing-php',
"enabled" => false,
'verbose_logger' => $this->verboseLogger,
]);
Rollbar::log(Level::WARNING, "Testing PHP Notifier");
$this->assertVerboseLogContains('Rollbar is disabled', LogLevel::NOTICE);
}
/**
* Test verbosity of \Rollbar\RollbarLogger::log with an invalid log level passed in the method call.
*
* @return void
*/
public function testRollbarLoggerInvalidLogLevel(): void
{
Rollbar::init([
'access_token' => $this->getTestAccessToken(),
'environment' => 'testing-php',
'verbose_logger' => $this->verboseLogger,
]);
$this->expectException(\Psr\Log\InvalidArgumentException::class);
Rollbar::log('nolevel', "Testing PHP Notifier");
$this->assertVerboseLogContains('Invalid log level \'nolevel\'\.', LogLevel::ERROR);
}
/**
* Test verbosity of \Rollbar\RollbarLogger::log when an occurrence gets ignored for whatever reason.
*
* @return void
*/
public function testRollbarLoggerInternalCheckIgnored(): void
{
$errorReporting = \error_reporting();
\error_reporting(0);
Rollbar::init([
'access_token' => $this->getTestAccessToken(),
'environment' => 'testing-php',
'verbose_logger' => $this->verboseLogger,
]);
Rollbar::log(LogLevel::INFO, "Testing PHP Notifier");
$this->assertVerboseLogContains('Occurrence ignored', LogLevel::INFO);
\error_reporting($errorReporting);
}
/**
* Test verbosity of \Rollbar\RollbarLogger::log when an occurrence gets ignored due to check ignore
*
* @return void
*/
public function testRollbarLoggerCheckIgnored(): void
{
Rollbar::init([
'access_token' => $this->getTestAccessToken(),
'environment' => 'testing-php',
'verbose_logger' => $this->verboseLogger,
'check_ignore' => function () {
return true;
},
]);
Rollbar::log(LogLevel::WARNING, "Testing PHP Notifier");
$this->assertVerboseLogContains('Occurrence ignored', LogLevel::INFO);
}
/**
* Test verbosity of \Rollbar\RollbarLogger::log when `max_items` is reached.
*
* @return void
*/
public function testRollbarLoggerSendMaxItems(): void
{
Rollbar::init([
'access_token' => $this->getTestAccessToken(),
'environment' => 'testing-php',
'verbose_logger' => $this->verboseLogger,
'max_items' => 0,
]);
Rollbar::log(LogLevel::INFO, "Testing PHP Notifier");
$this->assertVerboseLogContains('Maximum number of items per request has been reached.', LogLevel::WARNING);
}
/**
* Test verbosity of \Rollbar\RollbarLogger::log for adding occurrences to the queue when `batched` == true.
*
* @return void
*/
public function testRollbarLoggerSendBatched(): void
{
Rollbar::init([
'access_token' => $this->getTestAccessToken(),
'environment' => 'testing-php',
'verbose_logger' => $this->verboseLogger,
'batched' => true,
]);
Rollbar::log(LogLevel::INFO, "Testing PHP Notifier");
$this->assertVerboseLogContains('Added payload to the queue \(running in `batched` mode\)\.', LogLevel::DEBUG);
}
/**
* Test verbosity of \Rollbar\RollbarLogger::flush
*
* @return void
*/
public function testRollbarLoggerFlush(): void
{
Rollbar::init([
'access_token' => $this->getTestAccessToken(),
'environment' => 'testing-php',
'verbose_logger' => $this->verboseLogger,
]);
Rollbar::log(LogLevel::WARNING, "Testing PHP Notifier");
Rollbar::flush();
$this->assertVerboseLogContains('Queue flushed', LogLevel::DEBUG);
}
/**
* Test verbosity of \Rollbar\RollbarLogger::log for reports rejected by the SDK (response status == 0).
*
* @return void
*/
public function testRollbarLoggerResponseStatusZero(): void
{
Rollbar::init([
'access_token' => $this->getTestAccessToken(),
'environment' => 'testing-php',
'verbose_logger' => $this->verboseLogger,
'check_ignore' => function () {
return true;
},
]);
Rollbar::log(LogLevel::INFO, "Testing PHP Notifier");
$this->assertVerboseLogContains('Occurrence rejected by the SDK:', LogLevel::ERROR);
}
/**
* Test verbosity of \Rollbar\RollbarLogger::log for reports rejected by the API (response status >= 400).
*
* @return void
*/
public function testRollbarLoggerResponseStatusError(): void
{
Rollbar::init([
// Invalid access token should cause a 403 response.
'access_token' => '00000000000000000000000000000000',
'environment' => 'testing-php',
'verbose_logger' => $this->verboseLogger,
]);
Rollbar::log(LogLevel::INFO, "Testing PHP Notifier");
$this->assertVerboseLogContains('Occurrence rejected by the API: with status 403: ', LogLevel::ERROR);
}
/**
* Test verbosity of \Rollbar\RollbarLogger::log for reports successfully processed.
*
* @return void
*/
public function testRollbarLoggerResponseStatusSuccess(): void
{
Rollbar::init([
'access_token' => $this->getTestAccessToken(),
'environment' => 'testing-php',
'verbose_logger' => $this->verboseLogger,
]);
Rollbar::log(LogLevel::INFO, "Testing PHP Notifier");
$this->assertVerboseLogContains('Occurrence successfully logged', LogLevel::INFO);
}
/**
* Test verbosity of \Rollbar\Config::internalCheckIgnored when error_reporting === 0.
*
* @return void
*/
public function testRollbarConfigInternalCheckIgnoredShouldSuppress(): void
{
$errorReporting = \error_reporting();
\error_reporting(0);
Rollbar::init([
'access_token' => $this->getTestAccessToken(),
'environment' => 'testing-php',
'verbose_logger' => $this->verboseLogger,
]);
Rollbar::logger()->getConfig()->internalCheckIgnored(LogLevel::WARNING, "Some message");
$this->assertVerboseLogContains(
'Ignoring \(error reporting has been disabled in PHP config\)',
LogLevel::DEBUG,
);
\error_reporting($errorReporting);
}
/**
* Test verbosity of \Rollbar\Config::internalCheckIgnored when an occurrence gets ignored due to occurrence level
* being too low (`minimum_level` < log_level).
*
* @return void
*/
public function testRollbarConfigInternalCheckIgnoredLevelTooLow(): void
{
Rollbar::init([
'access_token' => $this->getTestAccessToken(),
'environment' => 'testing-php',
'verbose_logger' => $this->verboseLogger,
'minimum_level' => LogLevel::ERROR,
]);
Rollbar::logger()->getConfig()->internalCheckIgnored(LogLevel::WARNING, "Some message");
$this->assertVerboseLogContains('Occurrence\'s level is too low', LogLevel::DEBUG);
}
/**
* Test verbosity of \Rollbar\Config::shouldIgnoreError when `use_error_reporting` == true and the error level is
* below allowed error_reporting() level.
*
* @return void
*/
public function testRollbarConfigShouldIgnoreErrorErrorReporting(): void
{
$errorReporting = \error_reporting();
\error_reporting(\E_ERROR);
Rollbar::init([
'access_token' => $this->getTestAccessToken(),
'environment' => 'testing-php',
'verbose_logger' => $this->verboseLogger,
'use_error_reporting' => true,
]);
Rollbar::logger()->getConfig()->shouldIgnoreError(\E_WARNING);
$this->assertVerboseLogContains('Ignore \(error below allowed error_reporting level\)', LogLevel::DEBUG);
\error_reporting($errorReporting);
}
/**
* Test verbosity of \Rollbar\Config::shouldIgnoreError when `included_errno` is set.
*
* @return void
*/
public function testRollbarConfigShouldIgnoreErrorIncludedErrno(): void
{
$errorReporting = \error_reporting();
\error_reporting(0);
Rollbar::init([
'access_token' => $this->getTestAccessToken(),
'environment' => 'testing-php',
'verbose_logger' => $this->verboseLogger,
'included_errno' => \E_WARNING,
]);
Rollbar::logger()->getConfig()->shouldIgnoreError(\E_ERROR);
$this->assertVerboseLogContains('Ignore due to included_errno level', LogLevel::DEBUG);
\error_reporting($errorReporting);
}
/**
* Test verbosity of \Rollbar\Config::shouldIgnoreError when the error is skipped due to error sample rates.
*
* @return void
*/
public function testRollbarConfigShouldIgnoreErrorErrorSampleRates(): void
{
Rollbar::init([
'access_token' => $this->getTestAccessToken(),
'environment' => 'testing-php',
'verbose_logger' => $this->verboseLogger,
'error_sample_rates' => array(
\E_WARNING => 0,
),
]);
Rollbar::logger()->getConfig()->shouldIgnoreError(\E_WARNING);
$this->assertVerboseLogContains('Skip due to error sample rating', LogLevel::DEBUG);
}
/**
* Test verbosity of \Rollbar\Config::shouldIgnoreException when the exception is skipped due to exception sample
* rates.
*
* @return void
*/
public function testRollbarConfigShouldIgnoreException(): void
{
Rollbar::init([
'access_token' => $this->getTestAccessToken(),
'environment' => 'testing-php',
'verbose_logger' => $this->verboseLogger,
'exception_sample_rates' => array(
'Exception' => 0,
),
]);
Rollbar::logger()->getConfig()->shouldIgnoreException(new \Exception());
$this->assertVerboseLogContains('Skip exception due to exception sample rating', LogLevel::DEBUG);
}
/**
* Test verbosity of \Rollbar\Config::checkIgnored due to custom `check_ignore` logic.
*
* @return void
*/
public function testRollbarConfigCheckIgnored(): void
{
Rollbar::init([
'access_token' => $this->getTestAccessToken(),
'environment' => 'testing-php',
'verbose_logger' => $this->verboseLogger,
'check_ignore' => function () {
return true;
},
]);
$dataMock = $this->getMockBuilder(Data::class)->disableOriginalConstructor()->getMock();
$dataMock->method('getLevel')->willReturn(\Rollbar\LevelFactory::fromName(Level::INFO));
$payloadMock = $this->getMockBuilder(Payload::class)->disableOriginalConstructor()->getMock();
$payloadMock->method('getData')->willReturn($dataMock);
Rollbar::logger()->getConfig()->checkIgnored($payloadMock, $payloadMock, false);
$this->assertVerboseLogContains('Occurrence ignored due to custom check_ignore logic', LogLevel::INFO);
}
/**
* Test verbosity of \Rollbar\Config::checkIgnored due an exception in the custom check_ginore logic.
*
* @return void
*/
public function testRollbarConfigCheckIgnoredException(): void
{
Rollbar::init([
'access_token' => $this->getTestAccessToken(),
'environment' => 'testing-php',
'verbose_logger' => $this->verboseLogger,
'check_ignore' => function () {
throw new \Exception();
},
]);
Rollbar::log(LogLevel::WARNING, "Testing PHP Notifier");
$this->assertVerboseLogContains('Exception occurred in the custom checkIgnore logic:', LogLevel::ERROR);
}
/**
* Test verbosity of \Rollbar\Config::checkIgnored due the message being below `minimum_level`.
*
* @return void
*/
public function testRollbarConfigCheckIgnoredPayloadLevelTooLow(): void
{
Rollbar::init([
'access_token' => $this->getTestAccessToken(),
'environment' => 'testing-php',
'verbose_logger' => $this->verboseLogger,
'minimum_level' => \Rollbar\Payload\Level::ERROR,
]);
$config = Rollbar::logger()->getConfig();
$data = $config->getRollbarData(\Rollbar\Payload\Level::INFO, 'some message', array());
$payload = new \Rollbar\Payload\Payload($data, $this->getTestAccessToken());
$config->checkIgnored($payload, 'some message', false);
$this->assertVerboseLogContains('Occurrence\'s level is too low', LogLevel::DEBUG);
}
/**
* Test verbosity of \Rollbar\Config::checkIgnored due the custom `filter`.
*
* @return void
*/
public function testRollbarConfigCheckIgnoredFilter(): void
{
$filterMock = $this->getMockBuilder(FilterInterface::class)->getMock();
$filterMock->method('shouldSend')->willReturn(true);
Rollbar::init([
'access_token' => $this->getTestAccessToken(),
'environment' => 'testing-php',
'verbose_logger' => $this->verboseLogger,
'filter' => $filterMock,
]);
$config = Rollbar::logger()->getConfig();
$data = $config->getRollbarData(\Rollbar\Payload\Level::INFO, 'some message', array());
$payload = new \Rollbar\Payload\Payload($data, $this->getTestAccessToken());
$config->checkIgnored($payload, 'some message', false);
$unitTest = $this;
$filterMock = $this->getMockBuilder(FilterInterface::class)->getMock();
$filterMock->method('shouldSend')->willReturn(true);
$this->assertVerboseLogContains('Custom filter result: true', LogLevel::DEBUG);
}
/**
* Test verbosity of \Rollbar\Config::send due the custom `transmit` == false.
*
* @return void
*/
public function testRollbarConfigSendTransmit(): void
{
Rollbar::init([
'access_token' => $this->getTestAccessToken(),
'environment' => 'testing-php',
'verbose_logger' => $this->verboseLogger,
'transmit' => false,
]);
$config = Rollbar::logger()->getConfig();
$encoded = new \Rollbar\Payload\EncodedPayload(array());
$config->send($encoded, $this->getTestAccessToken());
$this->assertVerboseLogContains(
'Not transmitting \(transmitting disabled in configuration\)',
LogLevel::WARNING,
);
}
/**
* Test verbosity of \Rollbar\Config::sendBatch due the custom `transmit` == false.
*
* @return void
*/
public function testRollbarConfigSendBatchTransmit(): void
{
Rollbar::init([
'access_token' => $this->getTestAccessToken(),
'environment' => 'testing-php',
'verbose_logger' => $this->verboseLogger,
'transmit' => false,
'batched' => true,
]);
$config = Rollbar::logger()->getConfig();
$config->sendBatch(array(), $this->getTestAccessToken());
$this->assertVerboseLogContains(
'Not transmitting \(transmitting disabled in configuration\)',
LogLevel::WARNING,
);
}
/**
* Test verbosity of \Rollbar\Config::handleResponse with custom `responseHandler`.
*
* @return void
*/
public function testRollbarConfigHandleResponse(): void
{
$responseHandlerMock = $this->getMockBuilder(ResponseHandlerInterface::class)->getMock();
Rollbar::init([
'access_token' => $this->getTestAccessToken(),
'environment' => 'testing-php',
'verbose_logger' => $this->verboseLogger,
'responseHandler' => $responseHandlerMock,
]);
$config = Rollbar::logger()->getConfig();
$payloadMock = $this->getMockBuilder(Payload::class)->disableOriginalConstructor()->getMock();
$responseMock = $this->getMockBuilder(Response::class)->disableOriginalConstructor()->getMock();
$config->handleResponse($payloadMock, $responseMock);
$this->assertVerboseLogContains('Applying custom response handler:', LogLevel::DEBUG);
}
/**
* Test verbosity of \Rollbar\Truncation\Truncation::registerStrategy in truncate method.
*
* @return void
*/
public function testRollbarTruncation(): void
{
Rollbar::init([
'access_token' => $this->getTestAccessToken(),
'environment' => 'testing-php',
'verbose_logger' => $this->verboseLogger,
]);
Rollbar::logger()->log(
\Rollbar\Payload\Level::INFO,
\str_repeat("x", \Rollbar\Truncation\Truncation::MAX_PAYLOAD_SIZE),
);
$this->assertVerboseLogContains('Applying truncation strategy', LogLevel::DEBUG);
}
/**
* Asserts that the {@see $this->verboseLogger} contains the given message at the given level.
*
* @param string $messageRegEx The message regular expression to match.
* @param string $level The level to match.
*
* @return void
*/
private function assertVerboseLogContains(string $messageRegEx, string $level): void
{
self::assertGreaterThanOrEqual(
0,
$this->verboseLogger->indexOfRegex($level, $messageRegEx),
'Verbose log does not contain expected message: "' . $messageRegEx . '" at level "' . $level . '"',
);
}
/**
* Asserts that the {@see $this->verboseLogger} contains the given logs in the given order, and that they are
* consecutive.
*
* This function loops over all the logs until it finds a match for the first constraint. Then it checks the next
* log messages to see if they match the next constraint, and so on, until it reaches the end of the constraints or
* the end of the logs.
*
* If every constraint is matched in order, the test passes.
*
* @param array{level: string, messageRegEx: string} ...$constraints The constraints to match. The array must
* contain two keys: `level` and `messageRegEx`.
* The `level` key must contain the log level to
* match, and the `messageRegEx` key must contain
* the regular expression to match the log
* message.
*
* @return void
*/
private function assertVerboseLogsConsecutive(array ...$constraints): void
{
$matchCount = 0;
$constraintIndex = 0;
$consecutive = false;
for ($i = 0; $i < count($this->verboseLogger->logs); $i++) {
// If this is past the last constraint, we're done.
if ($constraintIndex >= count($constraints)) {
break;
}
$message = $constraints[$constraintIndex]['messageRegEx'];
$level = $constraints[$constraintIndex]['level'];
$matches = $this->verboseLogger->indexMatchesRegex($i, $level, $message);
if ($consecutive && !$matches) {
// If we are expecting consecutive matches, and the current log message does not match the current
// constraint, then we can fail early.
$this->fail(
'Expected log message at index "' . $i . '" to match "' . $message . '" at level "' . $level . '"'
);
}
if ($matches) {
// Since we have found a match for the current constraint, we can now expect all future matches to be
// consecutive.
$consecutive = true;
$matchCount++;
$constraintIndex++;
}
}
self::assertSame(
count($constraints),
$matchCount,
'Expected ' . count($constraints) . ' log messages, but found ' . $matchCount,
);
}
}