Skip to content

Commit

Permalink
Add generic and jsonrpc exception handler. #11
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Mar 17, 2019
1 parent c2be1c0 commit fc83d5a
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions tests/Unit/Server/ExceptionHandlerTest.php
Expand Up @@ -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;
Expand All @@ -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()
{
Expand All @@ -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');

Expand All @@ -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()
);
}
}

0 comments on commit fc83d5a

Please sign in to comment.