/
GithubExceptionThrowerTest.php
149 lines (141 loc) · 5.17 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
<?php
namespace Github\Tests\HttpClient\Plugin;
use Github\Exception\ExceptionInterface;
use Github\HttpClient\Plugin\GithubExceptionThrower;
use GuzzleHttp\Promise\FulfilledPromise;
use GuzzleHttp\Psr7\Response;
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
{
/**
* @param ResponseInterface $response
* @param ExceptionInterface|\Exception|null $exception
* @dataProvider responseProvider
*/
public function testHandleRequest(ResponseInterface $response, ExceptionInterface $exception = null)
{
/** @var RequestInterface $request */
$request = $this->getMockForAbstractClass(RequestInterface::class);
$promise = $this->getMockBuilder(FulfilledPromise::class)->disableOriginalConstructor()->getMock();
$promise->expects($this->once())
->method('then')
->willReturnCallback(function ($callback) use ($response) {
return $callback($response);
});
$plugin = new GithubExceptionThrower();
if ($exception) {
$this->expectException(get_class($exception));
$this->expectExceptionCode($exception->getCode());
$this->expectExceptionMessage($exception->getMessage());
}
$plugin->doHandleRequest(
$request,
function (RequestInterface $request) use ($promise) {
return $promise;
},
function (RequestInterface $request) use ($promise) {
return $promise;
}
);
}
/**
* @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),
],
'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' => 'Bad Request',
]
)
),
'exception' => new \Github\Exception\ErrorException('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),
],
'Default handling' => [
'response' => new Response(
555,
[],
'Error message'
),
'exception' => new \Github\Exception\RuntimeException('Error message', 555),
],
];
}
}