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