MyShaarli/tests/front/controller/visitor/ErrorNotFoundControllerTest.php

82 lines
2.3 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace Shaarli\Front\Controller\Visitor;
2020-09-29 14:41:40 +02:00
use Shaarli\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);
}
}