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

Solve No Data Issue #56

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/ResultDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,13 @@ public function transformHistoricalDataResult(string $responseBody): array
}

$result = $decoded['chart']['result'][0];
$entryCount = \count($result['timestamp']);

if (0 === \count($result['indicators']['quote'][0])) {
return [];
}

$entryCount = \count($result['indicators']['quote'][0]['open']);

$returnArray = [];
for ($i = 0; $i < $entryCount; ++$i) {
$returnArray[] = $this->createHistoricalData($result, $i);
Expand Down Expand Up @@ -249,6 +255,10 @@ public function transformDividendDataResult(string $responseBody): array
throw new ApiException('Response is not a valid JSON', ApiException::INVALID_RESPONSE);
}

if (!isset($decoded['chart']['result'][0]['events']['dividends'])) {
return [];
}

return array_map(function (array $item) {
return $this->createDividendData($item);
}, $decoded['chart']['result'][0]['events']['dividends']);
Expand All @@ -275,6 +285,10 @@ public function transformSplitDataResult(string $responseBody): array
throw new ApiException('Response is not a valid JSON', ApiException::INVALID_RESPONSE);
}

if (!isset($decoded['chart']['result'][0]['events']['splits'])) {
return [];
}

return array_map(function (array $item) {
return $this->createSplitData($item);
}, $decoded['chart']['result'][0]['events']['splits']);
Expand Down
33 changes: 33 additions & 0 deletions tests/ResultDecoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ public function transformHistoricalDataResult_csvGiven_returnArrayOfHistoricalDa
$this->assertEquals($expectedExchangeRate, $returnedResult[0]);
}

/**
* @test
*/
public function transformHistoricalDataResult_noData(): void
{
$returnedResult = $this->resultDecoder->transformHistoricalDataResult(file_get_contents(__DIR__.'/fixtures/noData.json'));

$this->assertIsArray($returnedResult);
$this->assertCount(0, $returnedResult);
}

/**
* @test
*/
Expand Down Expand Up @@ -186,6 +197,17 @@ public function transformDividendDataResult_csvGiven_returnArrayOfDividendData()
$this->assertEquals($expectedExchangeRate, $firstResult);
}

/**
* @test
*/
public function transformDividendDataResult_noData(): void
{
$returnedResult = $this->resultDecoder->transformDividendDataResult(file_get_contents(__DIR__.'/fixtures/noData.json'));

$this->assertIsArray($returnedResult);
$this->assertCount(0, $returnedResult);
}

/**
* @test
*/
Expand Down Expand Up @@ -249,6 +271,17 @@ public function transformSplitDataResult_csvGiven_returnArrayOfSplitData(): void
$this->assertEquals($expectedExchangeRate, $firstResult);
}

/**
* @test
*/
public function transformSplitDataResult_noData(): void
{
$returnedResult = $this->resultDecoder->transformSplitDataResult(file_get_contents(__DIR__.'/fixtures/noData.json'));

$this->assertIsArray($returnedResult);
$this->assertCount(0, $returnedResult);
}

/**
* @test
*/
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/noData.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"chart":{"result":[{"meta":{"currency":null,"symbol":"CAPIO.ST","exchangeName":"YHD","fullExchangeName":"YHD","instrumentType":"MUTUALFUND","firstTradeDate":null,"regularMarketTime":1561759658,"hasPrePostMarketData":false,"gmtoffset":-14400,"timezone":"EDT","exchangeTimezoneName":"America/New_York","longName":"Capio AB (publ)","priceHint":2,"currentTradingPeriod":{"pre":{"timezone":"EDT","start":1729065600,"end":1729085400,"gmtoffset":-14400},"regular":{"timezone":"EDT","start":1729085400,"end":1729108800,"gmtoffset":-14400},"post":{"timezone":"EDT","start":1729108800,"end":1729123200,"gmtoffset":-14400}},"dataGranularity":"1d","range":"","validRanges":["1mo","3mo","6mo","ytd","1y","2y","5y","10y","max"]},"indicators":{"quote":[{}],"adjclose":[{}]}}],"error":null}}
Loading