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;
|
2021-02-09 10:28:06 -06:00
|
|
|
use Laminas\Diactoros\Uri;
|
2020-01-01 14:11:53 -06:00
|
|
|
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;
|
2021-02-09 10:28:06 -06:00
|
|
|
use Shlinkio\Shlink\Core\Action\RedirectAction;
|
|
|
|
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
|
2019-11-23 03:25:12 -06:00
|
|
|
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 bool $readFileCalled;
|
2019-11-23 03:25:12 -06:00
|
|
|
|
2022-09-11 05:02:49 -05:00
|
|
|
protected function setUp(): void
|
2019-11-23 03:25:12 -06:00
|
|
|
{
|
2020-11-22 11:11:31 -06:00
|
|
|
$this->readFileCalled = false;
|
2021-02-09 10:28:06 -06:00
|
|
|
$readFile = function (string $fileName): string {
|
2020-11-22 11:11:31 -06:00
|
|
|
$this->readFileCalled = true;
|
|
|
|
return $fileName;
|
|
|
|
};
|
2021-02-09 10:28:06 -06:00
|
|
|
$this->handler = new NotFoundTemplateHandler($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
|
|
|
|
{
|
2021-02-09 10:28:06 -06:00
|
|
|
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/foo'));
|
2019-11-23 03:25:12 -06:00
|
|
|
|
2021-02-09 10:28:06 -06:00
|
|
|
yield 'base url' => [$this->withNotFoundType($request, '/foo'), NotFoundTemplateHandler::NOT_FOUND_TEMPLATE];
|
|
|
|
yield 'regular not found' => [$this->withNotFoundType($request), NotFoundTemplateHandler::NOT_FOUND_TEMPLATE];
|
|
|
|
yield 'invalid short code' => [
|
|
|
|
$this->withNotFoundType($request->withAttribute(
|
2019-11-23 03:25:12 -06:00
|
|
|
RouteResult::class,
|
2021-02-09 10:28:06 -06:00
|
|
|
RouteResult::fromRoute(
|
|
|
|
new Route(
|
|
|
|
'',
|
2022-10-21 12:25:29 -05:00
|
|
|
$this->createMock(MiddlewareInterface::class),
|
2021-02-09 10:28:06 -06:00
|
|
|
['GET'],
|
|
|
|
RedirectAction::class,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)),
|
2019-11-23 03:25:12 -06:00
|
|
|
NotFoundTemplateHandler::INVALID_SHORT_CODE_TEMPLATE,
|
|
|
|
];
|
|
|
|
}
|
2021-02-09 10:28:06 -06:00
|
|
|
|
|
|
|
private function withNotFoundType(ServerRequestInterface $req, string $baseUrl = ''): ServerRequestInterface
|
|
|
|
{
|
|
|
|
$type = NotFoundType::fromRequest($req, $baseUrl);
|
|
|
|
return $req->withAttribute(NotFoundType::class, $type);
|
|
|
|
}
|
2019-11-23 03:25:12 -06:00
|
|
|
}
|