Completed
Pull Request — master (#888)
by Yassine
02:05
created

testAuthorizationExceptions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 30
nc 1
nop 0
dl 0
loc 37
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright 2017 Facebook, Inc.
4
 *
5
 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
6
 * use, copy, modify, and distribute this software in source code or binary
7
 * form for use in connection with the web services and APIs provided by
8
 * Facebook.
9
 *
10
 * As with any software that integrates with the Facebook platform, your use
11
 * of this software is subject to the Facebook Developer Principles and
12
 * Policies [http://developers.facebook.com/policy/]. This copyright notice
13
 * shall be included in all copies or substantial portions of the software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * DEALINGS IN THE SOFTWARE.
22
 */
23
namespace Facebook\Tests\Exception;
24
25
use Facebook\Application;
26
use Facebook\Request;
27
use Facebook\Response;
28
use Facebook\Exception\ResponseException;
29
use Facebook\Exception\AuthenticationException;
30
use Facebook\Exception\ServerException;
31
use Facebook\Exception\ThrottleException;
32
use Facebook\Exception\AuthorizationException;
33
use Facebook\Exception\ClientException;
34
use Facebook\Exception\OtherException;
35
use PHPUnit\Framework\TestCase;
36
37
class ResponseExceptionTest extends TestCase
38
{
39
40
    /**
41
     * @var Request
42
     */
43
    protected $request;
44
45
    protected function setUp()
46
    {
47
        $this->request = new Request(new Application('123', 'foo'));
48
    }
49
50
    public function testAuthenticationExceptions()
51
    {
52
        $params = [
53
            'error' => [
54
                'code' => 100,
55
                'message' => 'errmsg',
56
                'error_subcode' => 0,
57
                'type' => 'exception'
58
            ],
59
        ];
60
61
        $response = new Response($this->request, json_encode($params), 401);
62
        $exception = ResponseException::create($response);
63
        $this->assertInstanceOf(AuthenticationException::class, $exception->getPrevious());
64
        $this->assertEquals(100, $exception->getCode());
65
        $this->assertEquals(0, $exception->getSubErrorCode());
66
        $this->assertEquals('exception', $exception->getErrorType());
67
        $this->assertEquals('errmsg', $exception->getMessage());
68
        $this->assertEquals(json_encode($params), $exception->getRawResponse());
69
        $this->assertEquals(401, $exception->getHttpStatusCode());
70
71
        $params['error']['code'] = 102;
72
        $response = new Response($this->request, json_encode($params), 401);
73
        $exception = ResponseException::create($response);
74
        $this->assertInstanceOf(AuthenticationException::class, $exception->getPrevious());
75
        $this->assertEquals(102, $exception->getCode());
76
77
        $params['error']['code'] = 190;
78
        $response = new Response($this->request, json_encode($params), 401);
79
        $exception = ResponseException::create($response);
80
        $this->assertInstanceOf(AuthenticationException::class, $exception->getPrevious());
81
        $this->assertEquals(190, $exception->getCode());
82
83
        $params['error']['type'] = 'OAuthException';
84
        $params['error']['code'] = 0;
85
        $params['error']['error_subcode'] = 458;
86
        $response = new Response($this->request, json_encode($params), 401);
87
        $exception = ResponseException::create($response);
88
        $this->assertInstanceOf(AuthenticationException::class, $exception->getPrevious());
89
        $this->assertEquals(458, $exception->getSubErrorCode());
90
91
        $params['error']['error_subcode'] = 460;
92
        $response = new Response($this->request, json_encode($params), 401);
93
        $exception = ResponseException::create($response);
94
        $this->assertInstanceOf(AuthenticationException::class, $exception->getPrevious());
95
        $this->assertEquals(460, $exception->getSubErrorCode());
96
97
        $params['error']['error_subcode'] = 463;
98
        $response = new Response($this->request, json_encode($params), 401);
99
        $exception = ResponseException::create($response);
100
        $this->assertInstanceOf(AuthenticationException::class, $exception->getPrevious());
101
        $this->assertEquals(463, $exception->getSubErrorCode());
102
103
        $params['error']['error_subcode'] = 467;
104
        $response = new Response($this->request, json_encode($params), 401);
105
        $exception = ResponseException::create($response);
106
        $this->assertInstanceOf(AuthenticationException::class, $exception->getPrevious());
107
        $this->assertEquals(467, $exception->getSubErrorCode());
108
109
        $params['error']['error_subcode'] = 0;
110
        $response = new Response($this->request, json_encode($params), 401);
111
        $exception = ResponseException::create($response);
112
        $this->assertInstanceOf(AuthenticationException::class, $exception->getPrevious());
113
        $this->assertEquals(0, $exception->getSubErrorCode());
114
    }
115
116 View Code Duplication
    public function testServerExceptions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
    {
118
        $params = [
119
            'error' => [
120
                'code' => 1,
121
                'message' => 'errmsg',
122
                'error_subcode' => 0,
123
                'type' => 'exception'
124
            ],
125
        ];
126
127
        $response = new Response($this->request, json_encode($params), 500);
128
        $exception = ResponseException::create($response);
129
        $this->assertInstanceOf(ServerException::class, $exception->getPrevious());
130
        $this->assertEquals(1, $exception->getCode());
131
        $this->assertEquals(0, $exception->getSubErrorCode());
132
        $this->assertEquals('exception', $exception->getErrorType());
133
        $this->assertEquals('errmsg', $exception->getMessage());
134
        $this->assertEquals(json_encode($params), $exception->getRawResponse());
135
        $this->assertEquals(500, $exception->getHttpStatusCode());
136
137
        $params['error']['code'] = 2;
138
        $response = new Response($this->request, json_encode($params), 500);
139
        $exception = ResponseException::create($response);
140
        $this->assertInstanceOf(ServerException::class, $exception->getPrevious());
141
        $this->assertEquals(2, $exception->getCode());
142
    }
143
144
    public function testThrottleExceptions()
145
    {
146
        $params = [
147
            'error' => [
148
                'code' => 4,
149
                'message' => 'errmsg',
150
                'error_subcode' => 0,
151
                'type' => 'exception'
152
            ],
153
        ];
154
        $response = new Response($this->request, json_encode($params), 401);
155
        $exception = ResponseException::create($response);
156
        $this->assertInstanceOf(ThrottleException::class, $exception->getPrevious());
157
        $this->assertEquals(4, $exception->getCode());
158
        $this->assertEquals(0, $exception->getSubErrorCode());
159
        $this->assertEquals('exception', $exception->getErrorType());
160
        $this->assertEquals('errmsg', $exception->getMessage());
161
        $this->assertEquals(json_encode($params), $exception->getRawResponse());
162
        $this->assertEquals(401, $exception->getHttpStatusCode());
163
164
        $params['error']['code'] = 17;
165
        $response = new Response($this->request, json_encode($params), 401);
166
        $exception = ResponseException::create($response);
167
        $this->assertInstanceOf(ThrottleException::class, $exception->getPrevious());
168
        $this->assertEquals(17, $exception->getCode());
169
170
        $params['error']['code'] = 341;
171
        $response = new Response($this->request, json_encode($params), 401);
172
        $exception = ResponseException::create($response);
173
        $this->assertInstanceOf(ThrottleException::class, $exception->getPrevious());
174
        $this->assertEquals(341, $exception->getCode());
175
    }
176
177 View Code Duplication
    public function testUserIssueExceptions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
    {
179
        $params = [
180
            'error' => [
181
                'code' => 230,
182
                'message' => 'errmsg',
183
                'error_subcode' => 459,
184
                'type' => 'exception'
185
            ],
186
        ];
187
        $response = new Response($this->request, json_encode($params), 401);
188
        $exception = ResponseException::create($response);
189
        $this->assertInstanceOf(AuthenticationException::class, $exception->getPrevious());
190
        $this->assertEquals(230, $exception->getCode());
191
        $this->assertEquals(459, $exception->getSubErrorCode());
192
        $this->assertEquals('exception', $exception->getErrorType());
193
        $this->assertEquals('errmsg', $exception->getMessage());
194
        $this->assertEquals(json_encode($params), $exception->getRawResponse());
195
        $this->assertEquals(401, $exception->getHttpStatusCode());
196
197
        $params['error']['error_subcode'] = 464;
198
        $response = new Response($this->request, json_encode($params), 401);
199
        $exception = ResponseException::create($response);
200
        $this->assertInstanceOf(AuthenticationException::class, $exception->getPrevious());
201
        $this->assertEquals(464, $exception->getSubErrorCode());
202
    }
203
204
    public function testAuthorizationExceptions()
205
    {
206
        $params = [
207
            'error' => [
208
                'code' => 10,
209
                'message' => 'errmsg',
210
                'error_subcode' => 0,
211
                'type' => 'exception'
212
            ],
213
        ];
214
        $response = new Response($this->request, json_encode($params), 401);
215
        $exception = ResponseException::create($response);
216
        $this->assertInstanceOf(AuthorizationException::class, $exception->getPrevious());
217
        $this->assertEquals(10, $exception->getCode());
218
        $this->assertEquals(0, $exception->getSubErrorCode());
219
        $this->assertEquals('exception', $exception->getErrorType());
220
        $this->assertEquals('errmsg', $exception->getMessage());
221
        $this->assertEquals(json_encode($params), $exception->getRawResponse());
222
        $this->assertEquals(401, $exception->getHttpStatusCode());
223
224
        $params['error']['code'] = 200;
225
        $response = new Response($this->request, json_encode($params), 401);
226
        $exception = ResponseException::create($response);
227
        $this->assertInstanceOf(AuthorizationException::class, $exception->getPrevious());
228
        $this->assertEquals(200, $exception->getCode());
229
230
        $params['error']['code'] = 250;
231
        $response = new Response($this->request, json_encode($params), 401);
232
        $exception = ResponseException::create($response);
233
        $this->assertInstanceOf(AuthorizationException::class, $exception->getPrevious());
234
        $this->assertEquals(250, $exception->getCode());
235
236
        $params['error']['code'] = 299;
237
        $response = new Response($this->request, json_encode($params), 401);
238
        $exception = ResponseException::create($response);
239
        $this->assertInstanceOf(AuthorizationException::class, $exception->getPrevious());
240
        $this->assertEquals(299, $exception->getCode());
241
    }
242
243 View Code Duplication
    public function testClientExceptions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
244
    {
245
        $params = [
246
            'error' => [
247
                'code' => 506,
248
                'message' => 'errmsg',
249
                'error_subcode' => 0,
250
                'type' => 'exception'
251
            ],
252
        ];
253
        $response = new Response($this->request, json_encode($params), 401);
254
        $exception = ResponseException::create($response);
255
        $this->assertInstanceOf(ClientException::class, $exception->getPrevious());
256
        $this->assertEquals(506, $exception->getCode());
257
        $this->assertEquals(0, $exception->getSubErrorCode());
258
        $this->assertEquals('exception', $exception->getErrorType());
259
        $this->assertEquals('errmsg', $exception->getMessage());
260
        $this->assertEquals(json_encode($params), $exception->getRawResponse());
261
        $this->assertEquals(401, $exception->getHttpStatusCode());
262
    }
263
264 View Code Duplication
    public function testOtherException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
265
    {
266
        $params = [
267
            'error' => [
268
                'code' => 42,
269
                'message' => 'ship love',
270
                'error_subcode' => 0,
271
                'type' => 'feature'
272
            ],
273
        ];
274
        $response = new Response($this->request, json_encode($params), 200);
275
        $exception = ResponseException::create($response);
276
        $this->assertInstanceOf(OtherException::class, $exception->getPrevious());
277
        $this->assertEquals(42, $exception->getCode());
278
        $this->assertEquals(0, $exception->getSubErrorCode());
279
        $this->assertEquals('feature', $exception->getErrorType());
280
        $this->assertEquals('ship love', $exception->getMessage());
281
        $this->assertEquals(json_encode($params), $exception->getRawResponse());
282
        $this->assertEquals(200, $exception->getHttpStatusCode());
283
    }
284
}
285