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(300, |
38
|
|
|
'foo', |
39
|
|
|
[ |
40
|
|
|
'file' => 'bar', |
41
|
|
|
'line' => 1, |
42
|
|
|
] |
43
|
|
|
); |
44
|
|
|
$record['context']['tags'] = ['foo']; |
45
|
|
|
$record['context']['timestamp'] = 1234567890; |
46
|
|
|
$record['extra'] = ['bar' => 'baz', 'tags' => ['bar']]; |
47
|
|
|
$formatted = array_merge($record, |
48
|
|
|
[ |
49
|
|
|
'tags' => ['foo', 'bar'], |
50
|
|
|
'timestamp' => 1234567890, |
51
|
|
|
'custom_data' => ['bar' => 'baz'], |
52
|
|
|
] |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$formatter = m::mock('Monolog\\Formatter\\FormatterInterface'); |
56
|
|
|
$handler = new RaygunHandler($this->client); |
57
|
|
|
$handler->setFormatter($formatter); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$formatter |
60
|
|
|
->shouldReceive('format') |
|
|
|
|
61
|
|
|
->once() |
62
|
|
|
->with($record) |
63
|
|
|
->andReturn($formatted); |
64
|
|
|
$this->client |
65
|
|
|
->shouldReceive('SendError') |
66
|
|
|
->once() |
67
|
|
|
->with(0, 'foo', 'bar', 1, ['foo', 'bar'], ['bar' => 'baz'], 1234567890); |
68
|
|
|
|
69
|
|
|
$handler->handle($record); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testHandleException() |
73
|
|
|
{ |
74
|
|
|
$exception = new \Exception('foo'); |
75
|
|
|
$record = $this->getRecord(300, 'foo', ['exception' => $exception]); |
76
|
|
|
$record['extra'] = ['bar' => 'baz', 'tags' => ['foo', 'bar']]; |
77
|
|
|
$record['extra']['timestamp'] = 1234567890; |
78
|
|
|
$formatted = array_merge($record, |
79
|
|
|
[ |
80
|
|
|
'tags' => ['foo', 'bar'], |
81
|
|
|
'timestamp' => 1234567890, |
82
|
|
|
'custom_data' => ['bar' => 'baz'], |
83
|
|
|
] |
84
|
|
|
); |
85
|
|
|
$formatted['context']['exception'] = [ |
86
|
|
|
'class' => get_class($exception), |
87
|
|
|
'message' => $exception->getMessage(), |
88
|
|
|
'code' => $exception->getCode(), |
89
|
|
|
'file' => $exception->getFile() . ':' . $exception->getLine(), |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
$formatter = m::mock('Monolog\\Formatter\\FormatterInterface'); |
93
|
|
|
$handler = new RaygunHandler($this->client); |
94
|
|
|
$handler->setFormatter($formatter); |
|
|
|
|
95
|
|
|
|
96
|
|
|
$formatter |
97
|
|
|
->shouldReceive('format') |
|
|
|
|
98
|
|
|
->once() |
99
|
|
|
->with($record) |
100
|
|
|
->andReturn($formatted); |
101
|
|
|
$this->client |
102
|
|
|
->shouldReceive('SendException') |
103
|
|
|
->once() |
104
|
|
|
->with($exception, ['foo', 'bar'], ['bar' => 'baz'], 1234567890); |
105
|
|
|
|
106
|
|
|
$handler->handle($record); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testHandleEmptyDoesNothing() |
110
|
|
|
{ |
111
|
|
|
$record = $this->getRecord(300, 'bar'); |
112
|
|
|
$record['extra'] = ['bar' => 'baz', 'tags' => ['foo', 'bar']]; |
113
|
|
|
$record['extra']['timestamp'] = 1234567890; |
114
|
|
|
$formatted = array_merge($record, |
115
|
|
|
[ |
116
|
|
|
'tags' => ['foo', 'bar'], |
117
|
|
|
'timestamp' => 1234567890, |
118
|
|
|
'custom_data' => ['bar' => 'baz'], |
119
|
|
|
] |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
$formatter = m::mock('Monolog\\Formatter\\FormatterInterface'); |
123
|
|
|
$handler = new RaygunHandler($this->client); |
124
|
|
|
$handler->setFormatter($formatter); |
|
|
|
|
125
|
|
|
|
126
|
|
|
$formatter |
127
|
|
|
->shouldReceive('format') |
|
|
|
|
128
|
|
|
->once() |
129
|
|
|
->with($record) |
130
|
|
|
->andReturn($formatted); |
131
|
|
|
|
132
|
|
|
$handler->handle($record); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @requires PHP 7 |
137
|
|
|
*/ |
138
|
|
|
public function testHandleThrowable() |
139
|
|
|
{ |
140
|
|
|
$exception = new \TypeError('foo'); |
141
|
|
|
$record = $this->getRecord(300, 'foo', ['exception' => $exception]); |
142
|
|
|
$record['context']['tags'] = ['foo']; |
143
|
|
|
$formatted = array_merge($record, |
144
|
|
|
[ |
145
|
|
|
'tags' => ['foo'], |
146
|
|
|
'custom_data' => [], |
147
|
|
|
'timestamp' => null, |
148
|
|
|
] |
149
|
|
|
); |
150
|
|
|
|
151
|
|
|
$formatter = m::mock('Monolog\\Formatter\\FormatterInterface'); |
152
|
|
|
$handler = new RaygunHandler($this->client); |
153
|
|
|
$handler->setFormatter($formatter); |
|
|
|
|
154
|
|
|
|
155
|
|
|
$formatter |
156
|
|
|
->shouldReceive('format') |
|
|
|
|
157
|
|
|
->once() |
158
|
|
|
->with($record) |
159
|
|
|
->andReturn($formatted); |
160
|
|
|
$this->client |
161
|
|
|
->shouldReceive('SendException') |
162
|
|
|
->once() |
163
|
|
|
->with($exception, ['foo'], [], null); |
164
|
|
|
|
165
|
|
|
$handler->handle($record); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|