1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace RemotelyLiving\PHPCommandBus\Tests\Unit\Middleware; |
6
|
|
|
|
7
|
|
|
use Psr\Log; |
8
|
|
|
use RemotelyLiving\PHPCommandBus\Enums; |
9
|
|
|
use RemotelyLiving\PHPCommandBus\Interfaces; |
10
|
|
|
use RemotelyLiving\PHPCommandBus\Middleware; |
11
|
|
|
use RemotelyLiving\PHPCommandBus\Tests; |
12
|
|
|
|
13
|
|
|
class CommandLoggerTest extends Tests\Unit\AbstractTestCase |
14
|
|
|
{ |
15
|
|
|
private Log\Test\TestLogger $testLogger; |
16
|
|
|
|
17
|
|
|
private Middleware\CommandLogger $commandLogger; |
18
|
|
|
|
19
|
|
|
protected function setUp(): void |
20
|
|
|
{ |
21
|
|
|
$this->testLogger = $this->createTestLogger(); |
22
|
|
|
$this->commandLogger = new Middleware\CommandLogger(); |
23
|
|
|
$this->commandLogger->setLogger($this->testLogger); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testSetsInfoAsDefaultIfLogLevelNotDefined(): void |
27
|
|
|
{ |
28
|
|
|
$commandWithLogLevelNotDefined = new class implements Interfaces\LoggableCommand { |
29
|
|
|
|
30
|
|
|
public function getLogContext(): array |
31
|
|
|
{ |
32
|
|
|
return ['foo' => 'bar']; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getLogMessage(): string |
36
|
|
|
{ |
37
|
|
|
return 'The log message'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getLogLevel(): ?Enums\LogLevel |
41
|
|
|
{ |
42
|
|
|
return null; |
43
|
|
|
} |
44
|
|
|
}; |
45
|
|
|
|
46
|
|
|
($this->commandLogger)($commandWithLogLevelNotDefined, fn() => null); |
47
|
|
|
|
48
|
|
|
$this->assertEquals( |
49
|
|
|
[ |
50
|
|
|
'level' => 'info', |
51
|
|
|
'message' => 'The log message', |
52
|
|
|
'context' => ['foo' => 'bar'], |
53
|
|
|
], |
54
|
|
|
$this->testLogger->records[0] |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testSetsInfoAsDefaultIfLogLevelIfDefined(): void |
59
|
|
|
{ |
60
|
|
|
$commandWithLogLevelDefined = new class implements Interfaces\LoggableCommand { |
61
|
|
|
|
62
|
|
|
public function getLogContext(): array |
63
|
|
|
{ |
64
|
|
|
return ['foo' => 'bar']; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getLogMessage(): string |
68
|
|
|
{ |
69
|
|
|
return 'The log message'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getLogLevel(): ?Enums\LogLevel |
73
|
|
|
{ |
74
|
|
|
return Enums\LogLevel::DEBUG(); |
75
|
|
|
} |
76
|
|
|
}; |
77
|
|
|
|
78
|
|
|
($this->commandLogger)($commandWithLogLevelDefined, fn() => null); |
79
|
|
|
|
80
|
|
|
$this->assertEquals( |
81
|
|
|
[ |
82
|
|
|
'level' => 'debug', |
83
|
|
|
'message' => 'The log message', |
84
|
|
|
'context' => ['foo' => 'bar'], |
85
|
|
|
], |
86
|
|
|
$this->testLogger->records[0] |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|