diff --git a/composer.json b/composer.json index 7032f73..bc12db6 100644 --- a/composer.json +++ b/composer.json @@ -22,10 +22,12 @@ ], "require": { "php": "^7.4 || ~8.0", + "guzzlehttp/guzzle": "^7.3", "illuminate/http": "~8.0" }, "require-dev": { "mockery/mockery": "^1.4", + "orchestra/testbench": "^6.0", "phpunit/phpunit": "^9.0", "squizlabs/php_codesniffer": "^3.5", "tightenco/tlint": "^5.0" diff --git a/src/Exceptions/AccountNotFoundException.php b/src/Exceptions/AccountNotFoundException.php new file mode 100644 index 0000000..4bc58e8 --- /dev/null +++ b/src/Exceptions/AccountNotFoundException.php @@ -0,0 +1,10 @@ +client->call('getAccountInfo', [$pubKey, ["encoding" => "jsonParsed"]])->json()['result']['value']; + $accountResponse = $this->client->call('getAccountInfo', [$pubKey, ["encoding" => "jsonParsed"]])->json()['result']['value']; + + if (! $accountResponse) { + throw new AccountNotFoundException("API Error: Account {$pubKey} not found."); + } + + return $accountResponse; } public function getBalance(string $pubKey): float diff --git a/src/SolanaRpcClient.php b/src/SolanaRpcClient.php index 14da6ae..6627595 100644 --- a/src/SolanaRpcClient.php +++ b/src/SolanaRpcClient.php @@ -67,4 +67,12 @@ protected function validateResponse(Response $response, string $method, array $p throw new GenericException('API Error: status code ' . $response->getStatusCode()); } } + + /** + * @return int + */ + public function getRandomKey(): int + { + return $this->randomKey; + } } diff --git a/tests/SolanaTest.php b/tests/SolanaTest.php index e4d7d2b..0cca4d2 100644 --- a/tests/SolanaTest.php +++ b/tests/SolanaTest.php @@ -3,8 +3,9 @@ namespace Tighten\SolanaPhpSdk\Tests; use Illuminate\Http\Client\Response; +use Illuminate\Support\Facades\Http; use Mockery as M; -use PHPUnit\Framework\TestCase; +use Tighten\SolanaPhpSdk\Exceptions\AccountNotFoundException; use Tighten\SolanaPhpSdk\Solana; use Tighten\SolanaPhpSdk\SolanaRpcClient; @@ -27,6 +28,29 @@ public function it_passes_undefined_calls_through_magically() $this->assertTrue(true); // Keep PHPUnit from squawking; there must be a better way? } + /** @test */ + public function it_will_throw_exception_when_rpc_account_response_is_null() + { + $client = new SolanaRpcClient(SolanaRpcClient::DEVNET_ENDPOINT); + $expectedIdInHttpResponse = $client->getRandomKey(); + $solana = new Solana($client); + Http::fake([ + SolanaRpcClient::DEVNET_ENDPOINT => Http::response([ + 'jsonrpc' => '2.0', + 'result' => [ + 'context' => [ + 'slot' => 6440 + ], + 'value' => null, // no account data. + ], + 'id' => $expectedIdInHttpResponse, + ]), + ]); + + $this->expectException(AccountNotFoundException::class); + $solana->getAccountInfo('abc123'); + } + protected function fakeResponse(): Response { return new Response(new class diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..9cb7176 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,10 @@ +