From 6e9c0996c4df7123b37f442694e5657da31b8e92 Mon Sep 17 00:00:00 2001 From: Andrea Marco Sartori Date: Tue, 21 Mar 2023 17:33:52 +1000 Subject: [PATCH] Test mixed pointers --- tests/Dataset.php | 45 +++++++++++++++++++++++++++++++++- tests/Feature/PointersTest.php | 4 +++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/tests/Dataset.php b/tests/Dataset.php index 50f1ab0..c6b7173 100644 --- a/tests/Dataset.php +++ b/tests/Dataset.php @@ -8,7 +8,6 @@ use Cerbero\JsonParser\Sources\Psr7Request; use DirectoryIterator; use Generator; use Mockery; -use Pest\Expectation; /** * The dataset provider. @@ -278,6 +277,50 @@ final class Dataset } } + /** + * Retrieve the dataset to test mixed pointers + * + * @return Generator + */ + public static function forMixedPointers(): Generator + { + $json = fixture('json/complex_object.json'); + $pointersList = [ + [ + '/name' => fn (string $name) => "name_{$name}", + ], + [ + '/id' => fn (string $id) => "id_{$id}", + '/type' => fn (string $type) => "type_{$type}", + ], + ]; + $lazyPointers = [ + [ + '/batters/batter' => fn (Parser $batter) => $batter::class, + ], + [ + '/batters' => fn (Parser $batters) => $batters::class, + '/topping' => fn (Parser $topping) => $topping::class, + ], + ]; + $expected = [ + [ + 'name' => 'name_Cake', + 'batter' => Parser::class, + ], + [ + 'id' => 'id_0001', + 'type' => 'type_donut', + 'batters' => Parser::class, + 'topping' => Parser::class, + ], + ]; + + foreach ($pointersList as $index => $pointers) { + yield [$json, $pointers, $lazyPointers[$index], $expected[$index]]; + } + } + /** * Retrieve the dataset to test syntax errors * diff --git a/tests/Feature/PointersTest.php b/tests/Feature/PointersTest.php index f75683b..2a44cd3 100644 --- a/tests/Feature/PointersTest.php +++ b/tests/Feature/PointersTest.php @@ -39,3 +39,7 @@ it('lazy loads JSON from multiple lazy JSON pointers', function (string $json, a it('lazy loads JSON recursively', function (string $json, string $pointer, array $keys, array $expected) { expect(JsonParser::parse($json)->lazyPointer($pointer))->toLazyLoadRecursively($keys, $expected); })->with(Dataset::forRecursiveLazyLoading()); + +it('mixes pointers and lazy pointers', function (string $json, array $pointers, array $lazyPointers, array $expected) { + expect(JsonParser::parse($json)->pointers($pointers)->lazyPointers($lazyPointers))->toParseTo($expected); +})->with(Dataset::forMixedPointers());