1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ShlinkioTest\Shlink\Rest\Middleware\ShortUrl; |
6
|
|
|
|
7
|
|
|
use Laminas\Diactoros\Response; |
8
|
|
|
use Laminas\Diactoros\Response\JsonResponse; |
9
|
|
|
use Laminas\Diactoros\ServerRequest; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use Prophecy\Argument; |
12
|
|
|
use Prophecy\PhpUnit\ProphecyTrait; |
13
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
14
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
15
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
16
|
|
|
use Shlinkio\Shlink\Rest\Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware; |
17
|
|
|
|
18
|
|
|
class CreateShortUrlContentNegotiationMiddlewareTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
use ProphecyTrait; |
21
|
|
|
|
22
|
|
|
private CreateShortUrlContentNegotiationMiddleware $middleware; |
23
|
|
|
private ObjectProphecy $requestHandler; |
24
|
|
|
|
25
|
|
|
public function setUp(): void |
26
|
|
|
{ |
27
|
|
|
$this->middleware = new CreateShortUrlContentNegotiationMiddleware(); |
28
|
|
|
$this->requestHandler = $this->prophesize(RequestHandlerInterface::class); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** @test */ |
32
|
|
|
public function whenNoJsonResponseIsReturnedNoFurtherOperationsArePerformed(): void |
33
|
|
|
{ |
34
|
|
|
$expectedResp = new Response(); |
35
|
|
|
$this->requestHandler->handle(Argument::type(ServerRequestInterface::class))->willReturn($expectedResp); |
36
|
|
|
|
37
|
|
|
$resp = $this->middleware->process(new ServerRequest(), $this->requestHandler->reveal()); |
38
|
|
|
|
39
|
|
|
self::assertSame($expectedResp, $resp); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @test |
44
|
|
|
* @dataProvider provideData |
45
|
|
|
* @param array $query |
46
|
|
|
*/ |
47
|
|
|
public function properResponseIsReturned(?string $accept, array $query, string $expectedContentType): void |
48
|
|
|
{ |
49
|
|
|
$request = (new ServerRequest())->withQueryParams($query); |
50
|
|
|
if ($accept !== null) { |
51
|
|
|
$request = $request->withHeader('Accept', $accept); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$handle = $this->requestHandler->handle(Argument::type(ServerRequestInterface::class))->willReturn( |
55
|
|
|
new JsonResponse(['shortUrl' => 'http://doma.in/foo']), |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
$response = $this->middleware->process($request, $this->requestHandler->reveal()); |
59
|
|
|
|
60
|
|
|
self::assertEquals($expectedContentType, $response->getHeaderLine('Content-type')); |
61
|
|
|
$handle->shouldHaveBeenCalled(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function provideData(): iterable |
65
|
|
|
{ |
66
|
|
|
yield [null, [], 'application/json']; |
67
|
|
|
yield [null, ['format' => 'json'], 'application/json']; |
68
|
|
|
yield [null, ['format' => 'invalid'], 'application/json']; |
69
|
|
|
yield [null, ['format' => 'txt'], 'text/plain']; |
70
|
|
|
yield ['application/json', [], 'application/json']; |
71
|
|
|
yield ['application/xml', [], 'application/json']; |
72
|
|
|
yield ['text/plain', [], 'text/plain']; |
73
|
|
|
yield ['application/json', ['format' => 'txt'], 'text/plain']; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @test |
78
|
|
|
* @dataProvider provideTextBodies |
79
|
|
|
* @param array $json |
80
|
|
|
*/ |
81
|
|
|
public function properBodyIsReturnedInPlainTextResponses(array $json, string $expectedBody): void |
82
|
|
|
{ |
83
|
|
|
$request = (new ServerRequest())->withQueryParams(['format' => 'txt']); |
84
|
|
|
|
85
|
|
|
$handle = $this->requestHandler->handle(Argument::type(ServerRequestInterface::class))->willReturn( |
86
|
|
|
new JsonResponse($json), |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$response = $this->middleware->process($request, $this->requestHandler->reveal()); |
90
|
|
|
|
91
|
|
|
self::assertEquals($expectedBody, (string) $response->getBody()); |
92
|
|
|
$handle->shouldHaveBeenCalled(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function provideTextBodies(): iterable |
96
|
|
|
{ |
97
|
|
|
yield 'shortUrl key' => [['shortUrl' => 'foobar'], 'foobar']; |
98
|
|
|
yield 'error key' => [['error' => 'FOO_BAR'], 'FOO_BAR']; |
99
|
|
|
yield 'no shortUrl or error keys' => [[], '']; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|