2019-11-23 03:25:12 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\ErrorHandler;
|
|
|
|
|
2020-01-01 14:11:53 -06:00
|
|
|
use Laminas\Diactoros\Response;
|
|
|
|
use Laminas\Diactoros\ServerRequestFactory;
|
|
|
|
use Mezzio\Router\Route;
|
|
|
|
use Mezzio\Router\RouteResult;
|
|
|
|
use Mezzio\Template\TemplateRendererInterface;
|
2019-11-23 03:25:12 -06:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Psr\Http\Server\MiddlewareInterface;
|
|
|
|
use Shlinkio\Shlink\Core\ErrorHandler\NotFoundTemplateHandler;
|
|
|
|
|
|
|
|
class NotFoundTemplateHandlerTest extends TestCase
|
|
|
|
{
|
2019-12-29 15:48:40 -06:00
|
|
|
private NotFoundTemplateHandler $handler;
|
|
|
|
private ObjectProphecy $renderer;
|
2019-11-23 03:25:12 -06:00
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
$this->renderer = $this->prophesize(TemplateRendererInterface::class);
|
|
|
|
$this->handler = new NotFoundTemplateHandler($this->renderer->reveal());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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->handler->handle($request);
|
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertInstanceOf(Response\HtmlResponse::class, $resp);
|
2019-11-23 03:25:12 -06:00
|
|
|
$render->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideTemplates(): iterable
|
|
|
|
{
|
|
|
|
$request = ServerRequestFactory::fromGlobals();
|
|
|
|
|
|
|
|
yield [$request, NotFoundTemplateHandler::NOT_FOUND_TEMPLATE];
|
|
|
|
yield [
|
|
|
|
$request->withAttribute(
|
|
|
|
RouteResult::class,
|
2020-01-01 13:48:31 -06:00
|
|
|
RouteResult::fromRoute(new Route('', $this->prophesize(MiddlewareInterface::class)->reveal())),
|
2019-11-23 03:25:12 -06:00
|
|
|
),
|
|
|
|
NotFoundTemplateHandler::INVALID_SHORT_CODE_TEMPLATE,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|