@@ -80,7 +80,7 @@ public function testNonExistingFilterThrowsAnException(): void
80
80
protected function getCustomFilter (
81
81
array $ customFilter ,
82
82
string $ token = 'customFilter ' ,
83
- array $ acceptedTypes = ['string ' ]
83
+ array $ acceptedTypes = ['string ' , ' null ' ]
84
84
): FilterInterface {
85
85
return new class ($ customFilter , $ token , $ acceptedTypes ) implements FilterInterface {
86
86
private $ customFilter ;
@@ -532,7 +532,7 @@ public function testMultipleTransformingFiltersAppliedToOnePropertyThrowsAnExcep
532
532
new class () extends DateTimeFilter {
533
533
public function getAcceptedTypes (): array
534
534
{
535
- return [DateTime::class];
535
+ return [DateTime::class, ' null ' ];
536
536
}
537
537
538
538
public function getToken (): string
@@ -590,11 +590,12 @@ public static function exceptionFilter(string $value): void
590
590
}
591
591
592
592
/**
593
- * @dataProvider namespaceDataProvider
593
+ * @dataProvider implicitNullNamespaceDataProvider
594
594
*
595
+ * @param bool $implicitNull
595
596
* @param string $namespace
596
597
*/
597
- public function testTransformingToScalarType (string $ namespace )
598
+ public function testTransformingToScalarType (bool $ implicitNull , string $ namespace )
598
599
{
599
600
$ className = $ this ->generateClassFromFile (
600
601
'TransformingScalarFilter.json ' ,
@@ -609,7 +610,9 @@ public function testTransformingToScalarType(string $namespace)
609
610
'binary ' ,
610
611
['integer ' ]
611
612
)
612
- )
613
+ ),
614
+ false ,
615
+ $ implicitNull
613
616
);
614
617
615
618
$ fqcn = $ namespace . $ className ;
@@ -621,6 +624,15 @@ public function testTransformingToScalarType(string $namespace)
621
624
622
625
$ this ->assertSame (['value ' => 11 ], $ object ->toArray ());
623
626
$ this ->assertSame ('{"value":11} ' , $ object ->toJSON ());
627
+
628
+ $ this ->expectException (ErrorRegistryException::class);
629
+ $ this ->expectExceptionMessage (
630
+ $ implicitNull
631
+ ? 'Filter binary is not compatible with property type NULL for property value '
632
+ : 'Invalid type for value. Requires [string, int], got NULL '
633
+ );
634
+
635
+ new $ fqcn (['value ' => null ]);
624
636
}
625
637
626
638
public static function filterIntToBinary (int $ value ): string
@@ -637,7 +649,8 @@ public function testInvalidFilterChainWithTransformingFilterThrowsAnException():
637
649
{
638
650
$ this ->expectException (SchemaException::class);
639
651
$ this ->expectExceptionMessage (
640
- 'Filter trim is not compatible with transformed property type DateTime for property filteredProperty '
652
+ 'Filter trim is not compatible with transformed property type ' .
653
+ '[null, DateTime] for property filteredProperty '
641
654
);
642
655
643
656
$ this ->generateClassFromFileTemplate ('FilterChain.json ' , ['["dateTime", "trim"] ' ], null , false );
@@ -654,7 +667,7 @@ public function testFilterChainWithTransformingFilter(): void
654
667
$ this ->getCustomFilter (
655
668
[self ::class, 'stripTimeFilter ' ],
656
669
'stripTime ' ,
657
- [DateTime::class]
670
+ [DateTime::class, ' null ' ]
658
671
)
659
672
),
660
673
false
@@ -692,7 +705,7 @@ public function testFilterChainWithTransformingFilterOnMultiTypeProperty(
692
705
$ this ->getCustomFilter (
693
706
[self ::class, 'stripTimeFilter ' ],
694
707
'stripTime ' ,
695
- [DateTime::class]
708
+ [DateTime::class, ' null ' ]
696
709
)
697
710
),
698
711
false ,
@@ -722,6 +735,27 @@ public function implicitNullNamespaceDataProvider(): array
722
735
);
723
736
}
724
737
738
+ public function testFilterChainWithIncompatibleFilterAfterTransformingFilterOnMultiTypeProperty (): void
739
+ {
740
+ $ this ->expectException (SchemaException::class);
741
+ $ this ->expectExceptionMessage (
742
+ 'Filter stripTime is not compatible with transformed ' .
743
+ 'property type [null, DateTime] for property filteredProperty '
744
+ );
745
+
746
+ $ this ->generateClassFromFile (
747
+ 'FilterChainMultiType.json ' ,
748
+ (new GeneratorConfiguration ())
749
+ ->addFilter (
750
+ $ this ->getCustomFilter (
751
+ [self ::class, 'stripTimeFilterStrict ' ],
752
+ 'stripTime ' ,
753
+ [DateTime::class]
754
+ )
755
+ )
756
+ );
757
+ }
758
+
725
759
public function testFilterAfterTransformingFilterIsSkippedIfTransformingFilterFails (): void
726
760
{
727
761
$ this ->expectException (ErrorRegistryException::class);
@@ -736,7 +770,7 @@ public function testFilterAfterTransformingFilterIsSkippedIfTransformingFilterFa
736
770
$ this ->getCustomFilter (
737
771
[self ::class, 'exceptionFilter ' ],
738
772
'stripTime ' ,
739
- [DateTime::class]
773
+ [DateTime::class, ' null ' ]
740
774
)
741
775
),
742
776
false
@@ -766,7 +800,7 @@ public function testFilterWhichAppliesToMultiTypePropertyPartiallyThrowsAnExcept
766
800
$ this ->getCustomFilter (
767
801
[self ::class, 'stripTimeFilter ' ],
768
802
'stripTime ' ,
769
- [DateTime::class]
803
+ [DateTime::class, ' null ' ]
770
804
)
771
805
),
772
806
false
@@ -778,6 +812,11 @@ public static function stripTimeFilter(?DateTime $value): ?DateTime
778
812
return $ value !== null ? $ value ->setTime (0 , 0 ) : null ;
779
813
}
780
814
815
+ public static function stripTimeFilterStrict (DateTime $ value ): DateTime
816
+ {
817
+ return $ value ->setTime (0 , 0 );
818
+ }
819
+
781
820
/**
782
821
* @dataProvider arrayFilterDataProvider
783
822
*
0 commit comments