Skip to content

Commit a47d50f

Browse files
authored
Merge pull request #58 from Art4/57-bugfix-parsing-relationship-links
Fix #57 bugfix parsing relationship links
2 parents 95c6160 + d1fd306 commit a47d50f

File tree

5 files changed

+51
-9
lines changed

5 files changed

+51
-9
lines changed

src/V1/RelationshipLink.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ protected function parse($object)
6060
$links = get_object_vars($object);
6161

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

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

6969
unset($links['self']);
7070
}

tests/Integration/ParsingTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,4 +665,19 @@ public function testParseIdAsInteger()
665665
// Test full array
666666
$this->assertEquals(json_decode($string, true), $document->asArray(true));
667667
}
668+
669+
/**
670+
* @test
671+
*/
672+
public function testParseLinksInRelationshipsCorrectly()
673+
{
674+
$string = $this->getJsonString('17_relationship_links.json');
675+
$document = Helper::parseResponseBody($string);
676+
677+
$this->assertInstanceOf('Art4\JsonApiClient\Document', $document);
678+
$this->assertSame(['data'], $document->getKeys());
679+
680+
// Test full array
681+
$this->assertEquals(json_decode($string, true), $document->asArray(true));
682+
}
668683
}

tests/Unit/RelationshipLinkTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,12 @@ public function testCreateWithoutSelfAndRelatedPropertiesThrowsException()
311311
*
312312
* @param mixed $input
313313
*/
314-
public function testSelfMustBeAString($input)
314+
public function testSelfMustBeAStringOrObject($input)
315315
{
316316
$link = new RelationshipLink($this->manager, $this->relationship);
317317

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

322322
return;
@@ -327,7 +327,7 @@ public function testSelfMustBeAString($input)
327327

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

333333
$link->parse($object);

tests/Unit/V1/RelationshipLinkTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,20 +264,20 @@ public function testCreateWithoutSelfAndRelatedPropertiesThrowsException()
264264
}
265265

266266
/**
267-
* @dataProvider jsonValuesProviderWithoutString
267+
* @dataProvider jsonValuesProviderWithoutObjectAndString
268268
*
269269
* self: a link for the relationship itself (a "relationship link").
270270
*
271271
* @param mixed $input
272272
*/
273-
public function testSelfMustBeAString($input)
273+
public function testSelfMustBeAStringOrObject($input)
274274
{
275275
$object = new \stdClass();
276276
$object->self = $input;
277277

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

283283
$link = new RelationshipLink($object, $this->manager, $this->relationship);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"data": [
3+
{
4+
"type": "articles",
5+
"id": "1",
6+
"attributes": {
7+
"title": "JSON API paints my bikeshed!"
8+
},
9+
"relationships": {
10+
"vid": {
11+
"data": {
12+
"type": "taxonomy_vocabulary--taxonomy_vocabulary",
13+
"id": "e0b18172-b9fa-4a2f-88c0-ed19dca65a1b"
14+
},
15+
"links": {
16+
"self": {
17+
"href": "*/jsonapi/taxonomy_term/district/3fa7e3cd-ce96-4be7-abac-7203bf8a9f4f/relationships/vid"
18+
},
19+
"related": {
20+
"href": "*/jsonapi/taxonomy_term/district/3fa7e3cd-ce96-4be7-abac-7203bf8a9f4f/vid"
21+
}
22+
}
23+
}
24+
}
25+
}
26+
]
27+
}

0 commit comments

Comments
 (0)