Skip to content
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

Do not fill type attribute in unmapped items in morph(to) relationships #28

Merged
merged 1 commit into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/ItemHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,12 @@ protected function hydrateHasManyRelation(array $attributes, string $availableRe
*/
protected function hydrateMorphToRelation(array $attributes, MorphToRelation $relation, string $availableRelation)
{
if (!array_key_exists('type', $attributes[$availableRelation])) {
$relationData = $attributes[$availableRelation];
if (!array_key_exists('type', $relationData)) {
throw new \InvalidArgumentException('Always provide a "type" attribute in a morphTo relationship');
}
$relationItem = $this->buildRelationItem($relation, array_diff_key($relationData, ['type' => 'type']), $relationData['type']);

$relationItem = $this->buildRelationItem($relation, $attributes[$availableRelation], $attributes[$availableRelation]['type']);
$relation->associate($relationItem);
}

Expand All @@ -173,7 +174,7 @@ protected function hydrateMorphToManyRelation(array $attributes, MorphToManyRela
if (!array_key_exists('type', $relationData)) {
throw new \InvalidArgumentException('Always provide a "type" attribute in a morphToMany relationship entry');
}
$relationItem = $this->buildRelationItem($relation, $relationData, $relationData['type']);
$relationItem = $this->buildRelationItem($relation, array_diff_key($relationData, ['type' => 'type']), $relationData['type']);

$relation->associate($relation->getIncluded()->push($relationItem));
}
Expand Down
57 changes: 57 additions & 0 deletions tests/ItemHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,38 @@ public function it_hydrates_items_with_morphto_relationship()
$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()
{
$data = [
'morphto_relation' => [
'id' => 1,
'type' => 'unmapped-item',
'test_related_attribute1' => 'test',
],
];

$item = new WithRelationshipItem();
$item = $this->getItemHydrator()->hydrate($item, $data);

/** @var \Swis\JsonApi\Client\Relations\MorphToRelation $morphTo */
$morphTo = $item->getRelationship('morphto_relation');

$this->assertEquals('unmapped-item', $morphTo->getType());
$this->assertEquals('unmapped-item', $morphTo->getIncluded()->getType());
$this->assertArrayNotHasKey('type', $morphTo->getIncluded()->getAttributes());
}

/**
* @test
*/
Expand Down Expand Up @@ -265,6 +290,38 @@ public function it_hydrates_items_with_morphtomany_relationship()
$this->assertArrayHasKey('morphtomany_relation', $item->toJsonApiArray()['relationships']);
}

/**
* @test
*/
public function it_hydrates_unmapped_items_with_morphtomany_relationship()
{
$data = [
'morphtomany_relation' => [
[
'id' => 1,
'type' => 'unmapped-item',
'test_related_attribute1' => 'test1',
],
[
'id' => 2,
'type' => 'unmapped-item',
'test_related_attribute1' => 'test2',
],
],
];

$item = new WithRelationshipItem();
$item = $this->getItemHydrator()->hydrate($item, $data);

/** @var \Swis\JsonApi\Client\Relations\MorphToManyRelation $morphToMany */
$morphToMany = $item->getRelationship('morphtomany_relation');

$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());
}

/**
* @test
*/
Expand Down