1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\Monolog\Handler; |
4
|
|
|
|
5
|
|
|
use Mockery as m; |
6
|
|
|
use Monolog\Logger; |
7
|
|
|
use Monolog\Test\TestCase; |
8
|
|
|
|
9
|
|
|
class RaygunHandlerTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
public function setUp(): void |
12
|
|
|
{ |
13
|
|
|
if (!class_exists('Raygun4php\RaygunClient')) { |
14
|
|
|
$this->markTestSkipped('mindscape/raygun4php not installed'); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
$this->client = m::mock('Raygun4php\RaygunClient'); |
|
|
|
|
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function tearDown(): void |
21
|
|
|
{ |
22
|
|
|
parent::tearDown(); |
23
|
|
|
m::close(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testConstruct() |
27
|
|
|
{ |
28
|
|
|
$this->assertInstanceOf('Graze\\Monolog\\Handler\\RaygunHandler', new RaygunHandler($this->client)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testInterface() |
32
|
|
|
{ |
33
|
|
|
$this->assertInstanceOf('Monolog\\Handler\\HandlerInterface', new RaygunHandler($this->client)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testGetFormatter() |
37
|
|
|
{ |
38
|
|
|
$handler = new RaygunHandler($this->client, Logger::DEBUG); |
39
|
|
|
$this->assertInstanceOf('Monolog\\Formatter\\NormalizerFormatter', $handler->getFormatter()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testHandleError() |
43
|
|
|
{ |
44
|
|
|
$record = $this->getRecord( |
45
|
|
|
300, |
46
|
|
|
'foo', |
47
|
|
|
[ |
48
|
|
|
'file' => 'bar', |
49
|
|
|
'line' => 1, |
50
|
|
|
] |
51
|
|
|
); |
52
|
|
|
$record['context']['tags'] = ['foo']; |
53
|
|
|
$record['context']['timestamp'] = 1234567890; |
54
|
|
|
$record['extra'] = ['bar' => 'baz', 'tags' => ['bar']]; |
55
|
|
|
$formatted = array_merge( |
56
|
|
|
$record, |
57
|
|
|
[ |
58
|
|
|
'tags' => ['foo', 'bar'], |
59
|
|
|
'timestamp' => 1234567890, |
60
|
|
|
'custom_data' => ['bar' => 'baz'] |
61
|
|
|
] |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
$formatter = m::mock('Monolog\\Formatter\\FormatterInterface'); |
65
|
|
|
$handler = new RaygunHandler($this->client); |
66
|
|
|
$handler->setFormatter($formatter); |
|
|
|
|
67
|
|
|
|
68
|
|
|
$formatter |
69
|
|
|
->shouldReceive('format') |
70
|
|
|
->once() |
71
|
|
|
->with($record) |
72
|
|
|
->andReturn($formatted); |
73
|
|
|
$this->client |
74
|
|
|
->shouldReceive('SendError') |
75
|
|
|
->once() |
76
|
|
|
->with(0, 'foo', 'bar', 1, ['foo', 'bar'], ['bar' => 'baz'], 1234567890); |
77
|
|
|
|
78
|
|
|
$this->assertFalse($handler->handle($record)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testHandleException() |
82
|
|
|
{ |
83
|
|
|
$exception = new \Exception('foo'); |
84
|
|
|
$record = $this->getRecord(300, 'foo', ['exception' => $exception]); |
85
|
|
|
$record['extra'] = ['bar' => 'baz', 'tags' => ['foo', 'bar']]; |
86
|
|
|
$record['extra']['timestamp'] = 1234567890; |
87
|
|
|
$formatted = array_merge( |
88
|
|
|
$record, |
89
|
|
|
[ |
90
|
|
|
'tags' => ['foo', 'bar'], |
91
|
|
|
'timestamp' => 1234567890, |
92
|
|
|
'custom_data' => ['bar' => 'baz'] |
93
|
|
|
] |
94
|
|
|
); |
95
|
|
|
$formatted['context']['exception'] = [ |
96
|
|
|
'class' => get_class($exception), |
97
|
|
|
'message' => $exception->getMessage(), |
98
|
|
|
'code' => $exception->getCode(), |
99
|
|
|
'file' => $exception->getFile() . ':' . $exception->getLine(), |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
$formatter = m::mock('Monolog\\Formatter\\FormatterInterface'); |
103
|
|
|
$handler = new RaygunHandler($this->client); |
104
|
|
|
$handler->setFormatter($formatter); |
|
|
|
|
105
|
|
|
|
106
|
|
|
$formatter |
107
|
|
|
->shouldReceive('format') |
108
|
|
|
->once() |
109
|
|
|
->with($record) |
110
|
|
|
->andReturn($formatted); |
111
|
|
|
$this->client |
112
|
|
|
->shouldReceive('SendException') |
113
|
|
|
->once() |
114
|
|
|
->with($exception, ['foo', 'bar'], ['bar' => 'baz'], 1234567890); |
115
|
|
|
|
116
|
|
|
$this->assertFalse($handler->handle($record)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testHandleEmptyDoesNothing() |
120
|
|
|
{ |
121
|
|
|
$record = $this->getRecord(300, 'bar'); |
122
|
|
|
$record['extra'] = ['bar' => 'baz', 'tags' => ['foo', 'bar']]; |
123
|
|
|
$record['extra']['timestamp'] = 1234567890; |
124
|
|
|
$formatted = array_merge( |
125
|
|
|
$record, |
126
|
|
|
[ |
127
|
|
|
'tags' => ['foo', 'bar'], |
128
|
|
|
'timestamp' => 1234567890, |
129
|
|
|
'custom_data' => ['bar' => 'baz'], |
130
|
|
|
] |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
$formatter = m::mock('Monolog\\Formatter\\FormatterInterface'); |
134
|
|
|
$handler = new RaygunHandler($this->client); |
135
|
|
|
$handler->setFormatter($formatter); |
|
|
|
|
136
|
|
|
|
137
|
|
|
$formatter |
138
|
|
|
->shouldReceive('format') |
139
|
|
|
->once() |
140
|
|
|
->with($record) |
141
|
|
|
->andReturn($formatted); |
142
|
|
|
|
143
|
|
|
$this->assertFalse($handler->handle($record)); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @requires PHP 7 |
148
|
|
|
*/ |
149
|
|
|
public function testHandleThrowable() |
150
|
|
|
{ |
151
|
|
|
$exception = new \TypeError('foo'); |
152
|
|
|
$record = $this->getRecord(300, 'foo', ['exception' => $exception]); |
153
|
|
|
$record['context']['tags'] = ['foo']; |
154
|
|
|
$formatted = array_merge( |
155
|
|
|
$record, |
156
|
|
|
[ |
157
|
|
|
'tags' => ['foo'], |
158
|
|
|
'custom_data' => [], |
159
|
|
|
'timestamp' => null, |
160
|
|
|
] |
161
|
|
|
); |
162
|
|
|
|
163
|
|
|
$formatter = m::mock('Monolog\\Formatter\\FormatterInterface'); |
164
|
|
|
$handler = new RaygunHandler($this->client); |
165
|
|
|
$handler->setFormatter($formatter); |
|
|
|
|
166
|
|
|
|
167
|
|
|
$formatter |
168
|
|
|
->shouldReceive('format') |
169
|
|
|
->once() |
170
|
|
|
->with($record) |
171
|
|
|
->andReturn($formatted); |
172
|
|
|
$this->client |
173
|
|
|
->shouldReceive('SendException') |
174
|
|
|
->once() |
175
|
|
|
->with($exception, ['foo'], [], null); |
176
|
|
|
|
177
|
|
|
$this->assertFalse($handler->handle($record)); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|