| 148 |
lars |
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 |
|
|
|
12 |
namespace Monolog\Test;
|
|
|
13 |
|
|
|
14 |
use Monolog\Logger;
|
|
|
15 |
use Monolog\DateTimeImmutable;
|
|
|
16 |
use 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 |
*
|
|
|
26 |
* @internal feel free to reuse this to test your own handlers, this is marked internal to avoid issues with PHPStorm https://github.com/Seldaek/monolog/issues/1677
|
|
|
27 |
*/
|
|
|
28 |
class TestCase extends \PHPUnit\Framework\TestCase
|
|
|
29 |
{
|
|
|
30 |
public function tearDown(): void
|
|
|
31 |
{
|
|
|
32 |
parent::tearDown();
|
|
|
33 |
|
|
|
34 |
if (isset($this->handler)) {
|
|
|
35 |
unset($this->handler);
|
|
|
36 |
}
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* @param mixed[] $context
|
|
|
41 |
*
|
|
|
42 |
* @return array Record
|
|
|
43 |
*
|
|
|
44 |
* @phpstan-param Level $level
|
|
|
45 |
* @phpstan-return Record
|
|
|
46 |
*/
|
|
|
47 |
protected function getRecord(int $level = Logger::WARNING, string $message = 'test', array $context = []): array
|
|
|
48 |
{
|
|
|
49 |
return [
|
|
|
50 |
'message' => (string) $message,
|
|
|
51 |
'context' => $context,
|
|
|
52 |
'level' => $level,
|
|
|
53 |
'level_name' => Logger::getLevelName($level),
|
|
|
54 |
'channel' => 'test',
|
|
|
55 |
'datetime' => new DateTimeImmutable(true),
|
|
|
56 |
'extra' => [],
|
|
|
57 |
];
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* @phpstan-return Record[]
|
|
|
62 |
*/
|
|
|
63 |
protected function getMultipleRecords(): array
|
|
|
64 |
{
|
|
|
65 |
return [
|
|
|
66 |
$this->getRecord(Logger::DEBUG, 'debug message 1'),
|
|
|
67 |
$this->getRecord(Logger::DEBUG, 'debug message 2'),
|
|
|
68 |
$this->getRecord(Logger::INFO, 'information'),
|
|
|
69 |
$this->getRecord(Logger::WARNING, 'warning'),
|
|
|
70 |
$this->getRecord(Logger::ERROR, 'error'),
|
|
|
71 |
];
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
protected function getIdentityFormatter(): FormatterInterface
|
|
|
75 |
{
|
|
|
76 |
$formatter = $this->createMock(FormatterInterface::class);
|
|
|
77 |
$formatter->expects($this->any())
|
|
|
78 |
->method('format')
|
|
|
79 |
->will($this->returnCallback(function ($record) {
|
|
|
80 |
return $record['message'];
|
|
|
81 |
}));
|
|
|
82 |
|
|
|
83 |
return $formatter;
|
|
|
84 |
}
|
|
|
85 |
}
|