Bugfix: Replaced Diactoros with Symfony PsrHttpFactory to use PhpExtended\HttpMessage implementation

Prevents warnings that get converted to errors when run in dev mode
This commit is contained in:
Igor Scheller 2019-03-17 18:17:33 +01:00 committed by msquare
parent a1f5154c21
commit d9f898a0bb
8 changed files with 77 additions and 75 deletions

View File

@ -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",

View File

@ -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;
}

View File

@ -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');
}
}

View File

@ -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()
);
}

View File

@ -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);

View File

@ -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);

View File

@ -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)
);
}
}
}

View File

@ -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]);