|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ShlinkioTest\Shlink\Rest\Middleware; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
9
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
10
|
|
|
use Shlinkio\Shlink\Rest\Middleware\BackwardsCompatibleProblemDetailsMiddleware; |
|
11
|
|
|
use Zend\Diactoros\Response; |
|
12
|
|
|
use Zend\Diactoros\ServerRequest; |
|
13
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
|
14
|
|
|
use Zend\Diactoros\Uri; |
|
15
|
|
|
|
|
16
|
|
|
class BackwardsCompatibleProblemDetailsMiddlewareTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
private BackwardsCompatibleProblemDetailsMiddleware $middleware; |
|
19
|
|
|
private ObjectProphecy $handler; |
|
20
|
|
|
|
|
21
|
|
|
public function setUp(): void |
|
22
|
|
|
{ |
|
23
|
|
|
$this->handler = $this->prophesize(RequestHandlerInterface::class); |
|
24
|
|
|
$this->middleware = new BackwardsCompatibleProblemDetailsMiddleware(0); |
|
|
|
|
|
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @test |
|
29
|
|
|
* @dataProvider provideNonProcessableResponses |
|
30
|
|
|
*/ |
|
31
|
|
|
public function nonProblemDetailsOrInvalidResponsesAreReturnedAsTheyAre( |
|
32
|
|
|
Response $response, |
|
33
|
|
|
?ServerRequest $request = null |
|
34
|
|
|
): void { |
|
35
|
|
|
$request = $request ?? ServerRequestFactory::fromGlobals(); |
|
36
|
|
|
$handle = $this->handler->handle($request)->willReturn($response); |
|
37
|
|
|
|
|
38
|
|
|
$result = $this->middleware->process($request, $this->handler->reveal()); |
|
39
|
|
|
|
|
40
|
|
|
$this->assertSame($response, $result); |
|
41
|
|
|
$handle->shouldHaveBeenCalledOnce(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function provideNonProcessableResponses(): iterable |
|
45
|
|
|
{ |
|
46
|
|
|
yield 'no problem details' => [new Response()]; |
|
47
|
|
|
yield 'invalid JSON' => [(new Response('data://text/plain,{invalid-json'))->withHeader( |
|
48
|
|
|
'Content-Type', |
|
49
|
|
|
'application/problem+json' |
|
50
|
|
|
)]; |
|
51
|
|
|
yield 'version 2' => [ |
|
52
|
|
|
(new Response())->withHeader('Content-type', 'application/problem+json'), |
|
53
|
|
|
ServerRequestFactory::fromGlobals()->withUri(new Uri('/v2/something')), |
|
54
|
|
|
]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @test |
|
59
|
|
|
* @dataProvider provideRequestPath |
|
60
|
|
|
*/ |
|
61
|
|
|
public function mapsDeprecatedPropertiesWhenRequestIsPerformedToVersionOne(string $requestPath): void |
|
62
|
|
|
{ |
|
63
|
|
|
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri($requestPath)); |
|
64
|
|
|
$response = $this->jsonResponse([ |
|
65
|
|
|
'type' => 'the_type', |
|
66
|
|
|
'detail' => 'the_detail', |
|
67
|
|
|
]); |
|
68
|
|
|
$handle = $this->handler->handle($request)->willReturn($response); |
|
69
|
|
|
|
|
70
|
|
|
/** @var Response\JsonResponse $result */ |
|
71
|
|
|
$result = $this->middleware->process($request, $this->handler->reveal()); |
|
72
|
|
|
$payload = $result->getPayload(); |
|
73
|
|
|
|
|
74
|
|
|
$this->assertEquals([ |
|
75
|
|
|
'type' => 'the_type', |
|
76
|
|
|
'detail' => 'the_detail', |
|
77
|
|
|
'error' => 'the_type', |
|
78
|
|
|
'message' => 'the_detail', |
|
79
|
|
|
], $payload); |
|
80
|
|
|
$handle->shouldHaveBeenCalledOnce(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function provideRequestPath(): iterable |
|
84
|
|
|
{ |
|
85
|
|
|
yield 'no version' => ['/foo']; |
|
86
|
|
|
yield 'version one' => ['/v1/foo']; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function jsonResponse(array $payload, int $status = 200): Response\JsonResponse |
|
90
|
|
|
{ |
|
91
|
|
|
$headers = ['Content-Type' => 'application/problem+json']; |
|
92
|
|
|
return new Response\JsonResponse($payload, $status, $headers); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|