From a6ca07c59fb2aed580eb69dd8c446681ccc705b5 Mon Sep 17 00:00:00 2001 From: Jasper Zonneveld Date: Fri, 11 Jan 2019 16:36:54 +0100 Subject: [PATCH 1/2] Align hydration of relations in ItemHydrator --- src/ItemHydrator.php | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/ItemHydrator.php b/src/ItemHydrator.php index 95f56e0..8aa79a2 100644 --- a/src/ItemHydrator.php +++ b/src/ItemHydrator.php @@ -71,9 +71,9 @@ protected function fillRelations(ItemInterface $item, array $attributes) // It is a valid relation if ($relation instanceof HasOneRelation) { - $this->hydrateHasOneRelation($item, $attributes, $relation, $availableRelation); + $this->hydrateHasOneRelation($attributes, $relation, $availableRelation); } elseif ($relation instanceof HasManyRelation) { - $this->hydrateHasManyRelation($attributes, $availableRelation, $relation); + $this->hydrateHasManyRelation($attributes, $relation, $availableRelation); } elseif ($relation instanceof MorphToRelation) { $this->hydrateMorphToRelation($attributes, $relation, $availableRelation); } elseif ($relation instanceof MorphToManyRelation) { @@ -101,26 +101,21 @@ protected function getRelationFromItem(ItemInterface $item, string $availableRel } /** - * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item * @param array $attributes * @param \Swis\JsonApi\Client\Relations\HasOneRelation $relation * @param string $availableRelation * * @throws \InvalidArgumentException */ - protected function hydrateHasOneRelation( - ItemInterface $item, - array $attributes, - HasOneRelation $relation, - string $availableRelation - ) { + protected function hydrateHasOneRelation(array $attributes, HasOneRelation $relation, string $availableRelation) + { if (is_array($attributes[$availableRelation])) { $relationItem = $this->buildRelationItem($relation, $attributes[$availableRelation]); - $relation->associate($relationItem); } else { - $relation->setId($attributes[$availableRelation]); - $item->setAttribute($availableRelation.'_id', $attributes[$availableRelation]); + $relationItem = $this->buildRelationItem($relation, ['id' => $attributes[$availableRelation]]); } + + $relation->associate($relationItem); } /** @@ -130,7 +125,7 @@ protected function hydrateHasOneRelation( * * @throws \InvalidArgumentException */ - protected function hydrateHasManyRelation(array $attributes, string $availableRelation, HasManyRelation $relation) + protected function hydrateHasManyRelation(array $attributes, HasManyRelation $relation, string $availableRelation) { foreach ($attributes[$availableRelation] as $relationData) { if (is_array($relationData)) { From 2447be89642ae13b82b488f013c2d07121f94775 Mon Sep 17 00:00:00 2001 From: Jasper Zonneveld Date: Fri, 11 Jan 2019 16:37:20 +0100 Subject: [PATCH 2/2] Add missing use cases to ItemHydratorTest --- tests/ItemHydratorTest.php | 358 ++++++++++++++++++++----------------- 1 file changed, 194 insertions(+), 164 deletions(-) diff --git a/tests/ItemHydratorTest.php b/tests/ItemHydratorTest.php index 0683461..a2ea427 100644 --- a/tests/ItemHydratorTest.php +++ b/tests/ItemHydratorTest.php @@ -17,10 +17,24 @@ class ItemHydratorTest extends AbstractTest { + /** + * @return \Swis\JsonApi\Client\ItemHydrator + */ + private function getItemHydrator() + { + $typeMapper = new TypeMapper(); + $typeMapper->setMapping('hydratedItem', Item::class); + + $typeMapper->setMapping('related-item', RelatedItem::class); + $typeMapper->setMapping('another-related-item', AnotherRelatedItem::class); + + return new ItemHydrator($typeMapper); + } + /** * @test */ - public function it_hydrates_items_without_relationships() + public function it_hydrates_attributes() { $data = [ 'testattribute1' => 'test', @@ -28,35 +42,44 @@ public function it_hydrates_items_without_relationships() ]; $item = new Item(); - $item = $this->getItemHydrator()->hydrate($item, $data); $this->assertEquals($data, $item->getAttributes()); } /** - * @return \Swis\JsonApi\Client\ItemHydrator + * @test */ - private function getItemHydrator() + public function it_hydrates_hasone_relationships_by_id() { - $typeMapper = new TypeMapper(); - $typeMapper->setMapping('hydratedItem', Item::class); + $data = [ + 'hasone_relation' => 1, + ]; - $typeMapper->setMapping('related-item', RelatedItem::class); - $typeMapper->setMapping('another-related-item', AnotherRelatedItem::class); + $item = new WithRelationshipItem(); + $item = $this->getItemHydrator()->hydrate($item, $data); - return new ItemHydrator($typeMapper); + /** @var \Swis\JsonApi\Client\Relations\HasOneRelation $hasOne */ + $hasOne = $item->getRelationship('hasone_relation'); + $this->assertInstanceOf(HasOneRelation::class, $hasOne); + + $this->assertEquals($data['hasone_relation'], $hasOne->getIncluded()->getId()); + $this->assertEquals('related-item', $hasOne->getIncluded()->getType()); + + $this->assertArrayHasKey('hasone_relation', $item->toJsonApiArray()['relationships']); } /** * @test */ - public function it_hydrates_items_with_hasone_relationships() + public function it_hydrates_hasone_relationships_with_attributes() { $data = [ - 'testattribute1' => 'test', - 'testattribute2' => 'test2', - 'hasone_relation' => 1, + 'hasone_relation' => [ + 'id' => 1, + 'test_related_attribute1' => 'test', + 'test_related_attribute2' => 'test2', + ], ]; $item = new WithRelationshipItem(); @@ -64,28 +87,52 @@ public function it_hydrates_items_with_hasone_relationships() /** @var \Swis\JsonApi\Client\Relations\HasOneRelation $hasOne */ $hasOne = $item->getRelationship('hasone_relation'); + $this->assertInstanceOf(HasOneRelation::class, $hasOne); - $this->assertInstanceOf( - HasOneRelation::class, - $hasOne - ); - - $this->assertEquals($data['testattribute1'], $item->getAttribute('testattribute1')); - $this->assertEquals($data['testattribute2'], $item->getAttribute('testattribute2')); + $this->assertEquals($data['hasone_relation']['id'], $hasOne->getIncluded()->getId()); + $this->assertEquals('related-item', $hasOne->getIncluded()->getType()); + $this->assertEquals($data['hasone_relation']['test_related_attribute1'], $hasOne->getIncluded()->getAttribute('test_related_attribute1')); + $this->assertEquals($data['hasone_relation']['test_related_attribute2'], $hasOne->getIncluded()->getAttribute('test_related_attribute2')); - $this->assertEquals($data['hasone_relation'], $hasOne->getId()); - $this->assertEquals('related-item', $hasOne->getType()); $this->assertArrayHasKey('hasone_relation', $item->toJsonApiArray()['relationships']); } /** * @test */ - public function it_hydrates_items_with_hasmany_relationships() + public function it_hydrates_hasmany_relationships_by_id() + { + $data = [ + 'hasmany_relation' => [ + 1, + 2, + ], + ]; + + $item = new WithRelationshipItem(); + $item = $this->getItemHydrator()->hydrate($item, $data); + + /** @var \Swis\JsonApi\Client\Relations\HasManyRelation $hasMany */ + $hasMany = $item->getRelationship('hasmany_relation'); + $this->assertInstanceOf(HasManyRelation::class, $hasMany); + + $this->assertInstanceOf(Collection::class, $hasMany->getIncluded()); + $this->assertCount(2, $hasMany->getIncluded()); + + $this->assertEquals($data['hasmany_relation'][0], $hasMany->getIncluded()->get(0)->getId()); + $this->assertEquals('related-item', $hasMany->getIncluded()->get(0)->getType()); + $this->assertEquals($data['hasmany_relation'][1], $hasMany->getIncluded()->get(1)->getId()); + $this->assertEquals('related-item', $hasMany->getIncluded()->get(1)->getType()); + + $this->assertArrayHasKey('hasmany_relation', $item->toJsonApiArray()['relationships']); + } + + /** + * @test + */ + public function it_hydrates_hasmany_relationships_with_attributes() { $data = [ - 'testattribute1' => 'test', - 'testattribute2' => 'test2', 'hasmany_relation' => [ [ 'id' => 1, @@ -101,70 +148,59 @@ public function it_hydrates_items_with_hasmany_relationships() ]; $item = new WithRelationshipItem(); - $item = $this->getItemHydrator()->hydrate($item, $data); + /** @var \Swis\JsonApi\Client\Relations\HasManyRelation $hasMany */ $hasMany = $item->getRelationship('hasmany_relation'); - - $this->assertInstanceOf( - HasManyRelation::class, - $hasMany - ); + $this->assertInstanceOf(HasManyRelation::class, $hasMany); $this->assertInstanceOf(Collection::class, $hasMany->getIncluded()); $this->assertCount(2, $hasMany->getIncluded()); - $expected = [ - [ - 'id' => 1, - 'type' => 'related-item', - 'attributes' => [ - 'test_related_attribute1' => 'test', - 'test_related_attribute2' => 'test2', - ], - ], - [ - 'id' => 2, - 'type' => 'related-item', - 'attributes' => [ - 'test_related_attribute1' => 'test', - 'test_related_attribute2' => 'test2', - ], - ], - ]; + $this->assertEquals($data['hasmany_relation'][0]['id'], $hasMany->getIncluded()->get(0)->getId()); + $this->assertEquals('related-item', $hasMany->getIncluded()->get(0)->getType()); + $this->assertEquals($data['hasmany_relation'][0]['test_related_attribute1'], $hasMany->getIncluded()->get(0)->getAttribute('test_related_attribute1')); + $this->assertEquals($data['hasmany_relation'][0]['test_related_attribute2'], $hasMany->getIncluded()->get(0)->getAttribute('test_related_attribute2')); + $this->assertEquals($data['hasmany_relation'][1]['id'], $hasMany->getIncluded()->get(1)->getId()); + $this->assertEquals('related-item', $hasMany->getIncluded()->get(1)->getType()); + $this->assertEquals($data['hasmany_relation'][1]['test_related_attribute1'], $hasMany->getIncluded()->get(1)->getAttribute('test_related_attribute1')); + $this->assertEquals($data['hasmany_relation'][1]['test_related_attribute2'], $hasMany->getIncluded()->get(1)->getAttribute('test_related_attribute2')); - $this->assertEquals($expected, $hasMany->getIncluded()->toJsonApiArray()); $this->assertArrayHasKey('hasmany_relation', $item->toJsonApiArray()['relationships']); } /** * @test */ - public function it_throws_exception_when_morphto_relationship_without_type_attribute() + public function it_hydrates_morphto_relationships_by_id() { $data = [ - 'testattribute1' => 'test', - 'testattribute2' => 'test2', 'morphto_relation' => [ - 'id' => 1, - 'test_related_attribute1' => 'test', + 'id' => 1, + 'type' => 'related-item', ], ]; $item = new WithRelationshipItem(); + $item = $this->getItemHydrator()->hydrate($item, $data); - $this->expectException(InvalidArgumentException::class); - $this->getItemHydrator()->hydrate($item, $data); + /** @var \Swis\JsonApi\Client\Relations\MorphToRelation $morphTo */ + $morphTo = $item->getRelationship('morphto_relation'); + $this->assertInstanceOf(MorphToRelation::class, $morphTo); + + $this->assertEquals($data['morphto_relation']['id'], $morphTo->getIncluded()->getId()); + $this->assertEquals($data['morphto_relation']['type'], $morphTo->getIncluded()->getType()); + $this->assertArrayNotHasKey('type', $morphTo->getIncluded()->getAttributes()); + + $this->assertArrayHasKey('morphto_relation', $item->toJsonApiArray()['relationships']); } /** * @test */ - public function it_hydrates_items_with_morphto_relationship() + public function it_hydrates_morphto_relationships_with_attributes() { $data = [ - 'testattribute1' => 'test', - 'testattribute2' => 'test2', 'morphto_relation' => [ 'id' => 1, 'type' => 'related-item', @@ -177,32 +213,25 @@ public function it_hydrates_items_with_morphto_relationship() /** @var \Swis\JsonApi\Client\Relations\MorphToRelation $morphTo */ $morphTo = $item->getRelationship('morphto_relation'); + $this->assertInstanceOf(MorphToRelation::class, $morphTo); + + $this->assertEquals($data['morphto_relation']['id'], $morphTo->getIncluded()->getId()); + $this->assertEquals($data['morphto_relation']['type'], $morphTo->getIncluded()->getType()); + $this->assertArrayNotHasKey('type', $morphTo->getIncluded()->getAttributes()); + $this->assertEquals($data['morphto_relation']['test_related_attribute1'], $morphTo->getIncluded()->getAttribute('test_related_attribute1')); - $this->assertInstanceOf( - MorphToRelation::class, - $morphTo - ); - $this->assertEquals($data['testattribute1'], $item->getAttribute('testattribute1')); - $this->assertEquals($data['testattribute2'], $item->getAttribute('testattribute2')); - $this->assertEquals('related-item', $morphTo->getType()); - $this->assertEquals('related-item', $morphTo->getIncluded()->getType()); - $this->assertEquals( - $data['morphto_relation']['test_related_attribute1'], - $morphTo->getIncluded()->getAttribute('test_related_attribute1') - ); $this->assertArrayHasKey('morphto_relation', $item->toJsonApiArray()['relationships']); } /** * @test */ - public function it_hydrates_unmapped_items_with_morphto_relationship() + public function it_hydrates_morphto_relationships_with_unmapped_items() { $data = [ 'morphto_relation' => [ - 'id' => 1, - 'type' => 'unmapped-item', - 'test_related_attribute1' => 'test', + 'id' => 1, + 'type' => 'unmapped-item', ], ]; @@ -211,25 +240,22 @@ public function it_hydrates_unmapped_items_with_morphto_relationship() /** @var \Swis\JsonApi\Client\Relations\MorphToRelation $morphTo */ $morphTo = $item->getRelationship('morphto_relation'); + $this->assertInstanceOf(MorphToRelation::class, $morphTo); - $this->assertEquals('unmapped-item', $morphTo->getType()); - $this->assertEquals('unmapped-item', $morphTo->getIncluded()->getType()); + $this->assertEquals($data['morphto_relation']['id'], $morphTo->getIncluded()->getId()); + $this->assertEquals($data['morphto_relation']['type'], $morphTo->getIncluded()->getType()); $this->assertArrayNotHasKey('type', $morphTo->getIncluded()->getAttributes()); } /** * @test */ - public function it_throws_exception_when_morphtomany_relationship_without_type_attribute() + public function it_throws_for_morphto_relationships_without_type_attribute() { $data = [ - 'testattribute1' => 'test', - 'testattribute2' => 'test2', - 'morphtomany_relation' => [ - [ - 'id' => 1, - 'test_related_attribute1' => 'test', - ], + 'morphto_relation' => [ + 'id' => 1, + 'test_related_attribute1' => 'test', ], ]; @@ -242,21 +268,17 @@ public function it_throws_exception_when_morphtomany_relationship_without_type_a /** * @test */ - public function it_hydrates_items_with_morphtomany_relationship() + public function it_hydrates_morphtomany_relationships_by_id() { $data = [ - 'testattribute1' => 'test', - 'testattribute2' => 'test2', 'morphtomany_relation' => [ [ - 'id' => 1, - 'type' => 'related-item', - 'test_related_attribute1' => 'test1', + 'id' => 1, + 'type' => 'related-item', ], [ - 'id' => 2, - 'type' => 'another-related-item', - 'test_related_attribute1' => 'test2', + 'id' => 2, + 'type' => 'another-related-item', ], ], ]; @@ -266,45 +288,36 @@ public function it_hydrates_items_with_morphtomany_relationship() /** @var \Swis\JsonApi\Client\Relations\MorphToManyRelation $morphToMany */ $morphToMany = $item->getRelationship('morphtomany_relation'); + $this->assertInstanceOf(MorphToManyRelation::class, $morphToMany); - $this->assertInstanceOf( - MorphToManyRelation::class, - $morphToMany - ); $this->assertInstanceOf(Collection::class, $morphToMany->getIncluded()); $this->assertCount(2, $morphToMany->getIncluded()); - $this->assertEquals($data['testattribute1'], $item->getAttribute('testattribute1')); - $this->assertEquals($data['testattribute2'], $item->getAttribute('testattribute2')); - - $this->assertEquals('related-item', $morphToMany->getIncluded()[0]->getType()); - $this->assertEquals('another-related-item', $morphToMany->getIncluded()[1]->getType()); - $this->assertEquals( - $data['morphtomany_relation'][0]['test_related_attribute1'], - $morphToMany->getIncluded()[0]->getAttribute('test_related_attribute1') - ); - $this->assertEquals( - $data['morphtomany_relation'][1]['test_related_attribute1'], - $morphToMany->getIncluded()[1]->getAttribute('test_related_attribute1') - ); + $this->assertEquals($data['morphtomany_relation'][0]['id'], $morphToMany->getIncluded()->get(0)->getId()); + $this->assertEquals($data['morphtomany_relation'][0]['type'], $morphToMany->getIncluded()->get(0)->getType()); + $this->assertArrayNotHasKey('type', $morphToMany->getIncluded()->get(0)->getAttributes()); + $this->assertEquals($data['morphtomany_relation'][1]['id'], $morphToMany->getIncluded()->get(1)->getId()); + $this->assertEquals($data['morphtomany_relation'][1]['type'], $morphToMany->getIncluded()->get(1)->getType()); + $this->assertArrayNotHasKey('type', $morphToMany->getIncluded()->get(1)->getAttributes()); + $this->assertArrayHasKey('morphtomany_relation', $item->toJsonApiArray()['relationships']); } /** * @test */ - public function it_hydrates_unmapped_items_with_morphtomany_relationship() + public function it_hydrates_morphtomany_relationships_with_attributes() { $data = [ 'morphtomany_relation' => [ [ 'id' => 1, - 'type' => 'unmapped-item', + 'type' => 'related-item', 'test_related_attribute1' => 'test1', ], [ 'id' => 2, - 'type' => 'unmapped-item', + 'type' => 'another-related-item', 'test_related_attribute1' => 'test2', ], ], @@ -315,88 +328,105 @@ public function it_hydrates_unmapped_items_with_morphtomany_relationship() /** @var \Swis\JsonApi\Client\Relations\MorphToManyRelation $morphToMany */ $morphToMany = $item->getRelationship('morphtomany_relation'); + $this->assertInstanceOf(MorphToManyRelation::class, $morphToMany); + + $this->assertInstanceOf(Collection::class, $morphToMany->getIncluded()); + $this->assertCount(2, $morphToMany->getIncluded()); + + $this->assertEquals($data['morphtomany_relation'][0]['id'], $morphToMany->getIncluded()->get(0)->getId()); + $this->assertEquals($data['morphtomany_relation'][0]['type'], $morphToMany->getIncluded()->get(0)->getType()); + $this->assertArrayNotHasKey('type', $morphToMany->getIncluded()->get(0)->getAttributes()); + $this->assertEquals($data['morphtomany_relation'][0]['test_related_attribute1'], $morphToMany->getIncluded()[0]->getAttribute('test_related_attribute1')); + $this->assertEquals($data['morphtomany_relation'][1]['id'], $morphToMany->getIncluded()->get(1)->getId()); + $this->assertEquals($data['morphtomany_relation'][1]['type'], $morphToMany->getIncluded()->get(1)->getType()); + $this->assertArrayNotHasKey('type', $morphToMany->getIncluded()->get(1)->getAttributes()); + $this->assertEquals($data['morphtomany_relation'][1]['test_related_attribute1'], $morphToMany->getIncluded()[1]->getAttribute('test_related_attribute1')); - $this->assertEquals('unmapped-item', $morphToMany->getIncluded()[0]->getType()); - $this->assertEquals('unmapped-item', $morphToMany->getIncluded()[1]->getType()); - $this->assertArrayNotHasKey('type', $morphToMany->getIncluded()[0]->getAttributes()); - $this->assertArrayNotHasKey('type', $morphToMany->getIncluded()[1]->getAttributes()); + $this->assertArrayHasKey('morphtomany_relation', $item->toJsonApiArray()['relationships']); } /** * @test */ - public function it_hydrates_nested_relationship_items() + public function it_hydrates_morphtomany_relationships_with_unmapped_items() { $data = [ - 'testattribute1' => 'test', - 'testattribute2' => 'test2', - 'hasone_relation' => [ - 'id' => 1, - 'parent_relation' => 5, + 'morphtomany_relation' => [ + [ + 'id' => 1, + 'type' => 'unmapped-item', + ], + [ + 'id' => 2, + 'type' => 'unmapped-item', + ], ], ]; $item = new WithRelationshipItem(); $item = $this->getItemHydrator()->hydrate($item, $data); - /** @var \Swis\JsonApi\Client\Relations\HasOneRelation $hasOne */ - $hasOne = $item->getRelationship('hasone_relation'); - $this->assertInstanceOf(HasOneRelation::class, $hasOne); + /** @var \Swis\JsonApi\Client\Relations\MorphToManyRelation $morphToMany */ + $morphToMany = $item->getRelationship('morphtomany_relation'); - $this->assertEquals($data['testattribute1'], $item->getAttribute('testattribute1')); - $this->assertEquals($data['testattribute2'], $item->getAttribute('testattribute2')); + $this->assertInstanceOf(Collection::class, $morphToMany->getIncluded()); + $this->assertCount(2, $morphToMany->getIncluded()); - $this->assertEquals($data['hasone_relation']['id'], $hasOne->getId()); - $this->assertEquals('related-item', $hasOne->getType()); + $this->assertEquals($data['morphtomany_relation'][0]['id'], $morphToMany->getIncluded()->get(0)->getId()); + $this->assertEquals($data['morphtomany_relation'][0]['type'], $morphToMany->getIncluded()->get(0)->getType()); + $this->assertArrayNotHasKey('type', $morphToMany->getIncluded()->get(0)->getAttributes()); + $this->assertEquals($data['morphtomany_relation'][1]['id'], $morphToMany->getIncluded()->get(1)->getId()); + $this->assertEquals($data['morphtomany_relation'][1]['type'], $morphToMany->getIncluded()->get(1)->getType()); + $this->assertArrayNotHasKey('type', $morphToMany->getIncluded()->get(1)->getAttributes()); + } - /** @var \Swis\JsonApi\Client\Relations\HasOneRelation $hasOne */ - $hasOneParent = $hasOne->getIncluded()->getRelationship('parent_relation'); - $this->assertInstanceOf(HasOneRelation::class, $hasOneParent); + /** + * @test + */ + public function it_throws_for_morphtomany_relationships_without_type_attribute() + { + $data = [ + 'morphtomany_relation' => [ + [ + 'id' => 1, + 'test_related_attribute1' => 'test', + ], + ], + ]; + + $item = new WithRelationshipItem(); - $this->assertEquals($data['hasone_relation']['parent_relation'], $hasOneParent->getId()); - $this->assertEquals('item-with-relationship', $hasOneParent->getType()); + $this->expectException(InvalidArgumentException::class); + $this->getItemHydrator()->hydrate($item, $data); } /** * @test */ - public function it_hydrates_a_hasmany_relationship_by_id() + public function it_hydrates_nested_relationship_items() { $data = [ - 'testattribute1' => 'test', - 'testattribute2' => 'test2', - 'hasmany_relation' => [ - 1, - 2, + 'hasone_relation' => [ + 'id' => 1, + 'parent_relation' => 5, ], ]; $item = new WithRelationshipItem(); - $item = $this->getItemHydrator()->hydrate($item, $data); - /** @var \Swis\JsonApi\Client\Relations\HasManyRelation $hasMany */ - $hasMany = $item->getRelationship('hasmany_relation'); - $this->assertInstanceOf( - HasManyRelation::class, - $hasMany - ); + /** @var \Swis\JsonApi\Client\Relations\HasOneRelation $hasOne */ + $hasOne = $item->getRelationship('hasone_relation'); + $this->assertInstanceOf(HasOneRelation::class, $hasOne); - $this->assertInstanceOf(Collection::class, $hasMany->getIncluded()); - $this->assertCount(2, $hasMany->getIncluded()); + $this->assertEquals($data['hasone_relation']['id'], $hasOne->getIncluded()->getId()); + $this->assertEquals('related-item', $hasOne->getIncluded()->getType()); - $expected = [ - [ - 'id' => 1, - 'type' => 'related-item', - ], - [ - 'id' => 2, - 'type' => 'related-item', - ], - ]; + /** @var \Swis\JsonApi\Client\Relations\HasOneRelation $hasOneParent */ + $hasOneParent = $hasOne->getIncluded()->getRelationship('parent_relation'); + $this->assertInstanceOf(HasOneRelation::class, $hasOneParent); - $this->assertEquals($expected, $hasMany->getIncluded()->toJsonApiArray()); - $this->assertArrayHasKey('hasmany_relation', $item->toJsonApiArray()['relationships']); + $this->assertEquals($data['hasone_relation']['parent_relation'], $hasOneParent->getIncluded()->getId()); + $this->assertEquals('item-with-relationship', $hasOneParent->getIncluded()->getType()); } }