1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Violet88\BugsnagModule\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Environment; |
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
7
|
|
|
use Violet88\BugsnagModule\BugsnagLogger; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @covers \Violet88\BugsnagModule\BugsnagLogger |
11
|
|
|
* @covers \Violet88\BugsnagModule\Bugsnag |
12
|
|
|
*/ |
13
|
|
|
class BugsnagLoggerTest extends SapphireTest |
14
|
|
|
{ |
15
|
|
|
protected function setUp(): void |
16
|
|
|
{ |
17
|
|
|
parent::setUp(); |
18
|
|
|
Environment::setEnv('BUGSNAG_API_KEY', '1234567890'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
protected static function getMethod($name) |
22
|
|
|
{ |
23
|
|
|
$class = new \ReflectionClass('Violet88\BugsnagModule\BugsnagLogger'); |
24
|
|
|
$method = $class->getMethod($name); |
25
|
|
|
$method->setAccessible(true); |
26
|
|
|
return $method; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testWriteSendsError() |
30
|
|
|
{ |
31
|
|
|
$write = self::getMethod('write'); |
32
|
|
|
$bugsnagMock = $this->getMockBuilder('Violet88\BugsnagModule\Bugsnag') |
33
|
|
|
->setMethods(['sendException', 'sendError']) |
34
|
|
|
->getMock(); |
35
|
|
|
$bugsnagMock->expects($this->exactly(2)) |
36
|
|
|
->method('sendError'); |
37
|
|
|
$obj = $this->getMockBuilder('Violet88\BugsnagModule\BugsnagLogger') |
38
|
|
|
->setConstructorArgs([$bugsnagMock]) |
39
|
|
|
->setMethods(['getBugsnag']) |
40
|
|
|
->getMock(); |
41
|
|
|
|
42
|
|
|
$obj->expects($this->exactly(2)) |
43
|
|
|
->method('getBugsnag') |
44
|
|
|
->willReturn($bugsnagMock); |
45
|
|
|
$args = [ |
46
|
|
|
'message' => 'test', |
47
|
|
|
'context' => [ |
48
|
|
|
'message' => 'test' |
49
|
|
|
], |
50
|
|
|
'level' => 300 |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
$args2 = [ |
54
|
|
|
'message' => 'test', |
55
|
|
|
'context' => [ |
56
|
|
|
'exception' => 'test' |
57
|
|
|
], |
58
|
|
|
'level' => 300 |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
$write->invokeArgs($obj, [$args]); |
62
|
|
|
$write->invokeArgs($obj, [$args2]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testWriteSendsException() |
66
|
|
|
{ |
67
|
|
|
$write = self::getMethod('write'); |
68
|
|
|
$bugsnagMock = $this->getMockBuilder('Violet88\BugsnagModule\Bugsnag') |
69
|
|
|
->setMethods(['sendException', 'sendError']) |
70
|
|
|
->getMock(); |
71
|
|
|
$bugsnagMock->expects($this->once()) |
72
|
|
|
->method('sendException'); |
73
|
|
|
$obj = $this->getMockBuilder('Violet88\BugsnagModule\BugsnagLogger') |
74
|
|
|
->setConstructorArgs([$bugsnagMock]) |
75
|
|
|
->setMethods(['getBugsnag']) |
76
|
|
|
->getMock(); |
77
|
|
|
|
78
|
|
|
$obj->expects($this->once()) |
79
|
|
|
->method('getBugsnag') |
80
|
|
|
->willReturn($bugsnagMock); |
81
|
|
|
$args = [ |
82
|
|
|
'message' => 'test', |
83
|
|
|
'context' => [ |
84
|
|
|
'exception' => new \Exception('test') |
85
|
|
|
], |
86
|
|
|
'level' => 300 |
87
|
|
|
]; |
88
|
|
|
$write->invokeArgs($obj, [$args]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testWriteSendsErrorWithCorrectSeverity100() |
92
|
|
|
{ |
93
|
|
|
$write = self::getMethod('write'); |
94
|
|
|
$exception = new \Exception('test'); |
95
|
|
|
$bugsnagMock = $this->getMockBuilder('Violet88\BugsnagModule\Bugsnag') |
96
|
|
|
->setMethods(['sendException', 'sendError']) |
97
|
|
|
->getMock(); |
98
|
|
|
$bugsnagMock->expects($this->once()) |
99
|
|
|
->method('sendException') |
100
|
|
|
->with($this->anything(), $this->equalTo('info'), $this->anything()); |
101
|
|
|
$obj = $this->getMockBuilder('Violet88\BugsnagModule\BugsnagLogger') |
102
|
|
|
->setConstructorArgs([$bugsnagMock]) |
103
|
|
|
->setMethods(['getBugsnag']) |
104
|
|
|
->getMock(); |
105
|
|
|
|
106
|
|
|
$obj->expects($this->once()) |
107
|
|
|
->method('getBugsnag') |
108
|
|
|
->willReturn($bugsnagMock); |
109
|
|
|
$args = [ |
110
|
|
|
'message' => 'test', |
111
|
|
|
'context' => [ |
112
|
|
|
'exception' => $exception |
113
|
|
|
], |
114
|
|
|
'level' => 100 |
115
|
|
|
]; |
116
|
|
|
$write->invokeArgs($obj, [$args]); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testWriteSendsErrorWithCorrectSeverity600() |
120
|
|
|
{ |
121
|
|
|
$write = self::getMethod('write'); |
122
|
|
|
$exception = new \Exception('test'); |
123
|
|
|
$bugsnagMock = $this->getMockBuilder('Violet88\BugsnagModule\Bugsnag') |
124
|
|
|
->setMethods(['sendException', 'sendError']) |
125
|
|
|
->getMock(); |
126
|
|
|
$bugsnagMock->expects($this->once()) |
127
|
|
|
->method('sendException') |
128
|
|
|
->with($this->anything(), $this->equalTo('error'), $this->anything()); |
129
|
|
|
$obj = $this->getMockBuilder('Violet88\BugsnagModule\BugsnagLogger') |
130
|
|
|
->setConstructorArgs([$bugsnagMock]) |
131
|
|
|
->setMethods(['getBugsnag']) |
132
|
|
|
->getMock(); |
133
|
|
|
|
134
|
|
|
$obj->expects($this->once()) |
135
|
|
|
->method('getBugsnag') |
136
|
|
|
->willReturn($bugsnagMock); |
137
|
|
|
$args = [ |
138
|
|
|
'message' => 'test', |
139
|
|
|
'context' => [ |
140
|
|
|
'exception' => $exception |
141
|
|
|
], |
142
|
|
|
'level' => 600 |
143
|
|
|
]; |
144
|
|
|
$write->invokeArgs($obj, [$args]); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testGetBugsnag() |
148
|
|
|
{ |
149
|
|
|
$bugsnagMock = $this->getMockBuilder('Violet88\BugsnagModule\Bugsnag') |
150
|
|
|
->setMethods(['sendException', 'sendError']) |
151
|
|
|
->getMock(); |
152
|
|
|
$obj = new BugsnagLogger($bugsnagMock); |
153
|
|
|
$this->assertEquals($bugsnagMock, $obj->getBugsnag()); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|