Skip to content

Commit 45e4b9e

Browse files
committed
test: correct test regression due to added tests
1 parent cd35373 commit 45e4b9e

File tree

7 files changed

+74
-26
lines changed

7 files changed

+74
-26
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>

src/JsonSchema/Constraints/BaseConstraint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function getErrors(int $errorContext = Validator::ERROR_ALL): array
9595
return $this->errors;
9696
}
9797

98-
return array_filter($this->errors, function ($error) use ($errorContext) {
98+
return array_filter($this->errors, static function ($error) use ($errorContext) {
9999
if ($errorContext & $error['context']) {
100100
return true;
101101
}

tests/Constraints/BaseTestCase.php

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

131131
$errorMask = $validator->validate($value, $schema);
132-
$this->assertEquals(0, $errorMask);
132+
$this->assertEquals(0, $errorMask, $this->validatorErrorsToString($validator));
133133
$this->assertTrue($validator->isValid(), print_r($validator->getErrors(), true));
134134
}
135135

@@ -146,4 +146,14 @@ public function getInvalidForAssocTests(): array
146146
{
147147
return $this->getInvalidTests();
148148
}
149+
150+
private function validatorErrorsToString(Validator $validator): string
151+
{
152+
return implode(
153+
', ',
154+
array_map(
155+
static function (array $error) { return $error['message']; }, $validator->getErrors()
156+
)
157+
);
158+
}
149159
}

tests/Constraints/NumberAndIntegerTypesTest.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,37 @@ public function getInvalidTests(): array
1717
{
1818
return [
1919
[
20-
'{
20+
'input' => '{
2121
"integer": 1.4
2222
}',
23-
'{
23+
'schema' => '{
2424
"type":"object",
2525
"properties":{
2626
"integer":{"type":"integer"}
2727
}
2828
}'
2929
],
3030
[
31-
'{"integer": 1.001}',
32-
'{
31+
'input' => '{"integer": 1.001}',
32+
'schema' => '{
3333
"type": "object",
3434
"properties": {
3535
"integer": {"type": "integer"}
3636
}
3737
}'
3838
],
3939
[
40-
'{"integer": true}',
41-
'{
40+
'input' => '{"integer": true}',
41+
'schema' => '{
4242
"type": "object",
4343
"properties": {
4444
"integer": {"type": "integer"}
4545
}
4646
}'
4747
],
4848
[
49-
'{"number": "x"}',
50-
'{
49+
'input' => '{"number": "x"}',
50+
'schema' => '{
5151
"type": "object",
5252
"properties": {
5353
"number": {"type": "number"}
@@ -61,39 +61,39 @@ public function getValidTests(): array
6161
{
6262
return [
6363
[
64-
'{
64+
'input' => '{
6565
"integer": 1
6666
}',
67-
'{
67+
'schema' => '{
6868
"type":"object",
6969
"properties":{
7070
"integer":{"type":"integer"}
7171
}
7272
}'
7373
],
7474
[
75-
'{
75+
'input' => '{
7676
"number": 1.4
7777
}',
78-
'{
78+
'schema' => '{
7979
"type":"object",
8080
"properties":{
8181
"number":{"type":"number"}
8282
}
8383
}'
8484
],
8585
[
86-
'{"number": 1e5}',
87-
'{
86+
'input' => '{"number": 1e5}',
87+
'schema' => '{
8888
"type": "object",
8989
"properties": {
9090
"number": {"type": "number"}
9191
}
9292
}'
9393
],
9494
[
95-
'{"number": 1}',
96-
'{
95+
'input' => '{"number": 1}',
96+
'schema' => '{
9797
"type": "object",
9898
"properties": {
9999
"number": {"type": "number"}
@@ -102,8 +102,8 @@ public function getValidTests(): array
102102
}'
103103
],
104104
[
105-
'{"number": -49.89}',
106-
'{
105+
'input' => '{"number": -49.89}',
106+
'schema' => '{
107107
"type": "object",
108108
"properties": {
109109
"number": {

tests/Drafts/Draft3Test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ protected function getSkippedTests(): array
5858
return [
5959
// Optional
6060
'bignum.json',
61+
'ecmascript-regex.json',
6162
'format.json',
6263
'jsregex.json',
6364
'zeroTerminatedFloats.json'

tests/Drafts/Draft4Test.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ public function getValidForAssocTests(): array
4343
{
4444
$tests = parent::getValidForAssocTests();
4545
unset(
46+
$tests['minProperties.json / minProperties validation / ignores arrays'],
47+
$tests['required.json / required properties whose names are Javascript object property names / ignores arrays'],
48+
$tests['required.json / required validation / ignores arrays'],
4649
$tests['type.json / object type matches objects / an array is not an object'],
47-
$tests['type.json / array type matches arrays / an object is not an array']
50+
$tests['type.json / array type matches arrays / an object is not an array'],
4851
);
4952

5053
return $tests;
@@ -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)