From d3df5809c15169b877e858e9a947b6c2d8e3e068 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Sun, 30 Jan 2022 12:59:00 +0100 Subject: [PATCH] Enhancement: Use JsonPointer as provided by ergebnis/json-pointer --- CHANGELOG.md | 2 + infection.json | 4 +- src/Exception/CanNotResolve.php | 6 +- src/JsonPointer.php | 47 ------------- src/SchemaValidator.php | 13 ++-- src/ValidationError.php | 10 +-- test/Unit/Exception/CanNotResolveTest.php | 8 +-- test/Unit/JsonPointerTest.php | 59 ----------------- test/Unit/SchemaValidatorTest.php | 81 +++++------------------ test/Unit/ValidationErrorTest.php | 5 +- test/Unit/ValidationResultTest.php | 9 ++- 11 files changed, 43 insertions(+), 201 deletions(-) delete mode 100644 src/JsonPointer.php delete mode 100644 test/Unit/JsonPointerTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index f2626835..4655cf45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ For a full diff see [`2.0.0...3.0.0`][2.0.0...3.0.0]. - Required [`ergebnis/json-pointer`](https://github.com/ergebnis/json-pointer) ([#195]), by [@localheinz] - Started throwing an `Exception\CanNotResolve` exception instead of an `Exception\ResolvedToRootSchema` when the `JsonPointer` is not a valid URI fragment identifier representation of a JSON pointer ([#202]), by [@localheinz] +- Started using `Ergebnis\Json\Pointer\JsonPointer` instead of `Ergebnis\Json\SchemaValidator\JsonPointer` ([#200]), by [@localheinz] ### Removed @@ -82,6 +83,7 @@ For a full diff see [`dcd4cfb...1.0.0`][dcd4cfb...1.0.0]. [#169]: https://github.com/ergebnis/json-schema-validator/pull/169 [#172]: https://github.com/ergebnis/json-schema-validator/pull/172 [#195]: https://github.com/ergebnis/json-schema-validator/pull/195 +[#200]: https://github.com/ergebnis/json-schema-validator/pull/200 [#202]: https://github.com/ergebnis/json-schema-validator/pull/202 [#203]: https://github.com/ergebnis/json-schema-validator/pull/203 diff --git a/infection.json b/infection.json index a503bb93..7e7f79c7 100644 --- a/infection.json +++ b/infection.json @@ -3,8 +3,8 @@ "logs": { "text": ".build/infection/infection-log.txt" }, - "minCoveredMsi": 90, - "minMsi": 88, + "minCoveredMsi": 88, + "minMsi": 86, "phpUnit": { "configDir": "test\/Unit" }, diff --git a/src/Exception/CanNotResolve.php b/src/Exception/CanNotResolve.php index 72541734..6c982db1 100644 --- a/src/Exception/CanNotResolve.php +++ b/src/Exception/CanNotResolve.php @@ -13,15 +13,15 @@ namespace Ergebnis\Json\SchemaValidator\Exception; -use Ergebnis\Json\SchemaValidator; +use Ergebnis\Json\Pointer; final class CanNotResolve extends \InvalidArgumentException { - public static function jsonPointer(SchemaValidator\JsonPointer $jsonPointer): self + public static function jsonPointer(Pointer\JsonPointer $jsonPointer): self { return new self(\sprintf( 'Can not resolve JSON pointer "%s".', - $jsonPointer->toString(), + $jsonPointer->toJsonString(), )); } } diff --git a/src/JsonPointer.php b/src/JsonPointer.php deleted file mode 100644 index d69667b5..00000000 --- a/src/JsonPointer.php +++ /dev/null @@ -1,47 +0,0 @@ -value = $value; - } - - public static function fromString(string $value): self - { - return new self($value); - } - - public static function empty(): self - { - return new self(''); - } - - public function toString(): string - { - return $this->value; - } - - public function equals(self $other): bool - { - return $this->value === $other->value; - } -} diff --git a/src/SchemaValidator.php b/src/SchemaValidator.php index bb8b89b8..912d9fed 100644 --- a/src/SchemaValidator.php +++ b/src/SchemaValidator.php @@ -13,6 +13,7 @@ namespace Ergebnis\Json\SchemaValidator; +use Ergebnis\Json\Pointer; use Ergebnis\Json\SchemaValidator\Exception\CanNotResolve; use JsonSchema\Constraints; use JsonSchema\Exception; @@ -28,7 +29,7 @@ final class SchemaValidator public function validate( Json $json, Json $schema, - JsonPointer $jsonPointer + Pointer\JsonPointer $jsonPointer ): ValidationResult { $schemaDecoded = \json_decode( $schema->toString(), @@ -37,20 +38,16 @@ public function validate( $uriRetriever = new Uri\UriRetriever(); - if (!$jsonPointer->equals(JsonPointer::empty())) { + if (!$jsonPointer->equals(Pointer\JsonPointer::document())) { try { $subSchemaDecoded = $uriRetriever->resolvePointer( $schemaDecoded, - $jsonPointer->toString(), + $jsonPointer->toUriFragmentIdentifierString(), ); } catch (Exception\ResourceNotFoundException $exception) { throw CanNotResolve::jsonPointer($jsonPointer); } - if ($schemaDecoded === $subSchemaDecoded) { - throw CanNotResolve::jsonPointer($jsonPointer); - } - $schemaDecoded = $subSchemaDecoded; } @@ -79,7 +76,7 @@ public function validate( $validationErrors = \array_map(static function (array $error): ValidationError { return ValidationError::create( - JsonPointer::fromString($error['pointer']), + Pointer\JsonPointer::fromJsonString($error['pointer']), Message::fromString($error['message']), ); }, $originalErrors); diff --git a/src/ValidationError.php b/src/ValidationError.php index 19312538..da635054 100644 --- a/src/ValidationError.php +++ b/src/ValidationError.php @@ -13,16 +13,18 @@ namespace Ergebnis\Json\SchemaValidator; +use Ergebnis\Json\Pointer; + /** * @psalm-immutable */ final class ValidationError { - private JsonPointer $jsonPointer; + private Pointer\JsonPointer $jsonPointer; private Message $message; private function __construct( - JsonPointer $jsonPointer, + Pointer\JsonPointer $jsonPointer, Message $message ) { $this->jsonPointer = $jsonPointer; @@ -30,7 +32,7 @@ private function __construct( } public static function create( - JsonPointer $jsonPointer, + Pointer\JsonPointer $jsonPointer, Message $message ): self { return new self( @@ -39,7 +41,7 @@ public static function create( ); } - public function jsonPointer(): JsonPointer + public function jsonPointer(): Pointer\JsonPointer { return $this->jsonPointer; } diff --git a/test/Unit/Exception/CanNotResolveTest.php b/test/Unit/Exception/CanNotResolveTest.php index 7f8ce12d..063d8b6c 100644 --- a/test/Unit/Exception/CanNotResolveTest.php +++ b/test/Unit/Exception/CanNotResolveTest.php @@ -13,28 +13,26 @@ namespace Ergebnis\Json\SchemaValidator\Test\Unit\Exception; +use Ergebnis\Json\Pointer; use Ergebnis\Json\SchemaValidator\Exception; -use Ergebnis\Json\SchemaValidator\JsonPointer; use PHPUnit\Framework; /** * @internal * * @covers \Ergebnis\Json\SchemaValidator\Exception\CanNotResolve - * - * @uses \Ergebnis\Json\SchemaValidator\JsonPointer */ final class CanNotResolveTest extends Framework\TestCase { public function testJsonPointerReturnsException(): void { - $jsonPointer = JsonPointer::fromString('#/foo/bar'); + $jsonPointer = Pointer\JsonPointer::fromUriFragmentIdentifierString('#/foo/bar'); $exception = Exception\CanNotResolve::jsonPointer($jsonPointer); $expected = \sprintf( 'Can not resolve JSON pointer "%s".', - $jsonPointer->toString(), + $jsonPointer->toJsonString(), ); self::assertSame($expected, $exception->getMessage()); diff --git a/test/Unit/JsonPointerTest.php b/test/Unit/JsonPointerTest.php deleted file mode 100644 index 002321d0..00000000 --- a/test/Unit/JsonPointerTest.php +++ /dev/null @@ -1,59 +0,0 @@ -toString()); - } - - public function testEmptyReturnsJsonPointer(): void - { - $jsonPointer = JsonPointer::empty(); - - self::assertSame('', $jsonPointer->toString()); - } - - public function testEqualsReturnsFalseWhenValueIsDifferent(): void - { - $one = JsonPointer::fromString('#/foo/bar'); - $two = JsonPointer::fromString('#/foo/baz'); - - self::assertFalse($one->equals($two)); - } - - public function testEqualsReturnsTrueWhenValueIsSame(): void - { - $value = '#/foo/bar'; - - $one = JsonPointer::fromString($value); - $two = JsonPointer::fromString($value); - - self::assertTrue($one->equals($two)); - } -} diff --git a/test/Unit/SchemaValidatorTest.php b/test/Unit/SchemaValidatorTest.php index 91dcd9ad..5c73bb07 100644 --- a/test/Unit/SchemaValidatorTest.php +++ b/test/Unit/SchemaValidatorTest.php @@ -13,9 +13,9 @@ namespace Ergebnis\Json\SchemaValidator\Test\Unit; +use Ergebnis\Json\Pointer; use Ergebnis\Json\SchemaValidator\Exception; use Ergebnis\Json\SchemaValidator\Json; -use Ergebnis\Json\SchemaValidator\JsonPointer; use Ergebnis\Json\SchemaValidator\Message; use Ergebnis\Json\SchemaValidator\SchemaValidator; use Ergebnis\Json\SchemaValidator\Test; @@ -29,7 +29,6 @@ * * @uses \Ergebnis\Json\SchemaValidator\Exception\CanNotResolve * @uses \Ergebnis\Json\SchemaValidator\Json - * @uses \Ergebnis\Json\SchemaValidator\JsonPointer * @uses \Ergebnis\Json\SchemaValidator\Message * @uses \Ergebnis\Json\SchemaValidator\ValidationError * @uses \Ergebnis\Json\SchemaValidator\ValidationResult @@ -38,7 +37,7 @@ final class SchemaValidatorTest extends Framework\TestCase { use Test\Util\Helper; - public function testValidateReturnsResultWhenJsonIsValidAccordingToSchemaAndJsonPointerIsEmpty(): void + public function testValidateReturnsResultWhenJsonIsValidAccordingToSchemaAndJsonPointerRefersToDocument(): void { $faker = self::faker(); @@ -75,7 +74,7 @@ public function testValidateReturnsResultWhenJsonIsValidAccordingToSchemaAndJson 'type' => 'object', ])); - $jsonPointer = JsonPointer::empty(); + $jsonPointer = Pointer\JsonPointer::document(); $schemaValidator = new SchemaValidator(); @@ -88,7 +87,7 @@ public function testValidateReturnsResultWhenJsonIsValidAccordingToSchemaAndJson self::assertTrue($result->isValid()); } - public function testValidateReturnsResultWhenJsonIsNotValidAccordingToSchemaAndJsonPointerIsEmpty(): void + public function testValidateReturnsResultWhenJsonIsNotValidAccordingToSchemaAndJsonPointerRefersToDocument(): void { $faker = self::faker(); @@ -126,7 +125,7 @@ public function testValidateReturnsResultWhenJsonIsNotValidAccordingToSchemaAndJ 'type' => 'object', ])); - $jsonPointer = JsonPointer::empty(); + $jsonPointer = Pointer\JsonPointer::document(); $schemaValidator = new SchemaValidator(); @@ -140,15 +139,15 @@ public function testValidateReturnsResultWhenJsonIsNotValidAccordingToSchemaAndJ $expected = [ ValidationError::create( - JsonPointer::fromString('/foo/bar'), + Pointer\JsonPointer::fromJsonString('/foo/bar'), Message::fromString('Integer value found, but a boolean is required'), ), ValidationError::create( - JsonPointer::fromString('/foo/baz'), + Pointer\JsonPointer::fromJsonString('/foo/baz'), Message::fromString('String value found, but an array is required'), ), ValidationError::create( - JsonPointer::empty(), + Pointer\JsonPointer::document(), Message::fromString('The property qux is not defined and the definition does not allow additional properties'), ), ]; @@ -156,7 +155,7 @@ public function testValidateReturnsResultWhenJsonIsNotValidAccordingToSchemaAndJ self::assertEquals($expected, $result->errors()); } - public function testValidateReturnsResultWhenJsonIsValidAccordingToSchemaAndJsonPointerIsNotEmpty(): void + public function testValidateReturnsResultWhenJsonIsValidAccordingToSchemaAndJsonPointerDoesNotReferToDocument(): void { $faker = self::faker(); @@ -191,7 +190,7 @@ public function testValidateReturnsResultWhenJsonIsValidAccordingToSchemaAndJson 'type' => 'object', ])); - $jsonPointer = JsonPointer::fromString('#/properties/foo'); + $jsonPointer = Pointer\JsonPointer::fromUriFragmentIdentifierString('#/properties/foo'); $schemaValidator = new SchemaValidator(); @@ -204,7 +203,7 @@ public function testValidateReturnsResultWhenJsonIsValidAccordingToSchemaAndJson self::assertTrue($result->isValid()); } - public function testValidateThrowsCanNotResolveWhenJsonPointerIsNotEmptyAndSubSchemaCouldNotBeResolved(): void + public function testValidateThrowsCanNotResolveWhenJsonPointerDoesNotReferToDocumentAndSubSchemaCouldNotBeResolved(): void { $faker = self::faker(); @@ -239,7 +238,7 @@ public function testValidateThrowsCanNotResolveWhenJsonPointerIsNotEmptyAndSubSc 'type' => 'object', ])); - $jsonPointer = JsonPointer::fromString('#/properties/qux'); + $jsonPointer = Pointer\JsonPointer::fromUriFragmentIdentifierString('#/properties/qux'); $schemaValidator = new SchemaValidator(); @@ -252,55 +251,7 @@ public function testValidateThrowsCanNotResolveWhenJsonPointerIsNotEmptyAndSubSc ); } - public function testValidateThrowsCanNotResolveWhenJsonPointerIsNotEmptyButCouldNotBeParsedAsUriFragmentIdentifierString(): void - { - $faker = self::faker(); - - $data = Json::fromString(\json_encode([ - 'bar' => $faker->boolean(), - 'baz' => $faker->words(), - ])); - - $schema = Json::fromString(\json_encode([ - 'additionalProperties' => false, - 'properties' => [ - 'foo' => [ - 'additionalProperties' => false, - 'properties' => [ - 'bar' => [ - 'type' => 'boolean', - ], - 'baz' => [ - 'type' => 'array', - ], - ], - 'required' => [ - 'bar', - 'baz', - ], - 'type' => 'object', - ], - ], - 'required' => [ - 'foo', - ], - 'type' => 'object', - ])); - - $jsonPointer = JsonPointer::fromString('/properties/qux'); - - $schemaValidator = new SchemaValidator(); - - $this->expectException(Exception\CanNotResolve::class); - - $schemaValidator->validate( - $data, - $schema, - $jsonPointer, - ); - } - - public function testValidateReturnsResultWhenJsonIsNotValidAccordingToSchemaAndJsonPathIsNotEmpty(): void + public function testValidateReturnsResultWhenJsonIsNotValidAccordingToSchemaAndJsonPointerDoesNotReferToDocument(): void { $faker = self::faker(); @@ -335,7 +286,7 @@ public function testValidateReturnsResultWhenJsonIsNotValidAccordingToSchemaAndJ 'type' => 'object', ])); - $jsonPointer = JsonPointer::fromString('#/properties/foo'); + $jsonPointer = Pointer\JsonPointer::fromUriFragmentIdentifierString('#/properties/foo'); $schemaValidator = new SchemaValidator(); @@ -349,11 +300,11 @@ public function testValidateReturnsResultWhenJsonIsNotValidAccordingToSchemaAndJ $expected = [ ValidationError::create( - JsonPointer::fromString('/bar'), + Pointer\JsonPointer::fromJsonString('/bar'), Message::fromString('String value found, but a boolean is required'), ), ValidationError::create( - JsonPointer::fromString('/baz'), + Pointer\JsonPointer::fromJsonString('/baz'), Message::fromString('Boolean value found, but an array is required'), ), ]; diff --git a/test/Unit/ValidationErrorTest.php b/test/Unit/ValidationErrorTest.php index 453a702c..c86550df 100644 --- a/test/Unit/ValidationErrorTest.php +++ b/test/Unit/ValidationErrorTest.php @@ -13,7 +13,7 @@ namespace Ergebnis\Json\SchemaValidator\Test\Unit; -use Ergebnis\Json\SchemaValidator\JsonPointer; +use Ergebnis\Json\Pointer; use Ergebnis\Json\SchemaValidator\Message; use Ergebnis\Json\SchemaValidator\Test; use Ergebnis\Json\SchemaValidator\ValidationError; @@ -24,7 +24,6 @@ * * @covers \Ergebnis\Json\SchemaValidator\ValidationError * - * @uses \Ergebnis\Json\SchemaValidator\JsonPointer * @uses \Ergebnis\Json\SchemaValidator\Message */ final class ValidationErrorTest extends Framework\TestCase @@ -33,7 +32,7 @@ final class ValidationErrorTest extends Framework\TestCase public function testCreateReturnsValidationError(): void { - $jsonPointer = JsonPointer::fromString('#/foo/bar'); + $jsonPointer = Pointer\JsonPointer::fromUriFragmentIdentifierString('#/foo/bar'); $message = Message::fromString(self::faker()->sentence()); $validationError = ValidationError::create( diff --git a/test/Unit/ValidationResultTest.php b/test/Unit/ValidationResultTest.php index 3c634a04..d67cdeae 100644 --- a/test/Unit/ValidationResultTest.php +++ b/test/Unit/ValidationResultTest.php @@ -13,7 +13,7 @@ namespace Ergebnis\Json\SchemaValidator\Test\Unit; -use Ergebnis\Json\SchemaValidator\JsonPointer; +use Ergebnis\Json\Pointer; use Ergebnis\Json\SchemaValidator\Message; use Ergebnis\Json\SchemaValidator\Test; use Ergebnis\Json\SchemaValidator\ValidationError; @@ -25,7 +25,6 @@ * * @covers \Ergebnis\Json\SchemaValidator\ValidationResult * - * @uses \Ergebnis\Json\SchemaValidator\JsonPointer * @uses \Ergebnis\Json\SchemaValidator\Message * @uses \Ergebnis\Json\SchemaValidator\ValidationError */ @@ -47,15 +46,15 @@ public function testCreateReturnsValidationResultWithErrors(): void $errors = [ ValidationError::create( - JsonPointer::fromString('/foo'), + Pointer\JsonPointer::fromJsonString('/foo'), Message::fromString($faker->sentence()), ), ValidationError::create( - JsonPointer::fromString('/bar'), + Pointer\JsonPointer::fromJsonString('/bar'), Message::fromString($faker->sentence()), ), ValidationError::create( - JsonPointer::fromString('/baz'), + Pointer\JsonPointer::fromJsonString('/baz'), Message::fromString($faker->sentence()), ), ];