Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
remove the random key override for unit tests. Using a getter to fetc…
Browse files Browse the repository at this point in the history
…h the random number from unit test. That is much better :D
  • Loading branch information
exzachlyvv committed Oct 9, 2021
1 parent 84946b6 commit 1ffb728
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
12 changes: 9 additions & 3 deletions src/SolanaRpcClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ class SolanaRpcClient
public const TESTNET_ENDPOINT = 'https://api.testnet.solana.com';
public const MAINNET_ENDPOINT = 'https://api.mainnet-beta.solana.com';

public static $randomKeyOverrideForUnitTetsing = null;

protected $endpoint;
protected $randomKey;

public function __construct(string $endpoint)
{
$this->endpoint = $endpoint;
$this->randomKey = static::$randomKeyOverrideForUnitTetsing ?? random_int(5, 25000);
$this->randomKey = random_int(5, 25000);
}

public function call(string $method, array $params = []): Response
Expand Down Expand Up @@ -68,4 +66,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;
}
}
8 changes: 4 additions & 4 deletions tests/SolanaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public function it_passes_undefined_calls_through_magically()
/** @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',
Expand All @@ -40,13 +43,10 @@ public function it_will_throw_exception_when_rpc_account_response_is_null()
],
'value' => null, // no account data.
],
'id' => 4051,
'id' => $expectedIdInHttpResponse,
]),
]);

SolanaRpcClient::$randomKeyOverrideForUnitTetsing = 4051;
$solana = new Solana(new SolanaRpcClient(SolanaRpcClient::DEVNET_ENDPOINT));

$this->expectException(AccountNotFoundException::class);
$solana->getAccountInfo('abc123');
}
Expand Down

0 comments on commit 1ffb728

Please sign in to comment.