Skip to content

Commit

Permalink
JSON validate VS test data - php
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Kowalleck <[email protected]>
  • Loading branch information
jkowalleck committed Jun 13, 2023
1 parent 21314a9 commit 8668905
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 3 deletions.
15 changes: 12 additions & 3 deletions tools/src/test/php/composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"minimum-stability": "stable",
"require": {
"php": "^7.4 || ^8.0",
"php": "^8.1",
"ext-json": "*",
"opis/json-schema": "2.3"
},
Expand All @@ -10,9 +10,18 @@
},
"scripts": {
"test": [
"@test:json-schema-lint"
"@test:json-schema-lint",
"@test:json-schema-functional"
],
"test:json-schema-lint": "@php -f json-schema-lint-tests.php --"
"test:json-schema-lint": "@php -f json-schema-lint-tests.php --",
"test:json-schema-functional": [
"@test:json-schema-functional:1.4",
"@test:json-schema-functional:1.3",
"@test:json-schema-functional:1.2"
],
"test:json-schema-functional:1.4": "@php -f json-schema-functional-tests.php -- -v 1.4 --",
"test:json-schema-functional:1.3": "@php -f json-schema-functional-tests.php -- -v 1.3 --",
"test:json-schema-functional:1.2": "@php -f json-schema-functional-tests.php -- -v 1.2 --"
},
"scripts-descriptions": {
"test": "run all tests",
Expand Down
93 changes: 93 additions & 0 deletions tools/src/test/php/json-schema-functional-tests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php declare(strict_types=1);

/**
* validate all test data for a given version of CycloneDX.
* call the script via `php -f <this-file> -- -v <CDX-version>`
*/

use Opis\JsonSchema;

require_once __DIR__ . '/vendor/autoload.php';

// region config

define('TESTSCHEMA_VERSION', getopt('v:')['v']);
define('SCHEMA_DIR', realpath(__DIR__ . '/../../../../schema'));
define('SCHEMA_FILE', SCHEMA_DIR . '/bom-' . TESTSCHEMA_VERSION . '.schema.json');
define('TESTDATA_DIR', realpath(__DIR__ . '/../resources/' . TESTSCHEMA_VERSION));

if (empty(TESTSCHEMA_VERSION)) {
throw new Exception('missing TESTSCHEMA_VERSION. expected via opt "-v"');
}
fwrite(STDOUT, 'DEBUG | TESTSCHEMA_VERSION = ' . TESTSCHEMA_VERSION . PHP_EOL);

if (!is_file(SCHEMA_FILE)) {
throw new Exception('missing SCHEMA_FILE: ' . SCHEMA_FILE);
}
fwrite(STDOUT, 'DEBUG | SCHEMA_FILE = ' . SCHEMA_FILE . PHP_EOL);

if (!is_dir(TESTDATA_DIR)) {
throw new Exception('missing TESTDATA_DIR: ' . TESTDATA_DIR);
}
fwrite(STDOUT, 'DEBUG | TESTDATA_DIR = ' . TESTDATA_DIR . PHP_EOL);

// endregion config

// region validator

$schemaId = uniqid('validate:cdx-test?f=' . SCHEMA_FILE . '&r=', true);
$resolver = new JsonSchema\Resolvers\SchemaResolver();
$resolver->registerFile($schemaId, SCHEMA_FILE);
$resolver->registerPrefix('http://cyclonedx.org/schema/', SCHEMA_DIR);
$validator = new JsonSchema\Validator();
$validator->setResolver($resolver);
$errorFormatter = new JsonSchema\Errors\ErrorFormatter();

/**
* @param string $file file path to validate
*/
function validateFile(string $file): ?JsonSchema\Errors\ValidationError
{
global $validator, $schemaId;
return $validator->validate(
json_decode(file_get_contents($file), false, 1024, \JSON_THROW_ON_ERROR),
$schemaId
)->error();
}

// endregion validator

$errCnt = 0;

foreach (glob(TESTDATA_DIR . '/valid-*.json') as $file) {
fwrite(STDOUT, PHP_EOL . "test $file ..." . PHP_EOL);
$validationError = validateFile($file);
if ($validationError === null) {
fwrite(STDOUT, 'OK.' . PHP_EOL);
} else {
++$errCnt;
fwrite(STDERR, "ERROR: Unexpected validation error for file: $file" . PHP_EOL);
fwrite(STDERR, json_encode(
$errorFormatter->format($validationError),
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
) . PHP_EOL);
}
unset($validationError);
}

foreach (glob(TESTDATA_DIR . '/invalid-*.json') as $file) {
fwrite(STDOUT, PHP_EOL . "test $file ..." . PHP_EOL);
$validationError = validateFile($file);
if ($validationError === null) {
++$errCnt;
fwrite(STDERR, "ERROR: Missing expected validation error for file: $file" . PHP_EOL);
} else {
fwrite(STDOUT, 'OK.' . PHP_EOL);
}
unset($validationError);
}


// Exit statuses should be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used.
// The status 0 is used to terminate the program successfully.
exit(min($errCnt, 254));

0 comments on commit 8668905

Please sign in to comment.