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 6, 2024
1 parent 35413a1 commit dea50c2
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
20 changes: 0 additions & 20 deletions tests/src/Driver/Cnb/DayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
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 dea50c2

Please sign in to comment.