Skip to content

Commit

Permalink
Merge pull request #4 from zfegg/develop
Browse files Browse the repository at this point in the history
`composer.json` update `zend-diactoros` version.
  • Loading branch information
Moln authored Nov 12, 2018
2 parents f58dd03 + fe0b8db commit eb92e41
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 17 additions & 5 deletions src/MockRequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down
25 changes: 25 additions & 0 deletions test/AbstractActionTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +27,12 @@ public function params()
];
}

public function testMockRequest()
{
$request = $this->mockRequest();
$this->assertInstanceOf(RequestInterface::class, $request);
}

/**
*
* @dataProvider params
Expand All @@ -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();
Expand Down

0 comments on commit eb92e41

Please sign in to comment.