Skip to content

Commit

Permalink
feat(Test): Utils::countTTL
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Jan 4, 2024
1 parent 5a97531 commit 0870647
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
62 changes: 62 additions & 0 deletions tests/src/UtilsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace h4kuna\Exchange\Tests;

use Closure;
use DateTime;
use h4kuna\Exchange\Utils;
use Tester\Assert;
use Tester\TestCase;

require __DIR__ . '/../bootstrap.php';

final class UtilsTest extends TestCase
{
/**
* @return array<string|int, array{0: Closure(static):void}>
*/
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();

0 comments on commit 0870647

Please sign in to comment.