From fc83d5a6d3faa8e0246af5afde95eee6f792116f Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Sun, 17 Mar 2019 18:05:35 +0800 Subject: [PATCH] Add generic and jsonrpc exception handler. #11 Signed-off-by: Mior Muhammad Zaki --- tests/Unit/Server/ExceptionHandlerTest.php | 43 ++++++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/tests/Unit/Server/ExceptionHandlerTest.php b/tests/Unit/Server/ExceptionHandlerTest.php index c9c8595..af41a75 100644 --- a/tests/Unit/Server/ExceptionHandlerTest.php +++ b/tests/Unit/Server/ExceptionHandlerTest.php @@ -3,7 +3,9 @@ namespace Minions\Tests\Unit\Server; use Illuminate\Database\Eloquent\ModelNotFoundException; +use Illuminate\Database\QueryException; use Illuminate\Validation\ValidationException; +use Minions\Exceptions\MissingSignature; use Minions\Server\ExceptionHandler; use Mockery as m; use PHPUnit\Framework\TestCase; @@ -18,6 +20,20 @@ protected function tearDown(): void m::close(); } + /** @test */ + public function it_can_handle_jsonrpc_exception() + { + $exception = new MissingSignature(); + + $handler = new ExceptionHandler(); + + $reply = $handler->handle($exception); + + $this->assertSame( + '{"jsonrpc":"2.0","id":null,"error":{"code":-32651,"message":"Missing Signature."}}', $reply->body() + ); + } + /** @test */ public function it_can_handle_model_not_found_exception() { @@ -28,11 +44,14 @@ public function it_can_handle_model_not_found_exception() $reply = $handler->handle($exception); - $this->assertSame('{"jsonrpc":"2.0","id":null,"error":{"code":-32602,"message":"No query results for model [User] 2","data":[2]}}', $reply->body()); + $this->assertSame( + '{"jsonrpc":"2.0","id":null,"error":{"code":-32602,"message":"No query results for model [User] 2","data":[2]}}', + $reply->body() + ); } /** @test */ - public function it_can_handle_validation_exception_exception() + public function it_can_handle_validation_exception() { $validator = m::mock('Illuminate\Contracts\Validation\Validator'); @@ -45,7 +64,25 @@ public function it_can_handle_validation_exception_exception() $reply = $handler->handle(new ValidationException($validator)); $this->assertSame( - '{"jsonrpc":"2.0","id":null,"error":{"code":-32602,"message":"The given data was invalid.","data":["Password is required"]}}', $reply->body() + '{"jsonrpc":"2.0","id":null,"error":{"code":-32602,"message":"The given data was invalid.","data":["Password is required"]}}', + $reply->body() + ); + } + + /** @test */ + public function it_can_handle_generic_exception() + { + $exception = new QueryException( + 'SELECT * FROM `users` WHERE email=?', ['crynobone@katsana.com'], m::mock('PDOException') + ); + + $handler = new ExceptionHandler(); + + $reply = $handler->handle($exception); + + $this->assertSame( + '{"jsonrpc":"2.0","id":null,"error":{"code":-32603,"message":"Illuminate\\\Database\\\QueryException - (SQL: SELECT * FROM `users` WHERE email=crynobone@katsana.com)"}}', + $reply->body() ); } }