shlink/module/Rest/test/Middleware/ShortUrl/CreateShortUrlContentNegotiationMiddlewareTest.php

102 lines
3.5 KiB
PHP
Raw Normal View History

<?php
2019-10-05 18:26:10 +03:00
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Middleware\ShortUrl;
2020-01-01 23:11:53 +03:00
use Laminas\Diactoros\Response;
use Laminas\Diactoros\Response\JsonResponse;
use Laminas\Diactoros\ServerRequest;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
2020-11-02 13:50:19 +03:00
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Rest\Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware;
class CreateShortUrlContentNegotiationMiddlewareTest extends TestCase
{
2020-11-02 13:50:19 +03:00
use ProphecyTrait;
private CreateShortUrlContentNegotiationMiddleware $middleware;
private ObjectProphecy $requestHandler;
protected function setUp(): void
{
$this->middleware = new CreateShortUrlContentNegotiationMiddleware();
$this->requestHandler = $this->prophesize(RequestHandlerInterface::class);
}
2019-02-17 22:28:34 +03:00
/** @test */
public function whenNoJsonResponseIsReturnedNoFurtherOperationsArePerformed(): void
{
$expectedResp = new Response();
$this->requestHandler->handle(Argument::type(ServerRequestInterface::class))->willReturn($expectedResp);
$resp = $this->middleware->process(new ServerRequest(), $this->requestHandler->reveal());
2020-10-04 01:35:14 +03:00
self::assertSame($expectedResp, $resp);
}
/**
* @test
* @dataProvider provideData
* @param array $query
*/
2019-02-17 22:28:34 +03:00
public function properResponseIsReturned(?string $accept, array $query, string $expectedContentType): void
{
$request = (new ServerRequest())->withQueryParams($query);
if ($accept !== null) {
$request = $request->withHeader('Accept', $accept);
}
$handle = $this->requestHandler->handle(Argument::type(ServerRequestInterface::class))->willReturn(
2020-01-01 22:48:31 +03:00
new JsonResponse(['shortUrl' => 'http://doma.in/foo']),
);
$response = $this->middleware->process($request, $this->requestHandler->reveal());
2020-10-04 01:35:14 +03:00
self::assertEquals($expectedContentType, $response->getHeaderLine('Content-type'));
$handle->shouldHaveBeenCalled();
}
2019-02-17 22:28:34 +03:00
public function provideData(): iterable
{
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'];
}
/**
* @test
* @dataProvider provideTextBodies
* @param array $json
*/
2019-02-17 22:28:34 +03:00
public function properBodyIsReturnedInPlainTextResponses(array $json, string $expectedBody): void
{
$request = (new ServerRequest())->withQueryParams(['format' => 'txt']);
$handle = $this->requestHandler->handle(Argument::type(ServerRequestInterface::class))->willReturn(
2020-01-01 22:48:31 +03:00
new JsonResponse($json),
);
$response = $this->middleware->process($request, $this->requestHandler->reveal());
2020-10-04 01:35:14 +03:00
self::assertEquals($expectedBody, (string) $response->getBody());
$handle->shouldHaveBeenCalled();
}
2019-02-17 22:28:34 +03:00
public function provideTextBodies(): iterable
{
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' => [[], ''];
}
}