2
2
declare (strict_types = 1 );
3
3
4
4
use Innmind \Validation \PointInTime ;
5
- use Innmind \TimeContinuum \Earth \{
5
+ use Innmind \TimeContinuum \{
6
+ Earth ,
7
+ Earth \Format \ISO8601 ,
6
8
Clock ,
7
- Format \ ISO8601 ,
9
+ Format ,
8
10
};
9
11
use Innmind \BlackBox \Set ;
10
- use Fixtures \Innmind \TimeContinuum \Earth \PointInTime as FPointInTime ;
12
+ use Fixtures \Innmind \TimeContinuum \{
13
+ PointInTime as FPointInTime ,
14
+ Earth as FEarth ,
15
+ };
11
16
12
17
return static function () {
13
18
yield proof (
14
19
'PointInTime::ofFormat() ' ,
15
20
given (
16
- FPointInTime::any (),
21
+ match (true ) {
22
+ \class_exists (FEarth \PointInTime::class) => FEarth \PointInTime::any (),
23
+ default => FPointInTime::any (),
24
+ },
17
25
Set \Strings::any (),
18
26
),
19
27
static function ($ assert , $ point , $ random ) {
20
- $ format = new ISO8601 ;
28
+ $ format = match (true ) {
29
+ \class_exists (ISO8601 ::class) => new ISO8601 ,
30
+ default => Format::of ('Y-m-d\TH:i:s.uP ' ), // to support microseconds
31
+ };
21
32
$ string = $ point ->format ($ format );
22
- $ clock = new Clock ;
33
+ $ clock = match (true ) {
34
+ \class_exists (Earth \Clock::class) => new Earth \Clock ,
35
+ default => Clock::live (),
36
+ };
23
37
24
38
$ assert ->true (
25
39
PointInTime::ofFormat ($ clock , $ format )->asPredicate ()($ string ),
@@ -58,8 +72,14 @@ static function($assert, $point, $random) {
58
72
Set \Strings::any (),
59
73
),
60
74
static function ($ assert , $ expected , $ random ) {
61
- $ format = new ISO8601 ;
62
- $ clock = new Clock ;
75
+ $ format = match (true ) {
76
+ \class_exists (ISO8601 ::class) => new ISO8601 ,
77
+ default => Format::iso8601 (),
78
+ };
79
+ $ clock = match (true ) {
80
+ \class_exists (Earth \Clock::class) => new Earth \Clock ,
81
+ default => Clock::live (),
82
+ };
63
83
64
84
[[$ path , $ message ]] = PointInTime::ofFormat ($ clock , $ format )->withFailure ($ expected )($ random )->match (
65
85
static fn () => null ,
@@ -73,4 +93,33 @@ static function($assert, $expected, $random) {
73
93
$ assert ->same ($ expected , $ message );
74
94
},
75
95
);
96
+
97
+ yield test (
98
+ 'PointInTime::ofFormat() with empty string fails ' ,
99
+ static function ($ assert ) {
100
+ $ format = match (true ) {
101
+ \class_exists (ISO8601 ::class) => new ISO8601 ,
102
+ default => Format::iso8601 (),
103
+ };
104
+ $ clock = match (true ) {
105
+ \class_exists (Earth \Clock::class) => new Earth \Clock ,
106
+ default => Clock::live (),
107
+ };
108
+
109
+ [[$ path , $ message ]] = PointInTime::ofFormat ($ clock , $ format )('' )->match (
110
+ static fn () => null ,
111
+ static fn ($ failures ) => $ failures
112
+ ->map (static fn ($ failure ) => [
113
+ $ failure ->path ()->toString (),
114
+ $ failure ->message (),
115
+ ])
116
+ ->toList (),
117
+ );
118
+ $ assert ->same ('$ ' , $ path );
119
+ $ assert
120
+ ->string ($ message )
121
+ ->startsWith ('Value is not a date of format ' )
122
+ ->endsWith ($ format ->toString ());
123
+ },
124
+ );
76
125
};
0 commit comments