Completed
Push — master ( e06013...6acaca )
by
unknown
02:06
created

test_setStatusCode_greater_than_max_status_code()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 11
loc 11
rs 9.4285
cc 2
eloc 7
nc 2
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_errorNotFound()
77
    {
78
        $code = 404;
79
        $message = '';
80
        $this->withError(
81
            $this->response->errorNotFound($message),
82
            $code,
83
            $message
84
        );
85
    }
86
87
    public function test_errorNotFound_with_message()
88
    {
89
        $code = 404;
90
        $message = 'go back to home page';
91
        $this->withError(
92
            $this->response->errorNotFound($message),
93
            $code,
94
            $message
95
        );
96
    }
97
98
    public function test_errorForbidden()
99
    {
100
        $code = 403;
101
        $message = '';
102
        $this->withError(
103
            $this->response->errorForbidden($message),
104
            $code,
105
            $message
106
        );
107
    }
108
109
    public function test_errorForbidden_with_message()
110
    {
111
        $code = 403;
112
        $message = 'forbid to access this';
113
        $this->withError(
114
            $this->response->errorForbidden($message),
115
            $code,
116
            $message
117
        );
118
    }
119
120
    public function test_errorInternalError()
121
    {
122
        $code = 500;
123
        $message = '';
124
        $this->withError(
125
            $this->response->errorInternalError($message),
126
            $code,
127
            $message
128
        );
129
    }
130
131
    public function test_errorInternalError_with_message()
132
    {
133
        $code = 500;
134
        $message = 'something wrong';
135
        $this->withError(
136
            $this->response->errorInternalError($message),
137
            $code,
138
            $message
139
        );
140
    }
141
142
    public function test_errorUnauthorized()
143
    {
144
        $code = 401;
145
        $message = '';
146
        $this->withError(
147
            $this->response->errorUnauthorized($message),
148
            $code,
149
            $message
150
        );
151
    }
152
153
    public function test_errorUnauthorized_with_message()
154
    {
155
        $code = 401;
156
        $message = 'access token required';
157
        $this->withError(
158
            $this->response->errorUnauthorized($message),
159
            $code,
160
            $message
161
        );
162
    }
163
164
    public function test_errorWrongArgs()
165
    {
166
        $code = 400;
167
        $message = [
168
            'username' => 'required',
169
            'password' => 'required'
170
        ];
171
        $this->withError(
172
            $this->response->errorWrongArgs($message),
173
            $code,
174
            $message
175
        );
176
    }
177
178
    public function test_errorGone()
179
    {
180
        $code = 410;
181
        $message = '';
182
        $this->withError(
183
            $this->response->errorGone($message),
184
            $code,
185
            $message
186
        );
187
    }
188
189
    public function test_errorGone_with_message()
190
    {
191
        $code = 410;
192
        $message = 'mysql gone away';
193
        $this->withError(
194
            $this->response->errorGone($message),
195
            $code,
196
            $message
197
        );
198
    }
199
200
    public function test_errorMethodNotAllowed()
201
    {
202
        $code = 405;
203
        $message = '';
204
        $this->withError(
205
            $this->response->errorMethodNotAllowed($message),
206
            $code,
207
            $message
208
        );
209
    }
210
211
    public function test_errorMethodNotAllowed_with_message()
212
    {
213
        $code = 405;
214
        $message = 'GET method is not allowed for this endpoint';
215
        $this->withError(
216
            $this->response->errorMethodNotAllowed($message),
217
            $code,
218
            $message
219
        );
220
    }
221
222
    public function test_errorUnwillingToProcess()
223
    {
224
        $code = 431;
225
        $message = '';
226
        $this->withError(
227
            $this->response->errorUnwillingToProcess($message),
228
            $code,
229
            $message
230
        );
231
    }
232
233
    public function test_errorUnwillingToProcess_with_message()
234
    {
235
        $code = 431;
236
        $message = 'Request size is too big';
237
        $this->withError(
238
            $this->response->errorUnwillingToProcess($message),
239
            $code,
240
            $message
241
        );
242
    }
243
244
    public function test_errorUnprocessable()
245
    {
246
        $code = 422;
247
        $message = '';
248
        $this->withError(
249
            $this->response->errorUnprocessable($message),
250
            $code,
251
            $message
252
        );
253
    }
254
255
    public function test_errorUnprocessable_with_message()
256
    {
257
        $code = 422;
258
        $message = 'Your request cannot be processed';
259
        $this->withError(
260
            $this->response->errorUnprocessable($message),
261
            $code,
262
            $message
263
        );
264
    }
265
266
    public function test_withStatus()
267
    {
268
        $this->response->withStatus(200);
269
        $this->assertEquals(200, $this->response->getStatusCode());
270
    }
271
272 View Code Duplication
    public function test_setStatusCode_less_than_min_status_code()
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...
273
    {
274
        try {
275
            $this->getMethodSetStatusCode()->invokeArgs($this->response, [99]);
276
        } catch (InvalidArgumentException $exception) {
277
            $this->assertEquals(
278
                sprintf('Invalid status code "%s"; must be an integer between %d and %d, inclusive', 99, Response::MIN_STATUS_CODE_VALUE, Response::MAX_STATUS_CODE_VALUE),
279
                $exception->getMessage()
280
            );
281
        }
282
    }
283
284 View Code Duplication
    public function test_setStatusCode_greater_than_max_status_code()
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...
285
    {
286
        try {
287
            $this->getMethodSetStatusCode()->invokeArgs($this->response, [600]);
288
        } catch (InvalidArgumentException $exception) {
289
            $this->assertEquals(
290
                sprintf('Invalid status code "%s"; must be an integer between %d and %d, inclusive', 600, Response::MIN_STATUS_CODE_VALUE, Response::MAX_STATUS_CODE_VALUE),
291
                $exception->getMessage()
292
            );
293
        }
294
    }
295
296
    public function test_setStatusCode()
297
    {
298
        $this->getMethodSetStatusCode()->invokeArgs($this->response, [201]);
299
        $this->assertEquals(201, $this->response->getStatusCode());
300
    }
301
302
    private function getMethodSetStatusCode()
303
    {
304
        $responseReflect = new ReflectionClass(Response::class);
305
        $method = $responseReflect->getMethod('setStatusCode');
306
        $method->setAccessible(true);
307
        return $method;
308
    }
309
310
311
    private function withError(Response $response, $code, $message = null)
312
    {
313
        $this->assertEquals($code, $response->getStatusCode());
314
        $this->assertEquals(json_encode([
315
            'error' => array_filter([
316
                'http_code' => $response->getStatusCode(),
317
                'phrase' => $response->getReasonPhrase(),
318
                'message' => $message
319
            ])
320
        ]), $response->getBody()->__toString());
321
322
    }
323
}