1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ColinODell\PsrTestLogger; |
6
|
|
|
|
7
|
|
|
use Psr\Log\AbstractLogger; |
8
|
|
|
use Psr\Log\LogLevel; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Used for testing purposes. |
12
|
|
|
* |
13
|
|
|
* It records all records and gives you access to them for verification. |
14
|
|
|
* |
15
|
|
|
* @method bool hasEmergency(string|array $record) |
16
|
|
|
* @method bool hasAlert(string|array $record) |
17
|
|
|
* @method bool hasCritical(string|array $record) |
18
|
|
|
* @method bool hasError(string|array $record) |
19
|
|
|
* @method bool hasWarning(string|array $record) |
20
|
|
|
* @method bool hasNotice(string|array $record) |
21
|
|
|
* @method bool hasInfo(string|array $record) |
22
|
|
|
* @method bool hasDebug(string|array $record) |
23
|
|
|
* @method bool hasEmergencyRecords() |
24
|
|
|
* @method bool hasAlertRecords() |
25
|
|
|
* @method bool hasCriticalRecords() |
26
|
|
|
* @method bool hasErrorRecords() |
27
|
|
|
* @method bool hasWarningRecords() |
28
|
|
|
* @method bool hasNoticeRecords() |
29
|
|
|
* @method bool hasInfoRecords() |
30
|
|
|
* @method bool hasDebugRecords() |
31
|
|
|
* @method bool hasEmergencyThatContains(string $message) |
32
|
|
|
* @method bool hasAlertThatContains(string $message) |
33
|
|
|
* @method bool hasCriticalThatContains(string $message) |
34
|
|
|
* @method bool hasErrorThatContains(string $message) |
35
|
|
|
* @method bool hasWarningThatContains(string $message) |
36
|
|
|
* @method bool hasNoticeThatContains(string $message) |
37
|
|
|
* @method bool hasInfoThatContains(string $message) |
38
|
|
|
* @method bool hasDebugThatContains(string $message) |
39
|
|
|
* @method bool hasEmergencyThatMatches(string $regex) |
40
|
|
|
* @method bool hasAlertThatMatches(string $regex) |
41
|
|
|
* @method bool hasCriticalThatMatches(string $regex) |
42
|
|
|
* @method bool hasErrorThatMatches(string $regex) |
43
|
|
|
* @method bool hasWarningThatMatches(string $regex) |
44
|
|
|
* @method bool hasNoticeThatMatches(string $regex) |
45
|
|
|
* @method bool hasInfoThatMatches(string $regex) |
46
|
|
|
* @method bool hasDebugThatMatches(string $regex) |
47
|
|
|
* @method bool hasEmergencyThatPasses(callable $predicate) |
48
|
|
|
* @method bool hasAlertThatPasses(callable $predicate) |
49
|
|
|
* @method bool hasCriticalThatPasses(callable $predicate) |
50
|
|
|
* @method bool hasErrorThatPasses(callable $predicate) |
51
|
|
|
* @method bool hasWarningThatPasses(callable $predicate) |
52
|
|
|
* @method bool hasNoticeThatPasses(callable $predicate) |
53
|
|
|
* @method bool hasInfoThatPasses(callable $predicate) |
54
|
|
|
* @method bool hasDebugThatPasses(callable $predicate) |
55
|
|
|
* |
56
|
|
|
* Adapted from psr/log, |
57
|
|
|
* Copyright (c) 2012 PHP Framework Interoperability Group |
58
|
|
|
* Used under the MIT license |
59
|
|
|
*/ |
60
|
|
|
final class TestLogger extends AbstractLogger |
61
|
|
|
{ |
62
|
|
|
/** @var array<int, array<string, mixed>> */ |
63
|
|
|
public array $records = []; |
64
|
|
|
|
65
|
|
|
/** @var array<string|int, array<int, array<string, mixed>>> */ |
66
|
|
|
public array $recordsByLevel = []; |
67
|
|
|
|
68
|
|
|
/** @var array<LogLevel::*, string|int> */ |
|
|
|
|
69
|
|
|
private array $levelMap = [ |
70
|
|
|
LogLevel::EMERGENCY => LogLevel::EMERGENCY, |
71
|
|
|
LogLevel::ALERT => LogLevel::ALERT, |
72
|
|
|
LogLevel::CRITICAL => LogLevel::CRITICAL, |
73
|
82 |
|
LogLevel::ERROR => LogLevel::ERROR, |
74
|
|
|
LogLevel::WARNING => LogLevel::WARNING, |
75
|
82 |
|
LogLevel::NOTICE => LogLevel::NOTICE, |
76
|
|
|
LogLevel::INFO => LogLevel::INFO, |
77
|
|
|
LogLevel::DEBUG => LogLevel::DEBUG, |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
/** |
81
|
82 |
|
* @param array<LogLevel::*, string|int>|null $levelMap |
82
|
82 |
|
* Keys are LogLevel::*, values are alternative strings or integers used as log levels in the SUT. |
83
|
|
|
*/ |
84
|
|
|
public function __construct(array|null $levelMap = null) |
85
|
|
|
{ |
86
|
|
|
if (\is_array($levelMap)) { |
87
|
|
|
$passedKeys = \array_keys($levelMap); |
88
|
18 |
|
if ($passedKeys !== \array_keys($this->levelMap)) { |
89
|
|
|
throw new \InvalidArgumentException('Level map keys must be the LogLevel::* values; passed ' . \print_r($passedKeys, true)); |
|
|
|
|
90
|
18 |
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->levelMap = $levelMap ?? $this->levelMap; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
16 |
|
* {@inheritDoc} |
98
|
|
|
* |
99
|
16 |
|
* @param array<array-key, mixed> $context |
|
|
|
|
100
|
16 |
|
*/ |
101
|
|
|
public function log($level, $message, array $context = []): void |
102
|
|
|
{ |
103
|
16 |
|
$record = [ |
104
|
16 |
|
'level' => $level, |
105
|
16 |
|
'message' => $message, |
106
|
|
|
'context' => $context, |
107
|
|
|
]; |
108
|
16 |
|
|
109
|
|
|
$this->recordsByLevel[$record['level']][] = $record; |
110
|
|
|
$this->records[] = $record; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function hasRecords(string|int $level): bool |
114
|
|
|
{ |
115
|
16 |
|
return isset($this->recordsByLevel[$level]); |
116
|
|
|
} |
117
|
16 |
|
|
118
|
16 |
|
/** |
119
|
|
|
* @param string|array<string, mixed> $record |
120
|
|
|
*/ |
121
|
|
|
public function hasRecord(string|array $record, string|int $level): bool |
122
|
|
|
{ |
123
|
|
|
if (\is_string($record)) { |
|
|
|
|
124
|
|
|
$record = ['message' => $record]; |
125
|
16 |
|
} |
126
|
|
|
|
127
|
16 |
|
return $this->hasRecordThatPasses(static function (array $rec) use ($record) { |
128
|
16 |
|
if ($rec['message'] !== $record['message']) { |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return ! isset($record['context']) || $rec['context'] === $record['context']; |
133
|
|
|
}, $level); |
134
|
|
|
} |
135
|
|
|
|
136
|
64 |
|
public function hasRecordThatContains(string $message, string|int $level): bool |
137
|
|
|
{ |
138
|
64 |
|
return $this->hasRecordThatPasses(static function (array $rec) use ($message) { |
139
|
64 |
|
return \strpos($rec['message'], $message) !== false; |
140
|
|
|
}, $level); |
141
|
|
|
} |
142
|
64 |
|
|
143
|
64 |
|
public function hasRecordThatMatches(string $regex, string|int $level): bool |
144
|
64 |
|
{ |
145
|
|
|
return $this->hasRecordThatPasses(static function ($rec) use ($regex) { |
146
|
|
|
return \preg_match($regex, $rec['message']) > 0; |
147
|
|
|
}, $level); |
148
|
32 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param callable(array<string, mixed>, int): bool $predicate |
152
|
|
|
*/ |
153
|
|
|
public function hasRecordThatPasses(callable $predicate, string|int $level): bool |
154
|
82 |
|
{ |
155
|
|
|
if (! isset($this->recordsByLevel[$level])) { |
156
|
82 |
|
return false; |
157
|
80 |
|
} |
158
|
80 |
|
|
159
|
80 |
|
foreach ($this->recordsByLevel[$level] as $i => $rec) { |
160
|
80 |
|
if (\call_user_func($predicate, $rec, $i)) { |
161
|
80 |
|
return true; |
162
|
|
|
} |
163
|
80 |
|
} |
164
|
|
|
|
165
|
|
|
return false; |
166
|
|
|
} |
167
|
2 |
|
|
168
|
|
|
/** |
169
|
|
|
* @param array<int, mixed> $args |
170
|
2 |
|
*/ |
171
|
|
|
public function __call(string $method, array $args): bool |
172
|
2 |
|
{ |
173
|
2 |
|
$levelNames = \implode('|', \array_map('ucfirst', \array_keys($this->levelMap))); |
174
|
|
|
if (\preg_match('/(.*)(' . $levelNames . ')(.*)/', $method, $matches) > 0) { |
175
|
|
|
$genericMethod = $matches[1] . ($matches[3] !== 'Records' ? 'Record' : '') . $matches[3]; |
176
|
|
|
$callable = [$this, $genericMethod]; |
177
|
|
|
$level = $this->levelMap[\strtolower($matches[2])]; |
178
|
|
|
if (\is_callable($callable)) { |
179
|
|
|
$args[] = $level; |
180
|
|
|
|
181
|
|
|
return \call_user_func_array($callable, $args); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
throw new \BadMethodCallException('Call to undefined method ' . static::class . '::' . $method . '()'); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function reset(): void |
189
|
|
|
{ |
190
|
|
|
$this->records = []; |
191
|
|
|
$this->recordsByLevel = []; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|