1
|
|
|
<?php |
2
|
|
|
namespace Graze\Monolog\Handler; |
3
|
|
|
|
4
|
|
|
use Mockery as m; |
5
|
|
|
use Monolog\Logger; |
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', array( |
|
|
|
|
39
|
|
|
'file' => 'bar', |
40
|
|
|
'line' => 1, |
41
|
|
|
) |
42
|
|
|
); |
43
|
|
|
$record['context']['tags'] = array('foo'); |
|
|
|
|
44
|
|
|
$record['context']['timestamp'] = 1234567890; |
45
|
|
|
$record['extra'] = array('bar' => 'baz', 'tags' => array('bar')); |
|
|
|
|
46
|
|
|
$formatted = array_merge($record, |
47
|
|
|
array( |
|
|
|
|
48
|
|
|
'tags' => array('foo', 'bar'), |
|
|
|
|
49
|
|
|
'timestamp' => 1234567890, |
50
|
|
|
'custom_data' => array('bar' => 'baz') |
|
|
|
|
51
|
|
|
) |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
$formatter = m::mock('Monolog\\Formatter\\FormatterInterface'); |
55
|
|
|
$handler = new RaygunHandler($this->client); |
56
|
|
|
$handler->setFormatter($formatter); |
|
|
|
|
57
|
|
|
|
58
|
|
|
$formatter |
59
|
|
|
->shouldReceive('format') |
|
|
|
|
60
|
|
|
->once() |
61
|
|
|
->with($record) |
62
|
|
|
->andReturn($formatted); |
63
|
|
|
$this->client |
64
|
|
|
->shouldReceive('SendError') |
65
|
|
|
->once() |
66
|
|
|
->with(0, 'foo', 'bar', 1, array('foo', 'bar'), array('bar' => 'baz'), 1234567890); |
|
|
|
|
67
|
|
|
|
68
|
|
|
$handler->handle($record); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testHandleException() |
72
|
|
|
{ |
73
|
|
|
$exception = new \Exception('foo'); |
74
|
|
|
$record = $this->getRecord(300, 'foo', array('exception' => $exception)); |
|
|
|
|
75
|
|
|
$record['extra'] = array('bar' => 'baz', 'tags' => array('foo', 'bar')); |
|
|
|
|
76
|
|
|
$record['extra']['timestamp'] = 1234567890; |
77
|
|
|
$formatted = array_merge($record, |
78
|
|
|
array( |
|
|
|
|
79
|
|
|
'tags' => array('foo', 'bar'), |
|
|
|
|
80
|
|
|
'timestamp' => 1234567890, |
81
|
|
|
'custom_data' => array('bar' => 'baz') |
|
|
|
|
82
|
|
|
) |
83
|
|
|
); |
84
|
|
|
$formatted['context']['exception'] = array( |
|
|
|
|
85
|
|
|
'class' => get_class($exception), |
86
|
|
|
'message' => $exception->getMessage(), |
87
|
|
|
'code' => $exception->getCode(), |
88
|
|
|
'file' => $exception->getFile().':'.$exception->getLine(), |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$formatter = m::mock('Monolog\\Formatter\\FormatterInterface'); |
92
|
|
|
$handler = new RaygunHandler($this->client); |
93
|
|
|
$handler->setFormatter($formatter); |
|
|
|
|
94
|
|
|
|
95
|
|
|
$formatter |
96
|
|
|
->shouldReceive('format') |
|
|
|
|
97
|
|
|
->once() |
98
|
|
|
->with($record) |
99
|
|
|
->andReturn($formatted); |
100
|
|
|
$this->client |
101
|
|
|
->shouldReceive('SendException') |
102
|
|
|
->once() |
103
|
|
|
->with($exception, array('foo', 'bar'), array('bar' => 'baz'), 1234567890); |
|
|
|
|
104
|
|
|
|
105
|
|
|
$handler->handle($record); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @dataProvider isHandleData |
110
|
|
|
* |
111
|
|
|
* @param array $record |
112
|
|
|
* @param bool $expected |
113
|
|
|
*/ |
114
|
|
|
public function testIsHandling(array $record, $expected) |
115
|
|
|
{ |
116
|
|
|
$handler = new RaygunHandler($this->client); |
117
|
|
|
|
118
|
|
|
$this->assertEquals($expected, $handler->isHandling($record)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public function isHandleData() |
125
|
|
|
{ |
126
|
|
|
return [ |
127
|
|
|
[$this->getRecord(300, 'foo', ['exception' => new \Exception('foo')]), true], |
128
|
|
|
[$this->getRecord(300, 'bar', ['file' => '/a/path/to/a/file', 'line' => 123]), true], |
129
|
|
|
[$this->getRecord(300, 'baz', ['file' => '/a/path/to/a/file']), false], |
130
|
|
|
[$this->getRecord(300, 'faz', ['line' => 456]), false], |
131
|
|
|
// no context entry |
132
|
|
|
[ |
133
|
|
|
[ |
134
|
|
|
'message' => 'foobar', |
135
|
|
|
'level' => 300, |
136
|
|
|
'level_name' => Logger::getLevelName(300), |
137
|
|
|
'channel' => 'test', |
138
|
|
|
'datetime' => \DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true))), |
139
|
|
|
'extra' => [], |
140
|
|
|
], |
141
|
|
|
false, |
142
|
|
|
], |
143
|
|
|
]; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testHandleCallsIsHandlingFirst() |
147
|
|
|
{ |
148
|
|
|
$handler = new RaygunHandler($this->client); |
149
|
|
|
$nonHandlingRecord = $this->getRecord(300, 'baz', array()); |
|
|
|
|
150
|
|
|
$this->assertFalse($handler->isHandling($nonHandlingRecord)); |
151
|
|
|
$this->assertFalse($handler->handle($nonHandlingRecord)); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @requires PHP 7 |
156
|
|
|
*/ |
157
|
|
|
public function testHandleThrowable() |
158
|
|
|
{ |
159
|
|
|
$exception = new \TypeError('foo'); |
160
|
|
|
$record = $this->getRecord(300, 'foo', array('exception' => $exception)); |
|
|
|
|
161
|
|
|
$record['context']['tags'] = array('foo'); |
|
|
|
|
162
|
|
|
$formatted = array_merge($record, |
163
|
|
|
array( |
|
|
|
|
164
|
|
|
'tags' => array('foo'), |
|
|
|
|
165
|
|
|
'custom_data' => array(), |
|
|
|
|
166
|
|
|
'timestamp' => null, |
167
|
|
|
) |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
$formatter = m::mock('Monolog\\Formatter\\FormatterInterface'); |
171
|
|
|
$handler = new RaygunHandler($this->client); |
172
|
|
|
$handler->setFormatter($formatter); |
|
|
|
|
173
|
|
|
|
174
|
|
|
$formatter |
175
|
|
|
->shouldReceive('format') |
|
|
|
|
176
|
|
|
->once() |
177
|
|
|
->with($record) |
178
|
|
|
->andReturn($formatted); |
179
|
|
|
$this->client |
180
|
|
|
->shouldReceive('SendException') |
181
|
|
|
->once() |
182
|
|
|
->with($exception, array('foo'), array(), null); |
|
|
|
|
183
|
|
|
|
184
|
|
|
$handler->handle($record); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|