@@ -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}
0 commit comments