Skip to content

Commit 82bf9e3

Browse files
Merge pull request #110 from jkowalleck/gh-workflow_php-ci
2 parents b8383da + 6623705 commit 82bf9e3

File tree

4 files changed

+71
-37
lines changed

4 files changed

+71
-37
lines changed

.github/workflows/php.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# docs: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions
2+
3+
name: PHP CI
4+
5+
on: [push, pull_request, workflow_dispatch]
6+
7+
defaults:
8+
run:
9+
working-directory: tools/src/test/php
10+
11+
jobs:
12+
test:
13+
timeout-minutes: 30
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout
17+
# see https://github.com/actions/checkout
18+
uses: actions/checkout@v2
19+
- name: Setup PHP
20+
# see https://github.com/shivammathur/setup-php
21+
uses: shivammathur/setup-php@v2
22+
with:
23+
php-version: "8.1"
24+
tools: composer:v2
25+
- name: Install Depenencies
26+
run: composer install
27+
- name: Run test
28+
run: composer run test

tools/src/test/php/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ for validation of a schema
55

66
## requirements
77

8-
* `php:^7.4`
8+
* php >= 7.4
99
* php composer
1010

1111
## setup

tools/src/test/php/composer.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
{
22
"minimum-stability": "stable",
33
"require": {
4-
"php": "^7.4|^8.0",
5-
"opis/json-schema": "^2.2"
4+
"php": "^7.4 || ^8.0",
5+
"ext-json": "*",
6+
"opis/json-schema": "2.3"
67
},
78
"require-dev": {
89
"roave/security-advisories": "dev-latest"
910
},
1011
"scripts": {
11-
"test": "@php -f json-schema-lint-tests.php"
12+
"test": [
13+
"@test:json-schema-lint"
14+
],
15+
"test:json-schema-lint": "@php -f json-schema-lint-tests.php --"
1216
},
1317
"scripts-descriptions": {
14-
"test": "run tests"
18+
"test": "run all tests",
19+
"test:json-schema-lint": "lint JSON schema."
1520
}
1621
}
Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,58 @@
11
<?php declare(strict_types=1);
22

3-
const SchemaDir = __DIR__.'/../../../../schema/';
4-
5-
const SchemasFiles = [
6-
SchemaDir.'/bom-1.2.schema.json',
7-
SchemaDir.'/bom-1.2-strict.schema.json',
8-
SchemaDir.'/bom-1.3.schema.json',
9-
SchemaDir.'/bom-1.3-strict.schema.json',
10-
SchemaDir.'/bom-1.4-SNAPSHOT.schema.json',
11-
SchemaDir.'/bom-1.4.schema.json',
12-
SchemaDir.'/bom-1.4-strict.schema.json',
13-
SchemaDir.'/jsf-0.82.schema.json',
14-
SchemaDir.'/spdx.schema.json',
15-
];
3+
// region config
4+
5+
const SchemaFileGlob = '*.schema.json';
6+
const SchemaDir = __DIR__.'/../../../../schema/';
7+
8+
// endregion config
9+
10+
// region pre-check
11+
12+
if (!is_dir(SchemaDir)) {
13+
throw new RuntimeException('No such dir: '.SchemaDir);
14+
}
15+
16+
$schemasFiles = glob(SchemaDir.SchemaFileGlob, GLOB_ERR);
17+
if (empty($schemasFiles)) {
18+
throw new RuntimeException('No schema files found');
19+
}
20+
21+
// region pre-check
1622

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

1925
$errCnt = 0;
20-
$schemaLoader = new Opis\JsonSchema\SchemaLoader();
2126

22-
foreach (SchemasFiles as $schemasFile) {
23-
$schemasFile = realpath($schemasFile);
24-
if (!$schemasFile) {
25-
// skip
26-
continue;
27-
}
28-
echo PHP_EOL, "Schema: $schemasFile", PHP_EOL;
27+
foreach ($schemasFiles as $schemasFilePath) {
28+
echo PHP_EOL, "SchemaFile: $schemasFilePath", PHP_EOL;
2929

3030
try {
31-
$schema = json_decode(file_get_contents($schemasFile), false, 512, JSON_THROW_ON_ERROR);
31+
$schema = json_decode(file_get_contents($schemasFilePath), false, 512, JSON_THROW_ON_ERROR);
3232
if (!is_object($schema)) {
33-
throw new DomainException("decode to a non-object");
33+
throw new DomainException('decode to a non-object');
3434
}
35-
}
36-
catch (Exception $exception)
37-
{
35+
} catch (Exception $exception) {
3836
++$errCnt;
39-
echo "JSON DECODE ERROR: ", $exception->getMessage(), PHP_EOL;
37+
echo 'JSON DECODE ERROR: ', $exception->getMessage(), PHP_EOL;
4038
continue;
4139
}
4240

4341
try {
44-
$schemaLoader->loadObjectSchema($schema);
45-
echo "OK.", PHP_EOL;
42+
// run on individual instances, so no pollution or artifacts can distort tests
43+
(new Opis\JsonSchema\SchemaLoader())->loadObjectSchema($schema);
44+
echo 'OK.', PHP_EOL;
4645
} catch (Opis\JsonSchema\Exceptions\SchemaException $exception) {
4746
++$errCnt;
48-
echo "SCHEMA ERROR: ", $exception->getMessage(), PHP_EOL;
47+
echo 'SCHEMA ERROR: ', $exception->getMessage(), PHP_EOL;
4948
continue;
5049
} catch (Exception $exception) {
5150
++$errCnt;
52-
echo "UNEXPECTED ERROR:", $exception->getMessage(), PHP_EOL;
51+
echo 'UNEXPECTED ERROR: ', $exception->getMessage(), PHP_EOL;
5352
continue;
5453
}
5554
}
5655

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

0 commit comments

Comments
 (0)