@@ -27,6 +27,7 @@ public function testTestCaseValidatesCorrectly(
27
27
string $ testDescription ,
28
28
$ schema ,
29
29
$ data ,
30
+ int $ checkMode ,
30
31
bool $ expectedValidationResult ,
31
32
bool $ optional
32
33
): void {
@@ -37,7 +38,7 @@ public function testTestCaseValidatesCorrectly(
37
38
$ validator = new Validator (new Factory ($ schemaStorage ));
38
39
39
40
try {
40
- $ validator ->validate ($ data , $ schema , Constraint:: CHECK_MODE_NORMAL | Constraint:: CHECK_MODE_STRICT );
41
+ $ validator ->validate ($ data , $ schema , $ checkMode );
41
42
} catch (\Exception $ e ) {
42
43
if ($ optional ) {
43
44
$ this ->markTestSkipped ('Optional test case throws exception during validate() invocation: " ' . $ e ->getMessage () . '" ' );
@@ -63,10 +64,11 @@ public function casesDataProvider(): \Generator
63
64
$ drafts = array_filter (glob ($ testDir . '/* ' ), static function (string $ filename ) {
64
65
return is_dir ($ filename );
65
66
});
66
- $ skippedDrafts = ['draft3 ' , ' draft4 ' , ' draft7 ' , 'draft2019-09 ' , 'draft2020-12 ' , 'draft-next ' , 'latest ' ];
67
+ $ skippedDrafts = ['draft7 ' , 'draft2019-09 ' , 'draft2020-12 ' , 'draft-next ' , 'latest ' ];
67
68
68
69
foreach ($ drafts as $ draft ) {
69
- if (in_array (basename ($ draft ), $ skippedDrafts , true )) {
70
+ $ baseDraftName = basename ($ draft );
71
+ if (in_array ($ baseDraftName , $ skippedDrafts , true )) {
70
72
continue ;
71
73
}
72
74
@@ -82,8 +84,10 @@ function ($file) {
82
84
foreach ($ files as $ file ) {
83
85
$ contents = json_decode (file_get_contents ($ file ->getPathname ()), false );
84
86
foreach ($ contents as $ testCase ) {
85
- if (is_object ($ testCase ->schema )) {
86
- $ testCase ->schema ->{'$schema ' } = 'http://json-schema.org/draft-06/schema# ' ; // Hardcode $schema property in schema
87
+ // Since draft6 can only be validated using the strict check mode we need to ensure the $schema
88
+ // property is set in the test schema
89
+ if ($ baseDraftName === 'draft6 ' && is_object ($ testCase ->schema )) {
90
+ $ testCase ->schema ->{'$schema ' } = 'http://json-schema.org/draft-06/schema# ' ;
87
91
}
88
92
foreach ($ testCase ->tests as $ test ) {
89
93
$ name = sprintf (
@@ -105,6 +109,7 @@ function ($file) {
105
109
'testDescription ' => $ test ->description ,
106
110
'schema ' => $ testCase ->schema ,
107
111
'data ' => $ test ->data ,
112
+ 'checkMode ' => $ this ->getCheckModeForDraft ($ baseDraftName ),
108
113
'expectedValidationResult ' => $ test ->valid ,
109
114
'optional ' => str_contains ($ file ->getPathname (), '/optional/ ' )
110
115
];
@@ -168,4 +173,17 @@ private function is32Bit(): bool
168
173
{
169
174
return PHP_INT_SIZE === 4 ;
170
175
}
176
+
177
+ /**
178
+ * @phpstan-return int-mask-of<Validator::ERROR_*>
179
+ */
180
+ private function getCheckModeForDraft (string $ draft ): int
181
+ {
182
+ switch ($ draft ) {
183
+ case 'draft6 ' :
184
+ return Constraint::CHECK_MODE_NORMAL | Constraint::CHECK_MODE_STRICT ;
185
+ default :
186
+ return Constraint::CHECK_MODE_NORMAL ;
187
+ }
188
+ }
171
189
}
0 commit comments