Passed
Push — master ( 29fb57...ab4d12 )
by
unknown
28s
created

test_withError_with_param_errorCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 12
c 1
b 0
f 1
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: harry
5
 * Date: 2/14/18
6
 * Time: 12:02 PM
7
 */
8
9
namespace PhpRestfulApiResponse\Tests\unit;
10
11
use PhpRestfulApiResponse\Response;
12
use PhpRestfulApiResponse\Tests\unit\Lib\Book;
13
use ReflectionClass;
14
use InvalidArgumentException;
15
16
class ResponseTest extends Base
17
{
18
    /**
19
     * @var Response
20
     */
21
    private $response;
22
23
    public function setUp()
24
    {
25
        parent::setUp(); // TODO: Change the autogenerated stub
26
        $this->response = new Response();
27
    }
28
29
    public function test_withArray()
30
    {
31
        /** @var Response $response */
32
        $response = $this->response->withArray(['status' => 'success'], 200);
33
        $this->assertEquals(200, $response->getStatusCode());
34
        $this->assertEquals('{"status":"success"}', $response->getBody()->__toString());
35
    }
36
37
    public function test_withItem()
38
    {
39
        /** @var Response $response */
40
        $response = $this->response->withItem(
41
            new Book('harry', 'harryosmarsitohang', 'how to be a ninja', 100000, 2017),
42
            new \PhpRestfulApiResponse\Tests\unit\Lib\Transformer\Book,
0 ignored issues
show
Documentation introduced by
new \PhpRestfulApiRespon...\Lib\Transformer\Book() is of type object<PhpRestfulApiResp...t\Lib\Transformer\Book>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
            200
44
        );
45
        $this->assertEquals(200, $response->getStatusCode());
46
        $this->assertEquals('{"data":{"title":"how to be a ninja","author":{"name":"harry","email":"harryosmarsitohang"},"year":2017,"price":100000}}', $response->getBody()->__toString());
47
    }
48
49
    public function test_withCollection()
50
    {
51
        /** @var Response $response */
52
        $response = $this->response->withCollection(
53
            [
54
                new Book('harry', 'harryosmarsitohang', 'how to be a ninja', 100000, 2017),
55
                new Book('harry', 'harryosmarsitohang', 'how to be a mage', 500000, 2016),
56
                new Book('harry', 'harryosmarsitohang', 'how to be a samurai', 25000, 2000),
57
            ],
58
            new \PhpRestfulApiResponse\Tests\unit\Lib\Transformer\Book,
0 ignored issues
show
Documentation introduced by
new \PhpRestfulApiRespon...\Lib\Transformer\Book() is of type object<PhpRestfulApiResp...t\Lib\Transformer\Book>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
            200
60
        );
61
        $this->assertEquals(200, $response->getStatusCode());
62
        $this->assertEquals('{"data":[{"title":"how to be a ninja","author":{"name":"harry","email":"harryosmarsitohang"},"year":2017,"price":100000},{"title":"how to be a mage","author":{"name":"harry","email":"harryosmarsitohang"},"year":2016,"price":500000},{"title":"how to be a samurai","author":{"name":"harry","email":"harryosmarsitohang"},"year":2000,"price":25000}]}', $response->getBody()->__toString());
63
    }
64
65
    public function test_withError()
66
    {
67
        $code = 400;
68
        $message = 'error occured';
69
        $this->withError(
70
            $this->response->withError($message, $code),
71
            $code,
72
            $message
73
        );
74
    }
75
76
    public function test_withError_with_param_errorCode()
77
    {
78
        $code = 400;
79
        $message = 'error occured';
80
        $errorCode = 'ERROR-CODE';
81
        $this->withError(
82
            $this->response->withError($message, $code, $errorCode),
83
            $code,
84
            $message,
85
            $errorCode
86
        );
87
    }
88
89
    public function test_errorNotFound()
90
    {
91
        $code = 404;
92
        $message = '';
93
        $this->withError(
94
            $this->response->errorNotFound($message),
95
            $code,
96
            $message
97
        );
98
    }
99
100
    public function test_errorNotFound_with_message()
101
    {
102
        $code = 404;
103
        $message = 'go back to home page';
104
        $this->withError(
105
            $this->response->errorNotFound($message),
106
            $code,
107
            $message
108
        );
109
    }
110
111
    public function test_errorForbidden()
112
    {
113
        $code = 403;
114
        $message = '';
115
        $this->withError(
116
            $this->response->errorForbidden($message),
117
            $code,
118
            $message
119
        );
120
    }
121
122
    public function test_errorForbidden_with_message()
123
    {
124
        $code = 403;
125
        $message = 'forbid to access this';
126
        $this->withError(
127
            $this->response->errorForbidden($message),
128
            $code,
129
            $message
130
        );
131
    }
132
133
    public function test_errorInternalError()
134
    {
135
        $code = 500;
136
        $message = '';
137
        $this->withError(
138
            $this->response->errorInternalError($message),
139
            $code,
140
            $message
141
        );
142
    }
143
144
    public function test_errorInternalError_with_message()
145
    {
146
        $code = 500;
147
        $message = 'something wrong';
148
        $this->withError(
149
            $this->response->errorInternalError($message),
150
            $code,
151
            $message
152
        );
153
    }
154
155
    public function test_errorUnauthorized()
156
    {
157
        $code = 401;
158
        $message = '';
159
        $this->withError(
160
            $this->response->errorUnauthorized($message),
161
            $code,
162
            $message
163
        );
164
    }
165
166
    public function test_errorUnauthorized_with_message()
167
    {
168
        $code = 401;
169
        $message = 'access token required';
170
        $this->withError(
171
            $this->response->errorUnauthorized($message),
172
            $code,
173
            $message
174
        );
175
    }
176
177
    public function test_errorWrongArgs()
178
    {
179
        $code = 400;
180
        $message = [
181
            'username' => 'required',
182
            'password' => 'required'
183
        ];
184
        $this->withError(
185
            $this->response->errorWrongArgs($message),
186
            $code,
187
            $message
188
        );
189
    }
190
191
    public function test_errorGone()
192
    {
193
        $code = 410;
194
        $message = '';
195
        $this->withError(
196
            $this->response->errorGone($message),
197
            $code,
198
            $message
199
        );
200
    }
201
202
    public function test_errorGone_with_message()
203
    {
204
        $code = 410;
205
        $message = 'mysql gone away';
206
        $this->withError(
207
            $this->response->errorGone($message),
208
            $code,
209
            $message
210
        );
211
    }
212
213
    public function test_errorMethodNotAllowed()
214
    {
215
        $code = 405;
216
        $message = '';
217
        $this->withError(
218
            $this->response->errorMethodNotAllowed($message),
219
            $code,
220
            $message
221
        );
222
    }
223
224
    public function test_errorMethodNotAllowed_with_message()
225
    {
226
        $code = 405;
227
        $message = 'GET method is not allowed for this endpoint';
228
        $this->withError(
229
            $this->response->errorMethodNotAllowed($message),
230
            $code,
231
            $message
232
        );
233
    }
234
235
    public function test_errorUnwillingToProcess()
236
    {
237
        $code = 431;
238
        $message = '';
239
        $this->withError(
240
            $this->response->errorUnwillingToProcess($message),
241
            $code,
242
            $message
243
        );
244
    }
245
246
    public function test_errorUnwillingToProcess_with_message()
247
    {
248
        $code = 431;
249
        $message = 'Request size is too big';
250
        $this->withError(
251
            $this->response->errorUnwillingToProcess($message),
252
            $code,
253
            $message
254
        );
255
    }
256
257
    public function test_errorUnprocessable()
258
    {
259
        $code = 422;
260
        $message = '';
261
        $this->withError(
262
            $this->response->errorUnprocessable($message),
263
            $code,
264
            $message
265
        );
266
    }
267
268
    public function test_errorUnprocessable_with_message()
269
    {
270
        $code = 422;
271
        $message = 'Your request cannot be processed';
272
        $this->withError(
273
            $this->response->errorUnprocessable($message),
274
            $code,
275
            $message
276
        );
277
    }
278
279
    public function test_withStatus()
280
    {
281
        $this->response->withStatus(200);
282
        $this->assertEquals(200, $this->response->getStatusCode());
283
    }
284
285
    public function test_setStatusCode_less_than_min_status_code()
286
    {
287
        $this->run_setStatusCode($this->getMethodSetStatusCode(), 99);
288
    }
289
290
    public function test_setStatusCode_greater_than_max_status_code()
291
    {
292
        $this->run_setStatusCode($this->getMethodSetStatusCode(), 600);
293
    }
294
295
    public function test_setStatusCode()
296
    {
297
        $this->run_setStatusCode($this->getMethodSetStatusCode(), 200);
298
    }
299
300
    public function test_setErrorCode()
301
    {
302
        $this->run_setErrorCode($this->getMethodSetErrorCode(), "ERROR-CODE");
303
    }
304
305
    private function run_setStatusCode(\ReflectionMethod $method, $code)
306
    {
307
        try {
308
            $method->invokeArgs($this->response, [$code]);
309
            $this->assertEquals($code, $this->response->getStatusCode());
310
        } catch (InvalidArgumentException $exception) {
311
            $this->assertEquals(
312
                sprintf('Invalid status code "%s"; must be an integer between %d and %d, inclusive', $code, Response::MIN_STATUS_CODE_VALUE, Response::MAX_STATUS_CODE_VALUE),
313
                $exception->getMessage()
314
            );
315
        }
316
    }
317
318
    private function run_setErrorCode(\ReflectionMethod $method, $code)
319
    {
320
        $method->invokeArgs($this->response, [$code]);
321
        $this->assertEquals($code, $this->response->getErrorCode());
322
    }
323
324 View Code Duplication
    private function getMethodSetErrorCode()
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...
325
    {
326
        $responseReflect = new ReflectionClass(Response::class);
327
        $method = $responseReflect->getMethod('setErrorCode');
328
        $method->setAccessible(true);
329
        return $method;
330
    }
331
332 View Code Duplication
    private function getMethodSetStatusCode()
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...
333
    {
334
        $responseReflect = new ReflectionClass(Response::class);
335
        $method = $responseReflect->getMethod('setStatusCode');
336
        $method->setAccessible(true);
337
        return $method;
338
    }
339
340
341
    private function withError(Response $response, $code, $message = null, $errorCode = null)
342
    {
343
        $this->assertEquals($code, $response->getStatusCode());
344
        $this->assertEquals($errorCode, $response->getErrorCode());
345
346
        $this->assertEquals(json_encode([
347
            'error' => array_filter([
348
                'http_code' => $response->getStatusCode(),
349
                'code' => $response->getErrorCode(),
350
                'phrase' => $response->getReasonPhrase(),
351
                'message' => $message
352
            ])
353
        ]), $response->getBody()->__toString());
354
355
    }
356
}