Completed
Push — master ( 76b665...536063 )
by
unknown
01:45
created

ResponseTest::test_setStatusCode()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 28
rs 8.8571
c 1
b 0
f 0
cc 3
eloc 18
nc 4
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_setStatusCode()
267
    {
268
        $responseReflect = new ReflectionClass(Response::class);
269
        $method = $responseReflect->getMethod('setStatusCode');
270
        $method->setAccessible(true);
271
272
        try {
273
            $method->invokeArgs($this->response, [99]);
274
        } catch (InvalidArgumentException $exception) {
275
            $this->assertEquals(
276
                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),
277
                $exception->getMessage()
278
            );
279
        }
280
281
        try {
282
            $method->invokeArgs($this->response, [600]);
283
        } catch (InvalidArgumentException $exception) {
284
            $this->assertEquals(
285
                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),
286
                $exception->getMessage()
287
            );
288
        }
289
290
        $method->invokeArgs($this->response, [201]);
291
292
        $this->assertEquals(201, $this->response->getStatusCode());
293
    }
294
295
    private function withError(Response $response, $code, $message = null)
296
    {
297
        $this->assertEquals($code, $response->getStatusCode());
298
        $this->assertEquals(json_encode([
299
            'error' => array_filter([
300
                'http_code' => $response->getStatusCode(),
301
                'phrase' => $response->getReasonPhrase(),
302
                'message' => $message
303
            ])
304
        ]), $response->getBody()->__toString());
305
306
    }
307
}