shlink/module/Core/test/Response/NotFoundHandlerTest.php

143 lines
4.8 KiB
PHP
Raw Normal View History

2017-10-13 13:02:00 +03:00
<?php
2019-10-05 18:26:10 +03:00
2017-10-13 13:02:00 +03:00
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Response;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Shlinkio\Shlink\Core\Action\RedirectAction;
use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions;
2018-05-07 11:58:49 +03:00
use Shlinkio\Shlink\Core\Response\NotFoundHandler;
2017-10-13 13:02:00 +03:00
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Uri;
use Zend\Expressive\Router\Route;
use Zend\Expressive\Router\RouteResult;
2017-10-13 13:02:00 +03:00
use Zend\Expressive\Template\TemplateRendererInterface;
2018-05-07 11:58:49 +03:00
class NotFoundHandlerTest extends TestCase
2017-10-13 13:02:00 +03:00
{
/** @var NotFoundHandler */
2017-10-13 13:02:00 +03:00
private $delegate;
/** @var ObjectProphecy */
2017-10-13 13:02:00 +03:00
private $renderer;
/** @var NotFoundRedirectOptions */
private $redirectOptions;
2017-10-13 13:02:00 +03:00
2019-02-16 12:53:45 +03:00
public function setUp(): void
2017-10-13 13:02:00 +03:00
{
$this->renderer = $this->prophesize(TemplateRendererInterface::class);
$this->redirectOptions = new NotFoundRedirectOptions();
$this->delegate = new NotFoundHandler($this->renderer->reveal(), $this->redirectOptions, '');
2017-10-13 13:02:00 +03:00
}
/**
* @test
* @dataProvider provideResponses
*/
2019-02-17 22:28:34 +03:00
public function properResponseTypeIsReturned(string $expectedResponse, string $accept, int $renderCalls): void
2017-10-13 13:02:00 +03:00
{
$request = (new ServerRequest())->withHeader('Accept', $accept);
2017-10-13 13:02:00 +03:00
$render = $this->renderer->render(Argument::cetera())->willReturn('');
2018-03-26 20:02:41 +03:00
$resp = $this->delegate->handle($request);
2017-10-13 13:02:00 +03:00
$this->assertInstanceOf($expectedResponse, $resp);
$render->shouldHaveBeenCalledTimes($renderCalls);
}
2019-02-17 22:28:34 +03:00
public function provideResponses(): iterable
2017-10-13 13:02:00 +03:00
{
2019-02-17 22:28:34 +03:00
yield 'application/json' => [Response\JsonResponse::class, 'application/json', 0];
yield 'text/json' => [Response\JsonResponse::class, 'text/json', 0];
yield 'application/x-json' => [Response\JsonResponse::class, 'application/x-json', 0];
yield 'text/html' => [Response\HtmlResponse::class, 'text/html', 1];
2017-10-13 13:02:00 +03:00
}
/**
* @test
* @dataProvider provideRedirects
*/
public function expectedRedirectionIsReturnedDependingOnTheCase(
ServerRequestInterface $request,
string $expectedRedirectTo
): void {
$this->redirectOptions->invalidShortUrl = 'invalidShortUrl';
$this->redirectOptions->regular404 = 'regular404';
$this->redirectOptions->baseUrl = 'baseUrl';
$resp = $this->delegate->handle($request);
$this->assertInstanceOf(Response\RedirectResponse::class, $resp);
$this->assertEquals($expectedRedirectTo, $resp->getHeaderLine('Location'));
$this->renderer->render(Argument::cetera())->shouldNotHaveBeenCalled();
}
public function provideRedirects(): iterable
{
yield 'base URL with trailing slash' => [
ServerRequestFactory::fromGlobals()->withUri(new Uri('/')),
'baseUrl',
];
yield 'base URL without trailing slash' => [
ServerRequestFactory::fromGlobals()->withUri(new Uri('')),
'baseUrl',
];
yield 'regular 404' => [
ServerRequestFactory::fromGlobals()->withUri(new Uri('/foo/bar')),
'regular404',
];
yield 'invalid short URL' => [
ServerRequestFactory::fromGlobals()
->withAttribute(
RouteResult::class,
RouteResult::fromRoute(
new Route(
'',
$this->prophesize(MiddlewareInterface::class)->reveal(),
['GET'],
RedirectAction::class
)
)
)
->withUri(new Uri('/abc123')),
'invalidShortUrl',
];
}
/**
* @test
* @dataProvider provideTemplates
*/
public function properErrorTemplateIsRendered(ServerRequestInterface $request, string $expectedTemplate): void
{
$request = $request->withHeader('Accept', 'text/html');
$render = $this->renderer->render($expectedTemplate)->willReturn('');
$resp = $this->delegate->handle($request);
$this->assertInstanceOf(Response\HtmlResponse::class, $resp);
$render->shouldHaveBeenCalledOnce();
}
public function provideTemplates(): iterable
{
$request = ServerRequestFactory::fromGlobals();
2019-11-02 21:08:07 +03:00
yield [$request, NotFoundHandler::NOT_FOUND_TEMPLATE];
yield [
$request->withAttribute(
RouteResult::class,
RouteResult::fromRoute(new Route('', $this->prophesize(MiddlewareInterface::class)->reveal()))
),
2019-11-02 21:08:07 +03:00
NotFoundHandler::INVALID_SHORT_CODE_TEMPLATE,
];
}
2017-10-13 13:02:00 +03:00
}