115 lines
3.7 KiB
PHP
115 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace TorstenHettstedt\TimekeepingApi\Tests\Unit\Middleware;
|
|
|
|
use Codeception\Stub\Expected;
|
|
use Codeception\Test\Unit;
|
|
use Exception;
|
|
use Psr\Container\ContainerInterface;
|
|
use Psr\Http\Message\ResponseFactoryInterface;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use Psr\Http\Message\StreamInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
use Slim\App;
|
|
use Slim\Exception\HttpException;
|
|
use Slim\Interfaces\RouteParserInterface;
|
|
use Slim\Routing\RouteContext;
|
|
use Slim\Routing\RoutingResults;
|
|
use TorstenHettstedt\TimekeepingApi\Middleware\ErrorHandler;
|
|
|
|
class ErrorHandlerTest extends Unit
|
|
{
|
|
|
|
/** @var App<ContainerInterface> */
|
|
protected App $app;
|
|
protected ServerRequestInterface $request;
|
|
protected Exception $exception;
|
|
protected LoggerInterface $logger;
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
protected function _before(): void
|
|
{
|
|
parent::_before();
|
|
$this->app = $this->makeEmpty(App::class, [
|
|
'getResponseFactory' => $this->makeEmpty(ResponseFactoryInterface::class, [
|
|
'createResponse' => $this->makeEmpty(ResponseInterface::class, [
|
|
'getBody' => $this->makeEmpty(StreamInterface::class, [
|
|
'write' => function (mixed $data) {
|
|
$this->assertIsString($data);
|
|
$this->assertJson($data);
|
|
return strlen($data);
|
|
},
|
|
]),
|
|
]),
|
|
]),
|
|
]);
|
|
$this->request = $this->makeEmpty(ServerRequestInterface::class, [
|
|
'getAttribute' => function ($name) {
|
|
/** @noinspection PhpSwitchCanBeReplacedWithMatchExpressionInspection */
|
|
switch ($name) {
|
|
case RouteContext::ROUTE_PARSER:
|
|
return $this->makeEmpty(RouteParserInterface::class);
|
|
case RouteContext::ROUTING_RESULTS:
|
|
return $this->makeEmpty(RoutingResults::class);
|
|
default:
|
|
return null;
|
|
}
|
|
},
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function testUseWithLogger(): void
|
|
{
|
|
$this->exception = $this->make(Exception::class, [
|
|
'message' => '',
|
|
'code' => 400,
|
|
'file' => '/path(to/file',
|
|
]);
|
|
$this->logger = $this->makeEmpty(LoggerInterface::class, [
|
|
'error' => Expected::once(),
|
|
]);
|
|
|
|
$middleWare = new ErrorHandler($this->app);
|
|
$middleWare($this->request, $this->exception, true, true, true, $this->logger);
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function testUseWithHttpException(): void
|
|
{
|
|
$this->exception = $this->make(HttpException::class, [
|
|
'message' => '',
|
|
'code' => 400,
|
|
'file' => '/path(to/file',
|
|
'getTitle' => Expected::once('The Title'),
|
|
]);
|
|
$this->logger = $this->makeEmpty(LoggerInterface::class);
|
|
|
|
$middleWare = new ErrorHandler($this->app);
|
|
$middleWare($this->request, $this->exception, true, true, true, $this->logger);
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function testUseWithGeneralException(): void
|
|
{
|
|
$this->exception = $this->make(Exception::class, [
|
|
'message' => '',
|
|
'code' => 400,
|
|
'file' => '/path(to/file',
|
|
]);
|
|
$this->logger = $this->makeEmpty(LoggerInterface::class);
|
|
|
|
$middleWare = new ErrorHandler($this->app);
|
|
$middleWare($this->request, $this->exception, true, true, true, $this->logger);
|
|
}
|
|
}
|