Skip to content

Commit c638641

Browse files
committed
feat: update to latest json schema test suite
1 parent 0f69f0e commit c638641

File tree

2 files changed

+95
-3
lines changed

2 files changed

+95
-3
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
},
3434
"require-dev": {
3535
"friendsofphp/php-cs-fixer": "3.3.0",
36-
"json-schema/json-schema-test-suite": "1.2.0",
36+
"json-schema/json-schema-test-suite": "^23.2",
3737
"phpunit/phpunit": "^8.5",
3838
"phpspec/prophecy": "^1.19",
3939
"phpstan/phpstan": "^1.12",
@@ -59,11 +59,11 @@
5959
"type": "package",
6060
"package": {
6161
"name": "json-schema/json-schema-test-suite",
62-
"version": "1.2.0",
62+
"version": "23.2.0",
6363
"source": {
6464
"type": "git",
6565
"url": "https://github.com/json-schema/JSON-Schema-Test-Suite",
66-
"reference": "1.2.0"
66+
"reference": "23.2.0"
6767
}
6868
}
6969
}

tests/JsonSchemaTestSuite.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace JsonSchema\Tests;
6+
7+
use JsonSchema\Constraints\Factory;
8+
use JsonSchema\SchemaStorage;
9+
use JsonSchema\SchemaStorageInterface;
10+
use JsonSchema\Validator;
11+
use PHPUnit\Framework\TestCase;
12+
13+
class JsonSchemaTestSuite extends TestCase
14+
{
15+
/**
16+
* @dataProvider casesDataProvider
17+
*/
18+
public function testIt(
19+
string $testCaseDescription,
20+
string $testDescription,
21+
\stdClass $schema,
22+
mixed $data,
23+
bool $expectedValidationResult
24+
): void
25+
{
26+
$schemaStorage = new SchemaStorage();
27+
$schemaStorage->addSchema('internal://mySchema', $schema);
28+
$this->loadRemotesIntoStorage($schemaStorage);
29+
$validator = new Validator(new Factory($schemaStorage));
30+
31+
$result = $validator->validate($data, $schema);
32+
33+
self::assertEquals($expectedValidationResult, count($validator->getErrors()) === 0);
34+
}
35+
36+
public function casesDataProvider(): \Generator
37+
{
38+
$testDir = __DIR__ . '/../vendor/json-schema/json-schema-test-suite/tests';
39+
$drafts = array_filter(glob($testDir . '/*'), is_dir(...));
40+
$skippedDrafts = ['draft4', 'draft6', 'draft7', 'draft2019-09', 'draft2020-12', 'draft-next', 'latest'];
41+
42+
foreach ($drafts as $draft) {
43+
$files = glob($draft . '/*.json');
44+
if (in_array(basename($draft), $skippedDrafts, true)) {
45+
continue;
46+
}
47+
48+
foreach ($files as $file) {
49+
$contents = json_decode(file_get_contents($file), false);
50+
foreach ($contents as $testCase) {
51+
foreach ($testCase->tests as $test) {
52+
$name = sprintf(
53+
'[%s/%s]: %s: %s is expected to be %s',
54+
basename($draft),
55+
basename($file),
56+
$testCase->description,
57+
$test->description,
58+
$test->valid ? 'valid' : 'invalid',
59+
);
60+
61+
yield $name => [
62+
'testCaseDescription' => $testCase->description,
63+
'testDescription' => $test->description,
64+
'schema' => $testCase->schema,
65+
'data' => $test->data,
66+
'expectedValidationResult' => $test->valid,
67+
];
68+
}
69+
70+
}
71+
}
72+
}
73+
}
74+
75+
private function loadRemotesIntoStorage(SchemaStorageInterface $storage): void
76+
{
77+
$remotesDir = __DIR__ . '/../vendor/json-schema/json-schema-test-suite/remotes';
78+
79+
$directory = new \RecursiveDirectoryIterator($remotesDir);
80+
$iterator = new \RecursiveIteratorIterator($directory);
81+
82+
foreach ($iterator as $info) {
83+
if (!$info->isFile()) {
84+
continue;
85+
}
86+
87+
$id = str_replace($remotesDir, 'http://localhost:1234', $info->getPathname());
88+
$storage->addSchema($id, json_decode(file_get_contents($info->getPathname()), false));
89+
}
90+
}
91+
92+
}

0 commit comments

Comments
 (0)