From b83ed4cca6089a46c3516ed34efdb4d361f86ca1 Mon Sep 17 00:00:00 2001 From: Moln Date: Mon, 12 Nov 2018 15:03:53 +0800 Subject: [PATCH 1/2] `composer.json` update `zend-diactoros` version. --- composer.json | 2 +- src/MockRequestFactory.php | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index d44cb8f..f6924d9 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "require": { "phpunit/phpunit": "^6.0 | ^7.0", "zendframework/zend-expressive": "^3.0", - "zendframework/zend-diactoros": "^1.7", + "zendframework/zend-diactoros": "^1.7 | ^2.0", "zendframework/zend-servicemanager": "^3.3" }, "license": "MIT", diff --git a/src/MockRequestFactory.php b/src/MockRequestFactory.php index 699352b..95cc301 100644 --- a/src/MockRequestFactory.php +++ b/src/MockRequestFactory.php @@ -5,6 +5,10 @@ namespace Zfegg\ExpressiveTest; use Psr\Http\Message\StreamInterface; +use function Zend\Diactoros\marshalHeadersFromSapi; +use function Zend\Diactoros\marshalUriFromSapi; +use function Zend\Diactoros\normalizeServer; +use function Zend\Diactoros\normalizeUploadedFiles; use Zend\Diactoros\ServerRequest; use Zend\Diactoros\ServerRequestFactory; use Zend\Diactoros\Stream; @@ -63,9 +67,17 @@ public static function create( $_POST = $parsedBody; - $server = ServerRequestFactory::normalizeServer($_SERVER); - $files = ServerRequestFactory::normalizeFiles($files); - $headers = ServerRequestFactory::marshalHeaders($server); + if (function_exists('\Zend\Diactoros\normalizeServer')) { // Diactoros v2.0 + $server = normalizeServer($_SERVER); + $files = normalizeUploadedFiles($files); + $headers = marshalHeadersFromSapi($_SERVER); + $uri = marshalUriFromSapi($_SERVER, $headers); + } else { // Diactoros v1.7 + $server = ServerRequestFactory::normalizeServer($_SERVER); + $files = ServerRequestFactory::normalizeFiles($files); + $headers = ServerRequestFactory::marshalHeaders($server); + $uri = ServerRequestFactory::marshalUriFromServer($server, $headers); + } if ($body instanceof StreamInterface) { $stream = $body; @@ -82,8 +94,8 @@ public static function create( $request = new ServerRequest( $server, $files, - ServerRequestFactory::marshalUriFromServer($server, $headers), - ServerRequestFactory::get('REQUEST_METHOD', $server, 'GET'), + $uri, + $server['REQUEST_METHOD'] ?? 'GET', $stream, $headers, $cookies, From 38616170f05b25e309b9b1e7401fb919989deb86 Mon Sep 17 00:00:00 2001 From: Moln Date: Mon, 12 Nov 2018 15:21:21 +0800 Subject: [PATCH 2/2] Add more unit test. --- test/AbstractActionTestCaseTest.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/AbstractActionTestCaseTest.php b/test/AbstractActionTestCaseTest.php index 4eeb329..55af89f 100644 --- a/test/AbstractActionTestCaseTest.php +++ b/test/AbstractActionTestCaseTest.php @@ -3,6 +3,7 @@ namespace ZfeggTest\ExpressiveTest; use PHPUnit\Framework\TestCase; +use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Zend\Diactoros\Response; @@ -26,6 +27,12 @@ public function params() ]; } + public function testMockRequest() + { + $request = $this->mockRequest(); + $this->assertInstanceOf(RequestInterface::class, $request); + } + /** * * @dataProvider params @@ -46,6 +53,24 @@ public function testAction($body) $this->assertInstanceOf(ResponseInterface::class, $response); } + /** + * + * @expectedException \RuntimeException + */ + public function testInvalidBodyParamAction() + { + $app = $this->container->get(Application::class); + $app->post('/', [$this, 'postHandler']); + $this->runApp( + 'POST', + '/?test=1', + ['body' => '2'], + ['HTTP_CONTENT_TYPE' => 'application/json'], + ['invalid test'], + ['cookie' => '3'] + ); + } + public function postHandler(ServerRequestInterface $request) { $servers = $request->getServerParams();