From c2c841b68bb121e8b0a603e617d5d867f2e6281d Mon Sep 17 00:00:00 2001 From: J-T-McC Date: Sun, 23 May 2021 13:35:10 -0700 Subject: [PATCH 1/5] PHP 8 Support - Updated composer requirements - Removed deprecated method - Added test for missing coverage - Updated types throughout - Updated phpunitxml to support new version --- composer.json | 7 +++--- phpunit.xml.dist | 25 +++++++++----------- src/Assert.php | 44 +++++++++++----------------------- src/Extension/Symfony.php | 10 ++++---- tests/AssertTraitImpl.php | 8 +++---- tests/AssertTraitTest.php | 50 ++++++++++++++++++++------------------- tests/Utils.php | 4 ++-- 7 files changed, 66 insertions(+), 82 deletions(-) diff --git a/composer.json b/composer.json index 00700cc..b1d7153 100644 --- a/composer.json +++ b/composer.json @@ -13,12 +13,13 @@ ], "minimum-stability": "stable", "require": { - "php": "^7.0", + "php": "^7.4|^8.0", "justinrainbow/json-schema": "^5.0", - "mtdowling/jmespath.php": "^2.3" + "mtdowling/jmespath.php": "^2.3", + "ext-json": "*" }, "require-dev": { - "phpunit/phpunit": "^6", + "phpunit/phpunit": "^9", "codacy/coverage": "dev-master", "symfony/http-foundation": "^2.8|^3.0" }, diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 14cbb33..46936b3 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,30 +1,27 @@ - + bootstrap="./vendor/autoload.php"> tests/ - - + + src/ - - + + + + + + - - - + - diff --git a/src/Assert.php b/src/Assert.php index aa0aca2..49bc458 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -11,6 +11,7 @@ namespace EnricoStahn\JsonAssert; +use JmesPath\Env; use JsonSchema\Constraints\Factory; use JsonSchema\SchemaStorage; use JsonSchema\Validator; @@ -26,9 +27,9 @@ trait Assert { /** - * @var SchemaStorage + * @var ?SchemaStorage */ - private static $schemaStorage = null; + private static ?SchemaStorage $schemaStorage = null; /** * Asserts that json content is valid according to the provided schema file. @@ -37,10 +38,10 @@ trait Assert * * static::assertJsonMatchesSchema(json_decode('{"foo":1}'), './schema.json') * - * @param string|null $schema Path to the schema file * @param array|object $content JSON array or object + * @param ?string $schema Path to the schema file */ - public static function assertJsonMatchesSchema($content, $schema = null) + public static function assertJsonMatchesSchema($content, ?string $schema = null): void { if (self::$schemaStorage === null) { self::$schemaStorage = new SchemaStorage(); @@ -60,7 +61,7 @@ public static function assertJsonMatchesSchema($content, $schema = null) $validator = new Validator(new Factory(self::$schemaStorage)); $validator->validate($content, $schemaObject); - $message = '- Property: %s, Contraint: %s, Message: %s'; + $message = '- Property: %s, Constraint: %s, Message: %s'; $messages = array_map(function ($exception) use ($message) { return sprintf($message, $exception['property'], $exception['constraint'], $exception['message']); }, $validator->getErrors()); @@ -69,30 +70,13 @@ public static function assertJsonMatchesSchema($content, $schema = null) \PHPUnit\Framework\Assert::assertTrue($validator->isValid(), implode("\n", $messages)); } - /** - * Asserts that json content is valid according to the provided schema file. - * - * Example: - * - * static::assertJsonMatchesSchema(json_decode('{"foo":1}'), './schema.json') - * - * @param string|null $schema Path to the schema file - * @param array|object $content JSON array or object - * - * @deprecated This will be removed in the next major version (4.x). - */ - public static function assertJsonMatchesSchemaDepr($schema, $content) - { - self::assertJsonMatchesSchema($content, $schema); - } - /** * Asserts that json content is valid according to the provided schema string. * * @param string $schema Schema data * @param array|object $content JSON content */ - public static function assertJsonMatchesSchemaString($schema, $content) + public static function assertJsonMatchesSchemaString(string $schema, $content): void { $file = tempnam(sys_get_temp_dir(), 'json-schema-'); file_put_contents($file, $schema); @@ -107,17 +91,17 @@ public static function assertJsonMatchesSchemaString($schema, $content) * * static::assertJsonValueEquals(33, 'foo.bar[0]', $json); * - * @param mixed $expected Expected value - * @param string $expression Expression to retrieve the result - * (e.g. locations[?state == 'WA'].name | sort(@)) - * @param array|object $json JSON Content + * @param mixed $expected Expected value + * @param string $expression Expression to retrieve the result + * (e.g. locations[?state == 'WA'].name | sort(@)) + * @param array|object|string $json JSON Content */ - public static function assertJsonValueEquals($expected, $expression, $json) + public static function assertJsonValueEquals($expected, string $expression, $json): void { - $result = \JmesPath\Env::search($expression, $json); + $result = Env::search($expression, $json); \PHPUnit\Framework\Assert::assertEquals($expected, $result); - \PHPUnit\Framework\Assert::assertInternalType(strtolower(gettype($expected)), $result); + \PHPUnit\Framework\Assert::assertEquals(gettype($expected), gettype($result)); } /** diff --git a/src/Extension/Symfony.php b/src/Extension/Symfony.php index 530876a..45889ed 100644 --- a/src/Extension/Symfony.php +++ b/src/Extension/Symfony.php @@ -26,9 +26,9 @@ trait Symfony * @param string $schema Path to the schema file * @param Response $response JSON array or object */ - public static function assertJsonMatchesSchema($schema, Response $response) + public static function assertJsonMatchesSchema(string $schema, Response $response): void { - Assert::assertJsonMatchesSchemaDepr($schema, json_decode($response->getContent())); + Assert::assertJsonMatchesSchema(json_decode($response->getContent()), $schema); } /** @@ -37,7 +37,7 @@ public static function assertJsonMatchesSchema($schema, Response $response) * @param string $schema Schema data * @param Response $response JSON content */ - public static function assertJsonMatchesSchemaString($schema, Response $response) + public static function assertJsonMatchesSchemaString(string $schema, Response $response): void { Assert::assertJsonMatchesSchemaString($schema, json_decode($response->getContent())); } @@ -54,7 +54,7 @@ public static function assertJsonMatchesSchemaString($schema, Response $response * (e.g. locations[?state == 'WA'].name | sort(@)) * @param Response $response JSON Content */ - public static function assertJsonValueEquals($expected, $expression, $response) + public static function assertJsonValueEquals($expected, string $expression, Response $response): void { Assert::assertJsonValueEquals($expected, $expression, json_decode($response->getContent())); } @@ -67,7 +67,7 @@ public static function assertJsonValueEquals($expected, $expression, $response) * * @see \Bazinga\Bundle\RestExtraBundle\Test\WebTestCase::assertJsonResponse() */ - public static function assertJsonResponse(Response $response, $statusCode = 200) + public static function assertJsonResponse(Response $response, int $statusCode = 200): void { \PHPUnit\Framework\Assert::assertEquals( $statusCode, diff --git a/tests/AssertTraitImpl.php b/tests/AssertTraitImpl.php index 17202e7..7056e9b 100644 --- a/tests/AssertTraitImpl.php +++ b/tests/AssertTraitImpl.php @@ -19,18 +19,18 @@ class AssertTraitImpl extends TestCase { use JsonAssert; - public function setUp() + public function setUp(): void { self::$schemaStorage = new SchemaStorage(); } /** - * @param string $id - * @param string $schema + * @param string $id + * @param array|object $schema * * @return SchemaStorage */ - public function testWithSchemaStore($id, $schema) + public function testWithSchemaStore(string $id, $schema): SchemaStorage { self::$schemaStorage->addSchema($id, $schema); diff --git a/tests/AssertTraitTest.php b/tests/AssertTraitTest.php index 3ca7721..b24df2c 100644 --- a/tests/AssertTraitTest.php +++ b/tests/AssertTraitTest.php @@ -13,6 +13,7 @@ use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException; class AssertTraitTest extends TestCase { @@ -25,24 +26,29 @@ public function testAssertJsonMatchesSchemaSimple() { $content = json_decode(file_get_contents(Utils::getJsonPath('assertJsonMatchesSchema_simple.json'))); - AssertTraitImpl::assertJsonMatchesSchemaDepr(Utils::getSchemaPath('assertJsonMatchesSchema_simple.schema.json'), $content); + AssertTraitImpl::assertJsonMatchesSchema($content, Utils::getSchemaPath('assertJsonMatchesSchema_simple.schema.json')); } public function testAssertJsonMatchesSchema() { $content = json_decode('{"foo":123}'); - AssertTraitImpl::assertJsonMatchesSchemaDepr(Utils::getSchemaPath('test.schema.json'), $content); + AssertTraitImpl::assertJsonMatchesSchema($content, Utils::getSchemaPath('test.schema.json')); } - /** - * @expectedException \PHPUnit\Framework\ExpectationFailedException - */ public function testAssertJsonMatchesSchemaFail() { + $this->expectException(ExpectationFailedException::class); + $content = json_decode('{"foo":"123"}'); + + AssertTraitImpl::assertJsonMatchesSchema($content, Utils::getSchemaPath('test.schema.json')); + } + + public function testAssertThrowsFileNotFoundException() { + $this->expectException(FileNotFoundException::class); $content = json_decode('{"foo":"123"}'); - AssertTraitImpl::assertJsonMatchesSchemaDepr(Utils::getSchemaPath('test.schema.json'), $content); + AssertTraitImpl::assertJsonMatchesSchema($content, 'not-found.json'); } public function testAssertJsonMatchesSchemaFailMessage() @@ -52,10 +58,10 @@ public function testAssertJsonMatchesSchemaFailMessage() $exception = null; try { - AssertTraitImpl::assertJsonMatchesSchemaDepr(Utils::getSchemaPath('test.schema.json'), $content); + AssertTraitImpl::assertJsonMatchesSchema($content, Utils::getSchemaPath('test.schema.json')); } catch (ExpectationFailedException $exception) { - self::assertContains('- Property: foo, Contraint: type, Message: String value found, but an integer is required', $exception->getMessage()); - self::assertContains('- Response: {"foo":"123"}', $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()); } self::assertInstanceOf('\PHPUnit\Framework\ExpectationFailedException', $exception); @@ -68,17 +74,15 @@ public function testAssertJsonMatchesSchemaWithRefs() { $content = json_decode('{"code":123, "message":"Nothing works."}'); - AssertTraitImpl::assertJsonMatchesSchemaDepr(Utils::getSchemaPath('error.schema.json'), $content); + AssertTraitImpl::assertJsonMatchesSchema($content, Utils::getSchemaPath('error.schema.json')); } - /** - * @expectedException \PHPUnit\Framework\ExpectationFailedException - */ public function testAssertJsonMatchesSchemaWithRefsFails() { + $this->expectException(ExpectationFailedException::class); $content = json_decode('{"code":"123", "message":"Nothing works."}'); - AssertTraitImpl::assertJsonMatchesSchemaDepr(Utils::getSchemaPath('error.schema.json'), $content); + AssertTraitImpl::assertJsonMatchesSchema($content, Utils::getSchemaPath('error.schema.json')); } public function testAssertJsonMatchesSchemaString() @@ -97,7 +101,7 @@ public function testAssertJsonMatchesSchemaString() * @param string $expression * @param mixed $value */ - public function testAssertJsonValueEquals($expression, $value) + public function testAssertJsonValueEquals(string $expression, $value) { $content = json_decode(file_get_contents(Utils::getJsonPath('testAssertJsonValueEquals.json'))); @@ -109,13 +113,13 @@ public function testAssertWithSchemaStore() $obj = new AssertTraitImpl(); $obj->setUp(); - $schemastore = $obj->testWithSchemaStore('foobar', (object) ['type' => 'string']); + $schemaStore = $obj->testWithSchemaStore('foobar', (object) ['type' => 'string']); - self::assertInstanceOf('JsonSchema\SchemaStorage', $schemastore); - self::assertEquals($schemastore->getSchema('foobar'), (object) ['type' => 'string']); + self::assertInstanceOf('JsonSchema\SchemaStorage', $schemaStore); + self::assertEquals($schemaStore->getSchema('foobar'), (object) ['type' => 'string']); } - public function assertJsonValueEqualsProvider() + public function assertJsonValueEqualsProvider(): array { return [ ['foo', '123'], @@ -123,14 +127,12 @@ public function assertJsonValueEqualsProvider() ]; } - /** - * @expectedException \PHPUnit\Framework\ExpectationFailedException - */ public function testAssertJsonValueEqualsFailsOnWrongDataType() { + $this->expectException(ExpectationFailedException::class); $content = json_decode(file_get_contents(Utils::getJsonPath('testAssertJsonValueEquals.json'))); - AssertTraitImpl::assertJsonValueEquals($content, 'a.b.c[0].d[1][0]', '1'); + AssertTraitImpl::assertJsonValueEquals($content, 'a.b.c[0].d[1][0]', '{}'); } /** @@ -141,7 +143,7 @@ public function testGetJsonObject($expected, $actual) self::assertEquals($expected, AssertTraitImpl::getJsonObject($actual)); } - public function jsonObjectProvider() + public function jsonObjectProvider(): array { return [ [[], []], diff --git a/tests/Utils.php b/tests/Utils.php index 0ad108c..17fe774 100644 --- a/tests/Utils.php +++ b/tests/Utils.php @@ -20,7 +20,7 @@ class Utils * * @return string */ - public static function getSchemaPath($filename) + public static function getSchemaPath(string $filename): string { return implode(DIRECTORY_SEPARATOR, [__DIR__, 'schemas', $filename]); } @@ -32,7 +32,7 @@ public static function getSchemaPath($filename) * * @return string */ - public static function getJsonPath($filename) + public static function getJsonPath(string $filename): string { return implode(DIRECTORY_SEPARATOR, [__DIR__, 'json', $filename]); } From 470b5298127aa758c9ea0aa1bb0f55f7201bcb9c Mon Sep 17 00:00:00 2001 From: J-T-McC Date: Sun, 23 May 2021 13:44:41 -0700 Subject: [PATCH 2/5] Corrected method formatting --- tests/AssertTraitTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/AssertTraitTest.php b/tests/AssertTraitTest.php index b24df2c..8885cd2 100644 --- a/tests/AssertTraitTest.php +++ b/tests/AssertTraitTest.php @@ -44,7 +44,8 @@ public function testAssertJsonMatchesSchemaFail() AssertTraitImpl::assertJsonMatchesSchema($content, Utils::getSchemaPath('test.schema.json')); } - public function testAssertThrowsFileNotFoundException() { + public function testAssertThrowsFileNotFoundException() + { $this->expectException(FileNotFoundException::class); $content = json_decode('{"foo":"123"}'); From 08742b871ff1f8f55a06cc9b3ea3812157fd0dfd Mon Sep 17 00:00:00 2001 From: J-T-McC Date: Mon, 21 Jun 2021 22:39:09 -0700 Subject: [PATCH 3/5] Updated php versions in build action --- .github/workflows/build.yml | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5fe9c86..79d3473 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -51,22 +51,13 @@ jobs: strategy: matrix: php: - - 7.1 - # - 7.2 - # - 7.3 - # - 7.4 - # - 8.0 + - 7.4 + - 8.0 include: - - php: 7.1 - phpunit: 7.5.20 - # - php: 7.2 - # phpunit: 8.5.13 - # - php: 7.3 - # phpunit: 9.5.0 - # - php: 7.4 - # phpunit: 9.5.0 - # - php: 8.0 - # phpunit: 9.5.0 + - php: 7.4 + phpunit: 9.5.0 + - php: 8.0 + phpunit: 9.5.0 steps: - uses: actions/checkout@v2 From 740d53a8e63a911cf6b66f741f7166a94158ffc3 Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Tue, 22 Jun 2021 16:39:31 +1000 Subject: [PATCH 4/5] chore: bump php-actions/phpunit to v9 --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 79d3473..e47d708 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -72,7 +72,7 @@ jobs: with: php_version: ${{ matrix.php }} - - uses: php-actions/phpunit@v2 + - uses: php-actions/phpunit@v9 with: php_version: ${{ matrix.php }} version: ${{ matrix.phpunit }} From 15cbe375e6b4c3ac812678994bfbe051085be3f0 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 5 Oct 2022 14:18:39 +0000 Subject: [PATCH 5/5] Bump actions/checkout from 2 to 3.1.0 Bumps [actions/checkout](https://github.com/actions/checkout) from 2 to 3.1.0. - [Release notes](https://github.com/actions/checkout/releases) - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md) - [Commits](https://github.com/actions/checkout/compare/v2...v3.1.0) --- updated-dependencies: - dependency-name: actions/checkout dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] --- .github/workflows/build.yml | 6 +++--- .github/workflows/release.yml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e47d708..e06963c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -5,7 +5,7 @@ jobs: psalm: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3.1.0 - name: Psalm uses: docker://vimeo/psalm-github-actions @@ -21,7 +21,7 @@ jobs: # codecov: # runs-on: ubuntu-latest # steps: - # - uses: actions/checkout@v2 + # - uses: actions/checkout@v3.1.0 # with: # fetch-depth: 0 @@ -60,7 +60,7 @@ jobs: phpunit: 9.5.0 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3.1.0 - name: Cache Composer dependencies uses: actions/cache@v2 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ddc702b..a5697db 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,7 +12,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3.1.0 - name: git/unshallow run: git fetch --prune --unshallow