From acb9ebacc057a13d69ca2b02fc88096369a5848a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rodr=C3=ADguez?= <100245095+jrodriguesd@users.noreply.github.com> Date: Wed, 16 Oct 2024 18:35:40 +0100 Subject: [PATCH] Solve No Data Issue (#56) --- src/ResultDecoder.php | 16 +++++++++++++++- tests/ResultDecoderTest.php | 33 +++++++++++++++++++++++++++++++++ tests/fixtures/noData.json | 1 + 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/noData.json diff --git a/src/ResultDecoder.php b/src/ResultDecoder.php index 9a5a4bc..35f9bb0 100644 --- a/src/ResultDecoder.php +++ b/src/ResultDecoder.php @@ -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); @@ -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']); @@ -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']); diff --git a/tests/ResultDecoderTest.php b/tests/ResultDecoderTest.php index 0fcedce..4b106c6 100644 --- a/tests/ResultDecoderTest.php +++ b/tests/ResultDecoderTest.php @@ -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 */ @@ -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 */ @@ -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 */ diff --git a/tests/fixtures/noData.json b/tests/fixtures/noData.json new file mode 100644 index 0000000..0c0bd85 --- /dev/null +++ b/tests/fixtures/noData.json @@ -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}}