Skip to content

Commit

Permalink
refactored test
Browse files Browse the repository at this point in the history
  • Loading branch information
pangz-lab committed Dec 15, 2022
1 parent 326f1cd commit 75ac4e6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 34 deletions.
7 changes: 3 additions & 4 deletions src/client/CoinGeckoApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ public function __call($name, $arguments): CoinGeckoApiClient
$arguments
);
}

$this->endpointKey = strtolower($this->endpoint);

return new CoinGeckoApiClient(
$this->apiClient,
$this->endpoint,
Expand Down Expand Up @@ -149,11 +148,11 @@ private function sendSwitch(

$endpoint = $this->getApiEndpoint();
$url = $baseUrl . $endpoint;
$this->apiClient = $this->apiClient->setUrl($url);
$this->apiClient = $this->apiClient->setUrl($url);

if (!is_null($urlBuilder)) {
$this->apiClient = $this->apiClient->setUrl(
$baseUrl . $endpoint . $urlBuilder->build()
$url . $urlBuilder->build()
);
}

Expand Down
31 changes: 4 additions & 27 deletions tests/client/ApiClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

final class ApiClientTest extends TestCase
{
const API_BASE_URL = "https://api.coingecko.com/api/";

public function testCanGetTheURL(): void
{
$urlBuilder = new CoinGeckoUrlBuilder([], "/coins/verus-coin/market_chart");
Expand Down Expand Up @@ -57,12 +55,7 @@ public function testCanBuildURLWithCustomEndpoint(): void
{
$urlBuilder = new CoinGeckoUrlBuilder([], "/coins/verus-coin/market_chart");
$client = new ApiClient($urlBuilder->build());
$client = $client
->setUrlBuilder(
$urlBuilder
->withDays(7)
->withVsCurrency("jpy")
);
$client = $client->setUrlBuilder($urlBuilder->withDays(7)->withVsCurrency("jpy"));

$this->assertEquals(
$client->getUrl(),
Expand All @@ -71,12 +64,7 @@ public function testCanBuildURLWithCustomEndpoint(): void

$urlBuilder = new CoinGeckoUrlBuilder([["days", "9999"], ["vs_currency", "usd"]], "/coins/verus-coin/market_chart");
$client = new ApiClient($urlBuilder->build());
$client = $client
->setUrlBuilder(
$urlBuilder
->withDays(7)
->withVsCurrency("jpy")
);
$client = $client->setUrlBuilder($urlBuilder->withDays(7)->withVsCurrency("jpy"));

$this->assertEquals(
$client->getUrl(),
Expand Down Expand Up @@ -109,21 +97,10 @@ public function testCanHandlerHttpResponse(): void

$urlBuilder = new CoinGeckoUrlBuilder([], "/coins/verus-coin/market_chart");
$client = new ApiClient($urlBuilder->build(), $httpClient);
$response = $client
->setUrlBuilder(
$urlBuilder
->withDays(1)
->withVsCurrency("jpy")
)
->send();
$response = $client->setUrlBuilder($urlBuilder->withDays(1)->withVsCurrency("jpy"))->send();
$this->assertEquals($response, $responsePayload);

$response = $client
->setUrlBuilder(
$urlBuilder
->withDays(1)
->withVsCurrency("jpy")
)->send();
$response = $client->setUrlBuilder($urlBuilder->withDays(1)->withVsCurrency("jpy"))->send();
$this->assertEquals($response, []);
}

Expand Down
6 changes: 3 additions & 3 deletions tests/client/CoinGeckoApiClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function testCanBuildNewEndpointWhenSetIsCalledAfterMultipleSend(): void
$apiClient->send();
$this->assertEquals($apiClient->getEndpointKey(), '/coins/markets');
}

public function testCanBuildTheEndpointWithParameters(): void
{
$apiClient = new CoinGeckoApiClient();
Expand All @@ -203,7 +203,7 @@ public function testCanBuildTheEndpointWithParameters(): void
$apiClient = $apiClient->indexes("PARAM1", "PARAM2")->tickers();
$this->assertEquals($apiClient->getEndpointKey(), '/indexes/%s/%s/tickers');
}

public function testCanBuildTheEndpointWithUriVariation(): void
{
$apiClient = new CoinGeckoApiClient();
Expand Down Expand Up @@ -258,7 +258,7 @@ public function testCanBuildTheEndpointWithUriVariation(): void
$apiClient = $apiClient->simple()->supported_vs_currencies();
$this->assertEquals($apiClient->getEndpointKey(), '/simple/supportedvscurrencies');
}

public function testCanBuildTheEndpointWithUriVariationAndParameters(): void
{
$apiClient = new CoinGeckoApiClient();
Expand Down

0 comments on commit 75ac4e6

Please sign in to comment.