Passed
Pull Request — master (#888)
by Tobias
01:50
created

ResponseExceptionTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 246
Duplicated Lines 37.4 %

Importance

Changes 0
Metric Value
dl 92
loc 246
rs 10
c 0
b 0
f 0
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testAuthenticationExceptions() 0 64 1
B testUserIssueExceptions() 25 25 1
A setUp() 0 3 1
B testAuthorizationExceptions() 0 37 1
B testServerExceptions() 26 26 1
A testClientExceptions() 19 19 1
A testOtherException() 19 19 1
B testThrottleExceptions() 0 31 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
 */
24
namespace Facebook\Tests\Exception;
25
26
use Facebook\Application;
27
use Facebook\Request;
28
use Facebook\Response;
29
use Facebook\Exception\ResponseException;
30
use Facebook\Exception\AuthenticationException;
31
use Facebook\Exception\ServerException;
32
use Facebook\Exception\ThrottleException;
33
use Facebook\Exception\AuthorizationException;
34
use Facebook\Exception\ClientException;
35
use Facebook\Exception\OtherException;
36
use PHPUnit\Framework\TestCase;
37
38
class ResponseExceptionTest extends TestCase
39
{
40
41
    /**
42
     * @var Request
43
     */
44
    protected $request;
45
46
    protected function setUp()
47
    {
48
        $this->request = new Request(new Application('123', 'foo'));
49
    }
50
51
    public function testAuthenticationExceptions()
52
    {
53
        $params = [
54
            'error' => [
55
                'code' => 100,
56
                'message' => 'errmsg',
57
                'error_subcode' => 0,
58
                'type' => 'exception'
59
            ],
60
        ];
61
62
        $response = new Response($this->request, json_encode($params), 401);
63
        $exception = ResponseException::create($response);
64
        $this->assertInstanceOf(AuthenticationException::class, $exception->getPrevious());
65
        $this->assertEquals(100, $exception->getCode());
66
        $this->assertEquals(0, $exception->getSubErrorCode());
67
        $this->assertEquals('exception', $exception->getErrorType());
68
        $this->assertEquals('errmsg', $exception->getMessage());
69
        $this->assertEquals(json_encode($params), $exception->getRawResponse());
70
        $this->assertEquals(401, $exception->getHttpStatusCode());
71
72
        $params['error']['code'] = 102;
73
        $response = new Response($this->request, json_encode($params), 401);
74
        $exception = ResponseException::create($response);
75
        $this->assertInstanceOf(AuthenticationException::class, $exception->getPrevious());
76
        $this->assertEquals(102, $exception->getCode());
77
78
        $params['error']['code'] = 190;
79
        $response = new Response($this->request, json_encode($params), 401);
80
        $exception = ResponseException::create($response);
81
        $this->assertInstanceOf(AuthenticationException::class, $exception->getPrevious());
82
        $this->assertEquals(190, $exception->getCode());
83
84
        $params['error']['type'] = 'OAuthException';
85
        $params['error']['code'] = 0;
86
        $params['error']['error_subcode'] = 458;
87
        $response = new Response($this->request, json_encode($params), 401);
88
        $exception = ResponseException::create($response);
89
        $this->assertInstanceOf(AuthenticationException::class, $exception->getPrevious());
90
        $this->assertEquals(458, $exception->getSubErrorCode());
91
92
        $params['error']['error_subcode'] = 460;
93
        $response = new Response($this->request, json_encode($params), 401);
94
        $exception = ResponseException::create($response);
95
        $this->assertInstanceOf(AuthenticationException::class, $exception->getPrevious());
96
        $this->assertEquals(460, $exception->getSubErrorCode());
97
98
        $params['error']['error_subcode'] = 463;
99
        $response = new Response($this->request, json_encode($params), 401);
100
        $exception = ResponseException::create($response);
101
        $this->assertInstanceOf(AuthenticationException::class, $exception->getPrevious());
102
        $this->assertEquals(463, $exception->getSubErrorCode());
103
104
        $params['error']['error_subcode'] = 467;
105
        $response = new Response($this->request, json_encode($params), 401);
106
        $exception = ResponseException::create($response);
107
        $this->assertInstanceOf(AuthenticationException::class, $exception->getPrevious());
108
        $this->assertEquals(467, $exception->getSubErrorCode());
109
110
        $params['error']['error_subcode'] = 0;
111
        $response = new Response($this->request, json_encode($params), 401);
112
        $exception = ResponseException::create($response);
113
        $this->assertInstanceOf(AuthenticationException::class, $exception->getPrevious());
114
        $this->assertEquals(0, $exception->getSubErrorCode());
115
    }
116
117 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...
118
    {
119
        $params = [
120
            'error' => [
121
                'code' => 1,
122
                'message' => 'errmsg',
123
                'error_subcode' => 0,
124
                'type' => 'exception'
125
            ],
126
        ];
127
128
        $response = new Response($this->request, json_encode($params), 500);
129
        $exception = ResponseException::create($response);
130
        $this->assertInstanceOf(ServerException::class, $exception->getPrevious());
131
        $this->assertEquals(1, $exception->getCode());
132
        $this->assertEquals(0, $exception->getSubErrorCode());
133
        $this->assertEquals('exception', $exception->getErrorType());
134
        $this->assertEquals('errmsg', $exception->getMessage());
135
        $this->assertEquals(json_encode($params), $exception->getRawResponse());
136
        $this->assertEquals(500, $exception->getHttpStatusCode());
137
138
        $params['error']['code'] = 2;
139
        $response = new Response($this->request, json_encode($params), 500);
140
        $exception = ResponseException::create($response);
141
        $this->assertInstanceOf(ServerException::class, $exception->getPrevious());
142
        $this->assertEquals(2, $exception->getCode());
143
    }
144
145
    public function testThrottleExceptions()
146
    {
147
        $params = [
148
            'error' => [
149
                'code' => 4,
150
                'message' => 'errmsg',
151
                'error_subcode' => 0,
152
                'type' => 'exception'
153
            ],
154
        ];
155
        $response = new Response($this->request, json_encode($params), 401);
156
        $exception = ResponseException::create($response);
157
        $this->assertInstanceOf(ThrottleException::class, $exception->getPrevious());
158
        $this->assertEquals(4, $exception->getCode());
159
        $this->assertEquals(0, $exception->getSubErrorCode());
160
        $this->assertEquals('exception', $exception->getErrorType());
161
        $this->assertEquals('errmsg', $exception->getMessage());
162
        $this->assertEquals(json_encode($params), $exception->getRawResponse());
163
        $this->assertEquals(401, $exception->getHttpStatusCode());
164
165
        $params['error']['code'] = 17;
166
        $response = new Response($this->request, json_encode($params), 401);
167
        $exception = ResponseException::create($response);
168
        $this->assertInstanceOf(ThrottleException::class, $exception->getPrevious());
169
        $this->assertEquals(17, $exception->getCode());
170
171
        $params['error']['code'] = 341;
172
        $response = new Response($this->request, json_encode($params), 401);
173
        $exception = ResponseException::create($response);
174
        $this->assertInstanceOf(ThrottleException::class, $exception->getPrevious());
175
        $this->assertEquals(341, $exception->getCode());
176
    }
177
178 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...
179
    {
180
        $params = [
181
            'error' => [
182
                'code' => 230,
183
                'message' => 'errmsg',
184
                'error_subcode' => 459,
185
                'type' => 'exception'
186
            ],
187
        ];
188
        $response = new Response($this->request, json_encode($params), 401);
189
        $exception = ResponseException::create($response);
190
        $this->assertInstanceOf(AuthenticationException::class, $exception->getPrevious());
191
        $this->assertEquals(230, $exception->getCode());
192
        $this->assertEquals(459, $exception->getSubErrorCode());
193
        $this->assertEquals('exception', $exception->getErrorType());
194
        $this->assertEquals('errmsg', $exception->getMessage());
195
        $this->assertEquals(json_encode($params), $exception->getRawResponse());
196
        $this->assertEquals(401, $exception->getHttpStatusCode());
197
198
        $params['error']['error_subcode'] = 464;
199
        $response = new Response($this->request, json_encode($params), 401);
200
        $exception = ResponseException::create($response);
201
        $this->assertInstanceOf(AuthenticationException::class, $exception->getPrevious());
202
        $this->assertEquals(464, $exception->getSubErrorCode());
203
    }
204
205
    public function testAuthorizationExceptions()
206
    {
207
        $params = [
208
            'error' => [
209
                'code' => 10,
210
                'message' => 'errmsg',
211
                'error_subcode' => 0,
212
                'type' => 'exception'
213
            ],
214
        ];
215
        $response = new Response($this->request, json_encode($params), 401);
216
        $exception = ResponseException::create($response);
217
        $this->assertInstanceOf(AuthorizationException::class, $exception->getPrevious());
218
        $this->assertEquals(10, $exception->getCode());
219
        $this->assertEquals(0, $exception->getSubErrorCode());
220
        $this->assertEquals('exception', $exception->getErrorType());
221
        $this->assertEquals('errmsg', $exception->getMessage());
222
        $this->assertEquals(json_encode($params), $exception->getRawResponse());
223
        $this->assertEquals(401, $exception->getHttpStatusCode());
224
225
        $params['error']['code'] = 200;
226
        $response = new Response($this->request, json_encode($params), 401);
227
        $exception = ResponseException::create($response);
228
        $this->assertInstanceOf(AuthorizationException::class, $exception->getPrevious());
229
        $this->assertEquals(200, $exception->getCode());
230
231
        $params['error']['code'] = 250;
232
        $response = new Response($this->request, json_encode($params), 401);
233
        $exception = ResponseException::create($response);
234
        $this->assertInstanceOf(AuthorizationException::class, $exception->getPrevious());
235
        $this->assertEquals(250, $exception->getCode());
236
237
        $params['error']['code'] = 299;
238
        $response = new Response($this->request, json_encode($params), 401);
239
        $exception = ResponseException::create($response);
240
        $this->assertInstanceOf(AuthorizationException::class, $exception->getPrevious());
241
        $this->assertEquals(299, $exception->getCode());
242
    }
243
244 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...
245
    {
246
        $params = [
247
            'error' => [
248
                'code' => 506,
249
                'message' => 'errmsg',
250
                'error_subcode' => 0,
251
                'type' => 'exception'
252
            ],
253
        ];
254
        $response = new Response($this->request, json_encode($params), 401);
255
        $exception = ResponseException::create($response);
256
        $this->assertInstanceOf(ClientException::class, $exception->getPrevious());
257
        $this->assertEquals(506, $exception->getCode());
258
        $this->assertEquals(0, $exception->getSubErrorCode());
259
        $this->assertEquals('exception', $exception->getErrorType());
260
        $this->assertEquals('errmsg', $exception->getMessage());
261
        $this->assertEquals(json_encode($params), $exception->getRawResponse());
262
        $this->assertEquals(401, $exception->getHttpStatusCode());
263
    }
264
265 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...
266
    {
267
        $params = [
268
            'error' => [
269
                'code' => 42,
270
                'message' => 'ship love',
271
                'error_subcode' => 0,
272
                'type' => 'feature'
273
            ],
274
        ];
275
        $response = new Response($this->request, json_encode($params), 200);
276
        $exception = ResponseException::create($response);
277
        $this->assertInstanceOf(OtherException::class, $exception->getPrevious());
278
        $this->assertEquals(42, $exception->getCode());
279
        $this->assertEquals(0, $exception->getSubErrorCode());
280
        $this->assertEquals('feature', $exception->getErrorType());
281
        $this->assertEquals('ship love', $exception->getMessage());
282
        $this->assertEquals(json_encode($params), $exception->getRawResponse());
283
        $this->assertEquals(200, $exception->getHttpStatusCode());
284
    }
285
}
286