Skip to content

Commit d2b5960

Browse files
gmponosdbu
authored andcommitted
Improve tests (#37)
* Use more specific assertions * Add more invalid cases to METHOD verb * Test that messages are indeed immutable
1 parent 031eb86 commit d2b5960

6 files changed

+55
-22
lines changed

src/MessageTrait.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ public function testProtocolVersion()
2323
}
2424

2525
$initialMessage = $this->getMessage();
26+
$original = clone $initialMessage;
27+
2628
$message = $initialMessage->withProtocolVersion('1.0');
29+
2730
$this->assertNotSameObject($initialMessage, $message);
31+
$this->assertEquals($initialMessage, $original, 'Message object MUST not be mutated');
2832

29-
$this->assertEquals('1.0', $message->getProtocolVersion());
33+
$this->assertSame('1.0', $message->getProtocolVersion());
3034
}
3135

3236
public function testGetHeaders()
@@ -35,12 +39,21 @@ public function testGetHeaders()
3539
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
3640
}
3741

38-
$message = $this->getMessage()->withAddedHeader('content-type', 'text/html');
39-
$message = $message->withAddedHeader('content-type', 'text/plain');
42+
$initialMessage = $this->getMessage();
43+
$original = clone $initialMessage;
44+
45+
$message = $initialMessage
46+
->withAddedHeader('content-type', 'text/html')
47+
->withAddedHeader('content-type', 'text/plain');
48+
49+
$this->assertEquals($initialMessage, $original, 'Message object MUST not be mutated');
50+
4051
$headers = $message->getHeaders();
4152

4253
$this->assertTrue(isset($headers['content-type']));
4354
$this->assertCount(2, $headers['content-type']);
55+
$this->assertContains('text/html', $headers['content-type']);
56+
$this->assertContains('text/plain', $headers['content-type']);
4457
}
4558

4659
public function testHasHeader()
@@ -69,7 +82,7 @@ public function testGetHeader()
6982
$this->assertCount(2, $message->getHeader('CONTENT-TYPE'));
7083
$emptyHeader = $message->getHeader('Bar');
7184
$this->assertCount(0, $emptyHeader);
72-
$this->assertTrue(is_array($emptyHeader));
85+
$this->assertInternalType('array', $emptyHeader);
7386
}
7487

7588
public function testGetHeaderLine()
@@ -84,7 +97,7 @@ public function testGetHeaderLine()
8497
$this->assertRegExp('|text/html, ?text/plain|', $message->getHeaderLine('Content-Type'));
8598
$this->assertRegExp('|text/html, ?text/plain|', $message->getHeaderLine('CONTENT-TYPE'));
8699

87-
$this->assertEquals('', $message->getHeaderLine('Bar'));
100+
$this->assertSame('', $message->getHeaderLine('Bar'));
88101
}
89102

90103
public function testWithHeader()
@@ -94,8 +107,11 @@ public function testWithHeader()
94107
}
95108

96109
$initialMessage = $this->getMessage();
110+
$original = clone $initialMessage;
111+
97112
$message = $initialMessage->withHeader('content-type', 'text/html');
98113
$this->assertNotSameObject($initialMessage, $message);
114+
$this->assertEquals($initialMessage, $original, 'Message object MUST not be mutated');
99115
$this->assertEquals('text/html', $message->getHeaderLine('content-type'));
100116

101117
$message = $initialMessage->withHeader('content-type', 'text/plain');
@@ -109,7 +125,7 @@ public function testWithHeader()
109125

110126
$message = $initialMessage->withHeader('Bar', '');
111127
$this->assertTrue($message->hasHeader('Bar'));
112-
$this->assertEquals([''], $message->getHeader('Bar'));
128+
$this->assertSame([''], $message->getHeader('Bar'));
113129
}
114130

115131
/**
@@ -227,9 +243,11 @@ public function testBody()
227243
}
228244

229245
$initialMessage = $this->getMessage();
246+
$original = clone $initialMessage;
230247
$stream = $this->buildStream('foo');
231248
$message = $initialMessage->withBody($stream);
232249
$this->assertNotSameObject($initialMessage, $message);
250+
$this->assertEquals($initialMessage, $original, 'Message object MUST not be mutated');
233251

234252
$this->assertEquals($stream, $message->getBody());
235253
}

src/RequestIntegrationTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ public function testRequestTarget()
4343
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
4444
}
4545

46+
$original = clone $this->request;
4647
$this->assertEquals('/', $this->request->getRequestTarget());
4748

4849
$request = $this->request->withRequestTarget('*');
4950
$this->assertNotSameObject($this->request, $request);
51+
$this->assertEquals($this->request, $original, 'Request object MUST not be mutated');
5052
$this->assertEquals('*', $request->getRequestTarget());
5153
}
5254

@@ -57,9 +59,11 @@ public function testMethod()
5759
}
5860

5961
$this->assertEquals('GET', $this->request->getMethod());
62+
$original = clone $this->request;
6063

6164
$request = $this->request->withMethod('POST');
6265
$this->assertNotSameObject($this->request, $request);
66+
$this->assertEquals($this->request, $original, 'Request object MUST not be mutated');
6367
$this->assertEquals('POST', $request->getMethod());
6468

6569
$request = $this->request->withMethod('head');
@@ -82,6 +86,9 @@ public function testMethodWithInvalidArguments($method)
8286
public function getInvalidMethods()
8387
{
8488
return [
89+
[null],
90+
[1],
91+
[1.01],
8592
[false],
8693
[['foo']],
8794
[new \stdClass()],
@@ -93,17 +100,21 @@ public function testUri()
93100
if (isset($this->skippedTests[__FUNCTION__])) {
94101
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
95102
}
103+
$original = clone $this->request;
96104

97105
$this->assertInstanceOf(UriInterface::class, $this->request->getUri());
98106

99107
$uri = $this->buildUri('http://www.foo.com/bar');
100108
$request = $this->request->withUri($uri);
101109
$this->assertNotSameObject($this->request, $request);
110+
$this->assertEquals($this->request, $original, 'Request object MUST not be mutated');
102111
$this->assertEquals('www.foo.com', $request->getHeaderLine('host'));
112+
$this->assertInstanceOf(UriInterface::class, $request->getUri());
103113
$this->assertEquals('http://www.foo.com/bar', (string) $request->getUri());
104114

105115
$request = $request->withUri($this->buildUri('/foobar'));
106116
$this->assertNotSameObject($this->request, $request);
117+
$this->assertEquals($this->request, $original, 'Request object MUST not be mutated');
107118
$this->assertEquals('www.foo.com', $request->getHeaderLine('host'), 'If the URI does not contain a host component, any pre-existing Host header MUST be carried over to the returned request.');
108119
$this->assertEquals('/foobar', (string) $request->getUri());
109120
}

src/ResponseIntegrationTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ public function testStatusCode()
4242
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
4343
}
4444

45+
$original = clone $this->response;
4546
$response = $this->response->withStatus(204);
4647
$this->assertNotSameObject($this->response, $response);
47-
$this->assertEquals(204, $response->getStatusCode());
48+
$this->assertEquals($this->response, $original, 'Response MUST not be mutated');
49+
$this->assertSame(204, $response->getStatusCode());
4850
}
4951

5052
/**
@@ -79,7 +81,7 @@ public function testReasonPhrase()
7981
}
8082

8183
$response = $this->response->withStatus(204, 'Foobar');
82-
$this->assertEquals(204, $response->getStatusCode());
84+
$this->assertSame(204, $response->getStatusCode());
8385
$this->assertEquals('Foobar', $response->getReasonPhrase());
8486
}
8587
}

src/ServerRequestIntegrationTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function testGetUploadedFiles()
8585
$this->assertEmpty($this->serverRequest->getUploadedFiles(), 'withUploadedFiles MUST be immutable');
8686

8787
$files = $new->getUploadedFiles();
88-
$this->assertEquals(1, count($files));
88+
$this->assertCount(1, $files);
8989
$this->assertEquals($file, $files[0]);
9090
}
9191

@@ -162,7 +162,7 @@ public function testGetAttribute()
162162
$new = $this->serverRequest->withAttribute('foo', 'bar');
163163
$this->assertEquals('bar', $new->getAttribute('foo'));
164164
$this->assertEquals('baz', $new->getAttribute('not found', 'baz'));
165-
$this->assertEquals(null, $new->getAttribute('not found'));
165+
$this->assertNull($new->getAttribute('not found'));
166166
}
167167

168168
public function testWithoutAttribute()
@@ -175,6 +175,6 @@ public function testWithoutAttribute()
175175
$without = $with->withoutAttribute('foo');
176176

177177
$this->assertEquals('bar', $with->getAttribute('foo'), 'withoutAttribute MUST be immutable');
178-
$this->assertEquals(null, $without->getAttribute('foo'));
178+
$this->assertNull($without->getAttribute('foo'));
179179
}
180180
}

src/StreamIntegrationTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ public function testTell()
101101
fwrite($resource, 'abcdef');
102102
$stream = $this->createStream($resource);
103103

104-
$this->assertEquals(6, $stream->tell());
104+
$this->assertSame(6, $stream->tell());
105105
$stream->seek(0);
106-
$this->assertEquals(0, $stream->tell());
106+
$this->assertSame(0, $stream->tell());
107107
$stream->seek(3);
108-
$this->assertEquals(3, $stream->tell());
108+
$this->assertSame(3, $stream->tell());
109109
$stream->seek(6);
110-
$this->assertEquals(6, $stream->tell());
110+
$this->assertSame(6, $stream->tell());
111111
}
112112

113113
public function testEof()
@@ -263,7 +263,7 @@ public function testWrite()
263263
$stream = $this->createStream($resource);
264264
$bytes = $stream->write('def');
265265

266-
$this->assertEquals(3, $bytes);
266+
$this->assertSame(3, $bytes);
267267
$this->assertEquals('abcdef', (string) $stream);
268268
}
269269

@@ -298,6 +298,6 @@ public function testGetContents()
298298

299299
$stream->seek(3);
300300
$this->assertEquals('def', $stream->getContents());
301-
$this->assertEquals('', $stream->getContents());
301+
$this->assertSame('', $stream->getContents());
302302
}
303303
}

src/UriIntegrationTest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function testScheme()
2828
}
2929

3030
$uri = $this->createUri('/');
31-
$this->assertEquals('', $uri->getScheme());
31+
$this->assertSame('', $uri->getScheme());
3232

3333
$uri = $this->createUri('https://foo.com/');
3434
$this->assertEquals('https', $uri->getScheme());
@@ -103,7 +103,7 @@ public function testHost()
103103
}
104104

105105
$uri = $this->createUri('/');
106-
$this->assertEquals('', $uri->getHost());
106+
$this->assertSame('', $uri->getHost());
107107

108108
$uri = $this->createUri('http://www.foo.com/');
109109
$this->assertEquals('www.foo.com', $uri->getHost());
@@ -128,7 +128,7 @@ public function testPort()
128128
$this->assertNull($uri->getPort());
129129

130130
$uri = $this->createUri('http://www.foo.com:81/');
131-
$this->assertEquals(81, $uri->getPort());
131+
$this->assertSame(81, $uri->getPort());
132132
}
133133

134134
/**
@@ -140,7 +140,7 @@ public function testPath(UriInterface $uri, $expected)
140140
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
141141
}
142142

143-
$this->assertEquals($expected, $uri->getPath());
143+
$this->assertSame($expected, $uri->getPath());
144144
}
145145

146146
public function getPaths()
@@ -164,7 +164,7 @@ public function testQuery(UriInterface $uri, $expected)
164164
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
165165
}
166166

167-
$this->assertEquals($expected, $uri->getQuery());
167+
$this->assertSame($expected, $uri->getQuery());
168168
}
169169

170170
public function getQueries()
@@ -205,6 +205,7 @@ public function testUriModification1()
205205
$expected = 'https://0:0@0:1/0?0#0';
206206
$uri = $this->createUri($expected);
207207

208+
$this->assertInstanceOf(UriInterface::class, $uri);
208209
$this->assertSame($expected, (string) $uri);
209210
}
210211

@@ -222,6 +223,7 @@ public function testUriModification2()
222223
->withFragment('0')
223224
;
224225

226+
$this->assertInstanceOf(UriInterface::class, $uri);
225227
$this->assertSame($expected, (string) $uri);
226228
}
227229
}

0 commit comments

Comments
 (0)