From 07f9afce1fc630cc4f3af1988f73da7ce48dbade Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Thu, 12 Oct 2023 07:38:42 +1100 Subject: [PATCH] wip --- src/JsonApiResource.php | 4 ++-- src/JsonApiResourceCollection.php | 5 +++-- tests/Feature/AttributesTest.php | 10 ---------- tests/Feature/FeatureTest.php | 1 - tests/Feature/JsonApiTest.php | 11 ----------- tests/Feature/RelationshipsTest.php | 6 ------ tests/Feature/ResourceIdentificationTest.php | 2 -- tests/TestCase.php | 3 +++ 8 files changed, 8 insertions(+), 34 deletions(-) diff --git a/src/JsonApiResource.php b/src/JsonApiResource.php index ef7e784..6d33cb7 100644 --- a/src/JsonApiResource.php +++ b/src/JsonApiResource.php @@ -143,10 +143,10 @@ public function toArray($request) public function with($request) { return [ - ...$included = $this->included($request) + ...($included = $this->included($request) ->uniqueStrict(fn (JsonApiResource $resource): string => $resource->toUniqueResourceIdentifier($request)) ->values() - ->all() ? ['included' => $included] : [], + ->all()) ? ['included' => $included] : [], 'jsonapi' => self::serverImplementationResolver()($request), ]; } diff --git a/src/JsonApiResourceCollection.php b/src/JsonApiResourceCollection.php index 03ce760..39ddb98 100644 --- a/src/JsonApiResourceCollection.php +++ b/src/JsonApiResourceCollection.php @@ -56,11 +56,12 @@ private function resolveResourceIdentifiers(Request $request) public function with($request) { return [ - 'included' => $this->collection + ...($included = $this->collection ->map(fn (JsonApiResource $resource): Collection => $resource->included($request)) ->flatten() ->uniqueStrict(fn (JsonApiResource $resource): string => $resource->toUniqueResourceIdentifier($request)) - ->values(), + ->values() + ->all()) ? ['included' => $included] : [], 'jsonapi' => JsonApiResource::serverImplementationResolver()($request), ]; } diff --git a/tests/Feature/AttributesTest.php b/tests/Feature/AttributesTest.php index 46e88c4..0827e3c 100644 --- a/tests/Feature/AttributesTest.php +++ b/tests/Feature/AttributesTest.php @@ -45,7 +45,6 @@ public function toAttributes($request): array 'jsonapi' => [ 'version' => '1.0', ], - 'included' => [], ]); $this->assertValidJsonApi($response); } @@ -84,7 +83,6 @@ public function toAttributes($request): array 'jsonapi' => [ 'version' => '1.0', ], - 'included' => [], ]); $this->assertValidJsonApi($response); } @@ -119,7 +117,6 @@ public function toAttributes($request): array 'jsonapi' => [ 'version' => '1.0', ], - 'included' => [], ]); $this->assertValidJsonApi($response); } @@ -153,7 +150,6 @@ public function toAttributes($request): array 'jsonapi' => [ 'version' => '1.0', ], - 'included' => [], ]); $this->assertValidJsonApi($response); } @@ -183,7 +179,6 @@ public function toAttributes($request): array 'jsonapi' => [ 'version' => '1.0', ], - 'included' => [], ]); $this->assertValidJsonApi($response); } @@ -234,7 +229,6 @@ public function testItCanSpecifyMinimalAttributes(): void 'jsonapi' => [ 'version' => '1.0', ], - 'included' => [], ]); $this->assertValidJsonApi($response); }); @@ -264,7 +258,6 @@ public function testItCanRequestAttributesWhenUsingMinimalAttributes() 'jsonapi' => [ 'version' => '1.0', ], - 'included' => [], ]); $this->assertValidJsonApi($response); }); @@ -367,7 +360,6 @@ public function toAttributes($request): array 'jsonapi' => [ 'version' => '1.0', ], - 'included' => [], ]); $this->assertValidJsonApi($response); } @@ -407,7 +399,6 @@ public function toAttributes($request): array 'jsonapi' => [ 'version' => '1.0', ], - 'included' => [], ]); $this->assertValidJsonApi($response); } @@ -448,7 +439,6 @@ public function toAttributes($request): array 'jsonapi' => [ 'version' => '1.0', ], - 'included' => [], ]); $this->assertValidJsonApi($response); } diff --git a/tests/Feature/FeatureTest.php b/tests/Feature/FeatureTest.php index 230479b..5e51795 100644 --- a/tests/Feature/FeatureTest.php +++ b/tests/Feature/FeatureTest.php @@ -51,7 +51,6 @@ public function testItCanPaginate(): void ], ], ], - 'included' => [], 'links' => [ 'first' => 'http://localhost/test-route?page=1', 'last' => 'http://localhost/test-route?page=3', diff --git a/tests/Feature/JsonApiTest.php b/tests/Feature/JsonApiTest.php index 823e17c..3c38bf2 100644 --- a/tests/Feature/JsonApiTest.php +++ b/tests/Feature/JsonApiTest.php @@ -37,7 +37,6 @@ public function testItCanReturnASingleResource(): void 'name' => 'user-name', ], ], - 'included' => [], 'jsonapi' => [ 'version' => '1.0', ], @@ -79,7 +78,6 @@ public function testItCanReturnACollection(): void ], ], ], - 'included' => [], 'jsonapi' => [ 'version' => '1.0', ], @@ -122,7 +120,6 @@ public function toMeta($request): array 'meta-key' => 'meta-value', ], ], - 'included' => [], 'jsonapi' => [ 'version' => '1.0', ], @@ -167,7 +164,6 @@ public function toLinks($request): array ], ], ], - 'included' => [], 'jsonapi' => [ 'version' => '1.0', ], @@ -207,7 +203,6 @@ public function testItCanCustomiseTheTypeResolution(): void 'id' => 'expected-id', 'type' => 'Tests\\Models\\BasicModel', ], - 'included' => [], 'jsonapi' => [ 'version' => '1.0', ], @@ -229,7 +224,6 @@ public function testItCanCustomiseTheIdResolution(): void 'id' => 'expected-id', 'type' => 'basicModels', ], - 'included' => [], 'jsonapi' => [ 'version' => '1.0', ], @@ -288,7 +282,6 @@ public function testItCanSpecifyAnImplementation(): void 'name' => 'user-name', ], ], - 'included' => [], 'jsonapi' => [ 'version' => '1.4.3', 'meta' => [ @@ -297,8 +290,6 @@ public function testItCanSpecifyAnImplementation(): void ], ]); $this->assertValidJsonApi($response); - - BasicJsonApiResource::resolveServerImplementationNormally(); } public function testItExcludesEmptyRelationshipLinkMeta() @@ -744,7 +735,5 @@ public static function collection($resource): JsonApiResourceCollection ], ]); $this->assertValidJsonApi($response); - - JsonApiResource::resolveServerImplementationNormally(); } } diff --git a/tests/Feature/RelationshipsTest.php b/tests/Feature/RelationshipsTest.php index 5976aa3..9c4facc 100644 --- a/tests/Feature/RelationshipsTest.php +++ b/tests/Feature/RelationshipsTest.php @@ -57,7 +57,6 @@ public function toRelationships($request): array 'content' => 'post-content', ], ], - 'included' => [], 'jsonapi' => [ 'version' => '1.0', ], @@ -967,7 +966,6 @@ public function testItHasIncludedArrayWhenIncludeParameterIsPresentForASingleRes 'jsonapi' => [ 'version' => '1.0', ], - 'included' => [], ]); $this->assertValidJsonApi($response); } @@ -998,7 +996,6 @@ public function testItHasIncludedArrayWhenIncludeParameterIsPresentForACollectio 'jsonapi' => [ 'version' => '1.0', ], - 'included' => [], ]); $this->assertValidJsonApi($response); } @@ -1029,7 +1026,6 @@ public function testItCanReturnNullForEmptyToOneRelationships(): void 'jsonapi' => [ 'version' => '1.0', ], - 'included' => [], ]); $this->assertValidJsonApi($response); } @@ -1060,7 +1056,6 @@ public function testItCanReturnAnEmptyArrayForEmptyToManyRelationships(): void 'jsonapi' => [ 'version' => '1.0', ], - 'included' => [], ]); $this->assertValidJsonApi($response); } @@ -1292,7 +1287,6 @@ public function toRelationships($request): array 'jsonapi' => [ 'version' => '1.0', ], - 'included' => [], ]); $this->assertValidJsonApi($response); } diff --git a/tests/Feature/ResourceIdentificationTest.php b/tests/Feature/ResourceIdentificationTest.php index 8e8c755..6ddf7e6 100644 --- a/tests/Feature/ResourceIdentificationTest.php +++ b/tests/Feature/ResourceIdentificationTest.php @@ -81,7 +81,6 @@ public function testItResolvesTheIdAndTypeOfAModel(): void 'jsonapi' => [ 'version' => '1.0', ], - 'included' => [], ]); $this->assertValidJsonApi($response); } @@ -106,7 +105,6 @@ public function testItCastsAModelsIntegerIdToAString(): void 'jsonapi' => [ 'version' => '1.0', ], - 'included' => [], ]); $this->assertValidJsonApi($response); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 3039cd7..2b49fee 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -9,6 +9,7 @@ use Orchestra\Testbench\TestCase as BaseTestCase; use RuntimeException; +use TiMacDonald\JsonApi\JsonApiResource; use function is_string; class TestCase extends BaseTestCase @@ -21,6 +22,8 @@ public function setUp(): void { parent::setUp(); + JsonApiResource::resolveServerImplementationNormally(); + $this->withoutExceptionHandling(); }