Skip to content

Commit

Permalink
Solve No Data Issue (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrodriguesd authored Oct 16, 2024
1 parent 34ec706 commit acb9eba
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
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}}

0 comments on commit acb9eba

Please sign in to comment.