From 700568610e061722becccb564f7f0951ef135a78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= Date: Fri, 17 Nov 2017 09:21:06 +0100 Subject: [PATCH] Fix Relationship::toArray() when the data member is missing --- CHANGELOG.md | 6 + src/JsonApi/Schema/Relationship.php | 25 ++- tests/JsonApi/Schema/RelationshipTest.php | 238 ++++++++++++++++++++-- 3 files changed, 245 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a483184..dfe3974 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,12 @@ REMOVED: FIXED: +## 1.3.1 - unreleased + +FIXED: + +- [#8](https://github.com/woohoolabs/yang/pulls/8): Fix for unexpected behaviour when To-One relationship data is null + ## 1.3.0 - 2017-10-17 CHANGED: diff --git a/src/JsonApi/Schema/Relationship.php b/src/JsonApi/Schema/Relationship.php index f4677cb..3953feb 100755 --- a/src/JsonApi/Schema/Relationship.php +++ b/src/JsonApi/Schema/Relationship.php @@ -40,15 +40,22 @@ public static function createFromArray(string $name, array $array, ResourceObjec $meta = self::isArrayKey($array, "meta") ? $array["meta"] : []; $links = Links::createFromArray(self::isArrayKey($array, "links") ? $array["links"] : []); - if (self::isArrayKey($array, "data") === false) { - $isToOneRelationship = array_key_exists("data", $array) && $array["data"] === null; - return self::createEmptyFromArray($name, $meta, $links, $resources, $isToOneRelationship); + // Data member is missing + if (array_key_exists("data", $array) === false) { + return self::createEmptyFromArray($name, $meta, $links, $resources, null); } + // Relationship is empty To-One + if ($array["data"] === null) { + return self::createEmptyFromArray($name, $meta, $links, $resources, true); + } + + // Relationship is To-One if (self::isAssociativeArray($array["data"])) { return self::createToOneFromArray($name, $meta, $links, $array["data"], $resources); } + // Relationship is To-Many return self::createToManyFromArray($name, $meta, $links, $array["data"], $resources); } @@ -57,7 +64,7 @@ private static function createEmptyFromArray( array $meta, Links $links, ResourceObjects $resources, - $isToOneRelationship = null + $isToOneRelationship ): Relationship { return new Relationship($name, $meta, $links, [], $resources, $isToOneRelationship); } @@ -141,10 +148,14 @@ public function toArray(): array $result["links"] = $this->links->toArray(); } - if (empty($this->resourceMap) === false) { - $result["data"] = $this->isToOneRelationship ? reset($this->resourceMap) : $this->resourceMap; - } else { + if ($this->isToOneRelationship === null) { + return $result; + } + + if (empty($this->resourceMap)) { $result["data"] = $this->isToOneRelationship ? null : []; + } else { + $result["data"] = $this->isToOneRelationship ? reset($this->resourceMap) : $this->resourceMap; } return $result; diff --git a/tests/JsonApi/Schema/RelationshipTest.php b/tests/JsonApi/Schema/RelationshipTest.php index 5d7621f..469c5d1 100644 --- a/tests/JsonApi/Schema/RelationshipTest.php +++ b/tests/JsonApi/Schema/RelationshipTest.php @@ -10,37 +10,185 @@ class RelationshipTest extends TestCase /** * @test */ - public function toArray() + public function toArrayWhenDataIsMissing() { $relationship = $this->createRelationship( [ "meta" => [ - "a" => "b" + "a" => "b", + ], + "links" => [ + "a" => "b", + ], + ] + ); + + $this->assertSame( + [ + "meta" => [ + "a" => "b", + ], + "links" => [ + "a" => [ + "href" => "b", + ], + ], + ], + $relationship->toArray() + ); + } + + /** + * @test + */ + public function toArrayWhenDataIsEmptyToOne() + { + $relationship = $this->createRelationship( + [ + "meta" => [ + "a" => "b", + ], + "links" => [ + "a" => "b", + ], + "data" => null, + ] + ); + + $this->assertSame( + [ + "meta" => [ + "a" => "b", + ], + "links" => [ + "a" => [ + "href" => "b", + ], + ], + "data" => null, + ], + $relationship->toArray() + ); + } + + /** + * @test + */ + public function toArrayWhenDataIsEmptyToMany() + { + $relationship = $this->createRelationship( + [ + "meta" => [ + "a" => "b", ], "links" => [ - "a" => "b" + "a" => "b", + ], + "data" => [], + ] + ); + + $this->assertSame( + [ + "meta" => [ + "a" => "b", + ], + "links" => [ + "a" => [ + "href" => "b", + ], + ], + "data" => [], + ], + $relationship->toArray() + ); + } + + /** + * @test + */ + public function toArrayWhenDataIsToOne() + { + $relationship = $this->createRelationship( + [ + "meta" => [ + "a" => "b", + ], + "links" => [ + "a" => "b", ], "data" => [ "type" => "a", "id" => "b", - ] + ], ] ); $this->assertSame( [ "meta" => [ - "a" => "b" + "a" => "b", ], "links" => [ "a" => [ - "href" => "b" - ] + "href" => "b", + ], ], "data" => [ "type" => "a", "id" => "b", - ] + ], + ], + $relationship->toArray() + ); + } + + /** + * @test + */ + public function toArrayWhenDataIsToMany() + { + $relationship = $this->createRelationship( + [ + "meta" => [ + "a" => "b", + ], + "links" => [ + "a" => "b", + ], + "data" => [ + [ + "type" => "a", + "id" => "1", + ], + [ + "type" => "a", + "id" => "2", + ], + ], + ] + ); + + $this->assertSame( + [ + "meta" => [ + "a" => "b", + ], + "links" => [ + "a" => [ + "href" => "b", + ], + ], + "data" => [ + [ + "type" => "a", + "id" => "1", + ], + [ + "type" => "a", + "id" => "2", + ], + ], ], $relationship->toArray() ); @@ -56,6 +204,20 @@ public function name() $this->assertSame("a", $relationship->name()); } + /** + * @test + */ + public function isToOneRelationshipIsTrueWhenEmpty() + { + $relationship = $this->createRelationship( + [ + "data" => null, + ] + ); + + $this->assertTrue($relationship->isToOneRelationship()); + } + /** * @test */ @@ -66,13 +228,27 @@ public function isToOneRelationshipIsTrue() "data" => [ "type" => "a", "id" => "b", - ] + ], ] ); $this->assertTrue($relationship->isToOneRelationship()); } + /** + * @test + */ + public function isToOneRelationshipIsFalseWhenEmpty() + { + $relationship = $this->createRelationship( + [ + "data" => [], + ] + ); + + $this->assertFalse($relationship->isToOneRelationship()); + } + /** * @test */ @@ -84,14 +260,28 @@ public function isToOneRelationshipIsFalse() [ "type" => "a", "id" => "b", - ] - ] + ], + ], ] ); $this->assertFalse($relationship->isToOneRelationship()); } + /** + * @test + */ + public function isToManyRelationshipIsTrueWhenEmpty() + { + $relationship = $this->createRelationship( + [ + "data" => [], + ] + ); + + $this->assertTrue($relationship->isToManyRelationship()); + } + /** * @test */ @@ -103,14 +293,28 @@ public function isToManyRelationshipIsTrue() [ "type" => "a", "id" => "b", - ] - ] + ], + ], ] ); $this->assertTrue($relationship->isToManyRelationship()); } + /** + * @test + */ + public function isToManyRelationshipIsFalseWhenEmpty() + { + $relationship = $this->createRelationship( + [ + "data" => null, + ] + ); + + $this->assertFalse($relationship->isToManyRelationship()); + } + /** * @test */ @@ -121,7 +325,7 @@ public function isToManyRelationshipIsFalse() "data" => [ "type" => "a", "id" => "b", - ] + ], ] ); @@ -137,7 +341,7 @@ public function hasMetaIsTrue() [ "meta" => [ "a" => "b", - ] + ], ] ); @@ -163,7 +367,7 @@ public function meta() [ "meta" => [ "a" => "b", - ] + ], ] ); @@ -179,7 +383,7 @@ public function hasLinksIsTrue() [ "links" => [ "a" => "b", - ] + ], ] );