Completed
Push — master ( 8d38f7...0c3e6a )
by Tobias
02:45
created

tests/RequestTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Tests\Nyholm\Psr7;
4
5
use Nyholm\Psr7\Request;
6
use Nyholm\Psr7\Uri;
7
use PHPUnit\Framework\TestCase;
8
use Psr\Http\Message\StreamInterface;
9
10
/**
11
 * @covers \Nyholm\Psr7\Request
12
 */
13
class RequestTest extends TestCase
14
{
15
    public function testRequestUriMayBeString()
16
    {
17
        $r = new Request('GET', '/');
18
        $this->assertEquals('/', (string) $r->getUri());
19
    }
20
21
    public function testRequestUriMayBeUri()
22
    {
23
        $uri = new Uri('/');
24
        $r = new Request('GET', $uri);
25
        $this->assertSame($uri, $r->getUri());
26
    }
27
28
    public function testValidateRequestUri()
29
    {
30
        $this->expectException(\InvalidArgumentException::class);
31
        $this->expectExceptionMessage('Unable to parse URI: ///');
32
33
        new Request('GET', '///');
34
    }
35
36
    public function testCanConstructWithBody()
37
    {
38
        $r = new Request('GET', '/', [], 'baz');
39
        $this->assertInstanceOf(StreamInterface::class, $r->getBody());
40
        $this->assertEquals('baz', (string) $r->getBody());
41
    }
42
43
    public function testNullBody()
44
    {
45
        $r = new Request('GET', '/', [], null);
46
        $this->assertInstanceOf(StreamInterface::class, $r->getBody());
47
        $this->assertSame('', (string) $r->getBody());
48
    }
49
50
    public function testFalseyBody()
51
    {
52
        $r = new Request('GET', '/', [], '0');
53
        $this->assertInstanceOf(StreamInterface::class, $r->getBody());
54
        $this->assertSame('0', (string) $r->getBody());
55
    }
56
57
    public function testConstructorDoesNotReadStreamBody()
58
    {
59
        $body = $this->getMockBuilder(StreamInterface::class)->getMock();
60
        $body->expects($this->never())
61
            ->method('__toString');
62
63
        $r = new Request('GET', '/', [], $body);
1 ignored issue
show
$body is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a string|resource|object<StreamInterface>|null.

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...
64
        $this->assertSame($body, $r->getBody());
65
    }
66
67
    public function testWithUri()
68
    {
69
        $r1 = new Request('GET', '/');
70
        $u1 = $r1->getUri();
71
        $u2 = new Uri('http://www.example.com');
72
        $r2 = $r1->withUri($u2);
73
        $this->assertNotSame($r1, $r2);
74
        $this->assertSame($u2, $r2->getUri());
75
        $this->assertSame($u1, $r1->getUri());
76
    }
77
78
    public function testSameInstanceWhenSameUri()
79
    {
80
        $r1 = new Request('GET', 'http://foo.com');
81
        $r2 = $r1->withUri($r1->getUri());
82
        $this->assertSame($r1, $r2);
83
    }
84
85
    public function testWithRequestTarget()
86
    {
87
        $r1 = new Request('GET', '/');
88
        $r2 = $r1->withRequestTarget('*');
89
        $this->assertEquals('*', $r2->getRequestTarget());
90
        $this->assertEquals('/', $r1->getRequestTarget());
91
    }
92
93 View Code Duplication
    public function testRequestTargetDoesNotAllowSpaces()
94
    {
95
        $this->expectException(\InvalidArgumentException::class);
96
        $this->expectExceptionMessage('Invalid request target provided; cannot contain whitespace');
97
98
        $r1 = new Request('GET', '/');
99
        $r1->withRequestTarget('/foo bar');
100
    }
101
102
    public function testRequestTargetDefaultsToSlash()
103
    {
104
        $r1 = new Request('GET', '');
105
        $this->assertEquals('/', $r1->getRequestTarget());
106
        $r2 = new Request('GET', '*');
107
        $this->assertEquals('*', $r2->getRequestTarget());
108
        $r3 = new Request('GET', 'http://foo.com/bar baz/');
109
        $this->assertEquals('/bar%20baz/', $r3->getRequestTarget());
110
    }
111
112
    public function testBuildsRequestTarget()
113
    {
114
        $r1 = new Request('GET', 'http://foo.com/baz?bar=bam');
115
        $this->assertEquals('/baz?bar=bam', $r1->getRequestTarget());
116
    }
117
118
    public function testBuildsRequestTargetWithFalseyQuery()
119
    {
120
        $r1 = new Request('GET', 'http://foo.com/baz?0');
121
        $this->assertEquals('/baz?0', $r1->getRequestTarget());
122
    }
123
124
    public function testHostIsAddedFirst()
125
    {
126
        $r = new Request('GET', 'http://foo.com/baz?bar=bam', ['Foo' => 'Bar']);
127
        $this->assertEquals([
128
            'Host' => ['foo.com'],
129
            'Foo' => ['Bar'],
130
        ], $r->getHeaders());
131
    }
132
133
    public function testCanGetHeaderAsCsv()
134
    {
135
        $r = new Request('GET', 'http://foo.com/baz?bar=bam', [
136
            'Foo' => ['a', 'b', 'c'],
137
        ]);
138
        $this->assertEquals('a, b, c', $r->getHeaderLine('Foo'));
139
        $this->assertEquals('', $r->getHeaderLine('Bar'));
140
    }
141
142 View Code Duplication
    public function testHostIsNotOverwrittenWhenPreservingHost()
143
    {
144
        $r = new Request('GET', 'http://foo.com/baz?bar=bam', ['Host' => 'a.com']);
145
        $this->assertEquals(['Host' => ['a.com']], $r->getHeaders());
146
        $r2 = $r->withUri(new Uri('http://www.foo.com/bar'), true);
147
        $this->assertEquals('a.com', $r2->getHeaderLine('Host'));
148
    }
149
150 View Code Duplication
    public function testOverridesHostWithUri()
151
    {
152
        $r = new Request('GET', 'http://foo.com/baz?bar=bam');
153
        $this->assertEquals(['Host' => ['foo.com']], $r->getHeaders());
154
        $r2 = $r->withUri(new Uri('http://www.baz.com/bar'));
155
        $this->assertEquals('www.baz.com', $r2->getHeaderLine('Host'));
156
    }
157
158
    public function testAggregatesHeaders()
159
    {
160
        $r = new Request('GET', '', [
161
            'ZOO' => 'zoobar',
162
            'zoo' => ['foobar', 'zoobar'],
163
        ]);
164
        $this->assertEquals(['ZOO' => ['zoobar', 'foobar', 'zoobar']], $r->getHeaders());
165
        $this->assertEquals('zoobar, foobar, zoobar', $r->getHeaderLine('zoo'));
166
    }
167
168 View Code Duplication
    public function testSupportNumericHeaders()
169
    {
170
        $r = new Request('GET', '', [
171
            'Content-Length' => 200,
172
        ]);
173
        $this->assertSame(['Content-Length' => ['200']], $r->getHeaders());
174
        $this->assertSame('200', $r->getHeaderLine('Content-Length'));
175
    }
176
177
    public function testAddsPortToHeader()
178
    {
179
        $r = new Request('GET', 'http://foo.com:8124/bar');
180
        $this->assertEquals('foo.com:8124', $r->getHeaderLine('host'));
181
    }
182
183
    public function testAddsPortToHeaderAndReplacePreviousPort()
184
    {
185
        $r = new Request('GET', 'http://foo.com:8124/bar');
186
        $r = $r->withUri(new Uri('http://foo.com:8125/bar'));
187
        $this->assertEquals('foo.com:8125', $r->getHeaderLine('host'));
188
    }
189
190 View Code Duplication
    public function testCannotHaveHeaderWithEmptyName()
191
    {
192
        $this->expectException(\InvalidArgumentException::class);
193
        $this->expectExceptionMessage('Header name must be an RFC 7230 compatible string.');
194
        $r = new Request('GET', 'https://example.com/');
195
        $r = $r->withHeader('', 'Bar');
196
    }
197
198
    public function testCanHaveHeaderWithEmptyValue()
199
    {
200
        $r = new Request('GET', 'https://example.com/');
201
        $r = $r->withHeader('Foo', '');
202
        $this->assertEquals([''], $r->getHeader('Foo'));
203
    }
204
}
205