2019-11-23 03:25:12 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\ErrorHandler;
|
|
|
|
|
2020-11-22 11:11:31 -06:00
|
|
|
use Closure;
|
2020-01-01 14:11:53 -06:00
|
|
|
use Laminas\Diactoros\Response;
|
|
|
|
use Laminas\Diactoros\ServerRequestFactory;
|
|
|
|
use Mezzio\Router\Route;
|
|
|
|
use Mezzio\Router\RouteResult;
|
2019-11-23 03:25:12 -06:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
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;
|
2020-11-22 11:11:31 -06:00
|
|
|
private Closure $readFile;
|
|
|
|
private bool $readFileCalled;
|
2019-11-23 03:25:12 -06:00
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
2020-11-22 11:11:31 -06:00
|
|
|
$this->readFileCalled = false;
|
|
|
|
$this->readFile = function (string $fileName): string {
|
|
|
|
$this->readFileCalled = true;
|
|
|
|
return $fileName;
|
|
|
|
};
|
|
|
|
$this->handler = new NotFoundTemplateHandler($this->readFile);
|
2019-11-23 03:25:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideTemplates
|
|
|
|
*/
|
|
|
|
public function properErrorTemplateIsRendered(ServerRequestInterface $request, string $expectedTemplate): void
|
|
|
|
{
|
2020-11-22 11:11:31 -06:00
|
|
|
$resp = $this->handler->handle($request->withHeader('Accept', 'text/html'));
|
2019-11-23 03:25:12 -06:00
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertInstanceOf(Response\HtmlResponse::class, $resp);
|
2020-11-22 11:11:31 -06:00
|
|
|
self::assertStringContainsString($expectedTemplate, (string) $resp->getBody());
|
|
|
|
self::assertTrue($this->readFileCalled);
|
2019-11-23 03:25:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|