mirror of
https://github.com/flarum/core.git
synced 2025-07-30 13:10:24 +02:00
Remove old error handler, middleware and tests
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
}
|
@@ -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());
|
||||
}
|
||||
}
|
@@ -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());
|
||||
}
|
||||
}
|
@@ -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());
|
||||
}
|
||||
}
|
@@ -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());
|
||||
}
|
||||
}
|
@@ -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());
|
||||
}
|
||||
}
|
@@ -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());
|
||||
}
|
||||
}
|
@@ -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());
|
||||
}
|
||||
}
|
@@ -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());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user