From 707f3de0ce8a171ce8169350751af760fe927d92 Mon Sep 17 00:00:00 2001 From: maciej-sz Date: Wed, 7 Aug 2024 00:19:21 +0200 Subject: [PATCH] #17 - fix getting rates for day before if there are gaps in table (#19) --- src/Service/CurrencyAverageRatesService.php | 4 +- .../CurrencyAverageRatesServiceTest.php | 54 +++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/Service/CurrencyAverageRatesService.php b/src/Service/CurrencyAverageRatesService.php index 805fcff..4f2d5aa 100644 --- a/src/Service/CurrencyAverageRatesService.php +++ b/src/Service/CurrencyAverageRatesService.php @@ -11,6 +11,7 @@ use MaciejSz\Nbp\Shared\Infrastructure\Repository\NbpRepository; use MaciejSz\Nbp\Shared\Infrastructure\Repository\NbpWebRepository; +use function MaciejSz\Nbp\Shared\Domain\DateFormatter\compare_days; use function MaciejSz\Nbp\Shared\Domain\DateFormatter\ensure_date_obj; use function MaciejSz\Nbp\Shared\Domain\DateFormatter\ensure_mutable_date_obj; use function MaciejSz\Nbp\Shared\Domain\DateFormatter\extract_ym; @@ -142,11 +143,10 @@ private function findTableFromDateBefore($date, iterable $tables): ?CurrencyAver $prevTable = null; foreach ($tables as $table) { $nextTableDay = ensure_mutable_date_obj($table->getEffectiveDate())->modify('+1 day'); - if (is_same_day($date, $nextTableDay)) { return $table; } - if (is_same_day($date, $table->getEffectiveDate())) { + if (compare_days($table->getEffectiveDate(), $date) >= 0) { return $prevTable; } $prevTable = $table; diff --git a/tests/unit/Service/CurrencyAverageRatesServiceTest.php b/tests/unit/Service/CurrencyAverageRatesServiceTest.php index 412b0f9..3978d47 100644 --- a/tests/unit/Service/CurrencyAverageRatesServiceTest.php +++ b/tests/unit/Service/CurrencyAverageRatesServiceTest.php @@ -331,4 +331,58 @@ public function testFromDayBeforeInvalidTableB(): void $service->fromDayBefore('2023-04-04')->fromTable('B'); } + + public function testFromDayBeforeWithDayGaps(): void + { + $rateA1 = $this->createStub(CurrencyAverageRate::class); + + $tableA1 = $this->createStub(CurrencyAveragesTable::class); + $tableA1->method('getRates')->willReturn([$rateA1]); + $tableA1->method('getEffectiveDate')->willReturn(new \DateTimeImmutable('2024-07-26')); + + $tableA2 = $this->createStub(CurrencyAveragesTable::class); + $tableA2->method('getEffectiveDate')->willReturn(new \DateTimeImmutable('2024-07-28')); + + $repository = $this->createStub(NbpRepository::class); + + $repository + ->method('getCurrencyAveragesTableA') + ->willReturnMap([ + [2024, 7, [$tableA1, $tableA2]], + ]) + ; + + $service = new CurrencyAverageRatesService($repository); + self::assertSame( + [$rateA1], + $service->fromDayBefore('2024-07-28')->fromTable('A')->getRates() + ); + } + + public function testFromDayBeforeWithDayGapsOnBothSides(): void + { + $rateA1 = $this->createStub(CurrencyAverageRate::class); + + $tableA1 = $this->createStub(CurrencyAveragesTable::class); + $tableA1->method('getRates')->willReturn([$rateA1]); + $tableA1->method('getEffectiveDate')->willReturn(new \DateTimeImmutable('2024-07-26')); + + $tableA2 = $this->createStub(CurrencyAveragesTable::class); + $tableA2->method('getEffectiveDate')->willReturn(new \DateTimeImmutable('2024-07-30')); + + $repository = $this->createStub(NbpRepository::class); + + $repository + ->method('getCurrencyAveragesTableA') + ->willReturnMap([ + [2024, 7, [$tableA1, $tableA2]], + ]) + ; + + $service = new CurrencyAverageRatesService($repository); + self::assertSame( + [$rateA1], + $service->fromDayBefore('2024-07-28')->fromTable('A')->getRates() + ); + } }