-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,13 +14,15 @@ class GetSpecificRate implements ProviderResource | |
{ | ||
private $apiKey; | ||
|
||
public $base = 'BTC'; | ||
private $base; | ||
|
||
public $quote = 'USD'; | ||
private $quote; | ||
|
||
public function __construct(string $apiKey) | ||
public function __construct(string $apiKey, string $base = 'BTC', string $quote = 'USD') | ||
{ | ||
$this->apiKey = $apiKey; | ||
$this->base = $base; | ||
$this->quote = $quote; | ||
} | ||
|
||
public function getProviderClassName(): string | ||
|
@@ -30,7 +32,7 @@ public function getProviderClassName(): string | |
|
||
public function fetch(ImportConnector $connector): \Iterator | ||
{ | ||
$response = \json_decode( | ||
$response[] = \json_decode( | ||
(string) $connector->fetch( | ||
CryptoMonitor::buildExchangeApiUrl( | ||
sprintf("v1/exchangerate/%s/%s", $this->base, $this->quote) | ||
|
@@ -39,15 +41,6 @@ public function fetch(ImportConnector $connector): \Iterator | |
true | ||
); | ||
|
||
$rate = function () use ($response) { | ||
yield [ | ||
'asset_id_base' => $response['asset_id_base'], | ||
'asset_id_quote' => $response['asset_id_quote'], | ||
'rate' => $response['rate'], | ||
'time' => $response['time'], | ||
]; | ||
}; | ||
|
||
return new SpecificRateRecord($rate(), count($response), $this); | ||
return new \ArrayIterator($response); | ||
This comment has been minimized.
Sorry, something went wrong.
Bilge
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,31 +17,35 @@ final class GetRateTest extends TestCase | |
|
||
public function testGetSpecificRateRecords() | ||
{ | ||
/** @var SpecificRateRecord $rates */ | ||
$rates = FixtureFactory::createPorter()->import(new ImportSpecification(new GetSpecificRate($this->apiKey))) | ||
->findFirstCollection(); | ||
/** @var \ArrayIterator $rates */ | ||
This comment has been minimized.
Sorry, something went wrong.
Bilge
|
||
$rate = FixtureFactory::createPorter()->import(new ImportSpecification(new GetSpecificRate($this->apiKey))); | ||
This comment has been minimized.
Sorry, something went wrong.
Bilge
|
||
|
||
$rateRecords = $rates->toAssociativeArray(); | ||
$this->assertCount(1, $rate); | ||
|
||
$this->assertCount(1, $rateRecords); | ||
$this->assertArrayHasKey('asset_id_base', $rateRecords[0]); | ||
$this->assertArrayHasKey('asset_id_quote', $rateRecords[0]); | ||
$this->assertArrayHasKey('rate', $rateRecords[0]); | ||
$this->assertArrayHasKey('time', $rateRecords[0]); | ||
$rateRecords = iterator_to_array($rate); | ||
This comment has been minimized.
Sorry, something went wrong.
Bilge
|
||
|
||
foreach($rateRecords as $rateRecord) { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
$this->assertArrayHasKey('asset_id_base', $rateRecord); | ||
$this->assertArrayHasKey('asset_id_quote', $rateRecord); | ||
$this->assertArrayHasKey('rate', $rateRecord); | ||
$this->assertArrayHasKey('time', $rateRecord); | ||
} | ||
} | ||
|
||
public function testGetAllRateRecords() | ||
{ | ||
/** @var AllRatesRecord $rates */ | ||
$rates = FixtureFactory::createPorter()->import(new ImportSpecification(new GetAllRate($this->apiKey))) | ||
->findFirstCollection(); | ||
$rates = FixtureFactory::createPorter()->import(new ImportSpecification(new GetAllRate($this->apiKey))); | ||
|
||
$this->assertSame('BTC', $rates->getBase()); | ||
$rates = $rates->findFirstCollection(); | ||
This comment has been minimized.
Sorry, something went wrong.
Bilge
|
||
$rateRecords = iterator_to_array($rates); | ||
This comment has been minimized.
Sorry, something went wrong.
Bilge
|
||
|
||
$rateRecords = $rates->toAssociativeArray(); | ||
$this->assertSame('BTC', $rates->getBase()); | ||
|
||
$this->assertArrayHasKey('asset_id_quote', $rateRecords[0]); | ||
$this->assertArrayHasKey('rate', $rateRecords[0]); | ||
$this->assertArrayHasKey('time', $rateRecords[0]); | ||
foreach ($rateRecords as $rateRecord) { | ||
$this->assertArrayHasKey('asset_id_quote', $rateRecord); | ||
$this->assertArrayHasKey('rate', $rateRecord); | ||
$this->assertArrayHasKey('time', $rateRecord); | ||
} | ||
} | ||
} |
1 comment
on commit fc13c82
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to understand:
- What an
Iterator
in PHP actually is. It is an object that may yield multiple results and can be iterated withforeach
. - You have two different types of resource: one yields a single record and one yields multiple records. The way you import them is different (
import()
vsimportOne()
) and the way you test them is different. Right now you're getting confused and trying to apply the same changes to both, as if they were the same thing. I suggest you split your tests into separate files because they are not related.
There's no way
count($response)
can be correct. Your tests should catch this error. This has to be the count of the number of records.