From 087064790c33aa797401158057c5850ef66b1074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Mat=C4=9Bj=C4=8Dek?= Date: Thu, 4 Jan 2024 08:56:55 +0100 Subject: [PATCH] feat(Test): Utils::countTTL --- src/Utils.php | 2 +- tests/src/UtilsTest.php | 62 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 tests/src/UtilsTest.php diff --git a/src/Utils.php b/src/Utils.php index d9bec28..d885c80 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -38,7 +38,7 @@ public static function transformCurrencies(array $currencies): array public static function countTTL(DateTime $dateTime, int $beforeExpiration = 900): int { $time = time(); - if (($dateTime->getTimestamp() - $beforeExpiration) < $time) { + if (($dateTime->getTimestamp() - $beforeExpiration) <= $time) { $dateTime->modify('+1 day'); } 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();