Skip to content

Commit 032bdb6

Browse files
committed
Add support for Draft 6
- updates json library dependency version for draft 6 support - adds assertions for some draft 6 scenarios - upgrades phpunit to version 10 - fixes github build pipeline
1 parent 54fb7c9 commit 032bdb6

9 files changed

+290
-40
lines changed

.github/workflows/build.yml

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
name: Test
2-
on: [pull_request]
2+
on: [push, pull_request]
33

44
jobs:
55
psalm:
66
runs-on: ubuntu-latest
77
steps:
8-
- uses: actions/checkout@v2
8+
- uses: actions/checkout@v3
99

1010
- name: Psalm
1111
uses: docker://vimeo/psalm-github-actions
@@ -51,16 +51,16 @@ jobs:
5151
strategy:
5252
matrix:
5353
php:
54-
- 7.4
55-
- 8.0
54+
- 8.2
55+
- 8.3
5656
include:
57-
- php: 7.4
58-
phpunit: 9.5.0
59-
- php: 8.0
60-
phpunit: 9.5.0
57+
- php: 8.2
58+
phpunit: 10
59+
- php: 8.3
60+
phpunit: 10
6161

6262
steps:
63-
- uses: actions/checkout@v2
63+
- uses: actions/checkout@v3
6464

6565
- name: Cache Composer dependencies
6666
uses: actions/cache@v2
@@ -72,7 +72,13 @@ jobs:
7272
with:
7373
php_version: ${{ matrix.php }}
7474

75-
- uses: php-actions/phpunit@v9
75+
- uses: php-actions/phpunit@v3
7676
with:
7777
php_version: ${{ matrix.php }}
7878
version: ${{ matrix.phpunit }}
79+
php_extensions: xdebug
80+
coverage_text: true
81+
bootstrap: vendor/autoload.php
82+
args: --coverage-filter src tests
83+
env:
84+
XDEBUG_MODE: coverage

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class MyTestCase extends \PHPUnit_Framework_TestCase
8686

8787
### Class
8888

89-
In case you don't want to use the `trait` you can use the provided class wich extends from `\PHPUnit_Framework_TestCase`.
89+
In case you don't want to use the `trait` you can use the provided class which extends from `\PHPUnit_Framework_TestCase`.
9090
You can either extend your test case or use the static methods like below.
9191

9292
```php

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
"minimum-stability": "stable",
1515
"require": {
1616
"php": "^7.4|^8.0",
17-
"justinrainbow/json-schema": "^5.0",
17+
"justinrainbow/json-schema": "^6.0",
1818
"mtdowling/jmespath.php": "^2.3",
1919
"ext-json": "*"
2020
},
2121
"require-dev": {
22-
"phpunit/phpunit": "^9",
22+
"phpunit/phpunit": "^10",
2323
"codacy/coverage": "dev-master",
2424
"symfony/http-foundation": "^2.8|^3.0|^5.0"
2525
},

phpunit.xml.dist

+16-20
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
3-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
5-
backupGlobals="false"
6-
colors="true"
7-
bootstrap="./vendor/autoload.php">
8-
<testsuites>
9-
<testsuite name="phpunit-json-assertions Test Suite">
10-
<directory>tests/</directory>
11-
</testsuite>
12-
</testsuites>
13-
14-
<coverage>
15-
<include>
16-
<directory suffix=".php">src/</directory>
17-
</include>
18-
<report>
19-
<clover outputFile="./build/logs/clover.xml"/>
20-
</report>
21-
</coverage>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" backupGlobals="false" colors="true" bootstrap="./vendor/autoload.php">
3+
<testsuites>
4+
<testsuite name="phpunit-json-assertions Test Suite">
5+
<directory>tests/</directory>
6+
</testsuite>
7+
</testsuites>
8+
<coverage>
9+
<report>
10+
<clover outputFile="./build/logs/clover.xml"/>
11+
</report>
12+
</coverage>
13+
<source>
14+
<include>
15+
<directory suffix=".php">src/</directory>
16+
</include>
17+
</source>
2218
</phpunit>

src/Assert.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static function assertJsonMatchesSchema($content, ?string $schema = null)
6363

6464
$message = '- Property: %s, Constraint: %s, Message: %s';
6565
$messages = array_map(function ($exception) use ($message) {
66-
return sprintf($message, $exception['property'], $exception['constraint'], $exception['message']);
66+
return sprintf($message, $exception['property'], $exception['constraint']['name'], $exception['message']);
6767
}, $validator->getErrors());
6868
$messages[] = '- Response: '.json_encode($content);
6969

tests/AssertClassTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ class AssertClassTest extends TestCase
1818
{
1919
public function testClassInstance()
2020
{
21-
static::assertInstanceOf('EnricoStahn\JsonAssert\AssertClass', new AssertClass());
21+
static::assertInstanceOf('EnricoStahn\JsonAssert\AssertClass', new AssertClass('AssertClassTest'));
2222
}
2323
}

tests/AssertTraitTest.php

+161-5
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,164 @@ class AssertTraitTest extends TestCase
2222
*
2323
* @see https://github.com/estahn/phpunit-json-assertions/wiki/assertJsonMatchesSchema
2424
*/
25+
public function testAssertJsonMatchesSchemaSimpleDraft6()
26+
{
27+
$content = json_decode(file_get_contents(Utils::getJsonPath('assertJsonMatchesSchema_simple_draft6.json')));
28+
29+
AssertTraitImpl::assertJsonMatchesSchema(
30+
$content,
31+
Utils::getSchemaPath('assertJsonMatchesSchema_draft6.schema.json')
32+
);
33+
}
34+
35+
/**
36+
* @testWith
37+
* ["{\"created_at\": \"2016-01-01T12:00:00Z\"}", true]
38+
* ["{\"created_at\": \"2016/01/01\"}", false]
39+
*/
40+
public function testAssertJsonMatchesSchemaDraft6DateTime($json, $pass)
41+
{
42+
if (!$pass) {
43+
$this->expectException(ExpectationFailedException::class);
44+
}
45+
46+
$content = json_decode($json);
47+
48+
AssertTraitImpl::assertJsonMatchesSchema(
49+
$content,
50+
Utils::getSchemaPath('assertJsonMatchesSchema_draft6.schema.json')
51+
);
52+
}
53+
54+
/**
55+
* @testWith
56+
* ["{\"status\": \"active\", \"created_at\": \"2016-01-01T12:00:00Z\"}", true]
57+
* ["{\"status\": \"completed\", \"created_at\": \"2016-01-01T12:00:00Z\"}", true]
58+
* ["{\"status\": \"deleted\", \"created_at\": \"2016-01-01T12:00:00Z\"}", false]
59+
*/
60+
public function testAssertJsonMatchesSchemaDraft6EnumAndNot($json, $pass)
61+
{
62+
if (!$pass) {
63+
$this->expectException(ExpectationFailedException::class);
64+
}
65+
66+
$content = json_decode($json);
67+
68+
AssertTraitImpl::assertJsonMatchesSchema(
69+
$content,
70+
Utils::getSchemaPath('assertJsonMatchesSchema_draft6.schema.json')
71+
);
72+
}
73+
74+
/**
75+
* @testWith
76+
* ["{\"status\": \"active\", \"created_at\": \"2016-01-01T12:00:00Z\"}", true]
77+
* ["{\"status\": \"active\"}", false]
78+
*/
79+
public function testAssertJsonMatchesSchemaDraft6Dependency($json, $pass)
80+
{
81+
if (!$pass) {
82+
$this->expectException(ExpectationFailedException::class);
83+
}
84+
85+
$content = json_decode($json);
86+
87+
AssertTraitImpl::assertJsonMatchesSchema(
88+
$content,
89+
Utils::getSchemaPath('assertJsonMatchesSchema_draft6.schema.json')
90+
);
91+
}
92+
93+
/**
94+
* @testWith
95+
* ["{\"id\": 2}", true]
96+
* ["{\"id\": 1}", false]
97+
* ["{\"id\": 0}", false]
98+
*/
99+
public function testAssertJsonMatchesSchemaDraft6ExclusiveMinimum($json, $pass)
100+
{
101+
if (!$pass) {
102+
$this->expectException(ExpectationFailedException::class);
103+
}
104+
105+
$content = json_decode($json);
106+
107+
AssertTraitImpl::assertJsonMatchesSchema(
108+
$content,
109+
Utils::getSchemaPath('assertJsonMatchesSchema_draft6.schema.json')
110+
);
111+
}
112+
113+
/**
114+
* @testWith
115+
* ["{\"title\": \"A brief description\"}", true]
116+
* ["{\"title\": \"A description that is too long\"}", false]
117+
* ["{\"title\": \"A\"}", false]
118+
*/
119+
public function testAssertJsonMatchesSchemaDraft6MaxMinLength($json, $pass)
120+
{
121+
if (!$pass) {
122+
$this->expectException(ExpectationFailedException::class);
123+
}
124+
125+
$content = json_decode($json);
126+
127+
AssertTraitImpl::assertJsonMatchesSchema(
128+
$content,
129+
Utils::getSchemaPath('assertJsonMatchesSchema_draft6.schema.json')
130+
);
131+
}
132+
133+
/**
134+
* @testWith
135+
* ["{\"invalid_name\": \"value\"}", false]
136+
*/
137+
public function testAssertJsonMatchesSchemaDraft6AdditionalProperties($json, $pass)
138+
{
139+
if (!$pass) {
140+
$this->expectException(ExpectationFailedException::class);
141+
}
142+
143+
$content = json_decode($json);
144+
145+
AssertTraitImpl::assertJsonMatchesSchema(
146+
$content,
147+
Utils::getSchemaPath('assertJsonMatchesSchema_draft6.schema.json')
148+
);
149+
}
150+
151+
/**
152+
* @testWith
153+
* ["{\"status\": \"completed\", \"completed_at\": \"2020-01-01T12:00:00Z\", \"created_at\": \"2020-01-01T12:00:00Z\"}", true]
154+
* ["{\"status\": \"completed\", \"created_at\": \"2020-01-01T12:00:00Z\"}", false]
155+
* ["{\"status\": \"pending\", \"expected_completion\": \"2020-01-01T12:00:00Z\", \"created_at\": \"2020-01-01T12:00:00Z\"}", true]
156+
* ["{\"status\": \"pending\", \"created_at\": \"2020-01-01T12:00:00Z\"}", false]
157+
* ["{\"status\": \"active\", \"created_at\": \"2020-01-01T12:00:00Z\"}", true]
158+
*/
159+
public function testAssertJsonMatchesSchemaDraft6Conditional($json, $pass)
160+
{
161+
$this->markTestSkipped('Conditional validation is not supported by the current implementation.');
162+
163+
if (!$pass) {
164+
$this->expectException(ExpectationFailedException::class);
165+
}
166+
167+
$content = json_decode($json);
168+
169+
AssertTraitImpl::assertJsonMatchesSchema(
170+
$content,
171+
Utils::getSchemaPath('assertJsonMatchesSchema_draft6.schema.json')
172+
);
173+
}
174+
25175
public function testAssertJsonMatchesSchemaSimple()
26176
{
27177
$content = json_decode(file_get_contents(Utils::getJsonPath('assertJsonMatchesSchema_simple.json')));
28178

29-
AssertTraitImpl::assertJsonMatchesSchema($content, Utils::getSchemaPath('assertJsonMatchesSchema_simple.schema.json'));
179+
AssertTraitImpl::assertJsonMatchesSchema(
180+
$content,
181+
Utils::getSchemaPath('assertJsonMatchesSchema_simple.schema.json')
182+
);
30183
}
31184

32185
public function testAssertJsonMatchesSchema()
@@ -61,7 +214,10 @@ public function testAssertJsonMatchesSchemaFailMessage()
61214
try {
62215
AssertTraitImpl::assertJsonMatchesSchema($content, Utils::getSchemaPath('test.schema.json'));
63216
} catch (ExpectationFailedException $exception) {
64-
self::assertStringContainsString('- Property: foo, Constraint: type, Message: String value found, but an integer is required', $exception->getMessage());
217+
self::assertStringContainsString(
218+
'- Property: foo, Constraint: type, Message: String value found, but an integer is required',
219+
$exception->getMessage()
220+
);
65221
self::assertStringContainsString('- Response: {"foo":"123"}', $exception->getMessage());
66222
}
67223

@@ -111,7 +267,7 @@ public function testAssertJsonValueEquals(string $expression, $value)
111267

112268
public function testAssertWithSchemaStore()
113269
{
114-
$obj = new AssertTraitImpl();
270+
$obj = new AssertTraitImpl('testAssertWithSchemaStore');
115271
$obj->setUp();
116272

117273
$schemaStore = $obj->testWithSchemaStore('foobar', (object) ['type' => 'string']);
@@ -120,7 +276,7 @@ public function testAssertWithSchemaStore()
120276
self::assertEquals($schemaStore->getSchema('foobar'), (object) ['type' => 'string']);
121277
}
122278

123-
public function assertJsonValueEqualsProvider(): array
279+
public static function assertJsonValueEqualsProvider(): array
124280
{
125281
return [
126282
['foo', '123'],
@@ -144,7 +300,7 @@ public function testGetJsonObject($expected, $actual)
144300
self::assertEquals($expected, AssertTraitImpl::getJsonObject($actual));
145301
}
146302

147-
public function jsonObjectProvider(): array
303+
public static function jsonObjectProvider(): array
148304
{
149305
return [
150306
[[], []],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"id": 33,
3+
"title": "Sample Title",
4+
"status": "active",
5+
"created_at": "2009-03-24T16:24:32Z",
6+
"tags": [
7+
"foo",
8+
"bar"
9+
],
10+
"meta_description": "A brief description."
11+
}

0 commit comments

Comments
 (0)