1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Facile\Sentry\LogTest; |
4
|
|
|
|
5
|
|
|
use Facile\Sentry\Common\Sanitizer\SanitizerInterface; |
6
|
|
|
use Facile\Sentry\Common\Sender\SenderInterface; |
7
|
|
|
use Facile\Sentry\Log\Logger; |
8
|
|
|
use Psr\Log\InvalidArgumentException; |
9
|
|
|
use Psr\Log\LogLevel; |
10
|
|
|
|
11
|
|
|
class LoggerTest extends \PHPUnit\Framework\TestCase |
12
|
|
|
{ |
13
|
|
|
public function testLogWithInvalidLevel(): void |
14
|
|
|
{ |
15
|
|
|
$this->expectException(InvalidArgumentException::class); |
16
|
|
|
|
17
|
|
|
$raven = $this->prophesize(\Raven_Client::class); |
18
|
|
|
$logger = new Logger($raven->reveal()); |
19
|
|
|
|
20
|
|
|
$logger->log('foo', 'message'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function testLogWithInvalidObject(): void |
24
|
|
|
{ |
25
|
|
|
$this->expectException(InvalidArgumentException::class); |
26
|
|
|
|
27
|
|
|
$raven = $this->prophesize(\Raven_Client::class); |
28
|
|
|
$logger = new Logger($raven->reveal()); |
29
|
|
|
|
30
|
|
|
$logger->log(LogLevel::ALERT, new \stdClass()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testLogWithObject(): void |
34
|
|
|
{ |
35
|
|
|
$raven = $this->prophesize(\Raven_Client::class); |
36
|
|
|
$sender = $this->prophesize(SenderInterface::class); |
37
|
|
|
$sanitizer = $this->prophesize(SanitizerInterface::class); |
38
|
|
|
$logger = new Logger( |
39
|
|
|
$raven->reveal(), |
40
|
|
|
$sender->reveal(), |
41
|
|
|
$sanitizer->reveal() |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
$object = new class() { |
45
|
|
|
public function __toString(): string |
46
|
|
|
{ |
47
|
|
|
return 'object string'; |
48
|
|
|
} |
49
|
|
|
}; |
50
|
|
|
|
51
|
|
|
$context = [ |
52
|
|
|
'foo' => 'name', |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
$sanitizer->sanitize($context)->shouldBeCalled()->willReturn($context); |
56
|
|
|
$sender->send(\Raven_Client::ERROR, 'object string', $context) |
57
|
|
|
->shouldBeCalled(); |
58
|
|
|
|
59
|
|
|
$logger->log(LogLevel::ALERT, $object, $context); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testLog(): void |
63
|
|
|
{ |
64
|
|
|
$raven = $this->prophesize(\Raven_Client::class); |
65
|
|
|
$sender = $this->prophesize(SenderInterface::class); |
66
|
|
|
$sanitizer = $this->prophesize(SanitizerInterface::class); |
67
|
|
|
$logger = new Logger( |
68
|
|
|
$raven->reveal(), |
69
|
|
|
$sender->reveal(), |
70
|
|
|
$sanitizer->reveal() |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
$context = [ |
74
|
|
|
'foo' => 'name', |
75
|
|
|
'placeholder' => 'value', |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
$sanitizer->sanitize($context)->shouldBeCalled()->willReturn($context); |
79
|
|
|
$sender->send(\Raven_Client::ERROR, 'message value', $context) |
80
|
|
|
->shouldBeCalled(); |
81
|
|
|
|
82
|
|
|
$logger->log(LogLevel::ALERT, 'message {placeholder}', $context); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testLogWithArrayPlaceholder(): void |
86
|
|
|
{ |
87
|
|
|
$raven = $this->prophesize(\Raven_Client::class); |
88
|
|
|
$sender = $this->prophesize(SenderInterface::class); |
89
|
|
|
$sanitizer = $this->prophesize(SanitizerInterface::class); |
90
|
|
|
$logger = new Logger( |
91
|
|
|
$raven->reveal(), |
92
|
|
|
$sender->reveal(), |
93
|
|
|
$sanitizer->reveal() |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
$context = [ |
97
|
|
|
'foo' => 'name', |
98
|
|
|
'placeholder' => [ |
99
|
|
|
'foo' => 'bar', |
100
|
|
|
], |
101
|
|
|
]; |
102
|
|
|
|
103
|
|
|
$sanitizer->sanitize($context)->shouldBeCalled()->willReturn($context); |
104
|
|
|
$sender->send(\Raven_Client::ERROR, 'message {placeholder}', $context) |
105
|
|
|
->shouldBeCalled(); |
106
|
|
|
|
107
|
|
|
$logger->log(LogLevel::ALERT, 'message {placeholder}', $context); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|