tests/Encoder/EncoderTest.php
/**
     * Test encode 2 duplicate resources with circular link to each other.
     */
Test encode 2 duplicate resources with circular link to each other.
Test encode 2 duplicate resources with circular link to each other .
public function testEncodeDuplicatesWithCircularReferencesInData(): void
    {
        $author = Author::instance(9, 'Dan', 'Gebhardt');
        $author->{Author::LINK_COMMENTS} = $author;
        $encoder = Encoder::instance(
            [
                Author::class => AuthorSchema::class,
            ]
        )->withUrlPrefix('http://example.com');
        $actual = $encoder->encodeData([$author, $author]);
        $expected = <<<EOL
        {
            "data" : [
                {
                    "type"       : "people",
                    "id"         : "9",
                    "attributes" : {
                        "first_name" : "Dan",
                        "last_name"  : "Gebhardt"
                    },
                    "relationships" : {
                        "comments" : {
                            "links": {
                                "self"    : "http://example.com/people/9/relationships/comments",
                                "related" : "http://example.com/people/9/comments"
                            },
                            "data" : { "type" : "people", "id" : "9" }
                        }
                    },
                    "links" : {
                        "self"     : "http://example.com/people/9"
                    }
                }, {
                    "type"       : "people",
                    "id"         : "9",
                    "attributes" : {
                        "first_name" : "Dan",
                        "last_name"  : "Gebhardt"
                    },
                    "relationships" : {
                        "comments" : {
                            "links": {
                                "self"    : "http://example.com/people/9/relationships/comments",
                                "related" : "http://example.com/people/9/comments"
                            },
                            "data" : { "type" : "people", "id" : "9" }
                        }
                    },
                    "links" : {
                        "self" : "http://example.com/people/9"
                    }
                }
            ]
        }
EOL;
        self::assertJsonStringEqualsJsonString($expected, $actual);
    }
public function testEncodeDuplicatesWithCircularReferencesInData ( ) : void { $ author = Author :: instance ( 9 , 'Dan' , 'Gebhardt' ) ; $ author -> { Author :: LINK_COMMENTS } = $ author ; $ encoder = Encoder :: instance ( [ Author :: class => AuthorSchema :: class , ] ) -> withUrlPrefix ( 'http://example.com' ) ; $ actual = $ encoder -> encodeData ( [ $ author , $ author ] ) ; $ expected = <<< EOL         {             "data" : [                 {                     "type"       : "people",                     "id"         : "9",                     "attributes" : {                         "first_name" : "Dan",                         "last_name"  : "Gebhardt"                     },                     "relationships" : {                         "comments" : {                             "links": {                                 "self"    : "http://example.com/people/9/relationships/comments",                                 "related" : "http://example.com/people/9/comments"                             },                             "data" : { "type" : "people", "id" : "9" }                         }                     },                     "links" : {                         "self"     : "http://example.com/people/9"                     }                 }, {                     "type"       : "people",                     "id"         : "9",                     "attributes" : {                         "first_name" : "Dan",                         "last_name"  : "Gebhardt"                     },                     "relationships" : {                         "comments" : {                             "links": {                                 "self"    : "http://example.com/people/9/relationships/comments",                                 "related" : "http://example.com/people/9/comments"                             },                             "data" : { "type" : "people", "id" : "9" }                         }                     },                     "links" : {                         "self" : "http://example.com/people/9"                     }                 }             ]         } 
EOL ; self :: assertJsonStringEqualsJsonString ( $ expected , $ actual ) ; }
Test encode 2 duplicate resources with circular link to each other.
Test encode 2 duplicate resources with circular link to each other .
// That's gonna be a bit of a hack. We put author himself to author->comments link. // Don't be confused by the link name. It does not matter and I just don't want to create a new schema.