JSendBasicTest::testFailResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Demv\JSend\Test;
4
5
use BadMethodCallException;
6
use Demv\JSend\JSend;
7
use Demv\JSend\Status;
8
use Dgame\Ensurance\Exception\EnsuranceException;
9
use PHPUnit\Framework\TestCase;
10
11
final class JSendBasicTest extends TestCase
12
{
13
    public function testEncodeSuccess(): void
14
    {
15
        $json = '{
16
            "status" : "success",
17
            "data" : {
18
                "post" : {
19
                    "id" : 1,
20
                    "title" : "A blog post",
21
                    "body" : "Some useful content"
22
                }
23
            }
24
        }';
25
26
        $result = JSend::encode(
27
            [
28
                'status' => 'success',
29
                'data'   => [
30
                    'post' => [
31
                        'id'    => 1,
32
                        'title' => 'A blog post',
33
                        'body'  => 'Some useful content'
34
                    ]
35
                ]
36
            ]
37
        );
38
39
        $this->assertJsonStringEqualsJsonString($json, $result);
40
    }
41
42
    public function testEncodeError(): void
43
    {
44
        $json = '{
45
            "status" : "error",
46
            "message" : "Unable to communicate with database"
47
        }';
48
49
        $result = JSend::encode(['status' => 'error', 'message' => 'Unable to communicate with database']);
50
        $this->assertJsonStringEqualsJsonString($json, $result);
51
    }
52
53
    public function testEncodeErrorWithoutMessage(): void
54
    {
55
        $this->expectException(EnsuranceException::class);
56
        $this->expectExceptionMessage('Need a descriptive error-message');
57
58
        JSend::encode(['status' => 'error']);
59
    }
60
61
    public function testEncodeEmpty(): void
62
    {
63
        $this->expectException(EnsuranceException::class);
64
        $this->expectExceptionMessage('Empty response cannot be converted to valid JSend-JSON');
65
66
        JSend::encode([]);
67
    }
68
69
    public function testEncodeWithoutStatus(): void
70
    {
71
        $this->expectException(EnsuranceException::class);
72
        $this->expectExceptionMessage('Key "status" is required');
73
74
        JSend::encode(['msg' => 'Foo']);
75
    }
76
77
    public function testSuccessResponse(): void
78
    {
79
        $json = '{
80
            "status" : "success",
81
            "data" : {
82
                "post" : {
83
                    "id" : 2,
84
                    "title" : "Another blog post",
85
                    "body" : "More content"
86
                }
87
            }
88
        }';
89
90
        $response = JSend::decode($json);
91
        $this->assertTrue($response->getStatus()->isSuccess());
92
        $this->assertFalse($response->getStatus()->isFail());
93
        $this->assertFalse($response->getStatus()->isError());
94
        $this->assertNotEmpty($response->getData());
95
        $this->assertEquals(
96
            [
97
                'post' => [
98
                    'id'    => 2,
99
                    'title' => 'Another blog post',
100
                    'body'  => 'More content'
101
                ]
102
            ],
103
            $response->getData()
104
        );
105
        $this->assertJsonStringEqualsJsonString($json, json_encode($response));
106
    }
107
108
    public function testSuccessResponseWithoutData(): void
109
    {
110
        $json = '{ "status": "success" }';
111
        $this->expectException(EnsuranceException::class);
112
        $this->expectExceptionMessage('Key "data" is required');
113
        JSend::decode($json);
114
    }
115
116 View Code Duplication
    public function testSuccessResponseWithNullData(): void
117
    {
118
        $json     = '{ "status": "success", "data": null }';
119
        $response = JSend::decode($json);
120
        $this->assertTrue($response->getStatus()->isSuccess());
121
        $this->assertEmpty($response->getData());
122
        $this->assertJsonStringEqualsJsonString($json, json_encode($response));
123
    }
124
125 View Code Duplication
    public function testSuccessResponseWithEmptyData(): void
126
    {
127
        $json     = '{ "status": "success", "data": [] }';
128
        $response = JSend::decode($json);
129
        $this->assertTrue($response->getStatus()->isSuccess());
130
        $this->assertEmpty($response->getData());
131
        $this->assertJsonStringEqualsJsonString($json, json_encode($response));
132
    }
133
134 View Code Duplication
    public function testFailResponse(): void
135
    {
136
        $json = '{
137
            "status" : "fail",
138
            "data" : { "title" : "A title is required" }
139
        }';
140
141
        $response = JSend::decode($json);
142
        $this->assertTrue($response->getStatus()->isFail());
143
        $this->assertEquals(['title' => 'A title is required'], $response->getData());
144
        $this->assertJsonStringEqualsJsonString($json, json_encode($response));
145
    }
146
147
    public function testGetErrorOnNoneError(): void
148
    {
149
        $response = Jsend::decode('{"status": "success", "data": null}');
150
        $this->expectException(BadMethodCallException::class);
151
        $this->expectExceptionMessage('This is not a JSend-Error');
152
        $response->getError();
153
    }
154
155 View Code Duplication
    public function testErrorResponse(): void
156
    {
157
        $json = '{
158
            "status" : "error",
159
            "message" : "Unable to communicate with database"
160
        }';
161
162
        $response = JSend::decode($json);
163
        $this->assertTrue($response->getStatus()->isError());
164
        $this->assertEquals('Unable to communicate with database', $response->getError()->getMessage());
165
        $this->assertJsonStringEqualsJsonString($json, json_encode($response));
166
    }
167
168
    public function testSuccess(): void
169
    {
170
        $this->assertTrue(Status::success()->isSuccess());
171
        $json = '{"status": "success", "data": null}';
172
        $this->assertJsonStringEqualsJsonString($json, JSend::success(['data' => null]));
173
    }
174
175
    public function testFail(): void
176
    {
177
        $this->assertTrue(Status::fail()->isFail());
178
        $json = '{"status": "fail", "data": null}';
179
        $this->assertJsonStringEqualsJsonString($json, JSend::fail(['data' => null]));
180
    }
181
182
    public function testError(): void
183
    {
184
        $this->assertTrue(Status::error()->isError());
185
        $json = '{"status": "error", "message": ""}';
186
        $this->assertJsonStringEqualsJsonString($json, JSend::error(['message' => '']));
187
    }
188
}
189