diff --git a/src/Utils.php b/src/Utils.php index 8abbcc7..388cd03 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -37,7 +37,8 @@ public static function transformCurrencies(array $currencies): array */ public static function countTTL(DateTime $dateTime, int $beforeExpiration = 900): int { - if (($dateTime->getTimestamp() - $beforeExpiration) < time()) { + $time = time(); + if (($dateTime->getTimestamp() - $beforeExpiration) <= $time) { $dateTime->modify('+1 day'); } diff --git a/tests/src/Driver/Cnb/DayTest.php b/tests/src/Driver/Cnb/DayTest.php index 3aeb09e..19f7a9f 100644 --- a/tests/src/Driver/Cnb/DayTest.php +++ b/tests/src/Driver/Cnb/DayTest.php @@ -2,12 +2,9 @@ namespace h4kuna\Exchange\Tests\Driver\Cnb; -use GuzzleHttp\Client; -use GuzzleHttp\Psr7\HttpFactory; use h4kuna\Exchange\Driver\Cnb\Day; use h4kuna\Exchange\Driver\Cnb\Property; use h4kuna\Exchange\Fixtures\SourceListBuilder; -use h4kuna\Exchange\Utils; use Tester\Assert; use Tester\TestCase; @@ -31,23 +28,6 @@ public function testDownloadHistory(): void Assert::equal($expected, $list); } - - public function testRefresh(): void - { - $client = new HttpFactory(); - $day = new Day(new Client(), $client); - - Assert::same((new \DateTime('now', new \DateTimeZone('Europe/Prague')))->format('Y-m-d'), $day->getRefresh()->format('Y-m-d')); - - $prevTtl = 900; - $refresh = new \DateTime('today 15:00:00', new \DateTimeZone('Europe/Prague')); - if ($refresh->getTimestamp() < (time() - $prevTtl)) { - $refresh->modify('+1 day'); - } - - Assert::same($refresh->getTimestamp() - time(), Utils::countTTL($day->getRefresh(), $prevTtl)); - } - } (new DayTest())->run(); diff --git a/tests/src/UtilsTest.php b/tests/src/UtilsTest.php new file mode 100644 index 0000000..71d1fe5 --- /dev/null +++ b/tests/src/UtilsTest.php @@ -0,0 +1,62 @@ + + */ + public function data(): array + { + return [ + [ + function (self $self) { + $self->assert( + 901, + new DateTime('+901 seconds'), + ); + }, + ], + [ + function (self $self) { + $self->assert( + 87300, + new DateTime('+900 seconds'), + ); + }, + ], + ]; + } + + + /** + * @param Closure(static):void $assert + * @dataProvider data + */ + public function testCountTTL(Closure $assert): void + { + $assert($this); + } + + + public function assert( + int $expectedTime, + DateTime $from + ): void + { + Assert::same($expectedTime, Utils::countTTL($from)); + } +} + +(new UtilsTest())->run();