Skip to content

Commit

Permalink
#17 - fix getting rates for day before if there are gaps in table (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciej-sz authored Aug 6, 2024
1 parent 65bde11 commit 707f3de
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Service/CurrencyAverageRatesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
54 changes: 54 additions & 0 deletions tests/unit/Service/CurrencyAverageRatesServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}

0 comments on commit 707f3de

Please sign in to comment.