Skip to content

Commit ff20bd6

Browse files
authored
Merge pull request #19 from Jeckel-Lab/feature/fallback-to-realclock-if-fake-failed
Allow to fallback to realclock if fake clock intialization…
2 parents 12ba57b + e19a782 commit ff20bd6

5 files changed

Lines changed: 55 additions & 10 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Configure default parameters in `config/packages/parameters.yaml`:
4646
parameters:
4747
clock:
4848
mode: real
49-
timezone: Europe/Paris
49+
timezone: Europe/Paris # Optional
5050
```
5151

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

6970
## Test with Codeception

‎src/CodeceptionHelper/Clock.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Clock extends Module
2828

2929
/**
3030
* @var string[]
31+
* @psalm-suppress NonInvariantDocblockPropertyType
3132
*/
3233
protected $config = [
3334
'date_format' => 'Y/m/d',
@@ -36,6 +37,7 @@ class Clock extends Module
3637

3738
/**
3839
* @var array
40+
* @psalm-suppress NonInvariantDocblockPropertyType
3941
*/
4042
protected $requiredFields = ['fake_time_path', 'date_format', 'time_format'];
4143

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/**
4+
* @author: Julien Mercier-Rojas <julien@jeckel-lab.fr>
5+
* Created at: 07/04/2021
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace JeckelLab\Clock\Exception;
11+
12+
/**
13+
* Class InvalidFakeClockInitialValueException
14+
* @package JeckelLab\Clock\Exception
15+
* @psalm-immutable
16+
*/
17+
class InvalidFakeClockInitialValueException extends RuntimeException
18+
{
19+
20+
}

‎src/Factory/ClockFactory.php‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use JeckelLab\Clock\Clock\FakedClock;
1010
use JeckelLab\Clock\Clock\FrozenClock;
1111
use JeckelLab\Clock\Clock\RealClock;
12+
use JeckelLab\Clock\Exception\InvalidFakeClockInitialValueException;
1213
use JeckelLab\Clock\Exception\RuntimeException;
1314
use JeckelLab\Contract\Infrastructure\System\Clock as ClockInterface;
1415

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

40-
switch ($config['mode'] ?? '') {
41-
case 'frozen':
42-
return new FrozenClock(self::getInitialTimeFromConfig($config));
43-
case 'faked':
44-
return new FakedClock(self::getInitialTimeFromConfig($config), $timezone);
45-
case 'real':
46-
default:
41+
try {
42+
switch ($config['mode'] ?? '') {
43+
case 'frozen':
44+
return new FrozenClock(self::getInitialTimeFromConfig($config));
45+
case 'faked':
46+
return new FakedClock(self::getInitialTimeFromConfig($config), $timezone);
47+
case 'real':
48+
default:
49+
return new RealClock($timezone);
50+
}
51+
} catch (InvalidFakeClockInitialValueException $e) {
52+
if (isset($config['fallback_to_current_date']) && $config['fallback_to_current_date'] === true) {
4753
return new RealClock($timezone);
54+
}
55+
throw $e;
4856
}
4957
}
5058

@@ -62,12 +70,12 @@ protected static function getInitialTimeFromConfig(array $config): DateTimeImmut
6270
if (isset($config['fake_time_path'])) {
6371
$filePath = (string) $config['fake_time_path'];
6472
if (! is_readable($filePath)) {
65-
throw new Exception('Impossible to read fake time file: ' . $filePath);
73+
throw new InvalidFakeClockInitialValueException('Impossible to read fake time file: ' . $filePath);
6674
}
6775
return new DateTimeImmutable(file_get_contents($filePath));
6876
}
6977
} catch (Exception $e) {
70-
throw new RuntimeException(
78+
throw new InvalidFakeClockInitialValueException(
7179
'Invalid fake time provided: ' . $e->getMessage(),
7280
(int) $e->getCode(),
7381
$e

‎tests/Factory/ClockFactoryTest.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,20 @@ public function testGetFakeClockWithUnreadableFile(): void
100100
ClockFactory::getClock(['mode' => 'frozen', 'fake_time_path' => '/foo/bar/baz']);
101101
}
102102

103+
public function testGetFakeClockWithUnreadableFileAndFallbackToCurrentDate(): void
104+
{
105+
self::assertInstanceOf(
106+
RealClock::class,
107+
ClockFactory::getClock(
108+
[
109+
'mode' => 'frozen',
110+
'fake_time_path' => '/foo/bar/baz',
111+
'fallback_to_current_date' => true
112+
]
113+
)
114+
);
115+
}
116+
103117
public function testGetFakeClockWithInvalidInitFileContent(): void
104118
{
105119
$root = vfsStream::setup('root', 444, ['clock' => 'foobarbaz']);

0 commit comments

Comments
 (0)