From 067f87eacb0295307e5f6bdd6d69d01d1a577174 Mon Sep 17 00:00:00 2001 From: Kirk Bushell Date: Tue, 27 Oct 2015 12:48:27 +0000 Subject: [PATCH 1/8] Updated tests namespace to be ps4-valid. Added tests for flooding exception, fixed broken code --- framework/core/composer.json | 2 +- framework/core/phpunit.yml | 0 .../RequireAdministrateAbilityTest.php | 2 +- .../Handler/FloodingExceptionHandlerTest.php | 22 +++++++++++++++++++ .../DatabaseSettingsRepositoryTest.php | 2 +- .../MemoryCacheSettingsRepositoryTest.php | 2 +- framework/core/tests/Test/TestCase.php | 2 +- 7 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 framework/core/phpunit.yml create mode 100644 framework/core/tests/Flarum/Api/Handler/FloodingExceptionHandlerTest.php diff --git a/framework/core/composer.json b/framework/core/composer.json index c81c3f702..10527f74e 100644 --- a/framework/core/composer.json +++ b/framework/core/composer.json @@ -67,7 +67,7 @@ }, "autoload-dev": { "psr-4": { - "tests\\": "tests/" + "Tests\\": "tests/" } }, "scripts": { diff --git a/framework/core/phpunit.yml b/framework/core/phpunit.yml new file mode 100644 index 000000000..e69de29bb diff --git a/framework/core/tests/Flarum/Admin/Middleware/RequireAdministrateAbilityTest.php b/framework/core/tests/Flarum/Admin/Middleware/RequireAdministrateAbilityTest.php index 607000928..261eba796 100644 --- a/framework/core/tests/Flarum/Admin/Middleware/RequireAdministrateAbilityTest.php +++ b/framework/core/tests/Flarum/Admin/Middleware/RequireAdministrateAbilityTest.php @@ -1,5 +1,5 @@ handler = new FloodingExceptionHandler; + } + + public function test_it_handles_recognisable_exceptions() + { + $this->assertFalse($this->handler->manages(new \Exception)); + $this->assertTrue($this->handler->manages(new FloodingException)); + } +} diff --git a/framework/core/tests/Flarum/Core/Settings/DatabaseSettingsRepositoryTest.php b/framework/core/tests/Flarum/Core/Settings/DatabaseSettingsRepositoryTest.php index 3e7929dc1..69b52ce64 100644 --- a/framework/core/tests/Flarum/Core/Settings/DatabaseSettingsRepositoryTest.php +++ b/framework/core/tests/Flarum/Core/Settings/DatabaseSettingsRepositoryTest.php @@ -1,5 +1,5 @@ Date: Tue, 27 Oct 2015 12:50:11 +0000 Subject: [PATCH 2/8] Fied broken test --- .../Admin/Middleware/RequireAdministrateAbilityTest.php | 3 +-- .../Flarum/Core/Settings/DatabaseSettingsRepositoryTest.php | 2 +- .../Core/Settings/MemoryCacheSettingsRepositoryTest.php | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/framework/core/tests/Flarum/Admin/Middleware/RequireAdministrateAbilityTest.php b/framework/core/tests/Flarum/Admin/Middleware/RequireAdministrateAbilityTest.php index 261eba796..6dfaf84aa 100644 --- a/framework/core/tests/Flarum/Admin/Middleware/RequireAdministrateAbilityTest.php +++ b/framework/core/tests/Flarum/Admin/Middleware/RequireAdministrateAbilityTest.php @@ -5,11 +5,10 @@ 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 { diff --git a/framework/core/tests/Flarum/Core/Settings/DatabaseSettingsRepositoryTest.php b/framework/core/tests/Flarum/Core/Settings/DatabaseSettingsRepositoryTest.php index 69b52ce64..74e0b3dbd 100644 --- a/framework/core/tests/Flarum/Core/Settings/DatabaseSettingsRepositoryTest.php +++ b/framework/core/tests/Flarum/Core/Settings/DatabaseSettingsRepositoryTest.php @@ -4,7 +4,7 @@ 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 { diff --git a/framework/core/tests/Flarum/Core/Settings/MemoryCacheSettingsRepositoryTest.php b/framework/core/tests/Flarum/Core/Settings/MemoryCacheSettingsRepositoryTest.php index 99888648e..0cd8ca7d6 100644 --- a/framework/core/tests/Flarum/Core/Settings/MemoryCacheSettingsRepositoryTest.php +++ b/framework/core/tests/Flarum/Core/Settings/MemoryCacheSettingsRepositoryTest.php @@ -4,7 +4,7 @@ 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); } From 47a978711661d2e95decec1deea3919139eed4cd Mon Sep 17 00:00:00 2001 From: Kirk Bushell Date: Tue, 27 Oct 2015 12:54:10 +0000 Subject: [PATCH 3/8] Added output test for flooding exception handler --- .../Flarum/Api/Handler/FloodingExceptionHandlerTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/framework/core/tests/Flarum/Api/Handler/FloodingExceptionHandlerTest.php b/framework/core/tests/Flarum/Api/Handler/FloodingExceptionHandlerTest.php index 9399b1524..504c462b7 100644 --- a/framework/core/tests/Flarum/Api/Handler/FloodingExceptionHandlerTest.php +++ b/framework/core/tests/Flarum/Api/Handler/FloodingExceptionHandlerTest.php @@ -19,4 +19,12 @@ class FloodingExceptionHandlerTest extends TestCase $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()); + } } From 765b894084dc70375e8d8ab2a2a3058179d16dc5 Mon Sep 17 00:00:00 2001 From: Kirk Bushell Date: Tue, 27 Oct 2015 13:22:30 +0000 Subject: [PATCH 4/8] Added more tests --- .../IlluminateValidationExceptionHandler.php | 13 +++++-- .../Handler/FloodingExceptionHandlerTest.php | 2 +- ...luminateValidationExceptionHandlerTest.php | 35 +++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 framework/core/tests/Flarum/Api/Handler/IlluminateValidationExceptionHandlerTest.php diff --git a/framework/core/src/Api/Handler/IlluminateValidationExceptionHandler.php b/framework/core/src/Api/Handler/IlluminateValidationExceptionHandler.php index c25def581..743b4ca78 100644 --- a/framework/core/src/Api/Handler/IlluminateValidationExceptionHandler.php +++ b/framework/core/src/Api/Handler/IlluminateValidationExceptionHandler.php @@ -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; } } diff --git a/framework/core/tests/Flarum/Api/Handler/FloodingExceptionHandlerTest.php b/framework/core/tests/Flarum/Api/Handler/FloodingExceptionHandlerTest.php index 504c462b7..fae69cc00 100644 --- a/framework/core/tests/Flarum/Api/Handler/FloodingExceptionHandlerTest.php +++ b/framework/core/tests/Flarum/Api/Handler/FloodingExceptionHandlerTest.php @@ -1,5 +1,5 @@ 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()); + } +} From b54f56a6831382530f0041540ca18be8de99a825 Mon Sep 17 00:00:00 2001 From: Kirk Bushell Date: Tue, 27 Oct 2015 14:47:03 +0000 Subject: [PATCH 5/8] Added tests for invalid confirmation token handling --- ...luminateValidationExceptionHandlerTest.php | 2 +- ...dConfirmationTokenExceptionHandlerTest.php | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 framework/core/tests/Flarum/Api/Handler/InvalidConfirmationTokenExceptionHandlerTest.php diff --git a/framework/core/tests/Flarum/Api/Handler/IlluminateValidationExceptionHandlerTest.php b/framework/core/tests/Flarum/Api/Handler/IlluminateValidationExceptionHandlerTest.php index 2478f4a7b..dac27528d 100644 --- a/framework/core/tests/Flarum/Api/Handler/IlluminateValidationExceptionHandlerTest.php +++ b/framework/core/tests/Flarum/Api/Handler/IlluminateValidationExceptionHandlerTest.php @@ -25,7 +25,7 @@ class IlluminateValidationExceptionHandlerTest extends TestCase public function test_it_creates_the_desired_output() { $this->markTestIncomplete(); - + $exception = new ValidationException(['field' => ['Some error']]); $response = $this->handler->handle($exception); diff --git a/framework/core/tests/Flarum/Api/Handler/InvalidConfirmationTokenExceptionHandlerTest.php b/framework/core/tests/Flarum/Api/Handler/InvalidConfirmationTokenExceptionHandlerTest.php new file mode 100644 index 000000000..0951581c2 --- /dev/null +++ b/framework/core/tests/Flarum/Api/Handler/InvalidConfirmationTokenExceptionHandlerTest.php @@ -0,0 +1,30 @@ +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()); + } +} From 52b75e4dd71f2247660fad97b4eb886fb1926316 Mon Sep 17 00:00:00 2001 From: Kirk Bushell Date: Wed, 28 Oct 2015 12:39:38 +0000 Subject: [PATCH 6/8] Added model not found error handler tests --- .../ModelNotFoundExceptionHandlerTest.php | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 framework/core/tests/Flarum/Api/Handler/ModelNotFoundExceptionHandlerTest.php diff --git a/framework/core/tests/Flarum/Api/Handler/ModelNotFoundExceptionHandlerTest.php b/framework/core/tests/Flarum/Api/Handler/ModelNotFoundExceptionHandlerTest.php new file mode 100644 index 000000000..7f173a144 --- /dev/null +++ b/framework/core/tests/Flarum/Api/Handler/ModelNotFoundExceptionHandlerTest.php @@ -0,0 +1,30 @@ +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()); + } +} From 74c756a8badf862a45320b8dbec8d5cf01071056 Mon Sep 17 00:00:00 2001 From: Kirk Bushell Date: Wed, 28 Oct 2015 12:41:28 +0000 Subject: [PATCH 7/8] Added tests for permission denied exception handler --- .../PermissionDeniedExceptionHandlerTest.php | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 framework/core/tests/Flarum/Api/Handler/PermissionDeniedExceptionHandlerTest.php diff --git a/framework/core/tests/Flarum/Api/Handler/PermissionDeniedExceptionHandlerTest.php b/framework/core/tests/Flarum/Api/Handler/PermissionDeniedExceptionHandlerTest.php new file mode 100644 index 000000000..0b644a089 --- /dev/null +++ b/framework/core/tests/Flarum/Api/Handler/PermissionDeniedExceptionHandlerTest.php @@ -0,0 +1,30 @@ +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()); + } +} From 8843d8411717065ef40a44edf4ae9e66f5cecc84 Mon Sep 17 00:00:00 2001 From: Kirk Bushell Date: Wed, 28 Oct 2015 12:46:49 +0000 Subject: [PATCH 8/8] Added validation handler tests --- .../ValidationExceptionHandlerTest.php | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 framework/core/tests/Flarum/Api/Handler/ValidationExceptionHandlerTest.php diff --git a/framework/core/tests/Flarum/Api/Handler/ValidationExceptionHandlerTest.php b/framework/core/tests/Flarum/Api/Handler/ValidationExceptionHandlerTest.php new file mode 100644 index 000000000..22d9c78a7 --- /dev/null +++ b/framework/core/tests/Flarum/Api/Handler/ValidationExceptionHandlerTest.php @@ -0,0 +1,30 @@ +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()); + } +}