@@ -154,6 +154,72 @@ public static function normalizeUsingTimeZonePassedInContextAndExpectedFormatWit
154
154
];
155
155
}
156
156
157
+ /**
158
+ * @dataProvider provideNormalizeUsingCastCases
159
+ */
160
+ public function testNormalizeUsingCastPassedInConstructor (\DateTimeInterface $ value , string $ format , ?string $ cast , string |int |float $ expectedResult )
161
+ {
162
+ $ normalizer = new DateTimeNormalizer ([DateTimeNormalizer::CAST_KEY => $ cast ]);
163
+
164
+ $ this ->assertSame ($ normalizer ->normalize ($ value , null , [DateTimeNormalizer::FORMAT_KEY => $ format ]), $ expectedResult );
165
+ }
166
+
167
+ /**
168
+ * @dataProvider provideNormalizeUsingCastCases
169
+ */
170
+ public function testNormalizeUsingCastPassedInContext (\DateTimeInterface $ value , string $ format , ?string $ cast , string |int |float $ expectedResult )
171
+ {
172
+ $ this ->assertSame ($ this ->normalizer ->normalize ($ value , null , [DateTimeNormalizer::FORMAT_KEY => $ format , DateTimeNormalizer::CAST_KEY => $ cast ]), $ expectedResult );
173
+ }
174
+
175
+ /**
176
+ * @return iterable<array{0: \DateTimeImmutable, 1: non-empty-string, 2: 'int'|'float'|null, 3: 'int'|'float'|'string'}>
177
+ */
178
+ public static function provideNormalizeUsingCastCases (): iterable
179
+ {
180
+ yield [
181
+ \DateTimeImmutable::createFromFormat ('U ' , '1703071202 ' ),
182
+ 'Y ' ,
183
+ null ,
184
+ '2023 ' ,
185
+ ];
186
+
187
+ yield [
188
+ \DateTimeImmutable::createFromFormat ('U ' , '1703071202 ' ),
189
+ 'Y ' ,
190
+ 'int ' ,
191
+ 2023 ,
192
+ ];
193
+
194
+ yield [
195
+ \DateTimeImmutable::createFromFormat ('U ' , '1703071202 ' ),
196
+ 'Ymd ' ,
197
+ 'int ' ,
198
+ 20231220 ,
199
+ ];
200
+
201
+ yield [
202
+ \DateTimeImmutable::createFromFormat ('U ' , '1703071202 ' ),
203
+ 'Y ' ,
204
+ 'int ' ,
205
+ 2023 ,
206
+ ];
207
+
208
+ yield [
209
+ \DateTimeImmutable::createFromFormat ('U.v ' , '1703071202.388 ' ),
210
+ 'U.v ' ,
211
+ 'float ' ,
212
+ 1703071202.388 ,
213
+ ];
214
+
215
+ yield [
216
+ \DateTimeImmutable::createFromFormat ('U.u ' , '1703071202.388811 ' ),
217
+ 'U.u ' ,
218
+ 'float ' ,
219
+ 1703071202.388811 ,
220
+ ];
221
+ }
222
+
157
223
public function testNormalizeInvalidObjectThrowsException ()
158
224
{
159
225
$ this ->expectException (InvalidArgumentException::class);
0 commit comments