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