2020-08-21 10:50:44 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Shaarli\Front\Controller\Visitor;
|
|
|
|
|
|
|
|
use Shaarli\Front\Exception\ShaarliFrontException;
|
2020-09-29 14:41:40 +02:00
|
|
|
use Shaarli\TestCase;
|
2020-08-21 10:50:44 +02:00
|
|
|
use Slim\Http\Request;
|
|
|
|
use Slim\Http\Response;
|
|
|
|
|
|
|
|
class ErrorControllerTest extends TestCase
|
|
|
|
{
|
|
|
|
use FrontControllerMockHelper;
|
|
|
|
|
|
|
|
/** @var ErrorController */
|
|
|
|
protected $controller;
|
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
$this->createContainer();
|
|
|
|
|
|
|
|
$this->controller = new ErrorController($this->container);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test displaying error with a ShaarliFrontException: display exception message and use its code for HTTTP code
|
|
|
|
*/
|
|
|
|
public function testDisplayFrontExceptionError(): void
|
|
|
|
{
|
|
|
|
$request = $this->createMock(Request::class);
|
|
|
|
$response = new Response();
|
|
|
|
|
|
|
|
$message = 'error message';
|
|
|
|
$errorCode = 418;
|
|
|
|
|
|
|
|
// Save RainTPL assigned variables
|
|
|
|
$assignedVariables = [];
|
|
|
|
$this->assignTemplateVars($assignedVariables);
|
|
|
|
|
|
|
|
$result = ($this->controller)(
|
|
|
|
$request,
|
|
|
|
$response,
|
|
|
|
new class($message, $errorCode) extends ShaarliFrontException {}
|
|
|
|
);
|
|
|
|
|
|
|
|
static::assertSame($errorCode, $result->getStatusCode());
|
|
|
|
static::assertSame($message, $assignedVariables['message']);
|
|
|
|
static::assertArrayNotHasKey('stacktrace', $assignedVariables);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-05 19:45:41 +01:00
|
|
|
* Test displaying error with any exception (no debug) while logged in:
|
|
|
|
* display full error details
|
|
|
|
*/
|
|
|
|
public function testDisplayAnyExceptionErrorNoDebugLoggedIn(): void
|
|
|
|
{
|
|
|
|
$request = $this->createMock(Request::class);
|
|
|
|
$response = new Response();
|
|
|
|
|
|
|
|
// Save RainTPL assigned variables
|
|
|
|
$assignedVariables = [];
|
|
|
|
$this->assignTemplateVars($assignedVariables);
|
|
|
|
|
|
|
|
$this->container->loginManager->method('isLoggedIn')->willReturn(true);
|
|
|
|
|
|
|
|
$result = ($this->controller)($request, $response, new \Exception('abc'));
|
|
|
|
|
|
|
|
static::assertSame(500, $result->getStatusCode());
|
|
|
|
static::assertSame('Error: abc', $assignedVariables['message']);
|
|
|
|
static::assertContainsPolyfill('Please report it on Github', $assignedVariables['text']);
|
|
|
|
static::assertArrayHasKey('stacktrace', $assignedVariables);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test displaying error with any exception (no debug) while logged out:
|
|
|
|
* display standard error without detail
|
2020-08-21 10:50:44 +02:00
|
|
|
*/
|
|
|
|
public function testDisplayAnyExceptionErrorNoDebug(): void
|
|
|
|
{
|
|
|
|
$request = $this->createMock(Request::class);
|
|
|
|
$response = new Response();
|
|
|
|
|
|
|
|
// Save RainTPL assigned variables
|
|
|
|
$assignedVariables = [];
|
|
|
|
$this->assignTemplateVars($assignedVariables);
|
|
|
|
|
2020-11-05 19:45:41 +01:00
|
|
|
$this->container->loginManager->method('isLoggedIn')->willReturn(false);
|
|
|
|
|
2020-08-21 10:50:44 +02:00
|
|
|
$result = ($this->controller)($request, $response, new \Exception('abc'));
|
|
|
|
|
|
|
|
static::assertSame(500, $result->getStatusCode());
|
|
|
|
static::assertSame('An unexpected error occurred.', $assignedVariables['message']);
|
2020-11-05 19:45:41 +01:00
|
|
|
static::assertArrayNotHasKey('text', $assignedVariables);
|
2020-08-21 10:50:44 +02:00
|
|
|
static::assertArrayNotHasKey('stacktrace', $assignedVariables);
|
|
|
|
}
|
|
|
|
}
|