Skip to content

Commit

Permalink
Improve tests (#37)
Browse files Browse the repository at this point in the history
* Use more specific assertions
* Add more invalid cases to METHOD verb
* Test that messages are indeed immutable
  • Loading branch information
gmponos authored and dbu committed Dec 13, 2018
1 parent 031eb86 commit d2b5960
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 22 deletions.
30 changes: 24 additions & 6 deletions src/MessageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ public function testProtocolVersion()
}

$initialMessage = $this->getMessage();
$original = clone $initialMessage;

$message = $initialMessage->withProtocolVersion('1.0');

$this->assertNotSameObject($initialMessage, $message);
$this->assertEquals($initialMessage, $original, 'Message object MUST not be mutated');

$this->assertEquals('1.0', $message->getProtocolVersion());
$this->assertSame('1.0', $message->getProtocolVersion());
}

public function testGetHeaders()
Expand All @@ -35,12 +39,21 @@ public function testGetHeaders()
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}

$message = $this->getMessage()->withAddedHeader('content-type', 'text/html');
$message = $message->withAddedHeader('content-type', 'text/plain');
$initialMessage = $this->getMessage();
$original = clone $initialMessage;

$message = $initialMessage
->withAddedHeader('content-type', 'text/html')
->withAddedHeader('content-type', 'text/plain');

$this->assertEquals($initialMessage, $original, 'Message object MUST not be mutated');

$headers = $message->getHeaders();

$this->assertTrue(isset($headers['content-type']));
$this->assertCount(2, $headers['content-type']);
$this->assertContains('text/html', $headers['content-type']);
$this->assertContains('text/plain', $headers['content-type']);
}

public function testHasHeader()
Expand Down Expand Up @@ -69,7 +82,7 @@ public function testGetHeader()
$this->assertCount(2, $message->getHeader('CONTENT-TYPE'));
$emptyHeader = $message->getHeader('Bar');
$this->assertCount(0, $emptyHeader);
$this->assertTrue(is_array($emptyHeader));
$this->assertInternalType('array', $emptyHeader);
}

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

$this->assertEquals('', $message->getHeaderLine('Bar'));
$this->assertSame('', $message->getHeaderLine('Bar'));
}

public function testWithHeader()
Expand All @@ -94,8 +107,11 @@ public function testWithHeader()
}

$initialMessage = $this->getMessage();
$original = clone $initialMessage;

$message = $initialMessage->withHeader('content-type', 'text/html');
$this->assertNotSameObject($initialMessage, $message);
$this->assertEquals($initialMessage, $original, 'Message object MUST not be mutated');
$this->assertEquals('text/html', $message->getHeaderLine('content-type'));

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

$message = $initialMessage->withHeader('Bar', '');
$this->assertTrue($message->hasHeader('Bar'));
$this->assertEquals([''], $message->getHeader('Bar'));
$this->assertSame([''], $message->getHeader('Bar'));
}

/**
Expand Down Expand Up @@ -227,9 +243,11 @@ public function testBody()
}

$initialMessage = $this->getMessage();
$original = clone $initialMessage;
$stream = $this->buildStream('foo');
$message = $initialMessage->withBody($stream);
$this->assertNotSameObject($initialMessage, $message);
$this->assertEquals($initialMessage, $original, 'Message object MUST not be mutated');

$this->assertEquals($stream, $message->getBody());
}
Expand Down
11 changes: 11 additions & 0 deletions src/RequestIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ public function testRequestTarget()
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}

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

$request = $this->request->withRequestTarget('*');
$this->assertNotSameObject($this->request, $request);
$this->assertEquals($this->request, $original, 'Request object MUST not be mutated');
$this->assertEquals('*', $request->getRequestTarget());
}

Expand All @@ -57,9 +59,11 @@ public function testMethod()
}

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

$request = $this->request->withMethod('POST');
$this->assertNotSameObject($this->request, $request);
$this->assertEquals($this->request, $original, 'Request object MUST not be mutated');
$this->assertEquals('POST', $request->getMethod());

$request = $this->request->withMethod('head');
Expand All @@ -82,6 +86,9 @@ public function testMethodWithInvalidArguments($method)
public function getInvalidMethods()
{
return [
[null],
[1],
[1.01],
[false],
[['foo']],
[new \stdClass()],
Expand All @@ -93,17 +100,21 @@ public function testUri()
if (isset($this->skippedTests[__FUNCTION__])) {
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}
$original = clone $this->request;

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

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

$request = $request->withUri($this->buildUri('/foobar'));
$this->assertNotSameObject($this->request, $request);
$this->assertEquals($this->request, $original, 'Request object MUST not be mutated');
$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.');
$this->assertEquals('/foobar', (string) $request->getUri());
}
Expand Down
6 changes: 4 additions & 2 deletions src/ResponseIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ public function testStatusCode()
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}

$original = clone $this->response;
$response = $this->response->withStatus(204);
$this->assertNotSameObject($this->response, $response);
$this->assertEquals(204, $response->getStatusCode());
$this->assertEquals($this->response, $original, 'Response MUST not be mutated');
$this->assertSame(204, $response->getStatusCode());
}

/**
Expand Down Expand Up @@ -79,7 +81,7 @@ public function testReasonPhrase()
}

$response = $this->response->withStatus(204, 'Foobar');
$this->assertEquals(204, $response->getStatusCode());
$this->assertSame(204, $response->getStatusCode());
$this->assertEquals('Foobar', $response->getReasonPhrase());
}
}
6 changes: 3 additions & 3 deletions src/ServerRequestIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function testGetUploadedFiles()
$this->assertEmpty($this->serverRequest->getUploadedFiles(), 'withUploadedFiles MUST be immutable');

$files = $new->getUploadedFiles();
$this->assertEquals(1, count($files));
$this->assertCount(1, $files);
$this->assertEquals($file, $files[0]);
}

Expand Down Expand Up @@ -162,7 +162,7 @@ public function testGetAttribute()
$new = $this->serverRequest->withAttribute('foo', 'bar');
$this->assertEquals('bar', $new->getAttribute('foo'));
$this->assertEquals('baz', $new->getAttribute('not found', 'baz'));
$this->assertEquals(null, $new->getAttribute('not found'));
$this->assertNull($new->getAttribute('not found'));
}

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

$this->assertEquals('bar', $with->getAttribute('foo'), 'withoutAttribute MUST be immutable');
$this->assertEquals(null, $without->getAttribute('foo'));
$this->assertNull($without->getAttribute('foo'));
}
}
12 changes: 6 additions & 6 deletions src/StreamIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ public function testTell()
fwrite($resource, 'abcdef');
$stream = $this->createStream($resource);

$this->assertEquals(6, $stream->tell());
$this->assertSame(6, $stream->tell());
$stream->seek(0);
$this->assertEquals(0, $stream->tell());
$this->assertSame(0, $stream->tell());
$stream->seek(3);
$this->assertEquals(3, $stream->tell());
$this->assertSame(3, $stream->tell());
$stream->seek(6);
$this->assertEquals(6, $stream->tell());
$this->assertSame(6, $stream->tell());
}

public function testEof()
Expand Down Expand Up @@ -263,7 +263,7 @@ public function testWrite()
$stream = $this->createStream($resource);
$bytes = $stream->write('def');

$this->assertEquals(3, $bytes);
$this->assertSame(3, $bytes);
$this->assertEquals('abcdef', (string) $stream);
}

Expand Down Expand Up @@ -298,6 +298,6 @@ public function testGetContents()

$stream->seek(3);
$this->assertEquals('def', $stream->getContents());
$this->assertEquals('', $stream->getContents());
$this->assertSame('', $stream->getContents());
}
}
12 changes: 7 additions & 5 deletions src/UriIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testScheme()
}

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

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

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

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

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

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

$this->assertEquals($expected, $uri->getPath());
$this->assertSame($expected, $uri->getPath());
}

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

$this->assertEquals($expected, $uri->getQuery());
$this->assertSame($expected, $uri->getQuery());
}

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

$this->assertInstanceOf(UriInterface::class, $uri);
$this->assertSame($expected, (string) $uri);
}

Expand All @@ -222,6 +223,7 @@ public function testUriModification2()
->withFragment('0')
;

$this->assertInstanceOf(UriInterface::class, $uri);
$this->assertSame($expected, (string) $uri);
}
}

0 comments on commit d2b5960

Please sign in to comment.