diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2018-08-11 15:05:55 +0200 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2018-08-14 00:17:19 +0200 |
commit | f3f05f6cc4bef3338dbfb6eb340da4fb1c5ba1e1 (patch) | |
tree | 88f9767bfb40a68776d4f458ac60c6edfd02e286 /tests/Unit | |
parent | 20c03a155d2017101a098cefa602116a4a331d71 (diff) |
Make Engelsystem\Http\Response PSR-7 compatible
Diffstat (limited to 'tests/Unit')
-rw-r--r-- | tests/Unit/HelpersTest.php | 28 | ||||
-rw-r--r-- | tests/Unit/Http/MessageTraitTest.php | 159 | ||||
-rw-r--r-- | tests/Unit/Http/Psr7ServiceProviderTest.php | 4 | ||||
-rw-r--r-- | tests/Unit/Http/ResponseTest.php | 30 | ||||
-rw-r--r-- | tests/Unit/Http/Stub/MessageTraitImplementation.php | 12 |
5 files changed, 229 insertions, 4 deletions
diff --git a/tests/Unit/HelpersTest.php b/tests/Unit/HelpersTest.php index 43c29c84..82030169 100644 --- a/tests/Unit/HelpersTest.php +++ b/tests/Unit/HelpersTest.php @@ -6,6 +6,7 @@ use Engelsystem\Application; use Engelsystem\Config\Config; use Engelsystem\Container\Container; use Engelsystem\Http\Request; +use Engelsystem\Http\Response; use Engelsystem\Renderer\Renderer; use Engelsystem\Routing\UrlGenerator; use PHPUnit\Framework\TestCase; @@ -127,6 +128,33 @@ class HelpersTest extends TestCase } /** + * @covers \response + */ + public function testResponse() + { + /** @var MockObject|Response $response */ + $response = $this->getMockBuilder(Response::class)->getMock(); + $this->getAppMock('psr7.response', $response); + + $response->expects($this->once()) + ->method('withContent') + ->with('Lorem Ipsum?') + ->willReturn($response); + + $response->expects($this->once()) + ->method('withStatus') + ->with(501) + ->willReturn($response); + + $response->expects($this->exactly(2)) + ->method('withAddedHeader') + ->withConsecutive(['lor', 'em'], ['foo', 'bar']) + ->willReturn($response); + + $this->assertEquals($response, response('Lorem Ipsum?', 501, ['lor' => 'em', 'foo' => 'bar',])); + } + + /** * @covers \session */ public function testSession() diff --git a/tests/Unit/Http/MessageTraitTest.php b/tests/Unit/Http/MessageTraitTest.php new file mode 100644 index 00000000..46076a67 --- /dev/null +++ b/tests/Unit/Http/MessageTraitTest.php @@ -0,0 +1,159 @@ +<?php + +namespace Engelsystem\Test\Unit\Http; + +use Engelsystem\Test\Unit\Http\Stub\MessageTraitImplementation; +use PHPUnit\Framework\TestCase; +use Psr\Http\Message\MessageInterface; +use Psr\Http\Message\StreamInterface; +use Symfony\Component\HttpFoundation\Response as SymfonyResponse; +use Zend\Diactoros\Stream; + +class MessageTraitTest extends TestCase +{ + /** + * @covers \Engelsystem\Http\MessageTrait + */ + public function testCreate() + { + $message = new MessageTraitImplementation(); + $this->assertInstanceOf(MessageInterface::class, $message); + $this->assertInstanceOf(SymfonyResponse::class, $message); + } + + /** + * @covers \Engelsystem\Http\MessageTrait::getProtocolVersion + * @covers \Engelsystem\Http\MessageTrait::withProtocolVersion + */ + public function testGetProtocolVersion() + { + $message = new MessageTraitImplementation(); + $newMessage = $message->withProtocolVersion('0.1'); + $this->assertNotEquals($message, $newMessage); + $this->assertEquals('0.1', $newMessage->getProtocolVersion()); + } + + /** + * @covers \Engelsystem\Http\MessageTrait::getHeaders + */ + public function testGetHeaders() + { + $message = new MessageTraitImplementation(); + $newMessage = $message->withHeader('Foo', 'bar'); + + $this->assertNotEquals($message, $newMessage); + $this->assertArraySubset(['Foo' => ['bar']], $newMessage->getHeaders()); + + $newMessage = $message->withHeader('lorem', ['ipsum', 'dolor']); + $this->assertArraySubset(['lorem' => ['ipsum', 'dolor']], $newMessage->getHeaders()); + } + + /** + * @covers \Engelsystem\Http\MessageTrait::hasHeader + */ + public function testHasHeader() + { + $message = new MessageTraitImplementation(); + $this->assertFalse($message->hasHeader('test')); + + $newMessage = $message->withHeader('test', '12345'); + $this->assertTrue($newMessage->hasHeader('Test')); + $this->assertTrue($newMessage->hasHeader('test')); + } + + /** + * @covers \Engelsystem\Http\MessageTrait::getHeader + */ + public function testGetHeader() + { + $message = new MessageTraitImplementation(); + $newMessage = $message->withHeader('foo', 'bar'); + + $this->assertEquals(['bar'], $newMessage->getHeader('Foo')); + $this->assertEquals([], $newMessage->getHeader('LoremIpsum')); + } + + /** + * @covers \Engelsystem\Http\MessageTrait::getHeaderLine + */ + public function testGetHeaderLine() + { + $message = new MessageTraitImplementation(); + $newMessage = $message->withHeader('foo', ['bar', 'bla']); + + $this->assertEquals('', $newMessage->getHeaderLine('Lorem-Ipsum')); + $this->assertEquals('bar,bla', $newMessage->getHeaderLine('Foo')); + } + + /** + * @covers \Engelsystem\Http\MessageTrait::withHeader + */ + public function testWithHeader() + { + $message = new MessageTraitImplementation(); + $newMessage = $message->withHeader('foo', 'bar'); + + $this->assertNotEquals($message, $newMessage); + $this->assertArraySubset(['foo' => ['bar']], $newMessage->getHeaders()); + + $newMessage = $newMessage->withHeader('Foo', ['lorem', 'ipsum']); + $this->assertArraySubset(['Foo' => ['lorem', 'ipsum']], $newMessage->getHeaders()); + } + + /** + * @covers \Engelsystem\Http\MessageTrait::withAddedHeader + */ + public function testWithAddedHeader() + { + $message = new MessageTraitImplementation(); + $newMessage = $message->withHeader('foo', 'bar'); + + $this->assertNotEquals($message, $newMessage); + $this->assertArraySubset(['foo' => ['bar']], $newMessage->getHeaders()); + + $newMessage = $newMessage->withAddedHeader('Foo', ['lorem', 'ipsum']); + $this->assertArraySubset(['Foo' => ['bar', 'lorem', 'ipsum']], $newMessage->getHeaders()); + } + + /** + * @covers \Engelsystem\Http\MessageTrait::withoutHeader + */ + public function testWithoutHeader() + { + $message = (new MessageTraitImplementation())->withHeader('foo', 'bar'); + $this->assertTrue($message->hasHeader('foo')); + + $newMessage = $message->withoutHeader('Foo'); + $this->assertNotEquals($message, $newMessage); + $this->assertFalse($newMessage->hasHeader('foo')); + } + + /** + * @covers \Engelsystem\Http\MessageTrait::getBody + */ + public function testGetBody() + { + $message = (new MessageTraitImplementation())->setContent('Foo bar!'); + $body = $message->getBody(); + + $this->assertInstanceOf(StreamInterface::class, $body); + $this->assertEquals('Foo bar!', $body->getContents()); + } + + /** + * @covers \Engelsystem\Http\MessageTrait::withBody + */ + public function testWithBody() + { + /** @var Stream $stream */ + $stream = new Stream('php://memory', 'wb+'); + $stream->write('Test content'); + $stream->rewind(); + + $message = new MessageTraitImplementation(); + $newMessage = $message->withBody($stream); + + $this->assertNotEquals($message, $newMessage); + $this->assertEquals('Test content', $newMessage->getContent()); + } +} diff --git a/tests/Unit/Http/Psr7ServiceProviderTest.php b/tests/Unit/Http/Psr7ServiceProviderTest.php index b4c3c042..a09e9572 100644 --- a/tests/Unit/Http/Psr7ServiceProviderTest.php +++ b/tests/Unit/Http/Psr7ServiceProviderTest.php @@ -54,10 +54,6 @@ class Psr7ServiceProviderTest extends ServiceProviderTest ->method('createRequest') ->with($request) ->willReturn($psr7request); - $psr7Factory->expects($this->once()) - ->method('createResponse') - ->with($response) - ->willReturn($psr7response); $serviceProvider = new Psr7ServiceProvider($app); $serviceProvider->register(); diff --git a/tests/Unit/Http/ResponseTest.php b/tests/Unit/Http/ResponseTest.php index 6bedf5c1..f6c24767 100644 --- a/tests/Unit/Http/ResponseTest.php +++ b/tests/Unit/Http/ResponseTest.php @@ -4,6 +4,7 @@ namespace Engelsystem\Test\Unit\Http; use Engelsystem\Http\Response; use PHPUnit\Framework\TestCase; +use Psr\Http\Message\ResponseInterface; use Symfony\Component\HttpFoundation\Response as SymfonyResponse; class ResponseTest extends TestCase @@ -15,5 +16,34 @@ class ResponseTest extends TestCase { $response = new Response(); $this->assertInstanceOf(SymfonyResponse::class, $response); + $this->assertInstanceOf(ResponseInterface::class, $response); + } + + /** + * @covers \Engelsystem\Http\Response::withStatus + * @covers \Engelsystem\Http\Response::getReasonPhrase + */ + public function testWithStatus() + { + $response = new Response(); + $newResponse = $response->withStatus(503); + $this->assertNotEquals($response, $newResponse); + $this->assertNotEquals('', $newResponse->getReasonPhrase()); + $this->assertEquals(503, $newResponse->getStatusCode()); + + $newResponse = $response->withStatus(503, 'Foo'); + $this->assertEquals('Foo', $newResponse->getReasonPhrase()); + } + + /** + * @covers \Engelsystem\Http\Response::withContent + */ + public function testWithContent() + { + $response = new Response(); + $newResponse = $response->withContent('Lorem Ipsum?'); + + $this->assertNotEquals($response, $newResponse); + $this->assertEquals('Lorem Ipsum?', $newResponse->getContent()); } } diff --git a/tests/Unit/Http/Stub/MessageTraitImplementation.php b/tests/Unit/Http/Stub/MessageTraitImplementation.php new file mode 100644 index 00000000..d78fd0b2 --- /dev/null +++ b/tests/Unit/Http/Stub/MessageTraitImplementation.php @@ -0,0 +1,12 @@ +<?php + +namespace Engelsystem\Test\Unit\Http\Stub; + +use Engelsystem\Http\MessageTrait; +use Psr\Http\Message\MessageInterface; +use Symfony\Component\HttpFoundation\Response; + +class MessageTraitImplementation extends Response implements MessageInterface +{ + use MessageTrait; +} |