-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRequestExceptionHandlerTest.php
More file actions
213 lines (172 loc) · 7.93 KB
/
Copy pathRequestExceptionHandlerTest.php
File metadata and controls
213 lines (172 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/** @noinspection PhpUnhandledExceptionInspection */
namespace Dru1x\ExpoPush\Tests\Unit\Request;
use Dru1x\ExpoPush\PushError\PushErrorCode;
use Dru1x\ExpoPush\PushError\PushErrorCollection;
use Dru1x\ExpoPush\Request\RequestExceptionHandler;
use JsonException;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Saloon\Exceptions\Request\FatalRequestException;
use Saloon\Exceptions\Request\RequestException;
use Saloon\Http\Response;
use Saloon\RateLimitPlugin\Exceptions\RateLimitReachedException;
use Saloon\RateLimitPlugin\Limit;
class RequestExceptionHandlerTest extends TestCase
{
protected PushErrorCollection $errors;
protected RequestExceptionHandler $handler;
protected function setUp(): void
{
parent::setUp();
$this->errors = new PushErrorCollection();
$this->handler = new RequestExceptionHandler(batchSize: 100, totalCount: 1000, errors: $this->errors);
}
#[Test]
public function invoke_with_fatal_request_exception_adds_push_error(): void
{
$fatalRequestException = $this->createMock(FatalRequestException::class);
$handler = $this->handler;
$handler($fatalRequestException, 1);
$this->assertCount(1, $this->errors);
$error = $this->errors->get(0);
$this->assertEquals(PushErrorCode::Failed, $error->code);
$this->assertEquals(100, $error->startIndex);
$this->assertEquals(199, $error->endIndex);
}
#[Test]
public function invoke_with_rate_limit_reached_exception_adds_push_error(): void
{
$limit = Limit::allow(100)->setPrefix('expo')->name('push-limit');
$rateLimitReachedException = new RateLimitReachedException($limit);
$handler = $this->handler;
$handler($rateLimitReachedException, 2);
$this->assertCount(1, $this->errors);
$error = $this->errors->get(0);
$this->assertEquals(PushErrorCode::TooManyRequests, $error->code);
$this->assertEquals('Request Rate Limit Reached (Name: expo:push-limit)', $error->message);
$this->assertEquals(200, $error->startIndex);
$this->assertEquals(299, $error->endIndex);
}
#[Test]
public function invoke_with_request_exception_adds_push_error(): void
{
$exceptionResponse = $this->createMock(Response::class);
$exceptionResponse
->method('json')
->with('errors')
->willReturn([
[
'code' => 'PUSH_TOO_MANY_NOTIFICATIONS',
'message' => 'You are trying to send more than 100 push notifications in one request',
],
]);
$requestException = $this->createMock(RequestException::class);
$requestException
->method('getResponse')
->willReturn($exceptionResponse);
$handler = $this->handler;
$handler($requestException, 3);
$this->assertCount(1, $this->errors);
$error = $this->errors->get(0);
$this->assertEquals(PushErrorCode::PushTooManyNotifications, $error->code);
$this->assertEquals('You are trying to send more than 100 push notifications in one request', $error->message);
$this->assertEquals(300, $error->startIndex);
$this->assertEquals(399, $error->endIndex);
}
#[Test]
public function invoke_with_request_exception_and_multiple_response_errors_adds_multiple_push_errors(): void
{
$exceptionResponse = $this->createMock(Response::class);
$exceptionResponse
->method('json')
->with('errors')
->willReturn([
[
'code' => 'PUSH_TOO_MANY_NOTIFICATIONS',
'message' => 'You are trying to send more than 100 push notifications in one request',
],
[
'code' => 'PUSH_TOO_MANY_RECEIPTS',
'message' => 'You are trying to fetch more than 1000 receipts in one request',
'details' => ['reason' => 'too many ids'],
],
[
'code' => 'SOME_UNKNOWN_CODE',
'message' => 'Something unexpected happened',
],
]);
$requestException = $this->createMock(RequestException::class);
$requestException
->method('getResponse')
->willReturn($exceptionResponse);
$handler = $this->handler;
$handler($requestException, 5);
$this->assertCount(3, $this->errors);
$firstError = $this->errors->get(0);
$this->assertEquals(PushErrorCode::PushTooManyNotifications, $firstError->code);
$this->assertEquals('You are trying to send more than 100 push notifications in one request', $firstError->message);
$this->assertNull($firstError->details);
$this->assertEquals(500, $firstError->startIndex);
$this->assertEquals(599, $firstError->endIndex);
$secondError = $this->errors->get(1);
$this->assertEquals(PushErrorCode::PushTooManyReceipts, $secondError->code);
$this->assertEquals('You are trying to fetch more than 1000 receipts in one request', $secondError->message);
$this->assertEquals(['reason' => 'too many ids'], $secondError->details);
$this->assertEquals(500, $secondError->startIndex);
$this->assertEquals(599, $secondError->endIndex);
$thirdError = $this->errors->get(2);
$this->assertEquals(PushErrorCode::Unknown, $thirdError->code);
$this->assertEquals('Something unexpected happened', $thirdError->message);
$this->assertNull($thirdError->details);
$this->assertEquals(500, $thirdError->startIndex);
$this->assertEquals(599, $thirdError->endIndex);
}
#[Test]
public function invoke_with_request_exception_and_non_json_response_adds_generic_push_error(): void
{
$exceptionResponse = $this->createMock(Response::class);
$exceptionResponse
->method('json')
->with('errors')
->willThrowException(new JsonException('Syntax error'));
$requestException = new RequestException($exceptionResponse, 'There was an error with the request: 502');
$handler = $this->handler;
$handler($requestException, 2);
$this->assertCount(1, $this->errors);
$error = $this->errors->get(0);
$this->assertEquals(PushErrorCode::Failed, $error->code);
$this->assertEquals('There was an error with the request: 502', $error->message);
$this->assertEquals(200, $error->startIndex);
$this->assertEquals(299, $error->endIndex);
}
#[Test]
public function invoke_with_request_exception_and_missing_errors_key_adds_generic_push_error(): void
{
$exceptionResponse = $this->createMock(Response::class);
$exceptionResponse
->method('json')
->with('errors')
->willReturn(null);
$requestException = new RequestException($exceptionResponse, 'There was an error with the request: 500');
$handler = $this->handler;
$handler($requestException, 4);
$this->assertCount(1, $this->errors);
$error = $this->errors->get(0);
$this->assertEquals(PushErrorCode::Failed, $error->code);
$this->assertEquals('There was an error with the request: 500', $error->message);
$this->assertEquals(400, $error->startIndex);
$this->assertEquals(499, $error->endIndex);
}
#[Test]
public function invoke_clamps_end_index_for_partial_final_batch(): void
{
$handler = new RequestExceptionHandler(batchSize: 100, totalCount: 950, errors: $this->errors);
$fatalRequestException = $this->createMock(FatalRequestException::class);
$handler($fatalRequestException, 9);
$this->assertCount(1, $this->errors);
$error = $this->errors->get(0);
$this->assertEquals(900, $error->startIndex);
$this->assertEquals(949, $error->endIndex);
}
}