diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2018-09-03 22:43:19 +0200 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2018-09-03 22:55:54 +0200 |
commit | 36dafdb68acbde2fe42ce36ef50f497c8c06411f (patch) | |
tree | 1e420597ae72c979361bf29b66ae7e27c73cf431 /tests/Unit/Http | |
parent | 9f1ee0c6c6497d43fb275491ec53fda420f64b81 (diff) | |
parent | b0e7bc0df2eb4975223582089c7a928903e8cd14 (diff) |
Merge remote-tracking branch 'MyIgel/rebuild-psr7'
Diffstat (limited to 'tests/Unit/Http')
-rw-r--r-- | tests/Unit/Http/MessageTraitRequestTest.php | 50 | ||||
-rw-r--r-- | tests/Unit/Http/MessageTraitResponseTest.php | 159 | ||||
-rw-r--r-- | tests/Unit/Http/Psr7ServiceProviderTest.php | 56 | ||||
-rw-r--r-- | tests/Unit/Http/RequestTest.php | 283 | ||||
-rw-r--r-- | tests/Unit/Http/ResponseServiceProviderTest.php | 29 | ||||
-rw-r--r-- | tests/Unit/Http/ResponseTest.php | 49 | ||||
-rw-r--r-- | tests/Unit/Http/Stub/MessageTraitRequestImplementation.php | 12 | ||||
-rw-r--r-- | tests/Unit/Http/Stub/MessageTraitResponseImplementation.php | 12 |
8 files changed, 650 insertions, 0 deletions
diff --git a/tests/Unit/Http/MessageTraitRequestTest.php b/tests/Unit/Http/MessageTraitRequestTest.php new file mode 100644 index 00000000..7430b5d7 --- /dev/null +++ b/tests/Unit/Http/MessageTraitRequestTest.php @@ -0,0 +1,50 @@ +<?php + +namespace Engelsystem\Test\Unit\Http; + +use Engelsystem\Test\Unit\Http\Stub\MessageTraitRequestImplementation; +use PHPUnit\Framework\TestCase; +use Zend\Diactoros\Stream; + +class MessageTraitRequestTest extends TestCase +{ + /** + * @covers \Engelsystem\Http\MessageTrait::withProtocolVersion + */ + public function testWithProtocolVersion() + { + $message = new MessageTraitRequestImplementation(); + $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 MessageTraitRequestImplementation(); + $newMessage = $message->withHeader('lorem', 'ipsum'); + + $this->assertNotEquals($message, $newMessage); + $this->assertArraySubset(['lorem' => ['ipsum']], $newMessage->getHeaders()); + } + + /** + * @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 MessageTraitRequestImplementation(); + $newMessage = $message->withBody($stream); + + $this->assertNotEquals($message, $newMessage); + $this->assertEquals('Test content', $newMessage->getContent()); + } +} diff --git a/tests/Unit/Http/MessageTraitResponseTest.php b/tests/Unit/Http/MessageTraitResponseTest.php new file mode 100644 index 00000000..f60360a3 --- /dev/null +++ b/tests/Unit/Http/MessageTraitResponseTest.php @@ -0,0 +1,159 @@ +<?php + +namespace Engelsystem\Test\Unit\Http; + +use Engelsystem\Test\Unit\Http\Stub\MessageTraitResponseImplementation; +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 MessageTraitResponseTest extends TestCase +{ + /** + * @covers \Engelsystem\Http\MessageTrait + */ + public function testCreate() + { + $message = new MessageTraitResponseImplementation(); + $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 MessageTraitResponseImplementation(); + $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 MessageTraitResponseImplementation(); + $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 MessageTraitResponseImplementation(); + $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 MessageTraitResponseImplementation(); + $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 MessageTraitResponseImplementation(); + $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 MessageTraitResponseImplementation(); + $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 MessageTraitResponseImplementation(); + $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 MessageTraitResponseImplementation())->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 MessageTraitResponseImplementation())->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 MessageTraitResponseImplementation(); + $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 new file mode 100644 index 00000000..e14daf2a --- /dev/null +++ b/tests/Unit/Http/Psr7ServiceProviderTest.php @@ -0,0 +1,56 @@ +<?php + +namespace Engelsystem\Test\Unit\Http; + +use Engelsystem\Http\Psr7ServiceProvider; +use Engelsystem\Http\Request; +use Engelsystem\Http\Response; +use Engelsystem\Test\Unit\ServiceProviderTest; +use PHPUnit_Framework_MockObject_MockObject as MockObject; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface as RequestInterface; +use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory; + +class Psr7ServiceProviderTest extends ServiceProviderTest +{ + /** + * @covers \Engelsystem\Http\Psr7ServiceProvider::register() + */ + public function testRegister() + { + /** @var MockObject|DiactorosFactory $psr7Factory */ + $psr7Factory = $this->createMock(DiactorosFactory::class); + /** @var MockObject|Request $request */ + $request = $this->createMock(Request::class); + /** @var MockObject|Response $response */ + $response = $this->createMock(Response::class); + /** @var MockObject|RequestInterface $psr7request */ + $psr7request = $this->createMock(Request::class); + /** @var MockObject|ResponseInterface $psr7response */ + $psr7response = $this->createMock(Response::class); + + $app = $this->getApp(['make', 'instance', 'get', 'bind']); + $this->setExpects($app, 'make', [DiactorosFactory::class], $psr7Factory); + + $app->expects($this->atLeastOnce()) + ->method('get') + ->withConsecutive(['request'], ['response']) + ->willReturnOnConsecutiveCalls($request, $response); + $app->expects($this->atLeastOnce()) + ->method('instance') + ->withConsecutive( + ['psr7.factory', $psr7Factory], + ['psr7.request', $psr7request], + ['psr7.response', $psr7response] + ); + $app->expects($this->atLeastOnce()) + ->method('bind') + ->withConsecutive( + [RequestInterface::class, 'psr7.request'], + [ResponseInterface::class, 'psr7.response'] + ); + + $serviceProvider = new Psr7ServiceProvider($app); + $serviceProvider->register(); + } +} diff --git a/tests/Unit/Http/RequestTest.php b/tests/Unit/Http/RequestTest.php index a68f8b8f..916aac35 100644 --- a/tests/Unit/Http/RequestTest.php +++ b/tests/Unit/Http/RequestTest.php @@ -5,10 +5,25 @@ namespace Engelsystem\Test\Unit\Http; use Engelsystem\Http\Request; use PHPUnit\Framework\TestCase; use PHPUnit_Framework_MockObject_MockObject as MockObject; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\UploadedFileInterface; +use Psr\Http\Message\UriInterface; +use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyFile; +use Symfony\Component\HttpFoundation\Request as SymfonyRequest; class RequestTest extends TestCase { /** + * @covers \Engelsystem\Http\Request + */ + public function testCreate() + { + $response = new Request(); + $this->assertInstanceOf(SymfonyRequest::class, $response); + $this->assertInstanceOf(RequestInterface::class, $response); + } + + /** * @covers \Engelsystem\Http\Request::postData */ public function testPostData() @@ -96,4 +111,272 @@ class RequestTest extends TestCase $this->assertEquals('http://foo.bar/bla/foo', $request->url()); $this->assertEquals('https://lorem.ipsum/dolor/sit', $request->url()); } + + /** + * @covers \Engelsystem\Http\Request::getRequestTarget + */ + public function testGetRequestTarget() + { + /** @var Request|MockObject $request */ + $request = $this + ->getMockBuilder(Request::class) + ->setMethods(['getQueryString', 'path']) + ->getMock(); + + $request->expects($this->exactly(2)) + ->method('getQueryString') + ->willReturnOnConsecutiveCalls(null, 'foo=bar&lorem=ipsum'); + $request->expects($this->exactly(2)) + ->method('path') + ->willReturn('foo/bar'); + + $this->assertEquals('/foo/bar', $request->getRequestTarget()); + $this->assertEquals('/foo/bar?foo=bar&lorem=ipsum', $request->getRequestTarget()); + } + + /** + * @covers \Engelsystem\Http\Request::withRequestTarget + */ + public function testWithRequestTarget() + { + $request = new Request(); + foreach ( + [ + '*', + '/foo/bar', + 'https://lorem.ipsum/test?lor=em' + ] as $target + ) { + $new = $request->withRequestTarget($target); + $this->assertNotEquals($request, $new); + } + } + + /** + * @covers \Engelsystem\Http\Request::withMethod + */ + public function testWithMethod() + { + $request = new Request(); + + $new = $request->withMethod('PUT'); + + $this->assertNotEquals($request, $new); + $this->assertEquals('PUT', $new->getMethod()); + } + + /** + * @covers \Engelsystem\Http\Request::withUri + */ + public function testWithUri() + { + /** @var UriInterface|MockObject $uri */ + $uri = $this->getMockForAbstractClass(UriInterface::class); + + $uri->expects($this->atLeastOnce()) + ->method('__toString') + ->willReturn('http://foo.bar/bla?foo=bar'); + + $request = Request::create('http://lor.em/'); + + $new = $request->withUri($uri); + $this->assertNotEquals($request, $new); + $this->assertEquals('http://foo.bar/bla?foo=bar', (string)$new->getUri()); + + $new = $request->withUri($uri, true); + $this->assertEquals('http://lor.em/bla?foo=bar', (string)$new->getUri()); + } + + /** + * @covers \Engelsystem\Http\Request::getUri + */ + public function testGetUri() + { + $request = Request::create('http://lor.em/test?bla=foo'); + + $uri = $request->getUri(); + $this->assertInstanceOf(UriInterface::class, $uri); + $this->assertEquals('http://lor.em/test?bla=foo', (string)$uri); + } + + /** + * @covers \Engelsystem\Http\Request::getServerParams + */ + public function testGetServerParams() + { + $server = ['foo' => 'bar']; + $request = new Request([], [], [], [], [], $server); + + $this->assertEquals($server, $request->getServerParams()); + } + + /** + * @covers \Engelsystem\Http\Request::getCookieParams + */ + public function testGetCookieParams() + { + $cookies = ['session' => 'LoremIpsumDolorSit']; + $request = new Request([], [], [], $cookies); + + $this->assertEquals($cookies, $request->getCookieParams()); + } + + /** + * @covers \Engelsystem\Http\Request::withCookieParams + */ + public function testWithCookieParams() + { + $cookies = ['lor' => 'em']; + $request = new Request(); + + $new = $request->withCookieParams($cookies); + + $this->assertNotEquals($request, $new); + $this->assertEquals($cookies, $new->getCookieParams()); + } + + /** + * @covers \Engelsystem\Http\Request::getQueryParams + */ + public function testGetQueryParams() + { + $params = ['foo' => 'baz']; + $request = new Request($params); + + $this->assertEquals($params, $request->getQueryParams()); + } + + /** + * @covers \Engelsystem\Http\Request::withQueryParams + */ + public function testWithQueryParams() + { + $params = ['test' => 'ing']; + $request = new Request(); + + $new = $request->withQueryParams($params); + + $this->assertNotEquals($request, $new); + $this->assertEquals($params, $new->getQueryParams()); + } + + /** + * @covers \Engelsystem\Http\Request::getUploadedFiles + */ + public function testGetUploadedFiles() + { + $filename = tempnam(sys_get_temp_dir(), 'test'); + file_put_contents($filename, 'LoremIpsum!'); + $files = [new SymfonyFile($filename, 'foo.html', 'text/html', 11)]; + $request = new Request([], [], [], [], $files); + + $uploadedFiles = $request->getUploadedFiles(); + $this->assertNotEmpty($uploadedFiles); + + /** @var UploadedFileInterface $file */ + $file = $uploadedFiles[0]; + $this->assertInstanceOf(UploadedFileInterface::class, $file); + $this->assertEquals('foo.html', $file->getClientFilename()); + $this->assertEquals('text/html', $file->getClientMediaType()); + $this->assertEquals(11, $file->getSize()); + } + + /** + * @covers \Engelsystem\Http\Request::withUploadedFiles + */ + public function testWithUploadedFiles() + { + $filename = tempnam(sys_get_temp_dir(), 'test'); + file_put_contents($filename, 'LoremIpsum!'); + $file = new \Zend\Diactoros\UploadedFile($filename, 11, UPLOAD_ERR_OK, 'test.txt', 'text/plain'); + + $request = new Request(); + $new = $request->withUploadedFiles([$file]); + $uploadedFiles = $new->getUploadedFiles(); + $this->assertNotEquals($request, $new); + $this->assertNotEmpty($uploadedFiles); + + /** @var UploadedFileInterface $file */ + $file = $uploadedFiles[0]; + $this->assertEquals('test.txt', $file->getClientFilename()); + $this->assertEquals('text/plain', $file->getClientMediaType()); + $this->assertEquals(11, $file->getSize()); + } + + /** + * @covers \Engelsystem\Http\Request::getParsedBody + */ + public function testGetParsedBody() + { + $body = ['foo' => 'lorem']; + $request = new Request(); + $request->request->add($body); + + $this->assertEquals($body, $request->getParsedBody()); + } + + /** + * @covers \Engelsystem\Http\Request::withParsedBody + */ + public function testWithParsedBody() + { + $data = ['test' => 'er']; + $request = new Request(); + + $new = $request->withParsedBody($data); + + $this->assertNotEquals($request, $new); + $this->assertEquals($data, $new->getParsedBody()); + } + + /** + * @covers \Engelsystem\Http\Request::getAttributes + */ + public function testGetAttributes() + { + $attributes = ['foo' => 'lorem', 'ipsum' => 'dolor']; + $request = new Request([], [], $attributes); + + $this->assertEquals($attributes, $request->getAttributes()); + } + + /** + * @covers \Engelsystem\Http\Request::getAttribute + */ + public function testGetAttribute() + { + $attributes = ['foo' => 'lorem', 'ipsum' => 'dolor']; + $request = new Request([], [], $attributes); + + $this->assertEquals($attributes['ipsum'], $request->getAttribute('ipsum')); + $this->assertEquals(null, $request->getAttribute('dolor')); + $this->assertEquals(1234, $request->getAttribute('test', 1234)); + } + + /** + * @covers \Engelsystem\Http\Request::withAttribute + */ + public function testWithAttribute() + { + $request = new Request(); + + $new = $request->withAttribute('lorem', 'ipsum'); + + $this->assertNotEquals($request, $new); + $this->assertEquals('ipsum', $new->getAttribute('lorem')); + } + + /** + * @covers \Engelsystem\Http\Request::withoutAttribute + */ + public function testWithoutAttribute() + { + $attributes = ['foo' => 'lorem', 'ipsum' => 'dolor']; + $request = new Request([], [], $attributes); + + $new = $request->withoutAttribute('ipsum'); + + $this->assertNotEquals($request, $new); + $this->assertEquals(['foo' => 'lorem'], $new->getAttributes()); + } } diff --git a/tests/Unit/Http/ResponseServiceProviderTest.php b/tests/Unit/Http/ResponseServiceProviderTest.php new file mode 100644 index 00000000..52e95714 --- /dev/null +++ b/tests/Unit/Http/ResponseServiceProviderTest.php @@ -0,0 +1,29 @@ +<?php + +namespace Engelsystem\Test\Unit\Http; + +use Engelsystem\Http\Response; +use Engelsystem\Http\ResponseServiceProvider; +use Engelsystem\Test\Unit\ServiceProviderTest; +use PHPUnit_Framework_MockObject_MockObject as MockObject; + +class ResponseServiceProviderTest extends ServiceProviderTest +{ + /** + * @covers \Engelsystem\Http\ResponseServiceProvider::register() + */ + public function testRegister() + { + /** @var MockObject|Response $response */ + $response = $this->getMockBuilder(Response::class) + ->getMock(); + + $app = $this->getApp(); + + $this->setExpects($app, 'make', [Response::class], $response); + $this->setExpects($app, 'instance', ['response', $response]); + + $serviceProvider = new ResponseServiceProvider($app); + $serviceProvider->register(); + } +} diff --git a/tests/Unit/Http/ResponseTest.php b/tests/Unit/Http/ResponseTest.php new file mode 100644 index 00000000..f6c24767 --- /dev/null +++ b/tests/Unit/Http/ResponseTest.php @@ -0,0 +1,49 @@ +<?php + +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 +{ + /** + * @covers \Engelsystem\Http\Response + */ + public function testCreate() + { + $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/MessageTraitRequestImplementation.php b/tests/Unit/Http/Stub/MessageTraitRequestImplementation.php new file mode 100644 index 00000000..04d08913 --- /dev/null +++ b/tests/Unit/Http/Stub/MessageTraitRequestImplementation.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\Request; + +class MessageTraitRequestImplementation extends Request implements MessageInterface +{ + use MessageTrait; +} diff --git a/tests/Unit/Http/Stub/MessageTraitResponseImplementation.php b/tests/Unit/Http/Stub/MessageTraitResponseImplementation.php new file mode 100644 index 00000000..2ec4b943 --- /dev/null +++ b/tests/Unit/Http/Stub/MessageTraitResponseImplementation.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 MessageTraitResponseImplementation extends Response implements MessageInterface +{ + use MessageTrait; +} |