2019-11-24 15:24:52 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Rest\Middleware;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
use Shlinkio\Shlink\Rest\Middleware\BackwardsCompatibleProblemDetailsMiddleware;
|
|
|
|
use Zend\Diactoros\Response;
|
2019-12-31 00:42:29 +03:00
|
|
|
use Zend\Diactoros\ServerRequest;
|
2019-11-24 15:24:52 +03:00
|
|
|
use Zend\Diactoros\ServerRequestFactory;
|
|
|
|
use Zend\Diactoros\Uri;
|
|
|
|
|
|
|
|
class BackwardsCompatibleProblemDetailsMiddlewareTest extends TestCase
|
|
|
|
{
|
2019-12-30 00:48:40 +03:00
|
|
|
private BackwardsCompatibleProblemDetailsMiddleware $middleware;
|
|
|
|
private ObjectProphecy $handler;
|
2019-11-24 15:24:52 +03:00
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
$this->handler = $this->prophesize(RequestHandlerInterface::class);
|
2019-12-31 00:42:29 +03:00
|
|
|
$this->middleware = new BackwardsCompatibleProblemDetailsMiddleware(0);
|
2019-11-24 15:24:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideNonProcessableResponses
|
|
|
|
*/
|
2019-12-31 00:42:29 +03:00
|
|
|
public function nonProblemDetailsOrInvalidResponsesAreReturnedAsTheyAre(
|
|
|
|
Response $response,
|
|
|
|
?ServerRequest $request = null
|
|
|
|
): void {
|
|
|
|
$request = $request ?? ServerRequestFactory::fromGlobals();
|
2019-11-24 15:24:52 +03:00
|
|
|
$handle = $this->handler->handle($request)->willReturn($response);
|
|
|
|
|
|
|
|
$result = $this->middleware->process($request, $this->handler->reveal());
|
|
|
|
|
|
|
|
$this->assertSame($response, $result);
|
|
|
|
$handle->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideNonProcessableResponses(): iterable
|
|
|
|
{
|
|
|
|
yield 'no problem details' => [new Response()];
|
|
|
|
yield 'invalid JSON' => [(new Response('data://text/plain,{invalid-json'))->withHeader(
|
|
|
|
'Content-Type',
|
|
|
|
'application/problem+json'
|
|
|
|
)];
|
2019-12-31 00:42:29 +03:00
|
|
|
yield 'version 2' => [
|
|
|
|
(new Response())->withHeader('Content-type', 'application/problem+json'),
|
|
|
|
ServerRequestFactory::fromGlobals()->withUri(new Uri('/v2/something')),
|
|
|
|
];
|
2019-11-24 15:24:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideRequestPath
|
|
|
|
*/
|
|
|
|
public function mapsDeprecatedPropertiesWhenRequestIsPerformedToVersionOne(string $requestPath): void
|
|
|
|
{
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri($requestPath));
|
|
|
|
$response = $this->jsonResponse([
|
|
|
|
'type' => 'the_type',
|
|
|
|
'detail' => 'the_detail',
|
|
|
|
]);
|
|
|
|
$handle = $this->handler->handle($request)->willReturn($response);
|
|
|
|
|
|
|
|
/** @var Response\JsonResponse $result */
|
|
|
|
$result = $this->middleware->process($request, $this->handler->reveal());
|
|
|
|
$payload = $result->getPayload();
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
'type' => 'the_type',
|
|
|
|
'detail' => 'the_detail',
|
|
|
|
'error' => 'the_type',
|
|
|
|
'message' => 'the_detail',
|
|
|
|
], $payload);
|
|
|
|
$handle->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideRequestPath(): iterable
|
|
|
|
{
|
|
|
|
yield 'no version' => ['/foo'];
|
|
|
|
yield 'version one' => ['/v1/foo'];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function jsonResponse(array $payload, int $status = 200): Response\JsonResponse
|
|
|
|
{
|
|
|
|
$headers = ['Content-Type' => 'application/problem+json'];
|
|
|
|
return new Response\JsonResponse($payload, $status, $headers);
|
|
|
|
}
|
|
|
|
}
|