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