2018-05-03 20:04:40 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2018-05-03 20:04:40 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-09-20 21:00:53 +03:00
|
|
|
namespace ShlinkioTest\Shlink\Rest\Middleware\ShortUrl;
|
2018-05-03 20:04:40 +03:00
|
|
|
|
2020-01-01 23:11:53 +03:00
|
|
|
use Laminas\Diactoros\Response;
|
|
|
|
use Laminas\Diactoros\Response\JsonResponse;
|
|
|
|
use Laminas\Diactoros\ServerRequest;
|
2022-10-23 23:53:48 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2018-05-03 20:04:40 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
2018-09-20 21:00:53 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware;
|
2018-05-03 20:04:40 +03:00
|
|
|
|
2018-09-20 21:00:53 +03:00
|
|
|
class CreateShortUrlContentNegotiationMiddlewareTest extends TestCase
|
2018-05-03 20:04:40 +03:00
|
|
|
{
|
2019-12-30 00:48:40 +03:00
|
|
|
private CreateShortUrlContentNegotiationMiddleware $middleware;
|
2022-10-24 20:53:13 +03:00
|
|
|
private MockObject & RequestHandlerInterface $requestHandler;
|
2018-05-03 20:04:40 +03:00
|
|
|
|
2022-09-11 13:02:49 +03:00
|
|
|
protected function setUp(): void
|
2018-05-03 20:04:40 +03:00
|
|
|
{
|
2018-09-20 21:00:53 +03:00
|
|
|
$this->middleware = new CreateShortUrlContentNegotiationMiddleware();
|
2022-10-23 23:53:48 +03:00
|
|
|
$this->requestHandler = $this->createMock(RequestHandlerInterface::class);
|
2018-05-06 13:28:22 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
|
|
|
public function whenNoJsonResponseIsReturnedNoFurtherOperationsArePerformed(): void
|
2018-05-06 13:28:22 +03:00
|
|
|
{
|
|
|
|
$expectedResp = new Response();
|
2022-10-23 23:53:48 +03:00
|
|
|
$this->requestHandler->method('handle')->with($this->isInstanceOf(ServerRequestInterface::class))->willReturn(
|
|
|
|
$expectedResp,
|
|
|
|
);
|
2018-05-06 13:28:22 +03:00
|
|
|
|
2022-10-23 23:53:48 +03:00
|
|
|
$resp = $this->middleware->process(new ServerRequest(), $this->requestHandler);
|
2018-05-06 13:28:22 +03:00
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertSame($expectedResp, $resp);
|
2018-05-03 20:04:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideData
|
|
|
|
* @param array $query
|
|
|
|
*/
|
2019-02-17 22:28:34 +03:00
|
|
|
public function properResponseIsReturned(?string $accept, array $query, string $expectedContentType): void
|
2018-05-03 20:04:40 +03:00
|
|
|
{
|
2018-12-26 01:01:30 +03:00
|
|
|
$request = (new ServerRequest())->withQueryParams($query);
|
2018-05-03 20:04:40 +03:00
|
|
|
if ($accept !== null) {
|
|
|
|
$request = $request->withHeader('Accept', $accept);
|
|
|
|
}
|
|
|
|
|
2022-10-23 23:53:48 +03:00
|
|
|
$this->requestHandler->expects($this->once())->method('handle')->with(
|
|
|
|
$this->isInstanceOf(ServerRequestInterface::class),
|
2023-01-19 11:05:52 +03:00
|
|
|
)->willReturn(new JsonResponse(['shortUrl' => 'http://s.test/foo']));
|
2018-05-06 13:28:22 +03:00
|
|
|
|
2022-10-23 23:53:48 +03:00
|
|
|
$response = $this->middleware->process($request, $this->requestHandler);
|
2018-05-03 20:04:40 +03:00
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals($expectedContentType, $response->getHeaderLine('Content-type'));
|
2018-05-03 20:04:40 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
public function provideData(): iterable
|
2018-05-03 20:04:40 +03:00
|
|
|
{
|
2019-02-17 22:28:34 +03:00
|
|
|
yield [null, [], 'application/json'];
|
|
|
|
yield [null, ['format' => 'json'], 'application/json'];
|
|
|
|
yield [null, ['format' => 'invalid'], 'application/json'];
|
|
|
|
yield [null, ['format' => 'txt'], 'text/plain'];
|
|
|
|
yield ['application/json', [], 'application/json'];
|
|
|
|
yield ['application/xml', [], 'application/json'];
|
|
|
|
yield ['text/plain', [], 'text/plain'];
|
|
|
|
yield ['application/json', ['format' => 'txt'], 'text/plain'];
|
2018-05-03 20:04:40 +03:00
|
|
|
}
|
2018-05-06 13:28:22 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideTextBodies
|
|
|
|
* @param array $json
|
|
|
|
*/
|
2019-02-17 22:28:34 +03:00
|
|
|
public function properBodyIsReturnedInPlainTextResponses(array $json, string $expectedBody): void
|
2018-05-06 13:28:22 +03:00
|
|
|
{
|
2018-12-26 01:01:30 +03:00
|
|
|
$request = (new ServerRequest())->withQueryParams(['format' => 'txt']);
|
2018-05-06 13:28:22 +03:00
|
|
|
|
2022-10-23 23:53:48 +03:00
|
|
|
$this->requestHandler->expects($this->once())->method('handle')->with(
|
|
|
|
$this->isInstanceOf(ServerRequestInterface::class),
|
|
|
|
)->willReturn(new JsonResponse($json));
|
2018-05-06 13:28:22 +03:00
|
|
|
|
2022-10-23 23:53:48 +03:00
|
|
|
$response = $this->middleware->process($request, $this->requestHandler);
|
2018-05-06 13:28:22 +03:00
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertEquals($expectedBody, (string) $response->getBody());
|
2018-05-06 13:28:22 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
public function provideTextBodies(): iterable
|
2018-05-06 13:28:22 +03:00
|
|
|
{
|
2019-02-17 22:28:34 +03:00
|
|
|
yield 'shortUrl key' => [['shortUrl' => 'foobar'], 'foobar'];
|
|
|
|
yield 'error key' => [['error' => 'FOO_BAR'], 'FOO_BAR'];
|
|
|
|
yield 'no shortUrl or error keys' => [[], ''];
|
2018-05-06 13:28:22 +03:00
|
|
|
}
|
2018-05-03 20:04:40 +03:00
|
|
|
}
|