|
| 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