From 469a3ecc140aec34cd606641b8439932bfc60881 Mon Sep 17 00:00:00 2001 From: HypeMC Date: Sun, 12 Feb 2023 10:32:16 +0100 Subject: [PATCH] Use Symfony Clock component --- .github/workflows/tests.yaml | 4 ++++ composer.json | 1 + config/services.xml | 1 + src/PathResolver/DefaultPathResolver.php | 5 ++++- .../PathResolver/DefaultPathResolverTest.php | 19 +++++++++++++++++-- 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 59e569e..340d049 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -33,6 +33,10 @@ jobs: ini-values: zend.exception_ignore_args=false tools: flex + - name: Remove Symfony Clock + if: ${{ matrix.symfony < 6.2 }} + run: composer remove --dev --no-update symfony/clock + - name: Install Dependencies uses: ramsey/composer-install@v2 with: diff --git a/composer.json b/composer.json index 9d41861..4014f94 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,7 @@ "symfony/monolog-bundle": "^3.4" }, "require-dev": { + "symfony/clock": "^6.2", "symfony/phpunit-bridge": "^6.4.1" }, "autoload": { diff --git a/config/services.xml b/config/services.xml index 7a12856..11f42ef 100644 --- a/config/services.xml +++ b/config/services.xml @@ -51,6 +51,7 @@ + date($handlerOptions['date_format'])]); + $date = $this->clock?->now()->format($handlerOptions['date_format']) ?? date($handlerOptions['date_format']); + $resolvedPath = strtr($resolvedPath, ['{date}' => $date]); } return $resolvedPath; diff --git a/tests/PathResolver/DefaultPathResolverTest.php b/tests/PathResolver/DefaultPathResolverTest.php index 6076492..a45a8bd 100644 --- a/tests/PathResolver/DefaultPathResolverTest.php +++ b/tests/PathResolver/DefaultPathResolverTest.php @@ -10,6 +10,7 @@ use Bizkit\LoggableCommandBundle\Tests\Fixtures\DummyLoggableOutput; use Bizkit\LoggableCommandBundle\Tests\TestCase; use Symfony\Bridge\PhpUnit\ClockMock; +use Symfony\Component\Clock\MockClock; /** * @covers \Bizkit\LoggableCommandBundle\PathResolver\DefaultPathResolver @@ -18,18 +19,32 @@ */ final class DefaultPathResolverTest extends TestCase { + private const MOCK_TIME = 1612711778; + /** * @dataProvider handlerOptions */ - public function testLoggerIsConfiguredAsExpected(array $handlerOptions, string $resolvedPath): void + public function testLoggerIsConfiguredAsExpectedWithoutClock(array $handlerOptions, string $resolvedPath): void { - ClockMock::withClockMock(1612711778); + ClockMock::withClockMock(self::MOCK_TIME); $pathResolver = new DefaultPathResolver(new DefaultFilenameProvider()); self::assertSame($resolvedPath, $pathResolver($handlerOptions, new DummyLoggableOutput())); } + /** + * @requires function \Symfony\Component\Clock\MockClock::now + * + * @dataProvider handlerOptions + */ + public function testLoggerIsConfiguredAsExpectedWithClock(array $handlerOptions, string $resolvedPath): void + { + $pathResolver = new DefaultPathResolver(new DefaultFilenameProvider(), new MockClock('@'.self::MOCK_TIME)); + + self::assertSame($resolvedPath, $pathResolver($handlerOptions, new DummyLoggableOutput())); + } + public static function handlerOptions(): iterable { yield 'Filename & date' => [[