Completed
Pull Request — master (#18)
by Randy
01:46
created

ResponseTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 148
Duplicated Lines 9.46 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 7
dl 14
loc 148
c 0
b 0
f 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testSuccessFactory() 7 7 1
A testFailFactory() 7 7 1
A testErrorFactory() 0 11 1
A testSuccessConversion() 0 13 1
A testFailConversion() 0 10 1
A testErrorConversion() 0 19 1
A testPsr7Response() 0 23 1
A testPsr7ResponseOptionalCode() 0 24 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Demv\JSend\Test;
4
5
use Demv\JSend\JSend;
6
use Demv\JSend\JSendError;
7
use PHPUnit\Framework\TestCase;
8
9
final class ResponseTest extends TestCase
10
{
11
    /**
12
     * @throws \Dgame\Ensurance\Exception\EnsuranceException
13
     */
14 View Code Duplication
    public function testSuccessFactory(): void
15
    {
16
        $jsend = JSend::success(['Erfolgreich!']);
17
18
        $this->assertTrue($jsend->isSuccess());
19
        $this->assertEquals(['Erfolgreich!'], $jsend->getData());
20
    }
21
22
    /**
23
     * @throws \Dgame\Ensurance\Exception\EnsuranceException
24
     */
25 View Code Duplication
    public function testFailFactory(): void
26
    {
27
        $jsend = JSend::fail(['Irgendwas lief schief']);
28
29
        $this->assertTrue($jsend->isFail());
30
        $this->assertEquals(['Irgendwas lief schief'], $jsend->getData());
31
    }
32
33
    public function testErrorFactory(): void
34
    {
35
        $jsend = JSend::error('Es ist ein Fehler aufgetreten');
36
37
        $this->assertTrue($jsend->isError());
38
        $this->assertEmpty($jsend->intoError()->getData());
39
        $this->assertEquals(
40
            'Es ist ein Fehler aufgetreten',
41
            $jsend->intoError()->getMessage()
42
        );
43
    }
44
45
    /**
46
     * @throws \Demv\JSend\InvalidJsonException
47
     * @throws \Dgame\Ensurance\Exception\EnsuranceException
48
     */
49
    public function testSuccessConversion(): void
50
    {
51
        $json = '{"status": "success", "data": ["Holy", "Moly"]}';
52
53
        $success = new DummyResponse();
54
        $success->withBody(new DummyStream($json));
55
        $success->withStatus(214);
56
57
        $jsend = JSend::translate($success);
58
        $this->assertTrue($jsend->getStatus()->isSuccess());
59
        $this->assertEquals(['Holy', 'Moly'], $jsend->getData());
60
        $this->assertJsonStringEqualsJsonString($json, json_encode($jsend));
61
    }
62
63
    /**
64
     * @throws \Demv\JSend\InvalidJsonException
65
     * @throws \Dgame\Ensurance\Exception\EnsuranceException
66
     */
67
    public function testFailConversion(): void
68
    {
69
        $fail = new DummyResponse();
70
        $fail->withBody(new DummyStream('{"status": "fail", "data": null}'));
71
72
        $jsend = JSend::translate($fail);
73
        $this->assertTrue($jsend->getStatus()->isFail());
74
        $this->assertEmpty($jsend->getData());
75
        $this->assertJsonStringEqualsJsonString('{"status": "fail", "data": null}', json_encode($jsend));
76
    }
77
78
    /**
79
     * @throws \Demv\JSend\InvalidJsonException
80
     * @throws \Dgame\Ensurance\Exception\EnsuranceException
81
     */
82
    public function testErrorConversion(): void
83
    {
84
        $json = '{"status": "error", "message": "Something is not right..."}';
85
86
        $error = new DummyResponse();
87
        $error->withBody(new DummyStream($json));
88
        $error->withStatus(501);
89
90
        $result         = json_decode($json, true);
91
        $result['code'] = $error->getStatusCode();
92
93
        /** @var JSendError $jsend */
94
        $jsend = JSend::translate($error);
95
        $this->assertTrue($jsend->isError());
96
        $this->assertEmpty($jsend->getData());
97
        $this->assertJsonStringEqualsJsonString(json_encode($result), $jsend->encode());
98
        $this->assertEquals('Something is not right...', $jsend->getMessage());
99
        $this->assertEquals($error->getStatusCode(), $jsend->getCode());
100
    }
101
102
    /**
103
     * @throws \Dgame\Ensurance\Exception\EnsuranceException
104
     */
105
    public function testPsr7Response(): void
106
    {
107
        $jsend = JSend::success(['Erfolgreich!'])->withStatus(200);
108
        $this->assertEquals(200, $jsend->getStatusCode());
109
        $this->assertEquals('{"status":"success","data":["Erfolgreich!"]}', $jsend->getBody()->getContents());
110
111
        $jsend = JSend::fail(['Irgendwas lief schief'])->withStatus(400);
112
        $this->assertEquals(400, $jsend->getStatusCode());
113
        $this->assertEquals('{"status":"fail","data":["Irgendwas lief schief"]}', $jsend->getBody()->getContents());
114
115
        $jsend = JSend::error('Es ist ein Fehler aufgetreten', 404)->withStatus(500);
116
        $this->assertEquals(500, $jsend->getStatusCode());
117
        $this->assertEquals(
118
            '{"status":"error","message":"Es ist ein Fehler aufgetreten","code":404}',
119
            $jsend->getBody()->getContents()
120
        );
121
122
        $jsend = JSend::success(['Stimmt der Header?']);
123
        $this->assertEquals(['application/json'], $jsend->getHeader('content-type'));
124
125
        $jsend = JSend::success(['Eigene Header werden übernommen'])->withHeader('foo', 'bar');
126
        $this->assertEquals(['bar'], $jsend->getHeader('foo'));
127
    }
128
129
    /**
130
     * @throws \Dgame\Ensurance\Exception\EnsuranceException
131
     */
132
    public function testPsr7ResponseOptionalCode(): void
133
    {
134
        $jsend = JSend::success(['Erfolgreich!']);
135
        $this->assertEquals(200, $jsend->getStatusCode());
136
        $this->assertEquals('{"status":"success","data":["Erfolgreich!"]}', $jsend->getBody()->getContents());
137
138
        $jsend = JSend::fail(['Irgendwas lief schief']);
139
        $this->assertEquals(400, $jsend->getStatusCode());
140
        $this->assertEquals('{"status":"fail","data":["Irgendwas lief schief"]}', $jsend->getBody()->getContents());
141
142
        $jsend = JSend::error('Es ist ein Fehler aufgetreten', 404);
143
        $this->assertEquals(404, $jsend->getStatusCode());
144
        $this->assertEquals(
145
            '{"status":"error","message":"Es ist ein Fehler aufgetreten","code":404}',
146
            $jsend->getBody()->getContents()
147
        );
148
149
        $jsend = JSend::error('Es ist ein Fehler aufgetreten');
150
        $this->assertEquals(500, $jsend->getStatusCode());
151
        $this->assertEquals(
152
            '{"status":"error","message":"Es ist ein Fehler aufgetreten"}',
153
            $jsend->getBody()->getContents()
154
        );
155
    }
156
}
157