Skip to content

Commit b68dd3a

Browse files
committed
v0.5.2
2 parents 4a25d2d + 8898462 commit b68dd3a

File tree

3 files changed

+155
-3
lines changed

3 files changed

+155
-3
lines changed

src/Document/Presenters/ElementPresenter.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,13 @@ public function setRelationshipTo(
6060
$name = $relation->getName();
6161
$parentExists = isset($target[$parentType][$parentId]);
6262

63-
assert('$parentExists === true');
64-
assert('isset($target[$parentType][$parentId][\''.Document::KEYWORD_RELATIONSHIPS.'\'][$name]) === false');
65-
63+
// parent object might be already fully parsed (with children) so
64+
// - it won't exist in $target
65+
// - it won't make any sense to parse it again (we'll got exactly the same result and it will be thrown away
66+
// as duplicate relations/included resources are not allowed)
6667
if ($parentExists === true) {
68+
assert('isset($target[$parentType][$parentId][\''.Document::KEYWORD_RELATIONSHIPS.'\'][$name]) === false');
69+
6770
$representation = [];
6871

6972
if ($relation->isShowData() === true) {

tests/Encoder/EncodeIncludedObjectsTest.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,4 +463,116 @@ public function testEncodeWithLinkWithPagination()
463463

464464
$this->assertEquals($expected, $actual);
465465
}
466+
467+
/**
468+
* Test encode deep duplicate hierarchies.
469+
*
470+
* Test for issue 35
471+
*/
472+
public function testEncodeDeepDuplicateHierarchies()
473+
{
474+
$actual = Encoder::instance([
475+
Author::class => AuthorSchema::class,
476+
Comment::class => CommentSchema::class,
477+
Post::class => PostSchema::class,
478+
Site::class => SiteSchema::class,
479+
], $this->encoderOptions)->encode([$this->site, $this->site]);
480+
481+
$expected = <<<EOL
482+
{
483+
"data" : [{
484+
"type" : "sites",
485+
"id" : "2",
486+
"attributes" : {
487+
"name" : "site name"
488+
},
489+
"relationships" : {
490+
"posts" : {
491+
"data" : {
492+
"type" : "posts",
493+
"id" : "1"
494+
}
495+
}
496+
},
497+
"links" : {
498+
"self" : "http://example.com/sites/2"
499+
}
500+
}, {
501+
"type" : "sites",
502+
"id" : "2",
503+
"attributes" : {
504+
"name" : "site name"
505+
},
506+
"relationships" : {
507+
"posts" : {
508+
"data" : {
509+
"type" : "posts",
510+
"id" : "1"
511+
}
512+
}
513+
},
514+
"links" : {
515+
"self" : "http://example.com/sites/2"
516+
}
517+
}],
518+
"included" : [{
519+
"type" : "people",
520+
"id" : "9",
521+
"attributes" : {
522+
"first_name" : "Dan",
523+
"last_name" : "Gebhardt"
524+
},
525+
"relationships":{
526+
"comments" : { "data":null }
527+
}
528+
}, {
529+
"type" : "comments",
530+
"id" : "5",
531+
"attributes" : {
532+
"body" : "First!"
533+
},
534+
"relationships" : {
535+
"author" : {
536+
"data" : { "type":"people", "id":"9" }
537+
}
538+
},
539+
"links":{
540+
"self" : "http://example.com/comments/5"
541+
}
542+
}, {
543+
"type" : "comments",
544+
"id" : "12",
545+
"attributes" : {
546+
"body" : "I like XML better"
547+
},
548+
"relationships":{
549+
"author" : {
550+
"data" : { "type":"people", "id":"9" }
551+
}
552+
},
553+
"links":{
554+
"self" : "http://example.com/comments/12"
555+
}
556+
}, {
557+
"type" : "posts",
558+
"id" : "1",
559+
"attributes" : {
560+
"title" : "JSON API paints my bikeshed!",
561+
"body" : "Outside every fat man there was an even fatter man trying to close in"
562+
},
563+
"relationships" : {
564+
"author" : { "data" : { "type" : "people", "id" : "9" } },
565+
"comments" : { "data" : [
566+
{ "type" : "comments", "id" : "5" },
567+
{ "type" : "comments", "id" : "12" }
568+
]}
569+
}
570+
}]
571+
}
572+
EOL;
573+
// remove formatting from 'expected'
574+
$expected = json_encode(json_decode($expected));
575+
576+
$this->assertEquals($expected, $actual);
577+
}
466578
}

tests/Encoder/EncodeSimpleObjectsTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,43 @@ public function testEncodeObjectWithAttributesOnlyInArray()
158158
$this->assertEquals($expected, $actual);
159159
}
160160

161+
/**
162+
* Test encode simple object with attributes only associative array.
163+
*/
164+
public function testEncodeObjectWithAttributesOnlyInAssocArray()
165+
{
166+
$author = Author::instance(9, 'Dan', 'Gebhardt');
167+
$endcoder = Encoder::instance([
168+
Author::class => function ($factory, $container) {
169+
$schema = new AuthorSchema($factory, $container);
170+
$schema->linkRemove(Author::LINK_COMMENTS);
171+
return $schema;
172+
}
173+
], $this->encoderOptions);
174+
175+
$actual = $endcoder->encode(['key_doesnt_matter' => $author]);
176+
177+
$expected = <<<EOL
178+
{
179+
"data" : [{
180+
"type" : "people",
181+
"id" : "9",
182+
"attributes" : {
183+
"first_name" : "Dan",
184+
"last_name" : "Gebhardt"
185+
},
186+
"links" : {
187+
"self" : "http://example.com/people/9"
188+
}
189+
}]
190+
}
191+
EOL;
192+
// remove formatting from 'expected'
193+
$expected = json_encode(json_decode($expected));
194+
195+
$this->assertEquals($expected, $actual);
196+
}
197+
161198
/**
162199
* Test encode simple object in pretty format.
163200
*/

0 commit comments

Comments
 (0)