shlink/module/Rest/test/Middleware/BodyParserMiddlewareTest.php

105 lines
3.3 KiB
PHP
Raw Normal View History

<?php
2017-10-12 11:13:20 +03:00
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Middleware;
2017-03-24 22:34:18 +03:00
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\MethodProphecy;
use Psr\Http\Message\ServerRequestInterface;
2018-03-26 20:02:41 +03:00
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Rest\Middleware\BodyParserMiddleware;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Stream;
use function array_shift;
class BodyParserMiddlewareTest extends TestCase
{
/** @var BodyParserMiddleware */
private $middleware;
public function setUp()
{
$this->middleware = new BodyParserMiddleware();
}
/**
* @test
*/
public function requestsFromOtherMethodsJustFallbackToNextMiddleware()
{
$request = ServerRequestFactory::fromGlobals()->withMethod('GET');
2018-03-26 20:02:41 +03:00
$delegate = $this->prophesize(RequestHandlerInterface::class);
/** @var MethodProphecy $process */
2018-03-26 20:02:41 +03:00
$process = $delegate->handle($request)->willReturn(new Response());
$this->middleware->process($request, $delegate->reveal());
2018-11-11 15:18:21 +03:00
$process->shouldHaveBeenCalledOnce();
}
/**
* @test
*/
public function jsonRequestsAreJsonDecoded()
{
$test = $this;
$body = new Stream('php://temp', 'wr');
$body->write('{"foo": "bar", "bar": ["one", 5]}');
$request = ServerRequestFactory::fromGlobals()->withMethod('PUT')
->withBody($body)
->withHeader('content-type', 'application/json');
2018-03-26 20:02:41 +03:00
$delegate = $this->prophesize(RequestHandlerInterface::class);
/** @var MethodProphecy $process */
2018-03-26 20:02:41 +03:00
$process = $delegate->handle(Argument::type(ServerRequestInterface::class))->will(
function (array $args) use ($test) {
/** @var ServerRequestInterface $req */
$req = array_shift($args);
$test->assertEquals([
'foo' => 'bar',
'bar' => ['one', 5],
], $req->getParsedBody());
return new Response();
}
);
$this->middleware->process($request, $delegate->reveal());
2018-11-11 15:18:21 +03:00
$process->shouldHaveBeenCalledOnce();
}
/**
* @test
*/
public function regularRequestsAreUrlDecoded()
{
$test = $this;
$body = new Stream('php://temp', 'wr');
$body->write('foo=bar&bar[]=one&bar[]=5');
$request = ServerRequestFactory::fromGlobals()->withMethod('PUT')
->withBody($body);
2018-03-26 20:02:41 +03:00
$delegate = $this->prophesize(RequestHandlerInterface::class);
/** @var MethodProphecy $process */
2018-03-26 20:02:41 +03:00
$process = $delegate->handle(Argument::type(ServerRequestInterface::class))->will(
function (array $args) use ($test) {
/** @var ServerRequestInterface $req */
$req = array_shift($args);
$test->assertEquals([
'foo' => 'bar',
'bar' => ['one', 5],
], $req->getParsedBody());
return new Response();
}
);
$this->middleware->process($request, $delegate->reveal());
2018-11-11 15:18:21 +03:00
$process->shouldHaveBeenCalledOnce();
}
}