66
77use PHPUnit \Framework \Attributes \DataProvider ;
88use TypeLang \Mapper \Context \RootContext ;
9+ use TypeLang \Mapper \Exception \Mapping \InvalidValueException ;
910use TypeLang \Mapper \Runtime \Configuration ;
1011use TypeLang \Mapper \Runtime \Extractor \TypeExtractorInterface ;
1112use TypeLang \Mapper \Runtime \Parser \TypeParserInterface ;
@@ -145,6 +146,33 @@ private static function defaultDataProviderSamples(): iterable
145146 yield StringBackedEnumStub::ExampleCase;
146147 }
147148
149+ protected static function assertIfNotException (mixed $ expected , mixed $ actual ): void
150+ {
151+ switch (true ) {
152+ case $ expected instanceof \Throwable:
153+ break ;
154+ case \is_array ($ expected ):
155+ case \is_object ($ expected ):
156+ self ::assertEquals ($ expected , $ actual );
157+ break ;
158+ case \is_float ($ expected ) && \is_nan ($ expected ):
159+ self ::assertNan ($ actual );
160+ break ;
161+ default :
162+ self ::assertSame ($ expected , $ actual );
163+ }
164+ }
165+
166+ protected function expectTypeErrorIfException (mixed $ expected ): void
167+ {
168+ if (!$ expected instanceof \Throwable) {
169+ return ;
170+ }
171+
172+ $ this ->expectExceptionMessage ($ expected ->getMessage ());
173+ $ this ->expectException (InvalidValueException::class);
174+ }
175+
148176 /**
149177 * @return non-empty-string
150178 */
@@ -261,9 +289,7 @@ public static function castStrictNormalizationDataProvider(): iterable
261289 #[DataProvider('castStrictNormalizationDataProvider ' )]
262290 public function testCastStrictNormalization (mixed $ value , mixed $ expected ): void
263291 {
264- if ($ expected instanceof \Throwable) {
265- self ::expectExceptionMessage ($ expected ->getMessage ());
266- }
292+ $ this ->expectTypeErrorIfException ($ expected );
267293
268294 $ type = static ::createType ();
269295
@@ -272,16 +298,7 @@ public function testCastStrictNormalization(mixed $value, mixed $expected): void
272298 strictTypes: true ,
273299 ));
274300
275- switch (true ) {
276- case $ expected instanceof \Throwable:
277- break ;
278- case \is_array ($ expected ):
279- case \is_object ($ expected ):
280- self ::assertEquals ($ expected , $ actual );
281- break ;
282- default :
283- self ::assertSame ($ expected , $ actual );
284- }
301+ self ::assertIfNotException ($ expected , $ actual );
285302 }
286303
287304 /**
@@ -295,9 +312,7 @@ public static function castNonStrictNormalizationDataProvider(): iterable
295312 #[DataProvider('castNonStrictNormalizationDataProvider ' )]
296313 public function testCastNonStrictNormalization (mixed $ value , mixed $ expected ): void
297314 {
298- if ($ expected instanceof \Throwable) {
299- self ::expectExceptionMessage ($ expected ->getMessage ());
300- }
315+ $ this ->expectTypeErrorIfException ($ expected );
301316
302317 $ type = static ::createType ();
303318
@@ -306,16 +321,7 @@ public function testCastNonStrictNormalization(mixed $value, mixed $expected): v
306321 strictTypes: false ,
307322 ));
308323
309- switch (true ) {
310- case $ expected instanceof \Throwable:
311- break ;
312- case \is_array ($ expected ):
313- case \is_object ($ expected ):
314- self ::assertEquals ($ expected , $ actual );
315- break ;
316- default :
317- self ::assertSame ($ expected , $ actual );
318- }
324+ self ::assertIfNotException ($ expected , $ actual );
319325 }
320326
321327 /**
@@ -329,9 +335,7 @@ public static function castStrictDenormalizationDataProvider(): iterable
329335 #[DataProvider('castStrictDenormalizationDataProvider ' )]
330336 public function testCastStrictDenormalization (mixed $ value , mixed $ expected ): void
331337 {
332- if ($ expected instanceof \Throwable) {
333- self ::expectExceptionMessage ($ expected ->getMessage ());
334- }
338+ $ this ->expectTypeErrorIfException ($ expected );
335339
336340 $ type = static ::createType ();
337341
@@ -340,16 +344,7 @@ public function testCastStrictDenormalization(mixed $value, mixed $expected): vo
340344 strictTypes: true ,
341345 ));
342346
343- switch (true ) {
344- case $ expected instanceof \Throwable:
345- break ;
346- case \is_array ($ expected ):
347- case \is_object ($ expected ):
348- self ::assertEquals ($ expected , $ actual );
349- break ;
350- default :
351- self ::assertSame ($ expected , $ actual );
352- }
347+ self ::assertIfNotException ($ expected , $ actual );
353348 }
354349
355350 /**
@@ -363,9 +358,7 @@ public static function castNonStrictDenormalizationDataProvider(): iterable
363358 #[DataProvider('castNonStrictDenormalizationDataProvider ' )]
364359 public function testCastNonStrictDenormalization (mixed $ value , mixed $ expected ): void
365360 {
366- if ($ expected instanceof \Throwable) {
367- self ::expectExceptionMessage ($ expected ->getMessage ());
368- }
361+ $ this ->expectTypeErrorIfException ($ expected );
369362
370363 $ type = static ::createType ();
371364
@@ -374,16 +367,7 @@ public function testCastNonStrictDenormalization(mixed $value, mixed $expected):
374367 strictTypes: false ,
375368 ));
376369
377- switch (true ) {
378- case $ expected instanceof \Throwable:
379- break ;
380- case \is_array ($ expected ):
381- case \is_object ($ expected ):
382- self ::assertEquals ($ expected , $ actual );
383- break ;
384- default :
385- self ::assertSame ($ expected , $ actual );
386- }
370+ self ::assertIfNotException ($ expected , $ actual );
387371 }
388372
389373 /**
0 commit comments