Skip to content

Commit

Permalink
Merge pull request #19 from Jeckel-Lab/feature/fallback-to-realclock-…
Browse files Browse the repository at this point in the history
…if-fake-failed

Allow to fallback to realclock if fake clock intialization…
  • Loading branch information
jeckel authored Apr 7, 2021
2 parents 12ba57b + e19a782 commit ff20bd6
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Configure default parameters in `config/packages/parameters.yaml`:
parameters:
clock:
mode: real
timezone: Europe/Paris
timezone: Europe/Paris # Optional
```

And then configure parameters for **tests** environment in `config/packages/test/parameters.yaml`:
Expand All @@ -64,6 +64,7 @@ parameters:
clock:
mode: frozen
fake_time_path: '%kernel.project_dir%/var/fake_time.txt'
fallback_to_current_date: true # Default: false, if true and file is not found or has invalid valid, then fallback to RealClock
```

## Test with Codeception
Expand Down
2 changes: 2 additions & 0 deletions src/CodeceptionHelper/Clock.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Clock extends Module

/**
* @var string[]
* @psalm-suppress NonInvariantDocblockPropertyType
*/
protected $config = [
'date_format' => 'Y/m/d',
Expand All @@ -36,6 +37,7 @@ class Clock extends Module

/**
* @var array
* @psalm-suppress NonInvariantDocblockPropertyType
*/
protected $requiredFields = ['fake_time_path', 'date_format', 'time_format'];

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

/**
* @author: Julien Mercier-Rojas <[email protected]>
* Created at: 07/04/2021
*/

declare(strict_types=1);

namespace JeckelLab\Clock\Exception;

/**
* Class InvalidFakeClockInitialValueException
* @package JeckelLab\Clock\Exception
* @psalm-immutable
*/
class InvalidFakeClockInitialValueException extends RuntimeException
{

}
26 changes: 17 additions & 9 deletions src/Factory/ClockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use JeckelLab\Clock\Clock\FakedClock;
use JeckelLab\Clock\Clock\FrozenClock;
use JeckelLab\Clock\Clock\RealClock;
use JeckelLab\Clock\Exception\InvalidFakeClockInitialValueException;
use JeckelLab\Clock\Exception\RuntimeException;
use JeckelLab\Contract\Infrastructure\System\Clock as ClockInterface;

Expand Down Expand Up @@ -37,14 +38,21 @@ public static function getClock(array $config = []): ClockInterface
);
}

switch ($config['mode'] ?? '') {
case 'frozen':
return new FrozenClock(self::getInitialTimeFromConfig($config));
case 'faked':
return new FakedClock(self::getInitialTimeFromConfig($config), $timezone);
case 'real':
default:
try {
switch ($config['mode'] ?? '') {
case 'frozen':
return new FrozenClock(self::getInitialTimeFromConfig($config));
case 'faked':
return new FakedClock(self::getInitialTimeFromConfig($config), $timezone);
case 'real':
default:
return new RealClock($timezone);
}
} catch (InvalidFakeClockInitialValueException $e) {
if (isset($config['fallback_to_current_date']) && $config['fallback_to_current_date'] === true) {
return new RealClock($timezone);
}
throw $e;
}
}

Expand All @@ -62,12 +70,12 @@ protected static function getInitialTimeFromConfig(array $config): DateTimeImmut
if (isset($config['fake_time_path'])) {
$filePath = (string) $config['fake_time_path'];
if (! is_readable($filePath)) {
throw new Exception('Impossible to read fake time file: ' . $filePath);
throw new InvalidFakeClockInitialValueException('Impossible to read fake time file: ' . $filePath);
}
return new DateTimeImmutable(file_get_contents($filePath));
}
} catch (Exception $e) {
throw new RuntimeException(
throw new InvalidFakeClockInitialValueException(
'Invalid fake time provided: ' . $e->getMessage(),
(int) $e->getCode(),
$e
Expand Down
14 changes: 14 additions & 0 deletions tests/Factory/ClockFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ public function testGetFakeClockWithUnreadableFile(): void
ClockFactory::getClock(['mode' => 'frozen', 'fake_time_path' => '/foo/bar/baz']);
}

public function testGetFakeClockWithUnreadableFileAndFallbackToCurrentDate(): void
{
self::assertInstanceOf(
RealClock::class,
ClockFactory::getClock(
[
'mode' => 'frozen',
'fake_time_path' => '/foo/bar/baz',
'fallback_to_current_date' => true
]
)
);
}

public function testGetFakeClockWithInvalidInitFileContent(): void
{
$root = vfsStream::setup('root', 444, ['clock' => 'foobarbaz']);
Expand Down

0 comments on commit ff20bd6

Please sign in to comment.