diff --git a/composer.json b/composer.json index c81c3f702..10527f74e 100644 --- a/composer.json +++ b/composer.json @@ -67,7 +67,7 @@ }, "autoload-dev": { "psr-4": { - "tests\\": "tests/" + "Tests\\": "tests/" } }, "scripts": { diff --git a/phpunit.yml b/phpunit.yml new file mode 100644 index 000000000..e69de29bb diff --git a/src/Api/Handler/IlluminateValidationExceptionHandler.php b/src/Api/Handler/IlluminateValidationExceptionHandler.php index c25def581..743b4ca78 100644 --- a/src/Api/Handler/IlluminateValidationExceptionHandler.php +++ b/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/tests/Flarum/Admin/Middleware/RequireAdministrateAbilityTest.php b/tests/Flarum/Admin/Middleware/RequireAdministrateAbilityTest.php index 607000928..6dfaf84aa 100644 --- a/tests/Flarum/Admin/Middleware/RequireAdministrateAbilityTest.php +++ b/tests/Flarum/Admin/Middleware/RequireAdministrateAbilityTest.php @@ -1,15 +1,14 @@ 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()); + } +} diff --git a/tests/Flarum/Api/Handler/IlluminateValidationExceptionHandlerTest.php b/tests/Flarum/Api/Handler/IlluminateValidationExceptionHandlerTest.php new file mode 100644 index 000000000..dac27528d --- /dev/null +++ b/tests/Flarum/Api/Handler/IlluminateValidationExceptionHandlerTest.php @@ -0,0 +1,35 @@ +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()); + } +} diff --git a/tests/Flarum/Api/Handler/InvalidConfirmationTokenExceptionHandlerTest.php b/tests/Flarum/Api/Handler/InvalidConfirmationTokenExceptionHandlerTest.php new file mode 100644 index 000000000..0951581c2 --- /dev/null +++ b/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()); + } +} diff --git a/tests/Flarum/Api/Handler/ModelNotFoundExceptionHandlerTest.php b/tests/Flarum/Api/Handler/ModelNotFoundExceptionHandlerTest.php new file mode 100644 index 000000000..7f173a144 --- /dev/null +++ b/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()); + } +} diff --git a/tests/Flarum/Api/Handler/PermissionDeniedExceptionHandlerTest.php b/tests/Flarum/Api/Handler/PermissionDeniedExceptionHandlerTest.php new file mode 100644 index 000000000..0b644a089 --- /dev/null +++ b/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()); + } +} diff --git a/tests/Flarum/Api/Handler/ValidationExceptionHandlerTest.php b/tests/Flarum/Api/Handler/ValidationExceptionHandlerTest.php new file mode 100644 index 000000000..22d9c78a7 --- /dev/null +++ b/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()); + } +} diff --git a/tests/Flarum/Core/Settings/DatabaseSettingsRepositoryTest.php b/tests/Flarum/Core/Settings/DatabaseSettingsRepositoryTest.php index 3e7929dc1..74e0b3dbd 100644 --- a/tests/Flarum/Core/Settings/DatabaseSettingsRepositoryTest.php +++ b/tests/Flarum/Core/Settings/DatabaseSettingsRepositoryTest.php @@ -1,10 +1,10 @@ baseRepository = m::mock(\Flarum\Settings\SettingsRepositoryInterface::class); + $this->baseRepository = m::mock(SettingsRepositoryInterface::class); $this->repository = new MemoryCacheSettingsRepository($this->baseRepository); } diff --git a/tests/Test/TestCase.php b/tests/Test/TestCase.php index d91ad6f63..17bbc4a57 100644 --- a/tests/Test/TestCase.php +++ b/tests/Test/TestCase.php @@ -1,5 +1,5 @@