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