Remove old error handler, middleware and tests

This commit is contained in:
Franz Liedke 2019-08-09 20:49:09 +02:00
parent 8e0cd27f54
commit f73a39d3f4
25 changed files with 0 additions and 1281 deletions

View File

@ -1,51 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api;
use Exception;
use Throwable;
use Tobscure\JsonApi\Document;
use Tobscure\JsonApi\ErrorHandler as JsonApiErrorHandler;
class ErrorHandler
{
/**
* @var JsonApiErrorHandler
*/
protected $errorHandler;
/**
* @param JsonApiErrorHandler $errorHandler
*/
public function __construct(JsonApiErrorHandler $errorHandler)
{
$this->errorHandler = $errorHandler;
}
/**
* @param Exception $e
* @return JsonApiResponse
*/
public function handle(Throwable $e)
{
if (! $e instanceof Exception) {
$e = new Exception($e->getMessage(), $e->getCode(), $e);
}
$response = $this->errorHandler->handle($e);
$document = new Document;
$document->setErrors($response->getErrors());
return new JsonApiResponse($document, $response->getStatus());
}
}

View File

@ -1,77 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api\ExceptionHandler;
use Exception;
use Psr\Log\LoggerInterface;
use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface;
use Tobscure\JsonApi\Exception\Handler\ResponseBag;
class FallbackExceptionHandler implements ExceptionHandlerInterface
{
/**
* @var bool
*/
protected $debug;
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @param bool $debug
* @param LoggerInterface $logger
*/
public function __construct($debug, LoggerInterface $logger)
{
$this->debug = $debug;
$this->logger = $logger;
}
/**
* {@inheritdoc}
*/
public function manages(Exception $e)
{
return true;
}
/**
* {@inheritdoc}
*/
public function handle(Exception $e)
{
$status = 500;
$error = $this->constructError($e, $status);
$this->logger->error($e);
return new ResponseBag($status, [$error]);
}
/**
* @param Exception $e
* @param $status
* @return array
*/
private function constructError(Exception $e, $status)
{
$error = ['code' => $status, 'title' => 'Internal server error'];
if ($this->debug) {
$error['detail'] = (string) $e;
}
return $error;
}
}

View File

@ -1,42 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api\ExceptionHandler;
use Exception;
use Flarum\Post\Exception\FloodingException;
use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface;
use Tobscure\JsonApi\Exception\Handler\ResponseBag;
class FloodingExceptionHandler implements ExceptionHandlerInterface
{
/**
* {@inheritdoc}
*/
public function manages(Exception $e)
{
return $e instanceof FloodingException;
}
/**
* {@inheritdoc}
*/
public function handle(Exception $e)
{
$status = 429;
$error = [
'status' => (string) $status,
'code' => 'too_many_requests'
];
return new ResponseBag($status, [$error]);
}
}

View File

@ -1,58 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api\ExceptionHandler;
use Exception;
use Illuminate\Validation\ValidationException;
use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface;
use Tobscure\JsonApi\Exception\Handler\ResponseBag;
class IlluminateValidationExceptionHandler implements ExceptionHandlerInterface
{
/**
* {@inheritdoc}
*/
public function manages(Exception $e)
{
return $e instanceof ValidationException;
}
/**
* {@inheritdoc}
*/
public function handle(Exception $e)
{
$status = 422;
$errors = $this->formatErrors($e->errors());
return new ResponseBag($status, $errors);
}
/**
* @param array $errors
* @return array
*/
protected function formatErrors(array $errors)
{
$errors = array_map(function ($field, $messages) {
return [
'status' => '422',
'code' => 'validation_error',
'detail' => implode("\n", $messages),
'source' => ['pointer' => "/data/attributes/$field"]
];
}, array_keys($errors), $errors);
return $errors;
}
}

View File

@ -1,42 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api\ExceptionHandler;
use Exception;
use Flarum\Api\Exception\InvalidAccessTokenException;
use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface;
use Tobscure\JsonApi\Exception\Handler\ResponseBag;
class InvalidAccessTokenExceptionHandler implements ExceptionHandlerInterface
{
/**
* {@inheritdoc}
*/
public function manages(Exception $e)
{
return $e instanceof InvalidAccessTokenException;
}
/**
* {@inheritdoc}
*/
public function handle(Exception $e)
{
$status = 401;
$error = [
'status' => (string) $status,
'code' => 'invalid_access_token'
];
return new ResponseBag($status, [$error]);
}
}

View File

@ -1,42 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api\ExceptionHandler;
use Exception;
use Flarum\User\Exception\InvalidConfirmationTokenException;
use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface;
use Tobscure\JsonApi\Exception\Handler\ResponseBag;
class InvalidConfirmationTokenExceptionHandler implements ExceptionHandlerInterface
{
/**
* {@inheritdoc}
*/
public function manages(Exception $e)
{
return $e instanceof InvalidConfirmationTokenException;
}
/**
* {@inheritdoc}
*/
public function handle(Exception $e)
{
$status = 403;
$error = [
'status' => (string) $status,
'code' => 'invalid_confirmation_token'
];
return new ResponseBag($status, [$error]);
}
}

View File

@ -1,42 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api\ExceptionHandler;
use Exception;
use Flarum\Http\Exception\MethodNotAllowedException;
use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface;
use Tobscure\JsonApi\Exception\Handler\ResponseBag;
class MethodNotAllowedExceptionHandler implements ExceptionHandlerInterface
{
/**
* {@inheritdoc}
*/
public function manages(Exception $e)
{
return $e instanceof MethodNotAllowedException;
}
/**
* {@inheritdoc}
*/
public function handle(Exception $e)
{
$status = 405;
$error = [
'status' => (string) $status,
'code' => 'method_not_allowed'
];
return new ResponseBag($status, [$error]);
}
}

View File

@ -1,42 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api\ExceptionHandler;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface;
use Tobscure\JsonApi\Exception\Handler\ResponseBag;
class ModelNotFoundExceptionHandler implements ExceptionHandlerInterface
{
/**
* {@inheritdoc}
*/
public function manages(Exception $e)
{
return $e instanceof ModelNotFoundException;
}
/**
* {@inheritdoc}
*/
public function handle(Exception $e)
{
$status = 404;
$error = [
'status' => (string) $status,
'code' => 'resource_not_found'
];
return new ResponseBag($status, [$error]);
}
}

View File

@ -1,42 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api\ExceptionHandler;
use Exception;
use Flarum\User\Exception\PermissionDeniedException;
use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface;
use Tobscure\JsonApi\Exception\Handler\ResponseBag;
class PermissionDeniedExceptionHandler implements ExceptionHandlerInterface
{
/**
* {@inheritdoc}
*/
public function manages(Exception $e)
{
return $e instanceof PermissionDeniedException;
}
/**
* {@inheritdoc}
*/
public function handle(Exception $e)
{
$status = 401;
$error = [
'status' => (string) $status,
'code' => 'permission_denied'
];
return new ResponseBag($status, [$error]);
}
}

View File

@ -1,42 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api\ExceptionHandler;
use Exception;
use Flarum\Http\Exception\RouteNotFoundException;
use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface;
use Tobscure\JsonApi\Exception\Handler\ResponseBag;
class RouteNotFoundExceptionHandler implements ExceptionHandlerInterface
{
/**
* {@inheritdoc}
*/
public function manages(Exception $e)
{
return $e instanceof RouteNotFoundException;
}
/**
* {@inheritdoc}
*/
public function handle(Exception $e)
{
$status = 404;
$error = [
'status' => (string) $status,
'code' => 'route_not_found'
];
return new ResponseBag($status, [$error]);
}
}

View File

@ -1,42 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api\ExceptionHandler;
use Exception;
use Flarum\Http\Exception\TokenMismatchException;
use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface;
use Tobscure\JsonApi\Exception\Handler\ResponseBag;
class TokenMismatchExceptionHandler implements ExceptionHandlerInterface
{
/**
* {@inheritdoc}
*/
public function manages(Exception $e)
{
return $e instanceof TokenMismatchException;
}
/**
* {@inheritdoc}
*/
public function handle(Exception $e)
{
$status = 400;
$error = [
'status' => (string) $status,
'code' => 'csrf_token_mismatch'
];
return new ResponseBag($status, [$error]);
}
}

View File

@ -1,53 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api\ExceptionHandler;
use Exception;
use Flarum\Foundation\ValidationException;
use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface;
use Tobscure\JsonApi\Exception\Handler\ResponseBag;
class ValidationExceptionHandler implements ExceptionHandlerInterface
{
/**
* {@inheritdoc}
*/
public function manages(Exception $e)
{
return $e instanceof ValidationException;
}
/**
* {@inheritdoc}
*/
public function handle(Exception $e)
{
$errors = array_merge(
$this->buildErrors($e->getAttributes(), '/data/attributes'),
$this->buildErrors($e->getRelationships(), '/data/relationships')
);
return new ResponseBag(422, $errors);
}
private function buildErrors(array $messages, $pointer)
{
return array_map(function ($path, $detail) use ($pointer) {
return [
'status' => '422',
'code' => 'validation_error',
'detail' => $detail,
'source' => ['pointer' => $pointer.'/'.$path]
];
}, array_keys($messages), $messages);
}
}

View File

@ -1,47 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api\Middleware;
use Flarum\Api\ErrorHandler;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Server\RequestHandlerInterface as Handler;
use Throwable;
class HandleErrors implements Middleware
{
/**
* @var ErrorHandler
*/
protected $errorHandler;
/**
* @param ErrorHandler $errorHandler
*/
public function __construct(ErrorHandler $errorHandler)
{
$this->errorHandler = $errorHandler;
}
/**
* Catch all errors that happen during further middleware execution.
*/
public function process(Request $request, Handler $handler): Response
{
try {
return $handler->handle($request);
} catch (Throwable $e) {
return $this->errorHandler->handle($e);
}
}
}

View File

@ -1,111 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Http\Middleware;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Server\RequestHandlerInterface as Handler;
use Psr\Log\LoggerInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Throwable;
use Zend\Diactoros\Response\HtmlResponse;
class HandleErrorsWithView implements Middleware
{
/**
* @var ViewFactory
*/
protected $view;
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @var TranslatorInterface
*/
protected $translator;
/**
* @var SettingsRepositoryInterface
*/
protected $settings;
/**
* @param ViewFactory $view
* @param LoggerInterface $logger
* @param TranslatorInterface $translator
* @param SettingsRepositoryInterface $settings
*/
public function __construct(ViewFactory $view, LoggerInterface $logger, TranslatorInterface $translator, SettingsRepositoryInterface $settings)
{
$this->view = $view;
$this->logger = $logger;
$this->translator = $translator;
$this->settings = $settings;
}
/**
* Catch all errors that happen during further middleware execution.
*/
public function process(Request $request, Handler $handler): Response
{
try {
return $handler->handle($request);
} catch (Throwable $e) {
return $this->formatException($e);
}
}
protected function formatException(Throwable $error)
{
$status = 500;
$errorCode = $error->getCode();
// If it seems to be a valid HTTP status code, we pass on the
// exception's status.
if (is_int($errorCode) && $errorCode >= 400 && $errorCode < 600) {
$status = $errorCode;
}
if (! $this->view->exists($name = "flarum.forum::error.$status")) {
$name = 'flarum.forum::error.default';
$this->logger->error($error);
}
$view = $this->view->make($name)
->with('error', $error)
->with('message', $this->getMessage($status));
return new HtmlResponse($view->render(), $status);
}
private function getMessage($status)
{
return $this->getTranslationIfExists($status)
?? $this->getTranslationIfExists(500)
?? 'An error occurred while trying to load this page.';
}
private function getTranslationIfExists($status)
{
$key = "core.views.error.${status}_message";
$translation = $this->translator->trans($key, ['{forum}' => $this->settings->get('forum_title')]);
return $translation === $key ? null : $translation;
}
}

View File

@ -1,60 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Http\Middleware;
use Franzl\Middleware\Whoops\WhoopsRunner;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Server\RequestHandlerInterface as Handler;
use Psr\Log\LoggerInterface;
use Throwable;
class HandleErrorsWithWhoops implements Middleware
{
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* Catch all errors that happen during further middleware execution.
*/
public function process(Request $request, Handler $handler): Response
{
try {
return $handler->handle($request);
} catch (Throwable $e) {
$status = 500;
$errorCode = $e->getCode();
if ($errorCode !== 404) {
$this->logger->error($e);
}
if (is_int($errorCode) && $errorCode >= 400 && $errorCode < 600) {
$status = $errorCode;
}
return WhoopsRunner::handle($e, $request)
->withStatus($status);
}
}
}

View File

@ -1,46 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Tests\unit\Api\ExceptionHandler;
use Exception;
use Flarum\Api\ExceptionHandler\FloodingExceptionHandler;
use Flarum\Post\Exception\FloodingException;
use PHPUnit\Framework\TestCase;
class FloodingExceptionHandlerTest extends TestCase
{
private $handler;
public function setUp()
{
$this->handler = new FloodingExceptionHandler;
}
public function test_it_handles_recognisable_exceptions()
{
$this->assertFalse($this->handler->manages(new Exception));
$this->assertTrue($this->handler->manages(new FloodingException));
}
public function test_it_provides_expected_output()
{
$result = $this->handler->handle(new FloodingException);
$this->assertEquals(429, $result->getStatus());
$this->assertEquals([
[
'status' => '429',
'code' => 'too_many_requests'
]
], $result->getErrors());
}
}

View File

@ -1,63 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Tests\unit\Api\ExceptionHandler;
use Exception;
use Flarum\Api\ExceptionHandler\IlluminateValidationExceptionHandler;
use Illuminate\Translation\ArrayLoader;
use Illuminate\Translation\Translator;
use Illuminate\Validation\Factory;
use Illuminate\Validation\ValidationException;
use PHPUnit\Framework\TestCase;
class IlluminateValidationExceptionHandlerTest extends TestCase
{
private $handler;
public function setUp()
{
$this->handler = new IlluminateValidationExceptionHandler;
}
public function test_it_handles_familiar_exceptions()
{
$validException = new ValidationException($this->makeValidator());
$this->assertFalse($this->handler->manages(new Exception));
$this->assertTrue($this->handler->manages($validException));
}
public function test_it_creates_the_desired_output()
{
$exception = new ValidationException($this->makeValidator(['foo' => ''], ['foo' => 'required']));
$response = $this->handler->handle($exception);
$this->assertEquals(422, $response->getStatus());
$this->assertEquals([
[
'status' => '422',
'code' => 'validation_error',
'detail' => 'validation.required',
'source' => ['pointer' => '/data/attributes/foo']
]
], $response->getErrors());
}
private function makeValidator($data = [], $rules = [])
{
$translator = new Translator(new ArrayLoader(), 'en');
$factory = new Factory($translator);
return $factory->make($data, $rules);
}
}

View File

@ -1,46 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Tests\unit\Api\ExceptionHandler;
use Exception;
use Flarum\Api\Exception\InvalidAccessTokenException;
use Flarum\Api\ExceptionHandler\InvalidAccessTokenExceptionHandler;
use PHPUnit\Framework\TestCase;
class InvalidAccessTokenExceptionHandlerTest extends TestCase
{
private $handler;
public function setUp()
{
$this->handler = new InvalidAccessTokenExceptionHandler;
}
public function test_it_handles_recognisable_exceptions()
{
$this->assertFalse($this->handler->manages(new Exception));
$this->assertTrue($this->handler->manages(new InvalidAccessTokenException));
}
public function test_output()
{
$response = $this->handler->handle(new InvalidAccessTokenException);
$this->assertEquals(401, $response->getStatus());
$this->assertEquals([
[
'status' => '401',
'code' => 'invalid_access_token'
]
], $response->getErrors());
}
}

View File

@ -1,46 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Tests\unit\Api\ExceptionHandler;
use Exception;
use Flarum\Api\ExceptionHandler\InvalidConfirmationTokenExceptionHandler;
use Flarum\User\Exception\InvalidConfirmationTokenException;
use PHPUnit\Framework\TestCase;
class InvalidConfirmationTokenExceptionHandlerTest extends TestCase
{
private $handler;
public function setUp()
{
$this->handler = new InvalidConfirmationTokenExceptionHandler;
}
public function test_it_handles_recognisable_exceptions()
{
$this->assertFalse($this->handler->manages(new Exception));
$this->assertTrue($this->handler->manages(new InvalidConfirmationTokenException));
}
public function test_output()
{
$response = $this->handler->handle(new InvalidConfirmationTokenException);
$this->assertEquals(403, $response->getStatus());
$this->assertEquals([
[
'status' => '403',
'code' => 'invalid_confirmation_token'
]
], $response->getErrors());
}
}

View File

@ -1,46 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Tests\unit\Api\ExceptionHandler;
use Exception;
use Flarum\Api\ExceptionHandler\MethodNotAllowedExceptionHandler;
use Flarum\Http\Exception\MethodNotAllowedException;
use PHPUnit\Framework\TestCase;
class MethodNotAllowedExceptionHandlerTest extends TestCase
{
private $handler;
public function setUp()
{
$this->handler = new MethodNotAllowedExceptionHandler();
}
public function test_it_handles_recognisable_exceptions()
{
$this->assertFalse($this->handler->manages(new Exception));
$this->assertTrue($this->handler->manages(new MethodNotAllowedException()));
}
public function test_managing_exceptions()
{
$response = $this->handler->handle(new MethodNotAllowedException);
$this->assertEquals(405, $response->getStatus());
$this->assertEquals([
[
'status' => '405',
'code' => 'method_not_allowed'
]
], $response->getErrors());
}
}

View File

@ -1,46 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Tests\unit\Api\ExceptionHandler;
use Exception;
use Flarum\Api\ExceptionHandler\ModelNotFoundExceptionHandler;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use PHPUnit\Framework\TestCase;
class ModelNotFoundExceptionHandlerTest extends TestCase
{
private $handler;
public function setUp()
{
$this->handler = new ModelNotFoundExceptionHandler;
}
public function test_it_handles_recognisable_exceptions()
{
$this->assertFalse($this->handler->manages(new Exception));
$this->assertTrue($this->handler->manages(new ModelNotFoundException));
}
public function test_managing_exceptions()
{
$response = $this->handler->handle(new ModelNotFoundException);
$this->assertEquals(404, $response->getStatus());
$this->assertEquals([
[
'status' => '404',
'code' => 'resource_not_found'
]
], $response->getErrors());
}
}

View File

@ -1,46 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Tests\unit\Api\ExceptionHandler;
use Exception;
use Flarum\Api\ExceptionHandler\PermissionDeniedExceptionHandler;
use Flarum\User\Exception\PermissionDeniedException;
use PHPUnit\Framework\TestCase;
class PermissionDeniedExceptionHandlerTest extends TestCase
{
private $handler;
public function setUp()
{
$this->handler = new PermissionDeniedExceptionHandler;
}
public function test_it_handles_recognisable_exceptions()
{
$this->assertFalse($this->handler->manages(new Exception));
$this->assertTrue($this->handler->manages(new PermissionDeniedException));
}
public function test_managing_exceptions()
{
$response = $this->handler->handle(new PermissionDeniedException);
$this->assertEquals(401, $response->getStatus());
$this->assertEquals([
[
'status' => '401',
'code' => 'permission_denied'
]
], $response->getErrors());
}
}

View File

@ -1,46 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Tests\unit\Api\ExceptionHandler;
use Exception;
use Flarum\Api\ExceptionHandler\RouteNotFoundExceptionHandler;
use Flarum\Http\Exception\RouteNotFoundException;
use PHPUnit\Framework\TestCase;
class RouteNotFoundExceptionHandlerTest extends TestCase
{
private $handler;
public function setUp()
{
$this->handler = new RouteNotFoundExceptionHandler();
}
public function test_it_handles_recognisable_exceptions()
{
$this->assertFalse($this->handler->manages(new Exception));
$this->assertTrue($this->handler->manages(new RouteNotFoundException()));
}
public function test_managing_exceptions()
{
$response = $this->handler->handle(new RouteNotFoundException);
$this->assertEquals(404, $response->getStatus());
$this->assertEquals([
[
'status' => '404',
'code' => 'route_not_found'
]
], $response->getErrors());
}
}

View File

@ -1,46 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Tests\unit\Api\ExceptionHandler;
use Exception;
use Flarum\Api\ExceptionHandler\TokenMismatchExceptionHandler;
use Flarum\Http\Exception\TokenMismatchException;
use PHPUnit\Framework\TestCase;
class TokenMismatchExceptionHandlerTest extends TestCase
{
private $handler;
public function setUp()
{
$this->handler = new TokenMismatchExceptionHandler;
}
public function test_it_handles_recognisable_exceptions()
{
$this->assertFalse($this->handler->manages(new Exception));
$this->assertTrue($this->handler->manages(new TokenMismatchException()));
}
public function test_managing_exceptions()
{
$response = $this->handler->handle(new TokenMismatchException);
$this->assertEquals(400, $response->getStatus());
$this->assertEquals([
[
'status' => '400',
'code' => 'csrf_token_mismatch'
]
], $response->getErrors());
}
}

View File

@ -1,57 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Tests\unit\Api\ExceptionHandler;
use Exception;
use Flarum\Api\ExceptionHandler\ValidationExceptionHandler;
use Flarum\Foundation\ValidationException;
use PHPUnit\Framework\TestCase;
class ValidationExceptionHandlerTest extends TestCase
{
private $handler;
public function setUp()
{
$this->handler = new ValidationExceptionHandler;
}
public function test_it_handles_recognisable_exceptions()
{
$this->assertFalse($this->handler->manages(new Exception));
$this->assertTrue($this->handler->manages(new ValidationException([])));
}
public function test_managing_exceptions()
{
$response = $this->handler->handle(new ValidationException(
['foo' => 'Attribute error'],
['bar' => 'Relationship error']
));
$this->assertEquals(422, $response->getStatus());
$this->assertEquals([
[
'status' => '422',
'code' => 'validation_error',
'detail' => 'Attribute error',
'source' => ['pointer' => '/data/attributes/foo']
],
[
'status' => '422',
'code' => 'validation_error',
'detail' => 'Relationship error',
'source' => ['pointer' => '/data/relationships/bar']
]
], $response->getErrors());
}
}