colinodell /
psr-testlogger
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace ColinODell\PsrTestLogger; |
||
| 6 | |||
| 7 | use Psr\Log\AbstractLogger; |
||
| 8 | use Psr\Log\InvalidArgumentException; |
||
| 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 | /** |
||
| 69 | * {@inheritDoc} |
||
| 70 | * |
||
| 71 | * @param mixed $message |
||
| 72 | * @param array<array-key, mixed> $context |
||
| 73 | */ |
||
| 74 | 108 | public function log($level, $message, array $context = []): void |
|
| 75 | { |
||
| 76 | 108 | if (! (\is_string($level) || \is_int($level))) { |
|
| 77 | 14 | throw new InvalidArgumentException('Unsupported log level. The psr-testlogger library only supports string and integer log levels; passed ' . \print_r($level, true)); |
|
| 78 | } |
||
| 79 | |||
| 80 | 94 | $record = [ |
|
| 81 | 94 | 'level' => $level, |
|
| 82 | 94 | 'message' => $message, |
|
| 83 | 94 | 'context' => $context, |
|
| 84 | 47 | ]; |
|
| 85 | |||
| 86 | 94 | $this->recordsByLevel[$record['level']][] = $record; |
|
| 87 | 94 | $this->records[] = $record; |
|
| 88 | 47 | } |
|
| 89 | |||
| 90 | 94 | public function hasRecords(string|int|null $level = null): bool |
|
| 91 | { |
||
| 92 | 94 | if ($level === null) { |
|
| 93 | 10 | return \count($this->records) !== 0; |
|
| 94 | } |
||
| 95 | |||
| 96 | 84 | return isset($this->recordsByLevel[$level]); |
|
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param string|array<string, mixed> $record |
||
| 101 | */ |
||
| 102 | 20 | public function hasRecord(string|array $record, string|int|null $level = null): bool |
|
| 103 | { |
||
| 104 | 20 | if (\is_string($record)) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 105 | 20 | $record = ['message' => $record]; |
|
| 106 | } |
||
| 107 | |||
| 108 | 20 | return $this->hasRecordThatPasses(static function (array $rec) use ($record) { |
|
| 109 | 20 | if ((string) $rec['message'] !== (string) $record['message']) { |
|
| 110 | 18 | return false; |
|
| 111 | } |
||
| 112 | |||
| 113 | 20 | return ! isset($record['context']) || $rec['context'] === $record['context']; |
|
| 114 | 20 | }, $level); |
|
| 115 | } |
||
| 116 | |||
| 117 | 18 | public function hasRecordThatContains(string $message, string|int|null $level = null): bool |
|
| 118 | { |
||
| 119 | 18 | return $this->hasRecordThatPasses(static function (array $rec) use ($message) { |
|
| 120 | 18 | return \str_contains((string) $rec['message'], $message); |
|
| 121 | 18 | }, $level); |
|
| 122 | } |
||
| 123 | |||
| 124 | 18 | public function hasRecordThatMatches(string $regex, string|int|null $level = null): bool |
|
| 125 | { |
||
| 126 | 18 | return $this->hasRecordThatPasses(static function ($rec) use ($regex) { |
|
| 127 | 18 | return \preg_match($regex, (string) $rec['message']) > 0; |
|
| 128 | 18 | }, $level); |
|
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param callable(array<string, mixed>, int): bool $predicate |
||
| 133 | */ |
||
| 134 | 74 | public function hasRecordThatPasses(callable $predicate, string|int|null $level = null): bool |
|
| 135 | { |
||
| 136 | 74 | if (! $this->hasRecords($level)) { |
|
| 137 | 72 | return false; |
|
| 138 | } |
||
| 139 | |||
| 140 | 74 | foreach ($level === null ? $this->records : $this->recordsByLevel[$level] as $i => $rec) { |
|
| 141 | 74 | if (\call_user_func($predicate, $rec, $i)) { |
|
| 142 | 74 | return true; |
|
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | 36 | return false; |
|
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param array<int, mixed> $args |
||
| 151 | */ |
||
| 152 | 84 | public function __call(string $method, array $args): bool |
|
| 153 | { |
||
| 154 | 84 | if (\preg_match('/(.*)(Debug|Info|Notice|Warning|Error|Critical|Alert|Emergency)(.*)/', $method, $matches) > 0) { |
|
| 155 | 82 | $genericMethod = $matches[1] . ($matches[3] !== 'Records' ? 'Record' : '') . $matches[3]; |
|
| 156 | 82 | $callable = [$this, $genericMethod]; |
|
| 157 | 82 | $level = \strtolower($matches[2]); |
|
| 158 | 82 | if (\is_callable($callable)) { |
|
| 159 | 82 | $args[] = $level; |
|
| 160 | |||
| 161 | 82 | return \call_user_func_array($callable, $args); |
|
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | 2 | throw new \BadMethodCallException('Call to undefined method ' . self::class . '::' . $method . '()'); |
|
| 166 | } |
||
| 167 | |||
| 168 | 2 | public function reset(): void |
|
| 169 | { |
||
| 170 | 2 | $this->records = []; |
|
| 171 | 2 | $this->recordsByLevel = []; |
|
| 172 | 1 | } |
|
| 173 | } |
||
| 174 |