Skip to content

Commit

Permalink
Use Symfony Clock component
Browse files Browse the repository at this point in the history
  • Loading branch information
HypeMC committed Feb 24, 2024
1 parent d2e938d commit a3c45d1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"symfony/monolog-bundle": "^3.4"
},
"require-dev": {
"symfony/clock": "^6.2",
"symfony/phpunit-bridge": "^6.4.1"
},
"autoload": {
Expand Down
1 change: 1 addition & 0 deletions config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

<service id="Bizkit\LoggableCommandBundle\PathResolver\DefaultPathResolver">
<argument type="service" id="Bizkit\LoggableCommandBundle\FilenameProvider\FilenameProviderInterface" />
<argument type="service" id="Symfony\Component\Clock\ClockInterface" on-invalid="null" />
</service>

<service id="Bizkit\LoggableCommandBundle\PathResolver\PathResolverInterface"
Expand Down
5 changes: 4 additions & 1 deletion src/PathResolver/DefaultPathResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

use Bizkit\LoggableCommandBundle\FilenameProvider\FilenameProviderInterface;
use Bizkit\LoggableCommandBundle\LoggableOutput\LoggableOutputInterface;
use Psr\Clock\ClockInterface;

final class DefaultPathResolver implements PathResolverInterface
{
public function __construct(
private readonly FilenameProviderInterface $filenameProvider,
private readonly ?ClockInterface $clock = null,
) {
}

Expand All @@ -24,7 +26,8 @@ public function __invoke(array $handlerOptions, LoggableOutputInterface $loggabl
}

if (str_contains($resolvedPath, '{date}')) {
$resolvedPath = strtr($resolvedPath, ['{date}' => date($handlerOptions['date_format'])]);
$date = $this->clock?->now()->format($handlerOptions['date_format']) ?? date($handlerOptions['date_format']);
$resolvedPath = strtr($resolvedPath, ['{date}' => $date]);
}

return $resolvedPath;
Expand Down
19 changes: 17 additions & 2 deletions tests/PathResolver/DefaultPathResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());

$this->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));

$this->assertSame($resolvedPath, $pathResolver($handlerOptions, new DummyLoggableOutput()));
}

public function handlerOptions(): iterable
{
yield 'Filename & date' => [[
Expand Down

0 comments on commit a3c45d1

Please sign in to comment.