Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Approve code review #6

Merged
merged 7 commits into from
Jun 7, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/Collection/AllRatesRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,4 @@ public function getBase(): string
{
return $this->base;
}

public function toAssociativeArray(): array
{
return iterator_to_array($this);
}
}
23 changes: 0 additions & 23 deletions src/Collection/SpecificRateRecord.php

This file was deleted.

19 changes: 5 additions & 14 deletions src/Resource/GetAllRate.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ class GetAllRate implements ProviderResource
{
private $apiKey;

public $base = 'BTC';
private $base;

public function __construct(string $apiKey)
public function __construct(string $apiKey, string $base = 'BTC')
{
$this->apiKey = $apiKey;
$this->base = $base;
}

public function getProviderClassName(): string
Expand All @@ -37,18 +38,8 @@ public function fetch(ImportConnector $connector): \Iterator
true
);

$rates = $response['rates'];
$rates = new \ArrayIterator($response['rates']);

$rate = function () use ($rates) {
foreach ($rates as $key => $value) {
yield [
'asset_id_quote' => $rates[$key]['asset_id_quote'],
'rate' => $rates[$key]['rate'],
'time' => $rates[$key]['time'],
];
}
};

return new AllRatesRecord($rate(), $response['asset_id_base'], count($rates), $this);
return new AllRatesRecord($rates, $this->base, count($rates), $this);
}
}
21 changes: 7 additions & 14 deletions src/Resource/GetSpecificRate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,7 +32,7 @@ public function getProviderClassName(): string

public function fetch(ImportConnector $connector): \Iterator
{
$response = \json_decode(
$response[] = \json_decode(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrapping your record in an outer array, which is not correct. This should just be $response instead of $response[].

(string) $connector->fetch(
CryptoMonitor::buildExchangeApiUrl(
sprintf("v1/exchangerate/%s/%s", $this->base, $this->quote)
Expand All @@ -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 yield $response;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return does nothing here, since yield $response has no return value. Should just be yield $response.

}
}
31 changes: 15 additions & 16 deletions tests/Functional/GetRateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,30 @@ final class GetRateTest extends TestCase

public function testGetSpecificRateRecords()
{
/** @var SpecificRateRecord $rates */
$rates = FixtureFactory::createPorter()->import(new ImportSpecification(new GetSpecificRate($this->apiKey)))
->findFirstCollection();
$rate = FixtureFactory::createPorter()->importOne(new ImportSpecification(new GetSpecificRate($this->apiKey)));

$rateRecords = $rates->toAssociativeArray();
$this->assertCount(1, $rate);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary because importOne guarantees the iterator only contains one result. If it contains more it throws an exception.


$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]);
$this->assertArrayHasKey('asset_id_base', $rate[0]);
$this->assertArrayHasKey('asset_id_quote', $rate[0]);
$this->assertArrayHasKey('rate', $rate[0]);
$this->assertArrayHasKey('time', $rate[0]);
}

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());
$this->assertCount(1267, $rates);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This magic number is too brittle. The test needs to prove the count is correct without breaking whenever a rate is added or removed. You have two options, either:

  1. Count the number of records during iteration and ensure it matches the reported count. To do this, the assertion would have to be moved after the loop.
  2. Estimate the number of expected values, e.g. self::assertGreaterThan(1200, count($rates)).


$rateRecords = $rates->toAssociativeArray();
foreach ($rates as $rateRecord) {
$this->assertArrayHasKey('asset_id_quote', $rateRecord);
$this->assertArrayHasKey('rate', $rateRecord);
$this->assertArrayHasKey('time', $rateRecord);
}

$this->assertArrayHasKey('asset_id_quote', $rateRecords[0]);
$this->assertArrayHasKey('rate', $rateRecords[0]);
$this->assertArrayHasKey('time', $rateRecords[0]);
$rates = $rates->findFirstCollection();
$this->assertSame('BTC', $rates->getBase());
}
}