Failed Conditions
Push — master ( 48f409...9d2706 )
by Randy
01:45
created

JSendBasicTest::testSuccessResponseWithNullData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 8
Ratio 100 %

Importance

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