From d743e56bc121149556a733983cd9a8d73de71a29 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sat, 5 Dec 2015 22:31:33 +1030 Subject: [PATCH] Fix tests and CS --- phpunit.yml | 0 .../Handler/ModelNotFoundExceptionHandler.php | 2 +- .../Middleware/AuthenticateWithSession.php | 2 +- .../RequireAdministrateAbilityTest.php | 28 ------------------- .../Handler/FloodingExceptionHandlerTest.php | 19 +++++++++++-- ...luminateValidationExceptionHandlerTest.php | 12 +++++++- ...dConfirmationTokenExceptionHandlerTest.php | 19 +++++++++++-- .../ModelNotFoundExceptionHandlerTest.php | 19 +++++++++++-- .../PermissionDeniedExceptionHandlerTest.php | 19 +++++++++++-- .../ValidationExceptionHandlerTest.php | 21 ++++++++++++-- 10 files changed, 100 insertions(+), 41 deletions(-) delete mode 100644 phpunit.yml delete mode 100644 tests/Flarum/Admin/Middleware/RequireAdministrateAbilityTest.php diff --git a/phpunit.yml b/phpunit.yml deleted file mode 100644 index e69de29bb..000000000 diff --git a/src/Api/Handler/ModelNotFoundExceptionHandler.php b/src/Api/Handler/ModelNotFoundExceptionHandler.php index ea80d07a7..941338631 100644 --- a/src/Api/Handler/ModelNotFoundExceptionHandler.php +++ b/src/Api/Handler/ModelNotFoundExceptionHandler.php @@ -33,7 +33,7 @@ class ModelNotFoundExceptionHandler implements ExceptionHandlerInterface { $status = 404; $error = [ - 'status' => '404', + 'status' => (string) $status, 'code' => 'resource_not_found' ]; diff --git a/src/Http/Middleware/AuthenticateWithSession.php b/src/Http/Middleware/AuthenticateWithSession.php index 8331d3156..093dec623 100644 --- a/src/Http/Middleware/AuthenticateWithSession.php +++ b/src/Http/Middleware/AuthenticateWithSession.php @@ -30,7 +30,7 @@ class AuthenticateWithSession implements MiddlewareInterface $request = $request->withAttribute('actor', $actor); - return $out ? $out($request, $response) : $response;; + return $out ? $out($request, $response) : $response; } private function getActor(SessionInterface $session) diff --git a/tests/Flarum/Admin/Middleware/RequireAdministrateAbilityTest.php b/tests/Flarum/Admin/Middleware/RequireAdministrateAbilityTest.php deleted file mode 100644 index 6dfaf84aa..000000000 --- a/tests/Flarum/Admin/Middleware/RequireAdministrateAbilityTest.php +++ /dev/null @@ -1,28 +0,0 @@ -setExpectedException(PermissionDeniedException::class); - - $gate = m::mock(Gate::class); - $request = m::mock(ServerRequestInterface::class)->shouldIgnoreMissing(); - $response = m::mock(ResponseInterface::class); - - $gate->shouldReceive('forUser->allows')->andReturn(false); - - $middleware = new RequireAdministrateAbility($gate); - $middleware->__invoke($request, $response); - } -} diff --git a/tests/Flarum/Api/Handler/FloodingExceptionHandlerTest.php b/tests/Flarum/Api/Handler/FloodingExceptionHandlerTest.php index fae69cc00..8f13415cd 100644 --- a/tests/Flarum/Api/Handler/FloodingExceptionHandlerTest.php +++ b/tests/Flarum/Api/Handler/FloodingExceptionHandlerTest.php @@ -1,6 +1,16 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Tests\Flarum\Api\Handler; +use Exception; use Flarum\Api\Handler\FloodingExceptionHandler; use Flarum\Core\Exception\FloodingException; use Tests\Test\TestCase; @@ -16,7 +26,7 @@ class FloodingExceptionHandlerTest extends TestCase public function test_it_handles_recognisable_exceptions() { - $this->assertFalse($this->handler->manages(new \Exception)); + $this->assertFalse($this->handler->manages(new Exception)); $this->assertTrue($this->handler->manages(new FloodingException)); } @@ -25,6 +35,11 @@ class FloodingExceptionHandlerTest extends TestCase $result = $this->handler->handle(new FloodingException); $this->assertEquals(429, $result->getStatus()); - $this->assertEquals([[]], $result->getErrors()); + $this->assertEquals([ + [ + 'status' => '429', + 'code' => 'too_many_requests' + ] + ], $result->getErrors()); } } diff --git a/tests/Flarum/Api/Handler/IlluminateValidationExceptionHandlerTest.php b/tests/Flarum/Api/Handler/IlluminateValidationExceptionHandlerTest.php index dac27528d..e33c5bdd9 100644 --- a/tests/Flarum/Api/Handler/IlluminateValidationExceptionHandlerTest.php +++ b/tests/Flarum/Api/Handler/IlluminateValidationExceptionHandlerTest.php @@ -1,6 +1,16 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Tests\Flarum\Api\Handler; +use Exception; use Flarum\Api\Handler\ValidationExceptionHandler; use Flarum\Core\Exception\ValidationException; use Tests\Test\TestCase; @@ -18,7 +28,7 @@ class IlluminateValidationExceptionHandlerTest extends TestCase { $validException = new ValidationException(['messages']); - $this->assertFalse($this->handler->manages(new \Exception)); + $this->assertFalse($this->handler->manages(new Exception)); $this->assertTrue($this->handler->manages($validException)); } diff --git a/tests/Flarum/Api/Handler/InvalidConfirmationTokenExceptionHandlerTest.php b/tests/Flarum/Api/Handler/InvalidConfirmationTokenExceptionHandlerTest.php index 0951581c2..ef3904c76 100644 --- a/tests/Flarum/Api/Handler/InvalidConfirmationTokenExceptionHandlerTest.php +++ b/tests/Flarum/Api/Handler/InvalidConfirmationTokenExceptionHandlerTest.php @@ -1,6 +1,16 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Tests\Flarum\Api\Handler; +use Exception; use Flarum\Api\Handler\InvalidConfirmationTokenExceptionHandler; use Flarum\Core\Exception\InvalidConfirmationTokenException; use Tests\Test\TestCase; @@ -16,7 +26,7 @@ class InvalidConfirmationTokenExceptionHandlerTest extends TestCase public function test_it_handles_recognisable_exceptions() { - $this->assertFalse($this->handler->manages(new \Exception)); + $this->assertFalse($this->handler->manages(new Exception)); $this->assertTrue($this->handler->manages(new InvalidConfirmationTokenException)); } @@ -25,6 +35,11 @@ class InvalidConfirmationTokenExceptionHandlerTest extends TestCase $response = $this->handler->handle(new InvalidConfirmationTokenException); $this->assertEquals(403, $response->getStatus()); - $this->assertEquals([['code' => 'invalid_confirmation_token']], $response->getErrors()); + $this->assertEquals([ + [ + 'status' => '403', + 'code' => 'invalid_confirmation_token' + ] + ], $response->getErrors()); } } diff --git a/tests/Flarum/Api/Handler/ModelNotFoundExceptionHandlerTest.php b/tests/Flarum/Api/Handler/ModelNotFoundExceptionHandlerTest.php index 7f173a144..9c3451af6 100644 --- a/tests/Flarum/Api/Handler/ModelNotFoundExceptionHandlerTest.php +++ b/tests/Flarum/Api/Handler/ModelNotFoundExceptionHandlerTest.php @@ -1,6 +1,16 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Tests\Flarum\Api\Handler; +use Exception; use Flarum\Api\Handler\ModelNotFoundExceptionHandler; use Illuminate\Database\Eloquent\ModelNotFoundException; use Tests\Test\TestCase; @@ -16,7 +26,7 @@ class ModelNotFoundExceptionHandlerTest extends TestCase public function test_it_handles_recognisable_exceptions() { - $this->assertFalse($this->handler->manages(new \Exception)); + $this->assertFalse($this->handler->manages(new Exception)); $this->assertTrue($this->handler->manages(new ModelNotFoundException)); } @@ -25,6 +35,11 @@ class ModelNotFoundExceptionHandlerTest extends TestCase $response = $this->handler->handle(new ModelNotFoundException); $this->assertEquals(404, $response->getStatus()); - $this->assertEquals([[]], $response->getErrors()); + $this->assertEquals([ + [ + 'status' => '404', + 'code' => 'resource_not_found' + ] + ], $response->getErrors()); } } diff --git a/tests/Flarum/Api/Handler/PermissionDeniedExceptionHandlerTest.php b/tests/Flarum/Api/Handler/PermissionDeniedExceptionHandlerTest.php index 0b644a089..b5b6da32e 100644 --- a/tests/Flarum/Api/Handler/PermissionDeniedExceptionHandlerTest.php +++ b/tests/Flarum/Api/Handler/PermissionDeniedExceptionHandlerTest.php @@ -1,6 +1,16 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Tests\Flarum\Api\Handler; +use Exception; use Flarum\Api\Handler\PermissionDeniedExceptionHandler; use Flarum\Core\Exception\PermissionDeniedException; use Tests\Test\TestCase; @@ -16,7 +26,7 @@ class PermissionDeniedExceptionHandlerTest extends TestCase public function test_it_handles_recognisable_exceptions() { - $this->assertFalse($this->handler->manages(new \Exception)); + $this->assertFalse($this->handler->manages(new Exception)); $this->assertTrue($this->handler->manages(new PermissionDeniedException)); } @@ -25,6 +35,11 @@ class PermissionDeniedExceptionHandlerTest extends TestCase $response = $this->handler->handle(new PermissionDeniedException); $this->assertEquals(401, $response->getStatus()); - $this->assertEquals([[]], $response->getErrors()); + $this->assertEquals([ + [ + 'status' => '401', + 'code' => 'permission_denied' + ] + ], $response->getErrors()); } } diff --git a/tests/Flarum/Api/Handler/ValidationExceptionHandlerTest.php b/tests/Flarum/Api/Handler/ValidationExceptionHandlerTest.php index 22d9c78a7..7fff7d56f 100644 --- a/tests/Flarum/Api/Handler/ValidationExceptionHandlerTest.php +++ b/tests/Flarum/Api/Handler/ValidationExceptionHandlerTest.php @@ -1,6 +1,16 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Tests\Flarum\Api\Handler; +use Exception; use Flarum\Api\Handler\ValidationExceptionHandler; use Flarum\Core\Exception\ValidationException; use Tests\Test\TestCase; @@ -16,7 +26,7 @@ class ValidationExceptionHandlerTest extends TestCase public function test_it_handles_recognisable_exceptions() { - $this->assertFalse($this->handler->manages(new \Exception)); + $this->assertFalse($this->handler->manages(new Exception)); $this->assertTrue($this->handler->manages(new ValidationException([]))); } @@ -25,6 +35,13 @@ class ValidationExceptionHandlerTest extends TestCase $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()); + $this->assertEquals([ + [ + 'status' => '422', + 'code' => 'validation_error', + 'detail' => 'There was an error', + 'source' => ['pointer' => '/data/attributes/0'] + ] + ], $response->getErrors()); } }