From 8898462141607065b6196aba2afbfbebf84668cc Mon Sep 17 00:00:00 2001 From: EK Date: Fri, 26 Jun 2015 00:16:03 +0300 Subject: [PATCH] Closes #35 --- src/Document/Presenters/ElementPresenter.php | 9 +- tests/Encoder/EncodeIncludedObjectsTest.php | 112 +++++++++++++++++++ 2 files changed, 118 insertions(+), 3 deletions(-) diff --git a/src/Document/Presenters/ElementPresenter.php b/src/Document/Presenters/ElementPresenter.php index d68a4dd9..595a178b 100644 --- a/src/Document/Presenters/ElementPresenter.php +++ b/src/Document/Presenters/ElementPresenter.php @@ -60,10 +60,13 @@ public function setRelationshipTo( $name = $relation->getName(); $parentExists = isset($target[$parentType][$parentId]); - assert('$parentExists === true'); - assert('isset($target[$parentType][$parentId][\''.Document::KEYWORD_RELATIONSHIPS.'\'][$name]) === false'); - + // parent object might be already fully parsed (with children) so + // - it won't exist in $target + // - it won't make any sense to parse it again (we'll got exactly the same result and it will be thrown away + // as duplicate relations/included resources are not allowed) if ($parentExists === true) { + assert('isset($target[$parentType][$parentId][\''.Document::KEYWORD_RELATIONSHIPS.'\'][$name]) === false'); + $representation = []; if ($relation->isShowData() === true) { diff --git a/tests/Encoder/EncodeIncludedObjectsTest.php b/tests/Encoder/EncodeIncludedObjectsTest.php index c1dfde9c..ae8bac81 100644 --- a/tests/Encoder/EncodeIncludedObjectsTest.php +++ b/tests/Encoder/EncodeIncludedObjectsTest.php @@ -463,4 +463,116 @@ public function testEncodeWithLinkWithPagination() $this->assertEquals($expected, $actual); } + + /** + * Test encode deep duplicate hierarchies. + * + * Test for issue 35 + */ + public function testEncodeDeepDuplicateHierarchies() + { + $actual = Encoder::instance([ + Author::class => AuthorSchema::class, + Comment::class => CommentSchema::class, + Post::class => PostSchema::class, + Site::class => SiteSchema::class, + ], $this->encoderOptions)->encode([$this->site, $this->site]); + + $expected = <<assertEquals($expected, $actual); + } }