Skip to content

Commit

Permalink
Add support for Draft 6
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
J-T-McC committed Aug 14, 2024
1 parent 54fb7c9 commit 032bdb6
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 40 deletions.
26 changes: 16 additions & 10 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: Test
on: [pull_request]
on: [push, pull_request]

jobs:
psalm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Psalm
uses: docker://vimeo/psalm-github-actions
Expand Down Expand Up @@ -51,16 +51,16 @@ jobs:
strategy:
matrix:
php:
- 7.4
- 8.0
- 8.2
- 8.3
include:
- php: 7.4
phpunit: 9.5.0
- php: 8.0
phpunit: 9.5.0
- php: 8.2
phpunit: 10
- php: 8.3
phpunit: 10

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Cache Composer dependencies
uses: actions/cache@v2
Expand All @@ -72,7 +72,13 @@ jobs:
with:
php_version: ${{ matrix.php }}

- uses: php-actions/phpunit@v9
- uses: php-actions/phpunit@v3
with:
php_version: ${{ matrix.php }}
version: ${{ matrix.phpunit }}
php_extensions: xdebug
coverage_text: true
bootstrap: vendor/autoload.php
args: --coverage-filter src tests
env:
XDEBUG_MODE: coverage
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class MyTestCase extends \PHPUnit_Framework_TestCase

### Class

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

```php
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
"minimum-stability": "stable",
"require": {
"php": "^7.4|^8.0",
"justinrainbow/json-schema": "^5.0",
"justinrainbow/json-schema": "^6.0",
"mtdowling/jmespath.php": "^2.3",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^9",
"phpunit/phpunit": "^10",
"codacy/coverage": "dev-master",
"symfony/http-foundation": "^2.8|^3.0|^5.0"
},
Expand Down
36 changes: 16 additions & 20 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="./vendor/autoload.php">
<testsuites>
<testsuite name="phpunit-json-assertions Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>

<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
<report>
<clover outputFile="./build/logs/clover.xml"/>
</report>
</coverage>
<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">
<testsuites>
<testsuite name="phpunit-json-assertions Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>
<coverage>
<report>
<clover outputFile="./build/logs/clover.xml"/>
</report>
</coverage>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
2 changes: 1 addition & 1 deletion src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static function assertJsonMatchesSchema($content, ?string $schema = null)

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

Expand Down
2 changes: 1 addition & 1 deletion tests/AssertClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ class AssertClassTest extends TestCase
{
public function testClassInstance()
{
static::assertInstanceOf('EnricoStahn\JsonAssert\AssertClass', new AssertClass());
static::assertInstanceOf('EnricoStahn\JsonAssert\AssertClass', new AssertClass('AssertClassTest'));
}
}
166 changes: 161 additions & 5 deletions tests/AssertTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,164 @@ class AssertTraitTest extends TestCase
*
* @see https://github.com/estahn/phpunit-json-assertions/wiki/assertJsonMatchesSchema
*/
public function testAssertJsonMatchesSchemaSimpleDraft6()
{
$content = json_decode(file_get_contents(Utils::getJsonPath('assertJsonMatchesSchema_simple_draft6.json')));

AssertTraitImpl::assertJsonMatchesSchema(
$content,
Utils::getSchemaPath('assertJsonMatchesSchema_draft6.schema.json')
);
}

/**
* @testWith
* ["{\"created_at\": \"2016-01-01T12:00:00Z\"}", true]
* ["{\"created_at\": \"2016/01/01\"}", false]
*/
public function testAssertJsonMatchesSchemaDraft6DateTime($json, $pass)
{
if (!$pass) {
$this->expectException(ExpectationFailedException::class);
}

$content = json_decode($json);

AssertTraitImpl::assertJsonMatchesSchema(
$content,
Utils::getSchemaPath('assertJsonMatchesSchema_draft6.schema.json')
);
}

/**
* @testWith
* ["{\"status\": \"active\", \"created_at\": \"2016-01-01T12:00:00Z\"}", true]
* ["{\"status\": \"completed\", \"created_at\": \"2016-01-01T12:00:00Z\"}", true]
* ["{\"status\": \"deleted\", \"created_at\": \"2016-01-01T12:00:00Z\"}", false]
*/
public function testAssertJsonMatchesSchemaDraft6EnumAndNot($json, $pass)
{
if (!$pass) {
$this->expectException(ExpectationFailedException::class);
}

$content = json_decode($json);

AssertTraitImpl::assertJsonMatchesSchema(
$content,
Utils::getSchemaPath('assertJsonMatchesSchema_draft6.schema.json')
);
}

/**
* @testWith
* ["{\"status\": \"active\", \"created_at\": \"2016-01-01T12:00:00Z\"}", true]
* ["{\"status\": \"active\"}", false]
*/
public function testAssertJsonMatchesSchemaDraft6Dependency($json, $pass)
{
if (!$pass) {
$this->expectException(ExpectationFailedException::class);
}

$content = json_decode($json);

AssertTraitImpl::assertJsonMatchesSchema(
$content,
Utils::getSchemaPath('assertJsonMatchesSchema_draft6.schema.json')
);
}

/**
* @testWith
* ["{\"id\": 2}", true]
* ["{\"id\": 1}", false]
* ["{\"id\": 0}", false]
*/
public function testAssertJsonMatchesSchemaDraft6ExclusiveMinimum($json, $pass)
{
if (!$pass) {
$this->expectException(ExpectationFailedException::class);
}

$content = json_decode($json);

AssertTraitImpl::assertJsonMatchesSchema(
$content,
Utils::getSchemaPath('assertJsonMatchesSchema_draft6.schema.json')
);
}

/**
* @testWith
* ["{\"title\": \"A brief description\"}", true]
* ["{\"title\": \"A description that is too long\"}", false]
* ["{\"title\": \"A\"}", false]
*/
public function testAssertJsonMatchesSchemaDraft6MaxMinLength($json, $pass)
{
if (!$pass) {
$this->expectException(ExpectationFailedException::class);
}

$content = json_decode($json);

AssertTraitImpl::assertJsonMatchesSchema(
$content,
Utils::getSchemaPath('assertJsonMatchesSchema_draft6.schema.json')
);
}

/**
* @testWith
* ["{\"invalid_name\": \"value\"}", false]
*/
public function testAssertJsonMatchesSchemaDraft6AdditionalProperties($json, $pass)
{
if (!$pass) {
$this->expectException(ExpectationFailedException::class);
}

$content = json_decode($json);

AssertTraitImpl::assertJsonMatchesSchema(
$content,
Utils::getSchemaPath('assertJsonMatchesSchema_draft6.schema.json')
);
}

/**
* @testWith
* ["{\"status\": \"completed\", \"completed_at\": \"2020-01-01T12:00:00Z\", \"created_at\": \"2020-01-01T12:00:00Z\"}", true]
* ["{\"status\": \"completed\", \"created_at\": \"2020-01-01T12:00:00Z\"}", false]
* ["{\"status\": \"pending\", \"expected_completion\": \"2020-01-01T12:00:00Z\", \"created_at\": \"2020-01-01T12:00:00Z\"}", true]
* ["{\"status\": \"pending\", \"created_at\": \"2020-01-01T12:00:00Z\"}", false]
* ["{\"status\": \"active\", \"created_at\": \"2020-01-01T12:00:00Z\"}", true]
*/
public function testAssertJsonMatchesSchemaDraft6Conditional($json, $pass)
{
$this->markTestSkipped('Conditional validation is not supported by the current implementation.');

if (!$pass) {
$this->expectException(ExpectationFailedException::class);
}

$content = json_decode($json);

AssertTraitImpl::assertJsonMatchesSchema(
$content,
Utils::getSchemaPath('assertJsonMatchesSchema_draft6.schema.json')
);
}

public function testAssertJsonMatchesSchemaSimple()
{
$content = json_decode(file_get_contents(Utils::getJsonPath('assertJsonMatchesSchema_simple.json')));

AssertTraitImpl::assertJsonMatchesSchema($content, Utils::getSchemaPath('assertJsonMatchesSchema_simple.schema.json'));
AssertTraitImpl::assertJsonMatchesSchema(
$content,
Utils::getSchemaPath('assertJsonMatchesSchema_simple.schema.json')
);
}

public function testAssertJsonMatchesSchema()
Expand Down Expand Up @@ -61,7 +214,10 @@ public function testAssertJsonMatchesSchemaFailMessage()
try {
AssertTraitImpl::assertJsonMatchesSchema($content, Utils::getSchemaPath('test.schema.json'));
} catch (ExpectationFailedException $exception) {
self::assertStringContainsString('- Property: foo, Constraint: type, Message: String value found, but an integer is required', $exception->getMessage());
self::assertStringContainsString(
'- Property: foo, Constraint: type, Message: String value found, but an integer is required',
$exception->getMessage()
);
self::assertStringContainsString('- Response: {"foo":"123"}', $exception->getMessage());
}

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

public function testAssertWithSchemaStore()
{
$obj = new AssertTraitImpl();
$obj = new AssertTraitImpl('testAssertWithSchemaStore');
$obj->setUp();

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

public function assertJsonValueEqualsProvider(): array
public static function assertJsonValueEqualsProvider(): array
{
return [
['foo', '123'],
Expand All @@ -144,7 +300,7 @@ public function testGetJsonObject($expected, $actual)
self::assertEquals($expected, AssertTraitImpl::getJsonObject($actual));
}

public function jsonObjectProvider(): array
public static function jsonObjectProvider(): array
{
return [
[[], []],
Expand Down
11 changes: 11 additions & 0 deletions tests/json/assertJsonMatchesSchema_simple_draft6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": 33,
"title": "Sample Title",
"status": "active",
"created_at": "2009-03-24T16:24:32Z",
"tags": [
"foo",
"bar"
],
"meta_description": "A brief description."
}
Loading

0 comments on commit 032bdb6

Please sign in to comment.