Skip to content

Commit b9d0e8f

Browse files
committed
add json schema
1 parent d91a336 commit b9d0e8f

File tree

5 files changed

+210
-15
lines changed

5 files changed

+210
-15
lines changed

Resources/errors.schema.json

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"$id": "https://github.com/Roave/BackwardCompatibilityCheck/tree/8.10.x/Resources/errors.schema.json",
4+
"title": "Backward Compatibility Errors",
5+
"description": "A list of errors",
6+
"required": [
7+
"errors"
8+
],
9+
"additionalProperties": false,
10+
"properties": {
11+
"errors": {
12+
"type": "array",
13+
"items": {
14+
"$ref": "#/definitions/Error"
15+
}
16+
}
17+
},
18+
"definitions": {
19+
"Error": {
20+
"type": "object",
21+
"additionalProperties": false,
22+
"properties": {
23+
"description": {
24+
"type": "string"
25+
},
26+
"path": {
27+
"anyOf": [
28+
{
29+
"type": "null"
30+
},
31+
{
32+
"type": "string"
33+
}
34+
]
35+
},
36+
"line": {
37+
"anyOf": [
38+
{
39+
"type": "integer"
40+
},
41+
{
42+
"type": "null"
43+
}
44+
]
45+
},
46+
"column": {
47+
"anyOf": [
48+
{
49+
"type": "integer"
50+
},
51+
{
52+
"type": "null"
53+
}
54+
]
55+
}
56+
},
57+
"required": [
58+
"column",
59+
"description",
60+
"line",
61+
"path"
62+
],
63+
"title": "Error"
64+
}
65+
}
66+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
],
2525
"require-dev": {
2626
"doctrine/coding-standard": "^12.0.0",
27+
"estahn/phpunit-json-assertions": "^4.0",
2728
"php-standard-library/psalm-plugin": "^2.3.0",
2829
"phpunit/phpunit": "^9.6.19",
2930
"psalm/plugin-phpunit": "^0.19.0",

composer.lock

Lines changed: 128 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Formatter/JsonFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Roave\BackwardCompatibility\Changes;
99
use Roave\BackwardCompatibility\Git\CheckedOutRepository;
1010
use Symfony\Component\Console\Output\OutputInterface;
11-
11+
use function json_encode;
1212

1313
/** @internal */
1414
final class JsonFormatter implements OutputFormatter

test/unit/Formatter/JsonFormatterTest.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace RoaveTest\BackwardCompatibility\Formatter;
66

7+
use EnricoStahn\JsonAssert\AssertClass as JsonAssert;
78
use PHPUnit\Framework\TestCase;
89
use Psl\Env;
910
use Psl\Filesystem;
@@ -13,7 +14,6 @@
1314
use Roave\BackwardCompatibility\Formatter\JsonFormatter;
1415
use Roave\BackwardCompatibility\Git\CheckedOutRepository;
1516
use Symfony\Component\Console\Output\BufferedOutput;
16-
use function Psl\Filesystem;
1717

1818
/** @covers \Roave\BackwardCompatibility\Formatter\JsonFormatter */
1919
final class JsonFormatterTest extends TestCase
@@ -56,30 +56,32 @@ public function testWrite(): void
5656

5757
$expected = [
5858
'errors' => [
59-
['description' => 'foo', 'path'=>null, 'line'=>null, 'column'=>null],
60-
['description' => 'bar', 'path'=>null, 'line'=>null, 'column'=>null],
61-
['description' => 'baz', 'path' => 'baz-file.php', 'line'=>null, 'column'=>null],
62-
['description' => 'tab', 'path' => 'tab-file.php', 'line'=>5, 'column'=>null],
63-
['description' => 'taz', 'path' => 'taz-file.php', 'line'=>6, 'column'=>15],
64-
['description' => 'tar', 'path' => 'tar-file.php', 'line'=>-1, 'column'=>-1],
65-
['description' => 'file-in-checked-out-dir', 'path' => 'subpath/file-in-checked-out-dir.php', 'line'=>10, 'column'=>20],
59+
['description' => 'foo', 'path' => null, 'line' => null, 'column' => null],
60+
['description' => 'bar', 'path' => null, 'line' => null, 'column' => null],
61+
['description' => 'baz', 'path' => 'baz-file.php', 'line' => null, 'column' => null],
62+
['description' => 'tab', 'path' => 'tab-file.php', 'line' => 5, 'column' => null],
63+
['description' => 'taz', 'path' => 'taz-file.php', 'line' => 6, 'column' => 15],
64+
['description' => 'tar', 'path' => 'tar-file.php', 'line' => -1, 'column' => -1],
65+
['description' => 'file-in-checked-out-dir', 'path' => 'subpath/file-in-checked-out-dir.php', 'line' => 10, 'column' => 20],
6666
]
6767
];
6868

69-
$result = $output->fetch();
70-
self::assertJson($result);
69+
$json = $output->fetch();
70+
self::assertJson($json);
7171

72-
$data = json_decode(trim($result), true);
72+
$data = json_decode(trim($json), true);
7373
self::assertIsArray($data);
7474
self::assertEquals($expected, $data);
7575

76+
JsonAssert::assertJsonMatchesSchema($json, dirname(__DIR__, 3).'/Resources/errors.schema.json');
77+
7678
self::assertJsonStringEqualsJsonString(
7779
<<<'OUTPUT'
7880
{"errors":[{"description":"foo","path":null,"line":null,"column":null},{"description":"bar","path":null,"line":null,"column":null},{"description":"baz","path":"baz-file.php","line":null,"column":null},{"description":"tab","path":"tab-file.php","line":5,"column":null},{"description":"taz","path":"taz-file.php","line":6,"column":15},{"description":"tar","path":"tar-file.php","line":-1,"column":-1},{"description":"file-in-checked-out-dir","path":"subpath\/file-in-checked-out-dir.php","line":10,"column":20}]}
7981

8082
OUTPUT
8183
,
82-
$result,
84+
$json,
8385
);
8486
}
8587
}

0 commit comments

Comments
 (0)