Skip to content

Commit

Permalink
make api configurable and refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsobries committed Aug 1, 2024
1 parent a8237d6 commit a9d830c
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 17 deletions.
24 changes: 16 additions & 8 deletions src/API/AbstractAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ abstract class AbstractAPI
*/
public $client;

private string $api = 'api';

/**
* Create a new API class instance.
*
Expand All @@ -55,7 +57,7 @@ public function __construct(ArkClient $client)
*/
protected function get(string $path, array $query = [])
{
$response = $this->client->getHttpClient()->get($this->buildUrl('get', $path), [
$response = $this->client->getHttpClient()->get($this->buildUrl($path), [
'query' => Arr::dot($query),
]);

Expand All @@ -73,21 +75,27 @@ protected function get(string $path, array $query = [])
protected function post(string $path, array $parameters = [])
{
$response = $this->client->getHttpClient()->post(
$this->buildUrl('post', $path),
$this->buildUrl($path),
['json' => $parameters]
);

return json_decode($response->getBody()->getContents(), true);
}

private function buildUrl(string $method, string $path): string
protected function withApi(string $api): self
{
$this->api = $api;

return $this;
}

private function buildUrl(string $path): string
{
if ($method === 'post') {
$baseUri = Arr::get($this->client->getHosts(), 'transactions', $this->client->getHosts()['api']);
} else {
$baseUri = $this->client->getHosts()['api'];
}
$baseUri = $this->client->getHosts()[$this->api];

// Reset the API to the default value.
$this->api = 'api';

return Str::finish($baseUri, '/').$path;
}
}
2 changes: 1 addition & 1 deletion src/API/Transactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function all(array $query = []): ?array
*/
public function create(array $transactions): ?array
{
return $this->post('transactions', compact('transactions'));
return $this->withApi('transactions')->post('transactions', compact('transactions'));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/API/CommitsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class CommitsTest extends TestCase
/** @test */
public function show_calls_correct_url()
{
$this->assertResponse('GET', 'commits', function ($client) {
$this->assertResponse('GET', 'commits/1', function ($client) {
return $client->commits()->show(1);
});
}
Expand Down
2 changes: 1 addition & 1 deletion tests/API/RoundsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function all_calls_correct_url()
/** @test */
public function view_calls_correct_url()
{
$this->assertResponse('GET', 'rounds/dummy', function ($client) {
$this->assertResponse('GET', 'rounds/12345', function ($client) {
return $client->rounds()->show(12345);
});
}
Expand Down
11 changes: 8 additions & 3 deletions tests/API/TransactionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ public function all_calls_correct_url()
/** @test */
public function create_calls_correct_url()
{
$this->assertResponse('POST', 'transactions', function ($client) {
return $client->transactions()->create(['transactions' => []]);
});
$this->assertResponse(
method: 'POST',
path: 'transactions',
callback: function ($client) {
return $client->transactions()->create(['transactions' => []]);
},
expectedApi: 'transactions'
);
}

/** @test */
Expand Down
19 changes: 16 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,28 @@ abstract class TestCase extends BaseTestCase
* @param callable $callback
* @param array|null $expectedBody
*/
protected function assertResponse(string $method, string $path, callable $callback, array $expectedBody = []): void
protected function assertResponse(string $method, string $path, callable $callback, array $expectedBody = [], string $expectedApi = 'api'): void
{
$mockHandler = new MockHandler([new Response(200, [], json_encode($expectedBody))]);
$hosts = [
'api' => 'https://dwallets-evm.mainsailhq.com/api',
'transactions' => 'https://dwallets-evm.mainsailhq.com/tx/api',
'evm' => 'https://dwallets-evm.mainsailhq.com/evm',
];

$mockHandler = new MockHandler([
function (Request $request) use ($method, $path, $expectedBody, $hosts, $expectedApi) {
$this->assertSame($method, $request->getMethod());
$this->assertSame($hosts[$expectedApi] . '/' . $path, $request->getUri()->__toString());
return new Response(200, [], json_encode($expectedBody));
}
]);

$client = new ArkClient(
hostOrHosts: 'https://dwallets-evm.mainsailhq.com/api',
hostOrHosts: $hosts,
handler: HandlerStack::create($mockHandler)
);

$this->assertSame($expectedBody, $callback($client));

}
}

0 comments on commit a9d830c

Please sign in to comment.