From d9f898a0bba0d44d8212ba2459f7c77808f02643 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sun, 17 Mar 2019 18:17:33 +0100 Subject: [PATCH] Bugfix: Replaced Diactoros with Symfony PsrHttpFactory to use PhpExtended\HttpMessage implementation Prevents warnings that get converted to errors when run in dev mode --- composer.json | 4 +- src/Http/MessageTrait.php | 6 +- src/Http/Psr7ServiceProvider.php | 35 +++++++--- src/Http/Request.php | 14 ++-- tests/Unit/Http/MessageTraitRequestTest.php | 8 +-- tests/Unit/Http/MessageTraitResponseTest.php | 8 +-- tests/Unit/Http/Psr7ServiceProviderTest.php | 68 +++++++++----------- tests/Unit/Http/RequestTest.php | 9 +-- 8 files changed, 77 insertions(+), 75 deletions(-) diff --git a/composer.json b/composer.json index e40899a4..d2628534 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,7 @@ "illuminate/database": "5.5.*", "illuminate/support": "5.5.*", "nikic/fast-route": "^1.3", + "php-extended/php-http-message-factory-psr17": "^1.0", "psr/container": "^1.0", "psr/http-server-middleware": "^1.0", "psr/log": "^1.0", @@ -35,8 +36,7 @@ "symfony/http-foundation": "^3.3", "symfony/psr-http-message-bridge": "^1.0", "twig/extensions": "^1.5", - "twig/twig": "~2.6.0", - "zendframework/zend-diactoros": "^1.7" + "twig/twig": "~2.6.0" }, "require-dev": { "filp/whoops": "^2.1", diff --git a/src/Http/MessageTrait.php b/src/Http/MessageTrait.php index e46d291e..63ff3682 100644 --- a/src/Http/MessageTrait.php +++ b/src/Http/MessageTrait.php @@ -3,8 +3,8 @@ namespace Engelsystem\Http; +use PhpExtended\HttpMessage\StringStream; use Psr\Http\Message\StreamInterface; -use Zend\Diactoros\Stream; /** * @implements \Psr\Http\Message\MessageInterface @@ -213,9 +213,7 @@ trait MessageTrait */ public function getBody() { - $stream = new Stream('php://memory', 'wb+'); - $stream->write($this->getContent()); - $stream->rewind(); + $stream = new StringStream($this->getContent()); return $stream; } diff --git a/src/Http/Psr7ServiceProvider.php b/src/Http/Psr7ServiceProvider.php index 72fdef8e..fe4449ff 100644 --- a/src/Http/Psr7ServiceProvider.php +++ b/src/Http/Psr7ServiceProvider.php @@ -3,27 +3,42 @@ namespace Engelsystem\Http; use Engelsystem\Container\ServiceProvider; +use PhpExtended\HttpMessage\ResponseFactory; +use PhpExtended\HttpMessage\ServerRequestFactory; +use PhpExtended\HttpMessage\StreamFactory; +use PhpExtended\HttpMessage\UploadedFileFactory; +use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestFactoryInterface; use Psr\Http\Message\ServerRequestInterface; -use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory; +use Psr\Http\Message\StreamFactoryInterface; +use Psr\Http\Message\UploadedFileFactoryInterface; +use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory; +use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface; class Psr7ServiceProvider extends ServiceProvider { public function register() { - /** @var DiactorosFactory $psr7Factory */ - $psr7Factory = $this->app->make(DiactorosFactory::class); - $this->app->instance('psr7.factory', $psr7Factory); + foreach ( + [ + ServerRequestFactory::class => ['psr7.factory.request', ServerRequestFactoryInterface::class], + ResponseFactory::class => ['psr7.factory.response', ResponseFactoryInterface::class], + UploadedFileFactory::class => ['psr7.factory.upload', UploadedFileFactoryInterface::class], + StreamFactory::class => ['psr7.factory.stream', StreamFactoryInterface::class], + PsrHttpFactory::class => ['psr7.factory', HttpMessageFactoryInterface::class], + ] as $class => $aliases + ) { + foreach ($aliases as $alias) { + $this->app->bind($alias, $class); + } + } - /** @var Request $request */ - $request = $this->app->get('request'); - $this->app->instance('psr7.request', $request); + $this->app->bind('psr7.request', 'request'); $this->app->bind(ServerRequestInterface::class, 'psr7.request'); - /** @var Response $response */ - $response = $this->app->get('response'); - $this->app->instance('psr7.response', $response); + $this->app->bind('psr7.response', 'response'); $this->app->bind(ResponseInterface::class, 'psr7.response'); } } diff --git a/src/Http/Request.php b/src/Http/Request.php index 06900a18..ee496035 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -2,13 +2,13 @@ namespace Engelsystem\Http; +use PhpExtended\HttpMessage\UploadedFile; +use PhpExtended\HttpMessage\Uri; use Psr\Http\Message\ServerRequestInterface; 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; -use Zend\Diactoros\UploadedFile; -use Zend\Diactoros\Uri; class Request extends SymfonyRequest implements ServerRequestInterface { @@ -207,7 +207,7 @@ class Request extends SymfonyRequest implements ServerRequestInterface { $uri = parent::getUri(); - return new Uri($uri); + return Uri::parseFromString($uri); } /** @@ -332,11 +332,11 @@ class Request extends SymfonyRequest implements ServerRequestInterface /** @var SymfonyFile $file */ $files[] = new UploadedFile( - $file->getPath(), - $file->getSize(), - $file->getError(), $file->getClientOriginalName(), - $file->getClientMimeType() + $file->getRealPath(), + $file->getMimeType(), + $file->getSize(), + $file->getError() ); } diff --git a/tests/Unit/Http/MessageTraitRequestTest.php b/tests/Unit/Http/MessageTraitRequestTest.php index 7430b5d7..c3d99afb 100644 --- a/tests/Unit/Http/MessageTraitRequestTest.php +++ b/tests/Unit/Http/MessageTraitRequestTest.php @@ -3,8 +3,8 @@ namespace Engelsystem\Test\Unit\Http; use Engelsystem\Test\Unit\Http\Stub\MessageTraitRequestImplementation; +use PhpExtended\HttpMessage\StringStream; use PHPUnit\Framework\TestCase; -use Zend\Diactoros\Stream; class MessageTraitRequestTest extends TestCase { @@ -36,11 +36,7 @@ class MessageTraitRequestTest extends TestCase */ public function testWithBody() { - /** @var Stream $stream */ - $stream = new Stream('php://memory', 'wb+'); - $stream->write('Test content'); - $stream->rewind(); - + $stream = new StringStream('Test content'); $message = new MessageTraitRequestImplementation(); $newMessage = $message->withBody($stream); diff --git a/tests/Unit/Http/MessageTraitResponseTest.php b/tests/Unit/Http/MessageTraitResponseTest.php index f60360a3..31b529ee 100644 --- a/tests/Unit/Http/MessageTraitResponseTest.php +++ b/tests/Unit/Http/MessageTraitResponseTest.php @@ -3,11 +3,11 @@ namespace Engelsystem\Test\Unit\Http; use Engelsystem\Test\Unit\Http\Stub\MessageTraitResponseImplementation; +use PhpExtended\HttpMessage\StringStream; 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 { @@ -145,11 +145,7 @@ class MessageTraitResponseTest extends TestCase */ public function testWithBody() { - /** @var Stream $stream */ - $stream = new Stream('php://memory', 'wb+'); - $stream->write('Test content'); - $stream->rewind(); - + $stream = new StringStream('Test content'); $message = new MessageTraitResponseImplementation(); $newMessage = $message->withBody($stream); diff --git a/tests/Unit/Http/Psr7ServiceProviderTest.php b/tests/Unit/Http/Psr7ServiceProviderTest.php index e14daf2a..25c2f6e3 100644 --- a/tests/Unit/Http/Psr7ServiceProviderTest.php +++ b/tests/Unit/Http/Psr7ServiceProviderTest.php @@ -2,14 +2,16 @@ namespace Engelsystem\Test\Unit\Http; +use Engelsystem\Application; 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\ResponseFactoryInterface; use Psr\Http\Message\ResponseInterface; -use Psr\Http\Message\ServerRequestInterface as RequestInterface; -use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory; +use Psr\Http\Message\ServerRequestFactoryInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Message\StreamFactoryInterface; +use Psr\Http\Message\UploadedFileFactoryInterface; +use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface; class Psr7ServiceProviderTest extends ServiceProviderTest { @@ -18,39 +20,33 @@ class Psr7ServiceProviderTest extends ServiceProviderTest */ 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'] - ); + $app = new Application; $serviceProvider = new Psr7ServiceProvider($app); $serviceProvider->register(); + + foreach ( + [ + 'psr7.factory.request', + 'psr7.factory.response', + 'psr7.factory.upload', + 'psr7.factory.stream', + 'psr7.factory', + 'psr7.request', + 'psr7.response', + ServerRequestFactoryInterface::class, + ResponseFactoryInterface::class, + UploadedFileFactoryInterface::class, + StreamFactoryInterface::class, + HttpMessageFactoryInterface::class, + ServerRequestInterface::class, + ResponseInterface::class, + ] as $id + ) { + $this->assertTrue( + $app->has($id), + sprintf('"%s" is not registered', $id) + ); + } } } diff --git a/tests/Unit/Http/RequestTest.php b/tests/Unit/Http/RequestTest.php index 11a8ed83..8210d583 100644 --- a/tests/Unit/Http/RequestTest.php +++ b/tests/Unit/Http/RequestTest.php @@ -3,6 +3,7 @@ namespace Engelsystem\Test\Unit\Http; use Engelsystem\Http\Request; +use PhpExtended\HttpMessage\UploadedFile; use PHPUnit\Framework\TestCase; use PHPUnit_Framework_MockObject_MockObject as MockObject; use Psr\Http\Message\RequestInterface; @@ -285,7 +286,7 @@ class RequestTest extends TestCase { $filename = tempnam(sys_get_temp_dir(), 'test'); file_put_contents($filename, 'LoremIpsum!'); - $files = [new SymfonyFile($filename, 'foo.html', 'text/html', 11)]; + $files = [new SymfonyFile($filename, 'foo.txt', 'text/plain', 11)]; $request = new Request([], [], [], [], $files); $uploadedFiles = $request->getUploadedFiles(); @@ -294,8 +295,8 @@ class RequestTest extends TestCase /** @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('foo.txt', $file->getClientFilename()); + $this->assertEquals('text/plain', $file->getClientMediaType()); $this->assertEquals(11, $file->getSize()); } @@ -306,7 +307,7 @@ class RequestTest extends TestCase { $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'); + $file = new UploadedFile('test.txt', $filename, 'text/plain', 11, UPLOAD_ERR_OK); $request = new Request(); $new = $request->withUploadedFiles([$file]);