|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Monolog package. |
| 5 | + * |
| 6 | + * (c) Jordi Boggiano <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Monolog\Test; |
| 13 | + |
| 14 | +use Monolog\Level; |
| 15 | +use Monolog\Logger; |
| 16 | +use Monolog\LogRecord; |
| 17 | +use Monolog\JsonSerializableDateTimeImmutable; |
| 18 | +use Monolog\Formatter\FormatterInterface; |
| 19 | +use Psr\Log\LogLevel; |
| 20 | + |
| 21 | +/** |
| 22 | + * Lets you easily generate log records and a dummy formatter for testing purposes |
| 23 | + * |
| 24 | + * @author Jordi Boggiano <[email protected]> |
| 25 | + */ |
| 26 | +class MonologTestCase extends \PHPUnit\Framework\TestCase |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @param array<mixed> $context |
| 30 | + * @param array<mixed> $extra |
| 31 | + * |
| 32 | + * @phpstan-param value-of<Level::VALUES>|value-of<Level::NAMES>|Level|LogLevel::* $level |
| 33 | + */ |
| 34 | + protected function getRecord(int|string|Level $level = Level::Warning, string|\Stringable $message = 'test', array $context = [], string $channel = 'test', \DateTimeImmutable $datetime = new JsonSerializableDateTimeImmutable(true), array $extra = []): LogRecord |
| 35 | + { |
| 36 | + return new LogRecord( |
| 37 | + message: (string) $message, |
| 38 | + context: $context, |
| 39 | + level: Logger::toMonologLevel($level), |
| 40 | + channel: $channel, |
| 41 | + datetime: $datetime, |
| 42 | + extra: $extra, |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @phpstan-return list<LogRecord> |
| 48 | + */ |
| 49 | + protected function getMultipleRecords(): array |
| 50 | + { |
| 51 | + return [ |
| 52 | + $this->getRecord(Level::Debug, 'debug message 1'), |
| 53 | + $this->getRecord(Level::Debug, 'debug message 2'), |
| 54 | + $this->getRecord(Level::Info, 'information'), |
| 55 | + $this->getRecord(Level::Warning, 'warning'), |
| 56 | + $this->getRecord(Level::Error, 'error'), |
| 57 | + ]; |
| 58 | + } |
| 59 | + |
| 60 | + protected function getIdentityFormatter(): FormatterInterface |
| 61 | + { |
| 62 | + $formatter = $this->createMock(FormatterInterface::class); |
| 63 | + $formatter->expects(self::any()) |
| 64 | + ->method('format') |
| 65 | + ->willReturnCallback(function ($record) { |
| 66 | + return $record->message; |
| 67 | + }); |
| 68 | + |
| 69 | + return $formatter; |
| 70 | + } |
| 71 | +} |
0 commit comments