-
-
Notifications
You must be signed in to change notification settings - Fork 597
/
GithubExceptionThrowerTest.php
253 lines (243 loc) · 10 KB
/
GithubExceptionThrowerTest.php
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
namespace Github\Tests\HttpClient\Plugin;
use Github\Exception\ExceptionInterface;
use Github\HttpClient\Plugin\GithubExceptionThrower;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Http\Client\Promise\HttpFulfilledPromise;
use Http\Client\Promise\HttpRejectedPromise;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* @author Sergii Ivashchenko <serg.ivashchenko@gmail.com>
*/
class GithubExceptionThrowerTest extends TestCase
{
/**
* @dataProvider responseProvider
*/
public function testHandleRequest(ResponseInterface $response, ExceptionInterface $exception = null): void
{
$request = new Request('GET', 'https://api.github.com/issues');
$promise = new HttpFulfilledPromise($response);
$plugin = new GithubExceptionThrower();
$result = $plugin->handleRequest(
$request,
function (RequestInterface $request) use ($promise) {
return $promise;
},
function (RequestInterface $request) use ($promise) {
return $promise;
}
);
if ($exception) {
$this->assertInstanceOf(HttpRejectedPromise::class, $result);
} else {
$this->assertInstanceOf(HttpFulfilledPromise::class, $result);
}
if ($exception) {
$this->expectException(get_class($exception));
$this->expectExceptionCode($exception->getCode());
$this->expectExceptionMessageMatches('/'.preg_quote($exception->getMessage(), '/').'$/');
}
$result->wait();
}
/**
* @return array
*/
public static function responseProvider()
{
return [
'200 Response' => [
'response' => new Response(),
'exception' => null,
],
'Rate Limit Exceeded' => [
'response' => new Response(
429,
[
'Content-Type' => 'application/json',
'X-RateLimit-Remaining' => 0,
'X-RateLimit-Limit' => 5000,
],
''
),
'exception' => new \Github\Exception\ApiLimitExceedException(5000),
],
'Rate Limit Exceeded via 403 status' => [
'response' => new Response(
403,
[
'Content-Type' => 'application/json',
'X-RateLimit-Remaining' => 0,
'X-RateLimit-Limit' => 5000,
'X-RateLimit-Reset' => 1609245810,
],
json_encode(
[
'message' => 'API rate limit exceeded for installation ID xxxxxxx.',
]
)
),
'exception' => new \Github\Exception\ApiLimitExceedException(5000),
],
'Secondary Rate Limit Exceeded via 403 status' => [
'response' => new Response(
403,
[
'Content-Type' => 'application/json',
'X-RateLimit-Remaining' => 100,
'X-RateLimit-Limit' => 5000,
'X-RateLimit-Reset' => 1609245810,
],
json_encode(
[
'message' => 'You have exceeded a secondary rate limit. Please wait a few minutes before you try again.',
]
)
),
'exception' => new \Github\Exception\ApiLimitExceedException(5000),
],
'Two Factor Authentication Required' => [
'response' => new Response(
401,
[
'Content-Type' => 'application/json',
'X-GitHub-OTP' => 'required; :2fa-type',
],
''
),
'exception' => new \Github\Exception\TwoFactorAuthenticationRequiredException('2fa-type'),
],
'400 Bad Request' => [
'response' => new Response(
400,
[
'Content-Type' => 'application/json',
],
json_encode(
[
'message' => 'Problems parsing JSON',
]
)
),
'exception' => new \Github\Exception\ErrorException('Problems parsing JSON (Bad Request)', 400),
],
'422 Unprocessable Entity' => [
'response' => new Response(
422,
[
'Content-Type' => 'application/json',
],
json_encode(
[
'message' => 'Bad Request',
'errors' => [
[
'code' => 'missing',
'field' => 'field',
'value' => 'value',
'resource' => 'resource',
],
],
]
)
),
'exception' => new \Github\Exception\ErrorException('Validation Failed: The field value does not exist, for resource "resource"', 422),
],
'502 Response' => [
'response' => new Response(
502,
[
'Content-Type' => 'application/json',
],
json_encode(
[
'errors' => [
['message' => 'Something went wrong with executing your query'],
],
]
)
),
'exception' => new \Github\Exception\RuntimeException('Something went wrong with executing your query', 502),
],
'Sso required Response' => [
'response' => new Response(
403,
[
'Content-Type' => 'application/json',
'X-GitHub-SSO' => 'required; url=https://github.com/orgs/octodocs-test/sso?authorization_request=AZSCKtL4U8yX1H3sCQIVnVgmjmon5fWxks5YrqhJgah0b2tlbl9pZM4EuMz4',
]
),
'exception' => new \Github\Exception\SsoRequiredException('https://github.com/orgs/octodocs-test/sso?authorization_request=AZSCKtL4U8yX1H3sCQIVnVgmjmon5fWxks5YrqhJgah0b2tlbl9pZM4EuMz4'),
],
'Default handling' => [
'response' => new Response(
555,
[],
'Error message'
),
'exception' => new \Github\Exception\RuntimeException('Error message', 555),
],
'Graphql error response (200)' => [
'response' => new Response(
200,
[
'content-type' => 'application/json',
],
json_encode(
[
'errors' => [
[
['path' => ['query', 'repository']],
'message' => 'Field "xxxx" doesn\'t exist on type "Issue"',
],
[
['path' => ['query', 'repository']],
'message' => 'Field "dummy" doesn\'t exist on type "PullRequest"',
],
],
]
)
),
'exception' => new \Github\Exception\RuntimeException('Field "xxxx" doesn\'t exist on type "Issue", Field "dummy" doesn\'t exist on type "PullRequest"'),
],
'Grapql requires authentication' => [
'response' => new Response(
401,
[
'content-type' => 'application/json',
'X-RateLimit-Limit' => 0,
'X-RateLimit-Remaining' => 0,
'X-RateLimit-Reset' => 1609245810,
'X-RateLimit-Used' => 0,
],
json_encode(
[
'message' => 'This endpoint requires you to be authenticated.',
'documentation_url' => 'https://docs.github.com/v3/#authentication',
]
)
),
'exception' => new \Github\Exception\RuntimeException('This endpoint requires you to be authenticated.', 401),
],
'Cannot delete active deployment' => [
'response' => new Response(
422,
[
'content-type' => 'application/json',
],
json_encode(
[
'message' => 'Validation Failed',
'errors' => ['We cannot delete an active deployment unless it is the only deployment in a given environment.'],
'documentation_url' => 'https://docs.github.com/rest/reference/repos#delete-a-deployment',
]
)
),
'exception' => new \Github\Exception\ValidationFailedException('Validation Failed: We cannot delete an active deployment unless it is the only deployment in a given environment.', 422),
],
];
}
}