mirror of
https://github.com/flarum/framework.git
synced 2025-01-19 07:42:48 +08:00
Merge pull request #611 from kirkbushell/master
Tests for all the exception handlers
This commit is contained in:
commit
aa7b4dd754
|
@ -67,7 +67,7 @@
|
|||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"tests\\": "tests/"
|
||||
"Tests\\": "tests/"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
|
|
0
phpunit.yml
Normal file
0
phpunit.yml
Normal file
|
@ -31,8 +31,17 @@ class IlluminateValidationExceptionHandler implements ExceptionHandlerInterface
|
|||
public function handle(Exception $e)
|
||||
{
|
||||
$status = 422;
|
||||
$errors = $this->formatErrors($e->errors()->toArray());
|
||||
|
||||
$errors = $e->errors()->toArray();
|
||||
return new ResponseBag($status, $errors);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $errors
|
||||
* @return array
|
||||
*/
|
||||
protected function formatErrors(array $errors)
|
||||
{
|
||||
$errors = array_map(function ($field, $messages) {
|
||||
return [
|
||||
'detail' => implode("\n", $messages),
|
||||
|
@ -40,6 +49,6 @@ class IlluminateValidationExceptionHandler implements ExceptionHandlerInterface
|
|||
];
|
||||
}, array_keys($errors), $errors);
|
||||
|
||||
return new ResponseBag($status, $errors);
|
||||
return $errors;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,15 +1,14 @@
|
|||
<?php
|
||||
namespace tests\Flarum\Admin\Middleware;
|
||||
namespace Tests\Flarum\Admin\Middleware;
|
||||
|
||||
use Flarum\Admin\Middleware\AuthenticateWithCookie;
|
||||
use Flarum\Admin\Middleware\RequireAdministrateAbility;
|
||||
use Flarum\Core\Access\Gate;
|
||||
use Flarum\Core\Exception\PermissionDeniedException;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Mockery as m;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use tests\Test\TestCase;
|
||||
use Tests\Test\TestCase;
|
||||
|
||||
class RequireAdministrateAbilityTest extends TestCase
|
||||
{
|
||||
|
|
30
tests/Flarum/Api/Handler/FloodingExceptionHandlerTest.php
Normal file
30
tests/Flarum/Api/Handler/FloodingExceptionHandlerTest.php
Normal file
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
namespace Tests\Flarum\Api\Handler;
|
||||
|
||||
use Flarum\Api\Handler\FloodingExceptionHandler;
|
||||
use Flarum\Core\Exception\FloodingException;
|
||||
use Tests\Test\TestCase;
|
||||
|
||||
class FloodingExceptionHandlerTest extends TestCase
|
||||
{
|
||||
private $handler;
|
||||
|
||||
public function init()
|
||||
{
|
||||
$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([[]], $result->getErrors());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
namespace Tests\Flarum\Api\Handler;
|
||||
|
||||
use Flarum\Api\Handler\ValidationExceptionHandler;
|
||||
use Flarum\Core\Exception\ValidationException;
|
||||
use Tests\Test\TestCase;
|
||||
|
||||
class IlluminateValidationExceptionHandlerTest extends TestCase
|
||||
{
|
||||
private $handler;
|
||||
|
||||
public function init()
|
||||
{
|
||||
$this->handler = new ValidationExceptionHandler;
|
||||
}
|
||||
|
||||
public function test_it_handles_familiar_exceptions()
|
||||
{
|
||||
$validException = new ValidationException(['messages']);
|
||||
|
||||
$this->assertFalse($this->handler->manages(new \Exception));
|
||||
$this->assertTrue($this->handler->manages($validException));
|
||||
}
|
||||
|
||||
public function test_it_creates_the_desired_output()
|
||||
{
|
||||
$this->markTestIncomplete();
|
||||
|
||||
$exception = new ValidationException(['field' => ['Some error']]);
|
||||
|
||||
$response = $this->handler->handle($exception);
|
||||
|
||||
$this->assertEquals(422, $response->getStatus());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
namespace Tests\Flarum\Api\Handler;
|
||||
|
||||
use Flarum\Api\Handler\InvalidConfirmationTokenExceptionHandler;
|
||||
use Flarum\Core\Exception\InvalidConfirmationTokenException;
|
||||
use Tests\Test\TestCase;
|
||||
|
||||
class InvalidConfirmationTokenExceptionHandlerTest extends TestCase
|
||||
{
|
||||
private $handler;
|
||||
|
||||
public function init()
|
||||
{
|
||||
$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([['code' => 'invalid_confirmation_token']], $response->getErrors());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
namespace Tests\Flarum\Api\Handler;
|
||||
|
||||
use Flarum\Api\Handler\ModelNotFoundExceptionHandler;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Tests\Test\TestCase;
|
||||
|
||||
class ModelNotFoundExceptionHandlerTest extends TestCase
|
||||
{
|
||||
private $handler;
|
||||
|
||||
public function init()
|
||||
{
|
||||
$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([[]], $response->getErrors());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
namespace Tests\Flarum\Api\Handler;
|
||||
|
||||
use Flarum\Api\Handler\PermissionDeniedExceptionHandler;
|
||||
use Flarum\Core\Exception\PermissionDeniedException;
|
||||
use Tests\Test\TestCase;
|
||||
|
||||
class PermissionDeniedExceptionHandlerTest extends TestCase
|
||||
{
|
||||
private $handler;
|
||||
|
||||
public function init()
|
||||
{
|
||||
$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([[]], $response->getErrors());
|
||||
}
|
||||
}
|
30
tests/Flarum/Api/Handler/ValidationExceptionHandlerTest.php
Normal file
30
tests/Flarum/Api/Handler/ValidationExceptionHandlerTest.php
Normal file
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
namespace Tests\Flarum\Api\Handler;
|
||||
|
||||
use Flarum\Api\Handler\ValidationExceptionHandler;
|
||||
use Flarum\Core\Exception\ValidationException;
|
||||
use Tests\Test\TestCase;
|
||||
|
||||
class ValidationExceptionHandlerTest extends TestCase
|
||||
{
|
||||
private $handler;
|
||||
|
||||
public function init()
|
||||
{
|
||||
$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(['There was an error']));
|
||||
|
||||
$this->assertEquals(422, $response->getStatus());
|
||||
$this->assertEquals([['source' => ['pointer' => '/data/attributes/0'], 'detail' => 'There was an error']], $response->getErrors());
|
||||
}
|
||||
}
|
|
@ -1,10 +1,10 @@
|
|||
<?php
|
||||
namespace tests\Flarum\Core\Settings;
|
||||
namespace Tests\Flarum\Core\Settings;
|
||||
|
||||
use Flarum\Settings\DatabaseSettingsRepository;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Mockery as m;
|
||||
use tests\Test\TestCase;
|
||||
use Tests\Test\TestCase;
|
||||
|
||||
class DatabaseSettingsRepositoryTest extends TestCase
|
||||
{
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
<?php
|
||||
namespace tests\Flarum\Core\Settings;
|
||||
namespace Tests\Flarum\Core\Settings;
|
||||
|
||||
use Flarum\Settings\MemoryCacheSettingsRepository;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Mockery as m;
|
||||
use tests\Test\TestCase;
|
||||
use Tests\Test\TestCase;
|
||||
|
||||
class MemoryCacheSettingsRepositoryTest extends TestCase
|
||||
{
|
||||
|
@ -13,7 +13,7 @@ class MemoryCacheSettingsRepositoryTest extends TestCase
|
|||
|
||||
public function init()
|
||||
{
|
||||
$this->baseRepository = m::mock(\Flarum\Settings\SettingsRepositoryInterface::class);
|
||||
$this->baseRepository = m::mock(SettingsRepositoryInterface::class);
|
||||
$this->repository = new MemoryCacheSettingsRepository($this->baseRepository);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<?php
|
||||
namespace tests\Test;
|
||||
namespace Tests\Test;
|
||||
|
||||
use Mockery;
|
||||
use PHPUnit_Framework_TestCase;
|
||||
|
|
Loading…
Reference in New Issue
Block a user