Skip to content

Commit 7dcf6fd

Browse files
committed
test: correct test regression due to added tests
1 parent b357300 commit 7dcf6fd

File tree

6 files changed

+72
-30
lines changed

6 files changed

+72
-30
lines changed

phpunit.xml.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@
2424
<directory>./src/JsonSchema/</directory>
2525
</whitelist>
2626
</filter>
27+
28+
<php>
29+
<ini name="memory_limit" value="-1"/>
30+
</php>
2731
</phpunit>

tests/Constraints/BaseTestCase.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function testValidCasesUsingAssoc($input, $schema, $checkMode = Constrain
124124
$validator = new Validator(new Factory($schemaStorage, null, $checkMode));
125125

126126
$errorMask = $validator->validate($value, $schema);
127-
$this->assertEquals(0, $errorMask);
127+
$this->assertEquals(0, $errorMask, $this->validatorErrorsToString($validator));
128128
$this->assertTrue($validator->isValid(), print_r($validator->getErrors(), true));
129129
}
130130

@@ -141,4 +141,14 @@ public function getInvalidForAssocTests(): Generator
141141
{
142142
yield from $this->getInvalidTests();
143143
}
144+
145+
private function validatorErrorsToString(Validator $validator): string
146+
{
147+
return implode(
148+
', ',
149+
array_map(
150+
static function (array $error) { return $error['message']; }, $validator->getErrors()
151+
)
152+
);
153+
}
144154
}

tests/Constraints/NumberAndIntegerTypesTest.php

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,35 @@ class NumberAndIntegerTypesTest extends BaseTestCase
1212
public function getInvalidTests(): \Generator
1313
{
1414
yield [
15-
'{
16-
"integer": 1.4
17-
}',
18-
'{
15+
'input' => '{ "integer": 1.4 }',
16+
'schema' => '{
1917
"type":"object",
2018
"properties":{
2119
"integer":{"type":"integer"}
2220
}
2321
}'
2422
];
2523
yield [
26-
'{"integer": 1.001}',
27-
'{
24+
'input' => '{"integer": 1.001}',
25+
'schema' => '{
2826
"type": "object",
2927
"properties": {
3028
"integer": {"type": "integer"}
3129
}
3230
}'
3331
];
3432
yield [
35-
'{"integer": true}',
36-
'{
33+
'input' => '{"integer": true}',
34+
'schema' => '{
3735
"type": "object",
3836
"properties": {
3937
"integer": {"type": "integer"}
4038
}
4139
}'
4240
];
4341
yield [
44-
'{"number": "x"}',
45-
'{
42+
'input' => '{"number": "x"}',
43+
'schema' => '{
4644
"type": "object",
4745
"properties": {
4846
"number": {"type": "number"}
@@ -54,39 +52,35 @@ public function getInvalidTests(): \Generator
5452
public function getValidTests(): \Generator
5553
{
5654
yield [
57-
'{
58-
"integer": 1
59-
}',
60-
'{
55+
'input' => '{ "integer": 1 }',
56+
'schema' => '{
6157
"type":"object",
6258
"properties":{
6359
"integer":{"type":"integer"}
6460
}
6561
}'
6662
];
6763
yield [
68-
'{
69-
"number": 1.4
70-
}',
71-
'{
64+
'input' => '{ "number": 1.4 }',
65+
'schema' => '{
7266
"type":"object",
7367
"properties":{
7468
"number":{"type":"number"}
7569
}
7670
}'
7771
];
7872
yield [
79-
'{"number": 1e5}',
80-
'{
73+
'input' => '{"number": 1e5}',
74+
'schema' => '{
8175
"type": "object",
8276
"properties": {
8377
"number": {"type": "number"}
8478
}
8579
}'
8680
];
8781
yield [
88-
'{"number": 1}',
89-
'{
82+
'input' => '{"number": 1}',
83+
'schema' => '{
9084
"type": "object",
9185
"properties": {
9286
"number": {"type": "number"}
@@ -95,8 +89,8 @@ public function getValidTests(): \Generator
9589
}'
9690
];
9791
yield [
98-
'{"number": -49.89}',
99-
'{
92+
'input' => '{"number": -49.89}',
93+
'schema' => '{
10094
"type": "object",
10195
"properties": {
10296
"number": {

tests/Drafts/Draft3Test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ protected function getSkippedTests(): array
113113
return [
114114
// Optional
115115
'bignum.json',
116+
'ecmascript-regex.json',
116117
'format.json',
117118
'jsregex.json',
118119
'zeroTerminatedFloats.json'

tests/Drafts/Draft4Test.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public function getInvalidForAssocTests(): \Generator
3838
public function getValidForAssocTests(): \Generator
3939
{
4040
$skip = [
41+
'minProperties.json / minProperties validation / ignores arrays',
42+
'required.json / required properties whose names are Javascript object property names / ignores arrays',
43+
'required.json / required validation / ignores arrays',
4144
'type.json / object type matches objects / an array is not an object',
4245
'type.json / array type matches arrays / an object is not an array',
4346
];
@@ -58,7 +61,9 @@ protected function getSkippedTests(): array
5861
return [
5962
// Optional
6063
'bignum.json',
64+
'ecmascript-regex.json',
6165
'format.json',
66+
'float-overflow.json',
6267
'zeroTerminatedFloats.json',
6368
// Required
6469
'not.json' // only one test case failing

tests/JsonSchemaTestSuite.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44

55
namespace JsonSchema\Tests;
66

7+
use CallbackFilterIterator;
78
use JsonSchema\Constraints\Factory;
89
use JsonSchema\SchemaStorage;
910
use JsonSchema\SchemaStorageInterface;
1011
use JsonSchema\Validator;
1112
use PHPUnit\Framework\TestCase;
13+
use RecursiveDirectoryIterator;
14+
use RecursiveIteratorIterator;
1215

1316
class JsonSchemaTestSuite extends TestCase
1417
{
@@ -33,28 +36,53 @@ public function testIt(
3336
self::assertEquals($expectedValidationResult, count($validator->getErrors()) === 0);
3437
}
3538

39+
public function testItOnce(): void
40+
{
41+
$schema = json_decode('{ "required": ["__proto__", "toString", "constructor"] }', false);
42+
$data = [];
43+
44+
$schemaStorage = new SchemaStorage();
45+
$schemaStorage->addSchema(SchemaStorage::INTERNAL_PROVIDED_SCHEMA_URI, $schema);
46+
$this->loadRemotesIntoStorage($schemaStorage);
47+
$validator = new Validator(new Factory($schemaStorage));
48+
49+
$result = $validator->validate($data, $schema);
50+
51+
self::assertEquals(true, count($validator->getErrors()) === 0);
52+
}
53+
54+
3655
public function casesDataProvider(): \Generator
3756
{
3857
$testDir = __DIR__ . '/../vendor/json-schema/json-schema-test-suite/tests';
3958
$drafts = array_filter(glob($testDir . '/*'), static function (string $filename) {
4059
return is_dir($filename);
4160
});
42-
$skippedDrafts = ['draft4', 'draft6', 'draft7', 'draft2019-09', 'draft2020-12', 'draft-next', 'latest'];
61+
$skippedDrafts = ['draft3', 'draft6', 'draft7', 'draft2019-09', 'draft2020-12', 'draft-next', 'latest'];
4362

4463
foreach ($drafts as $draft) {
45-
$files = glob($draft . '/*.json');
4664
if (in_array(basename($draft), $skippedDrafts, true)) {
4765
continue;
4866
}
4967

68+
$files = new CallbackFilterIterator(
69+
new RecursiveIteratorIterator(
70+
new RecursiveDirectoryIterator($draft)
71+
),
72+
function ($file) {
73+
return $file->isFile() && strtolower($file->getExtension()) === 'json';
74+
}
75+
);
76+
/** @var \SplFileInfo $file */
5077
foreach ($files as $file) {
51-
$contents = json_decode(file_get_contents($file), false);
78+
$contents = json_decode(file_get_contents($file->getPathname()), false);
5279
foreach ($contents as $testCase) {
5380
foreach ($testCase->tests as $test) {
5481
$name = sprintf(
55-
'[%s/%s]: %s: %s is expected to be %s',
82+
'[%s/%s%s]: %s: %s is expected to be %s',
5683
basename($draft),
57-
basename($file),
84+
str_contains($file->getPathname(), 'optional') ? 'optional/' : '',
85+
$file->getBasename(),
5886
$testCase->description,
5987
$test->description,
6088
$test->valid ? 'valid' : 'invalid',

0 commit comments

Comments
 (0)