Refactor RollingDateConverter to use ClockInterface

Introduced ClockInterface for better time mocking and handling, replacing default instantiation with dependency injection. Adjusted RollingDate logic to accept nullable pivot dates and updated related tests for improved flexibility and accuracy.
This commit is contained in:
2025-04-02 11:08:01 +02:00
parent 9124fa68e8
commit 89aed74355
3 changed files with 46 additions and 25 deletions

View File

@@ -14,6 +14,7 @@ namespace Services\RollingDate;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\MainBundle\Service\RollingDate\RollingDateConverter;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Clock\MockClock;
/**
* @internal
@@ -22,11 +23,9 @@ use PHPUnit\Framework\TestCase;
*/
final class RollingDateConverterTest extends TestCase
{
private RollingDateConverter $converter;
protected function setUp(): void
private function buildConverter(\DateTimeImmutable|string $pivot = 'now'): RollingDateConverter
{
$this->converter = new RollingDateConverter();
return new RollingDateConverter(new MockClock($pivot));
}
public static function generateDataConversionDate(): iterable
@@ -66,7 +65,7 @@ final class RollingDateConverterTest extends TestCase
$this->assertEquals(
'2022-01-01',
$this->converter->convert($rollingDate)->format('Y-m-d')
$this->buildConverter()->convert($rollingDate)->format('Y-m-d')
);
}
@@ -74,7 +73,7 @@ final class RollingDateConverterTest extends TestCase
{
$rollingDate = new RollingDate(RollingDate::T_YEAR_PREVIOUS_START);
$actual = $this->converter->convert($rollingDate);
$actual = $this->buildConverter()->convert($rollingDate);
$this->assertEquals(
(int) (new \DateTimeImmutable('now'))->format('Y') - 1,
@@ -94,7 +93,21 @@ final class RollingDateConverterTest extends TestCase
$this->assertEquals(
\DateTime::createFromFormat($format, $expectedDateTime),
$this->converter->convert($rollingDate)
$this->buildConverter()->convert($rollingDate)
);
}
/**
* @dataProvider generateDataConversionDate
*/
public function testConvertOnClock(string $roll, string $expectedDateTime, string $format)
{
$pivot = \DateTimeImmutable::createFromFormat('Y-m-d His', '2022-11-07 000000');
$rollingDate = new RollingDate($roll, null);
$this->assertEquals(
\DateTime::createFromFormat($format, $expectedDateTime),
$this->buildConverter($pivot)->convert($rollingDate)
);
}
}