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;
|
|
|
|
use Zend\Diactoros\ServerRequestFactory;
|
|
|
|
|
2018-09-20 21:00:53 +03:00
|
|
|
class CreateShortUrlContentNegotiationMiddlewareTest extends TestCase
|
2018-05-03 20:04:40 +03:00
|
|
|
{
|
|
|
|
/**
|
2018-09-20 21:00:53 +03:00
|
|
|
* @var CreateShortUrlContentNegotiationMiddleware
|
2018-05-03 20:04:40 +03:00
|
|
|
*/
|
|
|
|
private $middleware;
|
|
|
|
/**
|
|
|
|
* @var RequestHandlerInterface
|
|
|
|
*/
|
|
|
|
private $requestHandler;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function whenNoJsonResponseIsReturnedNoFurtherOperationsArePerformed()
|
|
|
|
{
|
|
|
|
$expectedResp = new Response();
|
|
|
|
$this->requestHandler->handle(Argument::type(ServerRequestInterface::class))->willReturn($expectedResp);
|
|
|
|
|
|
|
|
$resp = $this->middleware->process(ServerRequestFactory::fromGlobals(), $this->requestHandler->reveal());
|
|
|
|
|
|
|
|
$this->assertSame($expectedResp, $resp);
|
2018-05-03 20:04:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideData
|
|
|
|
* @param array $query
|
|
|
|
*/
|
|
|
|
public function properResponseIsReturned(?string $accept, array $query, string $expectedContentType)
|
|
|
|
{
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withQueryParams($query);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function provideData(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[null, [], 'application/json'],
|
|
|
|
[null, ['format' => 'json'], 'application/json'],
|
|
|
|
[null, ['format' => 'invalid'], 'application/json'],
|
|
|
|
[null, ['format' => 'txt'], 'text/plain'],
|
|
|
|
['application/json', [], 'application/json'],
|
|
|
|
['application/xml', [], 'application/json'],
|
|
|
|
['text/plain', [], 'text/plain'],
|
2018-05-07 12:17:10 +03:00
|
|
|
['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
|
|
|
|
*/
|
|
|
|
public function properBodyIsReturnedInPlainTextResponses(array $json, string $expectedBody)
|
|
|
|
{
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withQueryParams(['format' => 'txt']);
|
|
|
|
|
|
|
|
$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();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideTextBodies(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[['shortUrl' => 'foobar'], 'foobar'],
|
|
|
|
[['error' => 'FOO_BAR'], 'FOO_BAR'],
|
|
|
|
[[], ''],
|
|
|
|
];
|
|
|
|
}
|
2018-05-03 20:04:40 +03:00
|
|
|
}
|