Skip to content

Commit 8668905

Browse files
committed
JSON validate VS test data - php
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
1 parent 21314a9 commit 8668905

2 files changed

Lines changed: 105 additions & 3 deletions

File tree

tools/src/test/php/composer.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"minimum-stability": "stable",
33
"require": {
4-
"php": "^7.4 || ^8.0",
4+
"php": "^8.1",
55
"ext-json": "*",
66
"opis/json-schema": "2.3"
77
},
@@ -10,9 +10,18 @@
1010
},
1111
"scripts": {
1212
"test": [
13-
"@test:json-schema-lint"
13+
"@test:json-schema-lint",
14+
"@test:json-schema-functional"
1415
],
15-
"test:json-schema-lint": "@php -f json-schema-lint-tests.php --"
16+
"test:json-schema-lint": "@php -f json-schema-lint-tests.php --",
17+
"test:json-schema-functional": [
18+
"@test:json-schema-functional:1.4",
19+
"@test:json-schema-functional:1.3",
20+
"@test:json-schema-functional:1.2"
21+
],
22+
"test:json-schema-functional:1.4": "@php -f json-schema-functional-tests.php -- -v 1.4 --",
23+
"test:json-schema-functional:1.3": "@php -f json-schema-functional-tests.php -- -v 1.3 --",
24+
"test:json-schema-functional:1.2": "@php -f json-schema-functional-tests.php -- -v 1.2 --"
1625
},
1726
"scripts-descriptions": {
1827
"test": "run all tests",
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* validate all test data for a given version of CycloneDX.
5+
* call the script via `php -f <this-file> -- -v <CDX-version>`
6+
*/
7+
8+
use Opis\JsonSchema;
9+
10+
require_once __DIR__ . '/vendor/autoload.php';
11+
12+
// region config
13+
14+
define('TESTSCHEMA_VERSION', getopt('v:')['v']);
15+
define('SCHEMA_DIR', realpath(__DIR__ . '/../../../../schema'));
16+
define('SCHEMA_FILE', SCHEMA_DIR . '/bom-' . TESTSCHEMA_VERSION . '.schema.json');
17+
define('TESTDATA_DIR', realpath(__DIR__ . '/../resources/' . TESTSCHEMA_VERSION));
18+
19+
if (empty(TESTSCHEMA_VERSION)) {
20+
throw new Exception('missing TESTSCHEMA_VERSION. expected via opt "-v"');
21+
}
22+
fwrite(STDOUT, 'DEBUG | TESTSCHEMA_VERSION = ' . TESTSCHEMA_VERSION . PHP_EOL);
23+
24+
if (!is_file(SCHEMA_FILE)) {
25+
throw new Exception('missing SCHEMA_FILE: ' . SCHEMA_FILE);
26+
}
27+
fwrite(STDOUT, 'DEBUG | SCHEMA_FILE = ' . SCHEMA_FILE . PHP_EOL);
28+
29+
if (!is_dir(TESTDATA_DIR)) {
30+
throw new Exception('missing TESTDATA_DIR: ' . TESTDATA_DIR);
31+
}
32+
fwrite(STDOUT, 'DEBUG | TESTDATA_DIR = ' . TESTDATA_DIR . PHP_EOL);
33+
34+
// endregion config
35+
36+
// region validator
37+
38+
$schemaId = uniqid('validate:cdx-test?f=' . SCHEMA_FILE . '&r=', true);
39+
$resolver = new JsonSchema\Resolvers\SchemaResolver();
40+
$resolver->registerFile($schemaId, SCHEMA_FILE);
41+
$resolver->registerPrefix('http://cyclonedx.org/schema/', SCHEMA_DIR);
42+
$validator = new JsonSchema\Validator();
43+
$validator->setResolver($resolver);
44+
$errorFormatter = new JsonSchema\Errors\ErrorFormatter();
45+
46+
/**
47+
* @param string $file file path to validate
48+
*/
49+
function validateFile(string $file): ?JsonSchema\Errors\ValidationError
50+
{
51+
global $validator, $schemaId;
52+
return $validator->validate(
53+
json_decode(file_get_contents($file), false, 1024, \JSON_THROW_ON_ERROR),
54+
$schemaId
55+
)->error();
56+
}
57+
58+
// endregion validator
59+
60+
$errCnt = 0;
61+
62+
foreach (glob(TESTDATA_DIR . '/valid-*.json') as $file) {
63+
fwrite(STDOUT, PHP_EOL . "test $file ..." . PHP_EOL);
64+
$validationError = validateFile($file);
65+
if ($validationError === null) {
66+
fwrite(STDOUT, 'OK.' . PHP_EOL);
67+
} else {
68+
++$errCnt;
69+
fwrite(STDERR, "ERROR: Unexpected validation error for file: $file" . PHP_EOL);
70+
fwrite(STDERR, json_encode(
71+
$errorFormatter->format($validationError),
72+
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
73+
) . PHP_EOL);
74+
}
75+
unset($validationError);
76+
}
77+
78+
foreach (glob(TESTDATA_DIR . '/invalid-*.json') as $file) {
79+
fwrite(STDOUT, PHP_EOL . "test $file ..." . PHP_EOL);
80+
$validationError = validateFile($file);
81+
if ($validationError === null) {
82+
++$errCnt;
83+
fwrite(STDERR, "ERROR: Missing expected validation error for file: $file" . PHP_EOL);
84+
} else {
85+
fwrite(STDOUT, 'OK.' . PHP_EOL);
86+
}
87+
unset($validationError);
88+
}
89+
90+
91+
// Exit statuses should be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used.
92+
// The status 0 is used to terminate the program successfully.
93+
exit(min($errCnt, 254));

0 commit comments

Comments
 (0)