Completed
Push — master ( b546d2...b6b62a )
by Tobias
02:34
created

ResponseTest::trimmedHeaderValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Tests\Nyholm\Psr7;
4
5
use Nyholm\Psr7\Response;
6
use PHPUnit\Framework\TestCase;
7
use Psr\Http\Message\StreamInterface;
8
9
/**
10
 * @covers \Nyholm\Psr7\MessageTrait
11
 * @covers \Nyholm\Psr7\Response
12
 */
13
class ResponseTest extends TestCase
14
{
15
    public function testDefaultConstructor()
16
    {
17
        $r = new Response();
18
        $this->assertSame(200, $r->getStatusCode());
19
        $this->assertSame('1.1', $r->getProtocolVersion());
20
        $this->assertSame('OK', $r->getReasonPhrase());
21
        $this->assertSame([], $r->getHeaders());
22
        $this->assertInstanceOf(StreamInterface::class, $r->getBody());
23
        $this->assertSame('', (string) $r->getBody());
24
    }
25
26
    public function testCanConstructWithStatusCode()
27
    {
28
        $r = new Response(404);
29
        $this->assertSame(404, $r->getStatusCode());
30
        $this->assertSame('Not Found', $r->getReasonPhrase());
31
    }
32
33 View Code Duplication
    public function testConstructorDoesNotReadStreamBody()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        $body = $this->getMockBuilder(StreamInterface::class)->getMock();
36
        $body->expects($this->never())
37
            ->method('__toString');
38
39
        $r = new Response(200, [], $body);
0 ignored issues
show
Documentation introduced by
$body is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a string|null|resource|obj...essage\StreamInterface>.

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...
40
        $this->assertSame($body, $r->getBody());
41
    }
42
43
    public function testStatusCanBeNumericString()
44
    {
45
        $r = new Response('404');
46
        $r2 = $r->withStatus('201');
47
        $this->assertSame(404, $r->getStatusCode());
48
        $this->assertSame('Not Found', $r->getReasonPhrase());
49
        $this->assertSame(201, $r2->getStatusCode());
50
        $this->assertSame('Created', $r2->getReasonPhrase());
51
    }
52
53 View Code Duplication
    public function testCanConstructWithHeaders()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55
        $r = new Response(200, ['Foo' => 'Bar']);
56
        $this->assertSame(['Foo' => ['Bar']], $r->getHeaders());
57
        $this->assertSame('Bar', $r->getHeaderLine('Foo'));
58
        $this->assertSame(['Bar'], $r->getHeader('Foo'));
59
    }
60
61
    public function testCanConstructWithHeadersAsArray()
62
    {
63
        $r = new Response(200, [
64
            'Foo' => ['baz', 'bar'],
65
        ]);
66
        $this->assertSame(['Foo' => ['baz', 'bar']], $r->getHeaders());
67
        $this->assertSame('baz, bar', $r->getHeaderLine('Foo'));
68
        $this->assertSame(['baz', 'bar'], $r->getHeader('Foo'));
69
    }
70
71
    public function testCanConstructWithBody()
72
    {
73
        $r = new Response(200, [], 'baz');
74
        $this->assertInstanceOf(StreamInterface::class, $r->getBody());
75
        $this->assertSame('baz', (string) $r->getBody());
76
    }
77
78
    public function testNullBody()
79
    {
80
        $r = new Response(200, [], null);
81
        $this->assertInstanceOf(StreamInterface::class, $r->getBody());
82
        $this->assertSame('', (string) $r->getBody());
83
    }
84
85
    public function testFalseyBody()
86
    {
87
        $r = new Response(200, [], '0');
88
        $this->assertInstanceOf(StreamInterface::class, $r->getBody());
89
        $this->assertSame('0', (string) $r->getBody());
90
    }
91
92
    public function testCanConstructWithReason()
93
    {
94
        $r = new Response(200, [], null, '1.1', 'bar');
95
        $this->assertSame('bar', $r->getReasonPhrase());
96
97
        $r = new Response(200, [], null, '1.1', '0');
98
        $this->assertSame('0', $r->getReasonPhrase(), 'Falsey reason works');
99
    }
100
101
    public function testCanConstructWithProtocolVersion()
102
    {
103
        $r = new Response(200, [], null, '1000');
104
        $this->assertSame('1000', $r->getProtocolVersion());
105
    }
106
107
    public function testWithStatusCodeAndNoReason()
108
    {
109
        $r = (new Response())->withStatus(201);
110
        $this->assertSame(201, $r->getStatusCode());
111
        $this->assertSame('Created', $r->getReasonPhrase());
112
    }
113
114
    public function testWithStatusCodeAndReason()
115
    {
116
        $r = (new Response())->withStatus(201, 'Foo');
117
        $this->assertSame(201, $r->getStatusCode());
118
        $this->assertSame('Foo', $r->getReasonPhrase());
119
120
        $r = (new Response())->withStatus(201, '0');
121
        $this->assertSame(201, $r->getStatusCode());
122
        $this->assertSame('0', $r->getReasonPhrase(), 'Falsey reason works');
123
    }
124
125
    public function testWithProtocolVersion()
126
    {
127
        $r = (new Response())->withProtocolVersion('1000');
128
        $this->assertSame('1000', $r->getProtocolVersion());
129
    }
130
131
    public function testSameInstanceWhenSameProtocol()
132
    {
133
        $r = new Response();
134
        $this->assertSame($r, $r->withProtocolVersion('1.1'));
135
    }
136
137 View Code Duplication
    public function testWithBody()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
    {
139
        $b = (new \Nyholm\Psr7\Factory\StreamFactory())->createStream('0');
140
        $r = (new Response())->withBody($b);
141
        $this->assertInstanceOf(StreamInterface::class, $r->getBody());
142
        $this->assertSame('0', (string) $r->getBody());
143
    }
144
145
    public function testSameInstanceWhenSameBody()
146
    {
147
        $r = new Response();
148
        $b = $r->getBody();
149
        $this->assertSame($r, $r->withBody($b));
150
    }
151
152 View Code Duplication
    public function testWithHeader()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
    {
154
        $r = new Response(200, ['Foo' => 'Bar']);
155
        $r2 = $r->withHeader('baZ', 'Bam');
156
        $this->assertSame(['Foo' => ['Bar']], $r->getHeaders());
157
        $this->assertSame(['Foo' => ['Bar'], 'baZ' => ['Bam']], $r2->getHeaders());
158
        $this->assertSame('Bam', $r2->getHeaderLine('baz'));
159
        $this->assertSame(['Bam'], $r2->getHeader('baz'));
160
    }
161
162 View Code Duplication
    public function testWithHeaderAsArray()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
163
    {
164
        $r = new Response(200, ['Foo' => 'Bar']);
165
        $r2 = $r->withHeader('baZ', ['Bam', 'Bar']);
166
        $this->assertSame(['Foo' => ['Bar']], $r->getHeaders());
167
        $this->assertSame(['Foo' => ['Bar'], 'baZ' => ['Bam', 'Bar']], $r2->getHeaders());
168
        $this->assertSame('Bam, Bar', $r2->getHeaderLine('baz'));
169
        $this->assertSame(['Bam', 'Bar'], $r2->getHeader('baz'));
170
    }
171
172 View Code Duplication
    public function testWithHeaderReplacesDifferentCase()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
173
    {
174
        $r = new Response(200, ['Foo' => 'Bar']);
175
        $r2 = $r->withHeader('foO', 'Bam');
176
        $this->assertSame(['Foo' => ['Bar']], $r->getHeaders());
177
        $this->assertSame(['foO' => ['Bam']], $r2->getHeaders());
178
        $this->assertSame('Bam', $r2->getHeaderLine('foo'));
179
        $this->assertSame(['Bam'], $r2->getHeader('foo'));
180
    }
181
182 View Code Duplication
    public function testWithAddedHeader()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
183
    {
184
        $r = new Response(200, ['Foo' => 'Bar']);
185
        $r2 = $r->withAddedHeader('foO', 'Baz');
186
        $this->assertSame(['Foo' => ['Bar']], $r->getHeaders());
187
        $this->assertSame(['Foo' => ['Bar', 'Baz']], $r2->getHeaders());
188
        $this->assertSame('Bar, Baz', $r2->getHeaderLine('foo'));
189
        $this->assertSame(['Bar', 'Baz'], $r2->getHeader('foo'));
190
    }
191
192 View Code Duplication
    public function testWithAddedHeaderAsArray()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
193
    {
194
        $r = new Response(200, ['Foo' => 'Bar']);
195
        $r2 = $r->withAddedHeader('foO', ['Baz', 'Bam']);
196
        $this->assertSame(['Foo' => ['Bar']], $r->getHeaders());
197
        $this->assertSame(['Foo' => ['Bar', 'Baz', 'Bam']], $r2->getHeaders());
198
        $this->assertSame('Bar, Baz, Bam', $r2->getHeaderLine('foo'));
199
        $this->assertSame(['Bar', 'Baz', 'Bam'], $r2->getHeader('foo'));
200
    }
201
202 View Code Duplication
    public function testWithAddedHeaderThatDoesNotExist()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
203
    {
204
        $r = new Response(200, ['Foo' => 'Bar']);
205
        $r2 = $r->withAddedHeader('nEw', 'Baz');
206
        $this->assertSame(['Foo' => ['Bar']], $r->getHeaders());
207
        $this->assertSame(['Foo' => ['Bar'], 'nEw' => ['Baz']], $r2->getHeaders());
208
        $this->assertSame('Baz', $r2->getHeaderLine('new'));
209
        $this->assertSame(['Baz'], $r2->getHeader('new'));
210
    }
211
212 View Code Duplication
    public function testWithoutHeaderThatExists()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
213
    {
214
        $r = new Response(200, ['Foo' => 'Bar', 'Baz' => 'Bam']);
215
        $r2 = $r->withoutHeader('foO');
216
        $this->assertTrue($r->hasHeader('foo'));
217
        $this->assertSame(['Foo' => ['Bar'], 'Baz' => ['Bam']], $r->getHeaders());
218
        $this->assertFalse($r2->hasHeader('foo'));
219
        $this->assertSame(['Baz' => ['Bam']], $r2->getHeaders());
220
    }
221
222
    public function testWithoutHeaderThatDoesNotExist()
223
    {
224
        $r = new Response(200, ['Baz' => 'Bam']);
225
        $r2 = $r->withoutHeader('foO');
226
        $this->assertSame($r, $r2);
227
        $this->assertFalse($r2->hasHeader('foo'));
228
        $this->assertSame(['Baz' => ['Bam']], $r2->getHeaders());
229
    }
230
231
    public function testSameInstanceWhenRemovingMissingHeader()
232
    {
233
        $r = new Response();
234
        $this->assertSame($r, $r->withoutHeader('foo'));
235
    }
236
237
    public function trimmedHeaderValues()
238
    {
239
        return [
240
            [new Response(200, ['OWS' => " \t \tFoo\t \t "])],
241
            [(new Response())->withHeader('OWS', " \t \tFoo\t \t ")],
242
            [(new Response())->withAddedHeader('OWS', " \t \tFoo\t \t ")],
243
        ];
244
    }
245
246
    /**
247
     * @dataProvider trimmedHeaderValues
248
     */
249
    public function testHeaderValuesAreTrimmed($r)
250
    {
251
        $this->assertSame(['OWS' => ['Foo']], $r->getHeaders());
252
        $this->assertSame('Foo', $r->getHeaderLine('OWS'));
253
        $this->assertSame(['Foo'], $r->getHeader('OWS'));
254
    }
255
}
256