2018-05-03 20:04:40 +03:00
|
|
|
<?php
|
|
|
|
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
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2018-05-06 13:28:22 +03:00
|
|
|
use Prophecy\Argument;
|
2018-05-03 20:04:40 +03:00
|
|
|
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-06 13:28:22 +03:00
|
|
|
use Zend\Diactoros\Response;
|
2018-05-03 20:04:40 +03:00
|
|
|
use Zend\Diactoros\Response\JsonResponse;
|
2018-12-26 01:01:30 +03:00
|
|
|
use Zend\Diactoros\ServerRequest;
|
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
|
|
|
{
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var CreateShortUrlContentNegotiationMiddleware */
|
2018-05-03 20:04:40 +03:00
|
|
|
private $middleware;
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var RequestHandlerInterface */
|
2018-05-03 20:04:40 +03:00
|
|
|
private $requestHandler;
|
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
public function setUp(): void
|
2018-05-03 20:04:40 +03:00
|
|
|
{
|
2018-09-20 21:00:53 +03:00
|
|
|
$this->middleware = new CreateShortUrlContentNegotiationMiddleware();
|
2018-05-06 13:28:22 +03:00
|
|
|
$this->requestHandler = $this->prophesize(RequestHandlerInterface::class);
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
|
|
|
public function whenNoJsonResponseIsReturnedNoFurtherOperationsArePerformed(): void
|
2018-05-06 13:28:22 +03:00
|
|
|
{
|
|
|
|
$expectedResp = new Response();
|
|
|
|
$this->requestHandler->handle(Argument::type(ServerRequestInterface::class))->willReturn($expectedResp);
|
|
|
|
|
2018-12-26 01:01:30 +03:00
|
|
|
$resp = $this->middleware->process(new ServerRequest(), $this->requestHandler->reveal());
|
2018-05-06 13:28:22 +03:00
|
|
|
|
|
|
|
$this->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);
|
|
|
|
}
|
|
|
|
|
2018-05-06 13:28:22 +03:00
|
|
|
$handle = $this->requestHandler->handle(Argument::type(ServerRequestInterface::class))->willReturn(
|
|
|
|
new JsonResponse(['shortUrl' => 'http://doma.in/foo'])
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->middleware->process($request, $this->requestHandler->reveal());
|
2018-05-03 20:04:40 +03:00
|
|
|
|
|
|
|
$this->assertEquals($expectedContentType, $response->getHeaderLine('Content-type'));
|
2018-05-06 13:28:22 +03:00
|
|
|
$handle->shouldHaveBeenCalled();
|
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
|
|
|
|
|
|
|
$handle = $this->requestHandler->handle(Argument::type(ServerRequestInterface::class))->willReturn(
|
|
|
|
new JsonResponse($json)
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->middleware->process($request, $this->requestHandler->reveal());
|
|
|
|
|
|
|
|
$this->assertEquals($expectedBody, (string) $response->getBody());
|
|
|
|
$handle->shouldHaveBeenCalled();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|