-
Notifications
You must be signed in to change notification settings - Fork 701
Expand file tree
/
Copy pathLoggerTest.php
More file actions
135 lines (106 loc) · 3.47 KB
/
LoggerTest.php
File metadata and controls
135 lines (106 loc) · 3.47 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
<?php
declare(strict_types=1);
namespace SimpleSAML\Test;
use Exception;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use SimpleSAML\Configuration;
use SimpleSAML\Logger;
use SimpleSAML\TestUtils\ArrayLogger;
class LoggerTest extends TestCase
{
/**
* @var \SimpleSAML\Logger\LoggingHandlerInterface|null
*/
protected $originalLogger;
/**
* @param string $handler
*/
protected function setLoggingHandler(string $handler): void
{
$this->originalLogger = Logger::getLoggingHandler();
$config = [
'logging.handler' => $handler,
'logging.level' => Logger::DEBUG,
];
// testing static methods is slightly painful
Configuration::loadFromArray($config, '[ARRAY]', 'simplesaml');
Logger::setLoggingHandler(null);
}
/**
*/
protected function tearDown(): void
{
if (isset($this->originalLogger)) {
// reset the logger and Configuration
Configuration::clearInternalState();
Logger::clearCapturedLog();
Logger::setLogLevel(Logger::INFO);
Logger::setLoggingHandler($this->originalLogger);
}
}
/**
*/
public function testCreateLoggingHandlerHonorsCustomHandler(): void
{
$this->setLoggingHandler(ArrayLogger::class);
Logger::critical('array logger');
$logger = Logger::getLoggingHandler();
self::assertInstanceOf(ArrayLogger::class, $logger);
}
/**
*/
public function testCaptureLog(): void
{
$this->setLoggingHandler(ArrayLogger::class);
$payload = "catch this error";
Logger::setCaptureLog();
Logger::critical($payload);
// turn logging off
Logger::setCaptureLog(false);
Logger::critical("do not catch this");
$log = Logger::getCapturedLog();
self::assertCount(1, $log);
self::assertMatchesRegularExpression("/^[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}Z\ {$payload}$/", $log[0]);
}
/**
*/
public function testExceptionThrownOnInvalidLoggingHandler(): void
{
$this->setLoggingHandler('nohandler');
$this->expectException(Exception::class);
$this->expectExceptionMessage(
"Invalid value for the 'logging.handler' configuration option. Unknown handler 'nohandler'.",
);
Logger::critical('should throw exception');
}
/**
* @return array
*/
public static function provideLogLevels(): array
{
return [
'emergency' => ['emergency', Logger::EMERG],
'alert' => ['alert', Logger::ALERT],
'critical' => ['critical', Logger::CRIT],
'error' => ['error', Logger::ERR],
'warning' => ['warning', Logger::WARNING],
'notice' => ['notice', Logger::NOTICE],
'info' => ['info', Logger::INFO],
'debug' => ['debug', Logger::DEBUG],
];
}
/**
* @param string $method
* @param int $level
*/
#[DataProvider('provideLogLevels')]
public function testLevelMethods(string $method, int $level): void
{
$this->setLoggingHandler(ArrayLogger::class);
Logger::{$method}($payload = "test {$method}");
$logger = Logger::getLoggingHandler();
$this->assertInstanceOf(ArrayLogger::class, $logger);
self::assertMatchesRegularExpression("/\[CL[0-9a-f]{8}\]\ {$payload}$/", $logger->logs[$level][0]);
}
}