Skip to content

Commit

Permalink
Merge pull request #58 from Art4/57-bugfix-parsing-relationship-links
Browse files Browse the repository at this point in the history
Fix #57 bugfix parsing relationship links
  • Loading branch information
Art4 authored Jun 20, 2020
2 parents 95c6160 + d1fd306 commit a47d50f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/V1/RelationshipLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ protected function parse($object)
$links = get_object_vars($object);

if (array_key_exists('self', $links)) {
if (! is_string($links['self'])) {
throw new ValidationException('property "self" has to be a string, "' . gettype($links['self']) . '" given.');
if (! is_string($links['self']) and ! is_object($links['self'])) {
throw new ValidationException('property "self" has to be a string or object, "' . gettype($links['self']) . '" given.');
}

$this->set('self', strval($links['self']));
$this->setLink('self', $links['self']);

unset($links['self']);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Integration/ParsingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -665,4 +665,19 @@ public function testParseIdAsInteger()
// Test full array
$this->assertEquals(json_decode($string, true), $document->asArray(true));
}

/**
* @test
*/
public function testParseLinksInRelationshipsCorrectly()
{
$string = $this->getJsonString('17_relationship_links.json');
$document = Helper::parseResponseBody($string);

$this->assertInstanceOf('Art4\JsonApiClient\Document', $document);
$this->assertSame(['data'], $document->getKeys());

// Test full array
$this->assertEquals(json_decode($string, true), $document->asArray(true));
}
}
6 changes: 3 additions & 3 deletions tests/Unit/RelationshipLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,12 @@ public function testCreateWithoutSelfAndRelatedPropertiesThrowsException()
*
* @param mixed $input
*/
public function testSelfMustBeAString($input)
public function testSelfMustBeAStringOrObject($input)
{
$link = new RelationshipLink($this->manager, $this->relationship);

// Input must be a string
if (gettype($input) === 'string') {
if (gettype($input) === 'string' or gettype($input) === 'object') {
$this->assertInstanceOf('Art4\JsonApiClient\RelationshipLink', $link);

return;
Expand All @@ -327,7 +327,7 @@ public function testSelfMustBeAString($input)

$this->setExpectedException(
'Art4\JsonApiClient\Exception\ValidationException',
'property "self" has to be a string, "' . gettype($input) . '" given.'
'property "self" has to be a string or object, "' . gettype($input) . '" given.'
);

$link->parse($object);
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/V1/RelationshipLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,20 +264,20 @@ public function testCreateWithoutSelfAndRelatedPropertiesThrowsException()
}

/**
* @dataProvider jsonValuesProviderWithoutString
* @dataProvider jsonValuesProviderWithoutObjectAndString
*
* self: a link for the relationship itself (a "relationship link").
*
* @param mixed $input
*/
public function testSelfMustBeAString($input)
public function testSelfMustBeAStringOrObject($input)
{
$object = new \stdClass();
$object->self = $input;

$this->expectException(ValidationException::class);
$this->expectExceptionMessage(
'property "self" has to be a string, "' . gettype($input) . '" given.'
'property "self" has to be a string or object, "' . gettype($input) . '" given.'
);

$link = new RelationshipLink($object, $this->manager, $this->relationship);
Expand Down
27 changes: 27 additions & 0 deletions tests/files/17_relationship_links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"data": [
{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
},
"relationships": {
"vid": {
"data": {
"type": "taxonomy_vocabulary--taxonomy_vocabulary",
"id": "e0b18172-b9fa-4a2f-88c0-ed19dca65a1b"
},
"links": {
"self": {
"href": "*/jsonapi/taxonomy_term/district/3fa7e3cd-ce96-4be7-abac-7203bf8a9f4f/relationships/vid"
},
"related": {
"href": "*/jsonapi/taxonomy_term/district/3fa7e3cd-ce96-4be7-abac-7203bf8a9f4f/vid"
}
}
}
}
}
]
}

0 comments on commit a47d50f

Please sign in to comment.