-
Notifications
You must be signed in to change notification settings - Fork 1
Add ValidJsonVerifier, that checks, that every normalizer correctly generated JSON-compatible values
#13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add ValidJsonVerifier, that checks, that every normalizer correctly generated JSON-compatible values
#13
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace Torr\SimpleNormalizer\Exception; | ||
|
|
||
| /** | ||
| * @final | ||
| */ | ||
| class IncompleteNormalizationException extends \RuntimeException implements NormalizerExceptionInterface | ||
| { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace Torr\SimpleNormalizer\Normalizer\Validator; | ||
|
|
||
| /** | ||
| * @final | ||
| */ | ||
| readonly class InvalidJsonElement | ||
| { | ||
| /** | ||
| */ | ||
| public function __construct ( | ||
| public mixed $value, | ||
| public array $path, | ||
| ) {} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace Torr\SimpleNormalizer\Normalizer\Validator; | ||
|
|
||
| use Torr\SimpleNormalizer\Exception\IncompleteNormalizationException; | ||
|
|
||
| /** | ||
| * Helper to verify that a given value is valid JSON (scalars, lists or empty objects) | ||
| * | ||
| * @final | ||
| */ | ||
| class ValidJsonVerifier | ||
| { | ||
| /** | ||
| * Ensures that only valid JSON types are present in the value, that means scalars, arrays and empty objects. | ||
| */ | ||
| public function ensureValidOnlyJsonTypes (mixed $value) : void | ||
| { | ||
| $invalidElement = $this->findInvalidJsonElement($value); | ||
|
|
||
| if (null !== $invalidElement) | ||
| { | ||
| throw new IncompleteNormalizationException( | ||
| sprintf( | ||
| "Found a JSON-incompatible value when normalizing. Found '%s' at path '%s', but expected only scalars, arrays and empty objects.", | ||
| get_debug_type($invalidElement->value), | ||
| implode(".", $invalidElement->path), | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Searches through the value and looks for anything that isn't valid JSON | ||
| * (scalars, arrays or empty objects). | ||
| * | ||
| * @return InvalidJsonElement|null Returns null if everything is valid, otherwise the invalid value. | ||
| */ | ||
| private function findInvalidJsonElement (mixed $value, array $path = ["$"]) : ?InvalidJsonElement | ||
| { | ||
| // scalars are always valid | ||
| if (null === $value || \is_scalar($value)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| // only empty stdClass objects are allowed (as they are used to serialize to `{}`) | ||
| if (is_object($value)) | ||
| { | ||
| return $value instanceof \stdClass && [] === get_object_vars($value) | ||
| ? null | ||
| : new InvalidJsonElement($value, $path); | ||
| } | ||
|
|
||
| if (is_array($value)) | ||
| { | ||
| foreach ($value as $key => $item) | ||
| { | ||
| $invalidItem = $this->findInvalidJsonElement( | ||
| $item, | ||
| [...$path, $key], | ||
| ); | ||
|
|
||
| if (null !== $invalidItem) | ||
| { | ||
| return $invalidItem; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| return new InvalidJsonElement($value, $path); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace Tests\Torr\SimpleNormalizer\Normalizer; | ||
|
|
||
| use Symfony\Component\DependencyInjection\ServiceLocator; | ||
| use Tests\Torr\SimpleNormalizer\Fixture\DummyVO; | ||
| use Torr\SimpleNormalizer\Exception\IncompleteNormalizationException; | ||
| use Torr\SimpleNormalizer\Normalizer\SimpleNormalizer; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Torr\SimpleNormalizer\Normalizer\SimpleObjectNormalizerInterface; | ||
| use Torr\SimpleNormalizer\Normalizer\Validator\ValidJsonVerifier; | ||
|
|
||
| class SimpleNormalizerTest extends TestCase | ||
| { | ||
| /** | ||
| * | ||
| */ | ||
| public static function provideJsonVerifierEnabled () : iterable | ||
| { | ||
| yield "normalize" => [ | ||
| static fn (SimpleNormalizer $normalizer, mixed $value) => $normalizer->normalize($value), | ||
| ]; | ||
|
|
||
| yield "normalizeArray" => [ | ||
| static fn (SimpleNormalizer $normalizer, mixed $value) => $normalizer->normalizeArray($value), | ||
| ]; | ||
|
|
||
| yield "normalizeMap" => [ | ||
| static fn (SimpleNormalizer $normalizer, mixed $value) => $normalizer->normalizeMap($value), | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider provideJsonVerifierEnabled | ||
| */ | ||
| public function testJsonVerifierEnabled (callable $call) : void | ||
| { | ||
| $verifier = $this->createMock(ValidJsonVerifier::class); | ||
|
|
||
| $verifier | ||
| ->expects($this->once()) | ||
| ->method('ensureValidOnlyJsonTypes'); | ||
|
|
||
| $normalizer = new SimpleNormalizer( | ||
| objectNormalizers: $this->createNormalizerObjectNormalizers("ok"), | ||
| isDebug: true, | ||
| validJsonVerifier: $verifier, | ||
| ); | ||
|
|
||
| $call($normalizer, [ | ||
| "nested" => [ | ||
| "deeply" => [ | ||
| "ohai" => new DummyVO(42), | ||
| ], | ||
| "valid" => true, | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * | ||
| */ | ||
| public function testJsonVerifierDisabled () : void | ||
| { | ||
| $verifier = $this->createMock(ValidJsonVerifier::class); | ||
|
|
||
| $verifier | ||
| ->expects($this->never()) | ||
| ->method('ensureValidOnlyJsonTypes'); | ||
|
|
||
| $normalizer = new SimpleNormalizer( | ||
| objectNormalizers: $this->createNormalizerObjectNormalizers("ok"), | ||
| isDebug: false, | ||
| validJsonVerifier: $verifier, | ||
| ); | ||
|
|
||
| $normalizer->normalize([ | ||
| "key" => new DummyVO(42), | ||
| ]); | ||
| self::assertTrue(true); // Just to ensure the test runs without exceptions | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * | ||
| */ | ||
| public function testInvalidNormalizer () : void | ||
| { | ||
| $normalizer = new SimpleNormalizer( | ||
| objectNormalizers: $this->createNormalizerObjectNormalizers(new DummyVO(11)), | ||
| isDebug: true, | ||
| validJsonVerifier: new ValidJsonVerifier(), | ||
| ); | ||
|
|
||
| $this->expectException(IncompleteNormalizationException::class); | ||
| $this->expectExceptionMessage("Found a JSON-incompatible value when normalizing. Found 'Tests\Torr\SimpleNormalizer\Fixture\DummyVO' at path '$', but expected only scalars, arrays and empty objects."); | ||
| $normalizer->normalize(new DummyVO(42)); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * | ||
| */ | ||
| public function testInvalidNestedNormalizer () : void | ||
| { | ||
| $normalizer = new SimpleNormalizer( | ||
| objectNormalizers: $this->createNormalizerObjectNormalizers(new DummyVO(11)), | ||
| isDebug: true, | ||
| validJsonVerifier: new ValidJsonVerifier(), | ||
| ); | ||
|
|
||
| $this->expectException(IncompleteNormalizationException::class); | ||
| $this->expectExceptionMessage("Found a JSON-incompatible value when normalizing. Found 'Tests\Torr\SimpleNormalizer\Fixture\DummyVO' at path '$.nested.deeply.ohai', but expected only scalars, arrays and empty objects."); | ||
| $normalizer->normalize([ | ||
| "nested" => [ | ||
| "deeply" => [ | ||
| "ohai" => new DummyVO(42), | ||
| ], | ||
| "valid" => true, | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * | ||
| */ | ||
| private function createNormalizerObjectNormalizers (mixed $returnValue) : ServiceLocator | ||
| { | ||
| return new ServiceLocator([ | ||
| DummyVO::class => static fn () => new readonly class ($returnValue) implements SimpleObjectNormalizerInterface | ||
| { | ||
| public function __construct ( | ||
| private mixed $returnValue, | ||
| ) {} | ||
|
|
||
| public function normalize (object $value, array $context, SimpleNormalizer $normalizer) : mixed | ||
| { | ||
| return $this->returnValue; | ||
| } | ||
|
|
||
| public static function getNormalizedType () : string | ||
| { | ||
| return DummyVO::class; | ||
| } | ||
| }, | ||
| ]); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.