-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatcher.php
202 lines (183 loc) · 5.92 KB
/
Matcher.php
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
<?php
declare(strict_types=1);
namespace VStelmakh\PsrTestLogger\Match;
use VStelmakh\PsrTestLogger\Assert\AsserterInterface;
use VStelmakh\PsrTestLogger\Log\Collection;
use VStelmakh\PsrTestLogger\Log\Log;
class Matcher
{
/**
* @internal
*/
public function __construct(
private Collection $logs,
private readonly AsserterInterface $asserter,
) {
$this->asserter->assert($this->logs);
}
/**
* Return logs matching conditions.
*
* @return array<Log>
*/
public function getLogs(): array
{
return $this->logs->toArray();
}
/**
* Match logs with specified log level.
*
* @param mixed $level
* @return self
*/
public function withLevel(mixed $level): self
{
$criterion = sprintf('level "%s"', $level);
$callback = fn(Log $log) => $log->level === $level;
return $this->match($criterion, $callback);
}
/**
* Match logs with specified message.
*
* @param string $message
* @return self
*/
public function withMessage(string $message): self
{
$criterion = sprintf('message "%s"', $message);
$callback = fn(Log $log) => (string) $log->message === $message;
return $this->match($criterion, $callback);
}
/**
* Match logs with message contains substring.
*
* @param string $needle
* @return self
*/
public function withMessageContains(string $needle): self
{
$criterion = sprintf('message contains "%s"', $needle);
$callback = fn(Log $log) => str_contains((string) $log->message, $needle);
return $this->match($criterion, $callback);
}
/**
* Match logs with message contains substring (case-insensitive).
*
* @param string $needle
* @return self
*/
public function withMessageContainsIgnoreCase(string $needle): self
{
$criterion = sprintf('message contains ignore case "%s"', $needle);
$callback = fn(Log $log) => mb_stripos((string) $log->message, $needle, 0, 'UTF-8') !== false;
return $this->match($criterion, $callback);
}
/**
* Match logs with message starts with prefix.
*
* @param string $prefix
* @return self
*/
public function withMessageStartsWith(string $prefix): self
{
$criterion = sprintf('message starts with "%s"', $prefix);
$callback = fn(Log $log) => str_starts_with((string) $log->message, $prefix);
return $this->match($criterion, $callback);
}
/**
* Match logs with message matching regular expression.
*
* @param string $pattern RegEx pattern. Example value: "/^error/i".
* @return self
*/
public function withMessageMatches(string $pattern): self
{
$criterion = sprintf('message matches "%s"', $pattern);
$callback = fn(Log $log) => preg_match($pattern, (string) $log->message) === 1;
return $this->match($criterion, $callback);
}
/**
* Match logs with context as provided one, using loose comparison (==).
*
* @param array<mixed> $context
* @return self
*/
public function withContextEqualTo(array $context): self
{
$normalizedContext = [];
foreach ($context as $key => $value) {
$normalizedContext[] = sprintf('%s: {%s}', $key, gettype($value));
}
$criterion = sprintf('context equal to [%s]', implode(', ', $normalizedContext));
$callback = fn(Log $log) => $log->context == $context;
return $this->match($criterion, $callback);
}
/**
* Match logs with context as provided one, using strict comparison (===).
*
* @param array<mixed> $context
* @return self
*/
public function withContextSameAs(array $context): self
{
$normalizedContext = [];
foreach ($context as $key => $value) {
$normalizedContext[] = sprintf('%s: {%s}', $key, gettype($value));
}
$criterion = sprintf('context same as [%s]', implode(', ', $normalizedContext));
$callback = fn(Log $log) => $log->context === $context;
return $this->match($criterion, $callback);
}
/**
* Match logs with context contains key-value pair, using loose comparison (==).
*
* @param mixed $key
* @param mixed $value
* @return self
*/
public function withContextContainsEqualTo(mixed $key, mixed $value): self
{
$criterion = sprintf('context contains equal to [%s: {%s}]', $key, gettype($value));
$callback = fn(Log $log) => isset($log->context[$key]) && $log->context[$key] == $value;
return $this->match($criterion, $callback);
}
/**
* Match logs with context contains key-value pair, using strict comparison (===).
*
* @param mixed $key
* @param mixed $value
* @return self
*/
public function withContextContainsSameAs(mixed $key, mixed $value): self
{
$criterion = sprintf('context contains same as [%s: {%s}]', $key, gettype($value));
$callback = fn(Log $log) => isset($log->context[$key]) && $log->context[$key] === $value;
return $this->match($criterion, $callback);
}
/**
* Match logs by callback. Callback example:
* ```
* function (Log $log): bool {
* return $log->level === LogLevel::INFO;
* }
* ```
*
* @param callable(Log): bool $callback Return "true" on match, otherwise "false".
* @return self
*/
public function withCallback(callable $callback): self
{
$criterion = 'callback';
return $this->match($criterion, $callback);
}
/**
* @param callable(Log): bool $callback
*/
private function match(string $criterion, callable $callback): self
{
$logs = $this->logs->filter($callback);
$asserter = clone $this->asserter;
$asserter->addCriterion($criterion);
return new self($logs, $asserter);
}
}