reactphp-kernel/tests/Fixtures/SpyLogger.php

39 lines
976 B
PHP

<?php
declare(strict_types=1);
namespace Blax\ReactPhpKernel\Tests\Fixtures;
use Psr\Log\AbstractLogger;
/**
* A PSR-3 logger that records every log() call so tests can assert the kernel
* emitted the lifecycle lines it should.
*/
final class SpyLogger extends AbstractLogger
{
/** @var array<int,array{level:mixed,message:string,context:array}> */
public array $records = [];
public function log($level, \Stringable|string $message, array $context = []): void
{
$this->records[] = ['level' => $level, 'message' => (string) $message, 'context' => $context];
}
public function messages(): array
{
return array_map(fn ($r) => $r['message'], $this->records);
}
public function hasMessageContaining(string $needle): bool
{
foreach ($this->records as $record) {
if (str_contains($record['message'], $needle)) {
return true;
}
}
return false;
}
}