ResponseTest::testSuccessConversion()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Demv\JSend\Test;
4
5
use Demv\JSend\JSend;
6
use Demv\JSend\JSendResponse;
7
use Demv\JSend\ResponseFactory;
8
use Demv\JSend\Status;
9
use Demv\JSend\StatusInterface;
10
use PHPUnit\Framework\TestCase;
11
12
final class ResponseTest extends TestCase
13
{
14
    public function testDefaultHttpStatusCode(): void
15
    {
16
        $this->assertEquals(
17
            200,
18
            JSend::getDefaultHttpStatusCode(ResponseFactory::instance()->success())
19
        );
20
        $this->assertEquals(
21
            200,
22
            JSend::getDefaultHttpStatusCode(ResponseFactory::instance()->fail())
23
        );
24
        $this->assertEquals(
25
            500,
26
            JSend::getDefaultHttpStatusCode(ResponseFactory::instance()->error(['message' => 'wtf']))
27
        );
28
        $this->assertEquals(
29
            400,
30
            JSend::getDefaultHttpStatusCode(ResponseFactory::instance()->error(['message' => 'wtf', 'code' => 400]))
31
        );
32
    }
33
34 View Code Duplication
    public function testSuccessFactory(): void
35
    {
36
        $response = JSendResponse::success(['Erfolgreich!']);
37
38
        $this->assertTrue($response->getStatus()->isSuccess());
39
        $this->assertEquals(['Erfolgreich!'], $response->getData());
40
    }
41
42 View Code Duplication
    public function testFailFactory(): void
43
    {
44
        $response = JSendResponse::fail(['Irgendwas lief schief']);
45
46
        $this->assertTrue($response->getStatus()->isFail());
47
        $this->assertEquals(['Irgendwas lief schief'], $response->getData());
48
    }
49
50
    public function testErrorFactory(): void
51
    {
52
        $response = JSendResponse::error('Es ist ein Fehler aufgetreten');
53
54
        $this->assertTrue($response->getStatus()->isError());
55
        $this->assertEmpty($response->getData());
56
        $this->assertEquals(
57
            'Es ist ein Fehler aufgetreten',
58
            $response->getError()->getMessage()
59
        );
60
    }
61
62
    public function testSuccessConversion(): void
63
    {
64
        $json = '{"status": "success", "data": ["Holy", "Moly"]}';
65
66
        $success = new DummyResponse();
67
        $success->withBody(new DummyStream($json));
68
        $success->withStatus(214);
69
70
        $response = ResponseFactory::instance()->convert($success);
71
        $this->assertTrue($response->getStatus()->isSuccess());
72
        $this->assertEquals(['Holy', 'Moly'], $response->getData());
73
        $this->assertJsonStringEqualsJsonString($json, json_encode($response));
74
    }
75
76
    public function testFailConversion(): void
77
    {
78
        $fail = new DummyResponse();
79
        $fail->withBody(new DummyStream('{"status": "fail", "data": null}'));
80
81
        $response = ResponseFactory::instance()->convert($fail);
82
        $this->assertTrue($response->getStatus()->isFail());
83
        $this->assertEmpty($response->getData());
84
        $this->assertJsonStringEqualsJsonString('{"status": "fail", "data": null}', json_encode($response));
85
    }
86
87
    public function testErrorConversion(): void
88
    {
89
        $json = '{"status": "error", "data": ["Invalid"], "message": "Something is not right..."}';
90
91
        $error = new DummyResponse();
92
        $error->withBody(new DummyStream($json));
93
        $error->withStatus(501);
94
95
        $result         = json_decode($json, true);
96
        $result['code'] = $error->getStatusCode();
97
98
        $response = ResponseFactory::instance()->convert($error);
99
        $this->assertTrue($response->getStatus()->isError());
100
        $this->assertEquals(['Invalid'], $response->getData());
101
        $this->assertJsonStringEqualsJsonString(json_encode($result), json_encode($response));
102
        $this->assertEquals('Something is not right...', $response->getError()->getMessage());
103
        $this->assertEquals($error->getStatusCode(), $response->getError()->getCode());
104
    }
105
106
    public function testMapping(): void
107
    {
108
        $response = new JSendResponse(Status::translate(1), null);
109
        $this->assertTrue($response->getStatus()->isSuccess());
110
111
        $response = new JSendResponse(Status::translate(0), null);
112
        $this->assertTrue($response->getStatus()->isFail());
113
114
        $response = new JSendResponse(Status::translate(-1), null);
115
        $this->assertTrue($response->getStatus()->isError());
116
117
        $response = new JSendResponse(Status::translate(true), null);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

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...
118
        $this->assertTrue($response->getStatus()->isSuccess());
119
120
        $response = new JSendResponse(Status::translate(false), null);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a integer.

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...
121
        $this->assertTrue($response->getStatus()->isFail());
122
123
        $response = new JSendResponse(Status::translate(false, [false => StatusInterface::STATUS_ERROR]), null);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a integer.

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...
124
        $this->assertTrue($response->getStatus()->isError());
125
    }
126
127
    public function testPsr7Response(): void
128
    {
129
        $response = JSendResponse::success(['Erfolgreich!'])->asResponse(200);
130
        $this->assertEquals(200, $response->getStatusCode());
131
        $this->assertEquals('{"status":"success","data":["Erfolgreich!"]}', $response->getBody()->getContents());
132
133
        $response = JSendResponse::fail(['Irgendwas lief schief'])->asResponse(400);
134
        $this->assertEquals(400, $response->getStatusCode());
135
        $this->assertEquals('{"status":"fail","data":["Irgendwas lief schief"]}', $response->getBody()->getContents());
136
137
        $response = JSendResponse::error('Es ist ein Fehler aufgetreten', 404)->asResponse(500);
138
        $this->assertEquals(500, $response->getStatusCode());
139
        $this->assertEquals(
140
            '{"status":"error","message":"Es ist ein Fehler aufgetreten","code":404}',
141
            $response->getBody()->getContents()
142
        );
143
144
        $response = JSendResponse::success(['Stimmt der Header?'])->asResponse();
145
        $this->assertEquals(['application/json'], $response->getHeader('content-type'));
146
147
        $response = JSendResponse::success(['Eigene Header werden übernommen'])->asResponse(null, ['foo' => 'bar']);
148
        $this->assertEquals(['bar'], $response->getHeader('foo'));
149
    }
150
151
    public function testPsr7ResponseOptionalCode(): void
152
    {
153
        $response = JSendResponse::success(['Erfolgreich!'])->asResponse();
154
        $this->assertEquals(200, $response->getStatusCode());
155
        $this->assertEquals('{"status":"success","data":["Erfolgreich!"]}', $response->getBody()->getContents());
156
157
        $response = JSendResponse::fail(['Irgendwas lief schief'])->asResponse();
158
        $this->assertEquals(200, $response->getStatusCode());
159
        $this->assertEquals('{"status":"fail","data":["Irgendwas lief schief"]}', $response->getBody()->getContents());
160
161
        $response = JSendResponse::error('Es ist ein Fehler aufgetreten', 404)->asResponse();
162
        $this->assertEquals(404, $response->getStatusCode());
163
        $this->assertEquals(
164
            '{"status":"error","message":"Es ist ein Fehler aufgetreten","code":404}',
165
            $response->getBody()->getContents()
166
        );
167
168
        $response = JSendResponse::error('Es ist ein Fehler aufgetreten')->asResponse();
169
        $this->assertEquals(500, $response->getStatusCode());
170
        $this->assertEquals(
171
            '{"status":"error","message":"Es ist ein Fehler aufgetreten"}',
172
            $response->getBody()->getContents()
173
        );
174
    }
175
}
176