Use 404 template instead of default Slim error page if the route is not found. Fixes #827
82 lines
2.3 KiB
PHP
82 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Shaarli\Front\Controller\Visitor;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use Slim\Http\Request;
|
|
use Slim\Http\Response;
|
|
use Slim\Http\Uri;
|
|
|
|
class ErrorNotFoundControllerTest extends TestCase
|
|
{
|
|
use FrontControllerMockHelper;
|
|
|
|
/** @var ErrorNotFoundController */
|
|
protected $controller;
|
|
|
|
public function setUp(): void
|
|
{
|
|
$this->createContainer();
|
|
|
|
$this->controller = new ErrorNotFoundController($this->container);
|
|
}
|
|
|
|
/**
|
|
* Test displaying 404 error
|
|
*/
|
|
public function testDisplayNotFoundError(): void
|
|
{
|
|
$request = $this->createMock(Request::class);
|
|
$request->expects(static::once())->method('getRequestTarget')->willReturn('/');
|
|
$request->method('getUri')->willReturnCallback(function (): Uri {
|
|
$uri = $this->createMock(Uri::class);
|
|
$uri->method('getBasePath')->willReturn('/subfolder');
|
|
|
|
return $uri;
|
|
});
|
|
|
|
$response = new Response();
|
|
|
|
// Save RainTPL assigned variables
|
|
$assignedVariables = [];
|
|
$this->assignTemplateVars($assignedVariables);
|
|
|
|
$result = ($this->controller)(
|
|
$request,
|
|
$response
|
|
);
|
|
|
|
static::assertSame(404, $result->getStatusCode());
|
|
static::assertSame('404', (string) $result->getBody());
|
|
static::assertSame('Requested page could not be found.', $assignedVariables['error_message']);
|
|
}
|
|
|
|
/**
|
|
* Test displaying 404 error from REST API
|
|
*/
|
|
public function testDisplayNotFoundErrorFromAPI(): void
|
|
{
|
|
$request = $this->createMock(Request::class);
|
|
$request->expects(static::once())->method('getRequestTarget')->willReturn('/sufolder/api/v1/links');
|
|
$request->method('getUri')->willReturnCallback(function (): Uri {
|
|
$uri = $this->createMock(Uri::class);
|
|
$uri->method('getBasePath')->willReturn('/subfolder');
|
|
|
|
return $uri;
|
|
});
|
|
|
|
$response = new Response();
|
|
|
|
// Save RainTPL assigned variables
|
|
$assignedVariables = [];
|
|
$this->assignTemplateVars($assignedVariables);
|
|
|
|
$result = ($this->controller)($request, $response);
|
|
|
|
static::assertSame(404, $result->getStatusCode());
|
|
static::assertSame([], $assignedVariables);
|
|
}
|
|
}
|