ResponseTest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 285
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 12
Bugs 0 Features 2
Metric Value
wmc 12
c 12
b 0
f 2
lcom 0
cbo 1
dl 0
loc 285
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testDefaultValues() 0 7 1
A testProperStatusCode() 0 6 1
A testBadStatusCode() 0 5 1
A testSimpleBodyOperations() 0 16 1
A testSimpleHeader() 0 22 1
B testReplacingHeaderInstance() 0 33 1
A testSimpleLocationHeaderAddition() 0 23 1
A testLocationHeaderAdditionWithHostnameSet() 0 23 1
A provideLocationHeaderAdditionWithHostnameSet() 0 20 1
A testSimpleRemoveCookie() 0 12 1
A testMoreComplicatedSimpleRemoveCookie() 0 15 1
A testAddCookie() 0 12 1
1
<?php
2
3
4
namespace Fracture\Http;
5
6
use Exception;
7
use ReflectionClass;
8
use PHPUnit_Framework_TestCase;
9
10
class ResponseTest extends PHPUnit_Framework_TestCase
11
{
12
13
    /**
14
     * @covers Fracture\Http\Response::getBody
15
     * @covers Fracture\Http\Response::getStatusCode
16
     * @covers Fracture\Http\Response::getHeaders
17
     */
18
    public function testDefaultValues()
19
    {
20
        $instance = new Response;
21
        $this->assertSame(200, $instance->getStatusCode());
22
        $this->assertSame('', $instance->getBody());
23
        $this->assertSame([], $instance->getHeaders());
24
    }
25
26
27
    /**
28
     * @covers Fracture\Http\Response::getBody
29
     * @covers Fracture\Http\Response::setStatusCode
30
     * @covers Fracture\Http\Response::getStatusCode
31
     */
32
    public function testProperStatusCode()
33
    {
34
        $instance = new Response;
35
        $instance->setStatusCode(404);
36
        $this->assertSame(404, $instance->getStatusCode());
37
    }
38
39
40
    /**
41
     * @expectedException InvalidArgumentException
42
     *
43
     * @covers Fracture\Http\Response::getBody
44
     * @covers Fracture\Http\Response::setStatusCode
45
     */
46
    public function testBadStatusCode()
47
    {
48
        $instance = new Response;
49
        $instance->setStatusCode(9999);
50
    }
51
52
53
    /**
54
     * @covers Fracture\Http\Response::setBody
55
     * @covers Fracture\Http\Response::appendBody
56
     * @covers Fracture\Http\Response::prependBody
57
     * @covers Fracture\Http\Response::getBody
58
     */
59
    public function testSimpleBodyOperations()
60
    {
61
        $instance = new Response;
62
63
        $instance->setBody('sit');
64
        $this->assertSame('sit', $instance->getBody());
65
66
        $instance->appendBody(' dolor amet');
67
        $this->assertSame('sit dolor amet', $instance->getBody());
68
69
        $instance->prependBody('lorem ipsum ');
70
        $this->assertSame('lorem ipsum sit dolor amet', $instance->getBody());
71
72
        $instance->setBody(null);
73
        $this->assertNull($instance->getBody());
74
    }
75
76
77
    /**
78
     * @covers Fracture\Http\Response::addHeader
79
     * @covers Fracture\Http\Response::getHeaders
80
     *
81
     * @covers Fracture\Http\Response::populateHeaderList
82
     */
83
    public function testSimpleHeader()
84
    {
85
        $header = $this->getMock('Fracture\Http\Headers\ContentType', ['getName', 'getValue']);
86
        $header->expects($this->any())
87
               ->method('getName')
88
               ->will($this->returnValue('Alpha'));
89
90
        $header->expects($this->any())
91
               ->method('getValue')
92
               ->will($this->returnValue('beta'));
93
94
95
        $instance = new Response;
96
        $instance->addHeader($header);
97
98
        $this->assertEquals([
99
            [
100
                'value' => 'Alpha: beta',
101
                'replace' => true,
102
            ],
103
        ], $instance->getHeaders());
104
    }
105
106
107
    /**
108
     * @covers Fracture\Http\Response::addHeader
109
     * @covers Fracture\Http\Response::getHeaders
110
     *
111
     * @covers Fracture\Http\Response::populateHeaderList
112
     */
113
    public function testReplacingHeaderInstance()
114
    {
115
        $original = $this->getMock('Fracture\Http\Headers\ContentType', ['getName', 'getValue']);
116
        $original->expects($this->any())
117
                 ->method('getName')
118
                 ->will($this->returnValue('Alpha'));
119
120
        $original->expects($this->any())
121
                 ->method('getValue')
122
                 ->will($this->returnValue('beta'));
123
124
        $replacement = $this->getMock('Fracture\Http\Headers\ContentType', ['getName', 'getValue']);
125
        $replacement->expects($this->any())
126
                    ->method('getName')
127
                    ->will($this->returnValue('Alpha'));
128
129
        $replacement->expects($this->any())
130
                    ->method('getValue')
131
                    ->will($this->returnValue('gamma'));
132
133
134
        $instance = new Response;
135
        $instance->addHeader($original);
136
        $instance->addHeader($replacement);
137
138
139
        $this->assertEquals([
140
            [
141
                'value' => 'Alpha: gamma',
142
                'replace' => true,
143
            ],
144
        ], $instance->getHeaders());
145
    }
146
147
148
    /**
149
     * @covers Fracture\Http\Response::addHeader
150
     * @covers Fracture\Http\Response::getHeaders
151
     *
152
     * @covers Fracture\Http\Response::populateHeaderList
153
     * @covers Fracture\Http\Response::adjustForHostname
154
     */
155
    public function testSimpleLocationHeaderAddition()
156
    {
157
        $headerMock = $this->getMock('Fracture\Http\Headers\Location', ['getName', 'getValue']);
158
        $headerMock->expects($this->any())
159
                   ->method('getName')
160
                   ->will($this->returnValue('Location'));
161
162
        $headerMock->expects($this->any())
163
                   ->method('getValue')
164
                   ->will($this->returnValue('beta'));
165
166
        $instance = new Response;
167
        $instance->addHeader($headerMock);
168
169
        $this->assertEquals([
170
            [
171
                'value' => 'Location: beta',
172
                'replace' => true,
173
            ],
174
        ], $instance->getHeaders());
175
176
        $this->assertSame(302, $instance->getStatusCode());
177
    }
178
179
180
    /**
181
     * @covers Fracture\Http\Response::addHeader
182
     * @covers Fracture\Http\Response::getHeaders
183
     * @covers Fracture\Http\Response::setHostname
184
     *
185
     * @covers Fracture\Http\Response::populateHeaderList
186
     * @covers Fracture\Http\Response::adjustForHostname
187
     *
188
     * @dataProvider provideLocationHeaderAdditionWithHostnameSet
189
     */
190
    public function testLocationHeaderAdditionWithHostnameSet($host, $path, $expected)
191
    {
192
        $headerMock = $this->getMock('Fracture\Http\Headers\Location', ['getName', 'getValue']);
193
        $headerMock->expects($this->any())
194
                   ->method('getName')
195
                   ->will($this->returnValue('Location'));
196
197
        $headerMock->expects($this->any())
198
                   ->method('getValue')
199
                   ->will($this->returnValue($path));
200
201
        $instance = new Response;
202
        $instance->addHeader($headerMock);
203
204
        $instance->setHostname($host);
205
206
        $this->assertEquals([
207
            [
208
                'value' => $expected,
209
                'replace' => true,
210
            ],
211
        ], $instance->getHeaders());
212
    }
213
214
215
    public function provideLocationHeaderAdditionWithHostnameSet()
216
    {
217
        return [
218
            [
219
                'host' => 'http://example.tld',
220
                'path' => '/beta',
221
                'expected' => 'Location: http://example.tld/beta',
222
            ],
223
            [
224
                'host' => 'https://example.tld',
225
                'path' => '/beta',
226
                'expected' => 'Location: https://example.tld/beta',
227
            ],
228
            [
229
                'host' => 'http://example.tld',
230
                'path' => 'http://domain.tld/beta',
231
                'expected' => 'Location: http://domain.tld/beta',
232
            ],
233
        ];
234
    }
235
236
    /**
237
     * @covers Fracture\Http\Response::removeCookie
238
     * @covers Fracture\Http\Response::getHeaders
239
     */
240
    public function testSimpleRemoveCookie()
241
    {
242
        $instance = new Response;
243
        $instance->removeCookie('name');
244
245
        $this->assertEquals([
246
            [
247
                'value' => 'Set-Cookie: name=deleted; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Path=/; HttpOnly',
248
                'replace' => false,
249
            ],
250
        ], $instance->getHeaders());
251
    }
252
253
254
    /**
255
     * @covers Fracture\Http\Response::removeCookie
256
     * @covers Fracture\Http\Response::getHeaders
257
     */
258
    public function testMoreComplicatedSimpleRemoveCookie()
259
    {
260
        $instance = new Response;
261
        $instance->removeCookie('name', [
262
            'path' => '/test',
263
            'expires' => 1,
264
        ]);
265
266
        $this->assertEquals([
267
            [
268
                'value' => 'Set-Cookie: name=deleted; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Path=/test; HttpOnly',
269
                'replace' => false,
270
            ],
271
        ], $instance->getHeaders());
272
    }
273
274
275
    /**
276
     * @covers Fracture\Http\Response::addCookie
277
     * @covers Fracture\Http\Response::getHeaders
278
     */
279
    public function testAddCookie()
280
    {
281
        $instance = new Response;
282
        $instance->addCookie('alpha', 'beta');
283
284
        $this->assertEquals([
285
            [
286
                'value' => 'Set-Cookie: alpha=beta; Path=/; HttpOnly',
287
                'replace' => false,
288
            ],
289
        ], $instance->getHeaders());
290
    }
291
292
293
294
}
295