tests/Encoder/EncodeSimpleObjectsTest.php
/**
     * Test encode simple object as resource identity with included resources.
     */
Test encode simple object as resource identity with included resources.
Test encode simple object as resource identity with included resources .
public function testEncodeObjectAsResourceIdentityWithIncludes(): void
    {
        $comment                         = Comment::instance(1, 'One!');
        $author                          = Author::instance(9, 'Dan', 'Gebhardt', [$comment]);
        $comment->{Comment::LINK_AUTHOR} = $author;
        $actual = Encoder::instance(
            [
                Author::class  => AuthorSchema::class,
                Comment::class => CommentSchema::class,
            ]
        )
            ->withUrlPrefix('http://example.com')
            ->withIncludedPaths([Comment::LINK_AUTHOR])
            ->encodeIdentifiers($comment);
        $expected = <<<EOL
        {
            "data" : {
                "type" : "comments",
                "id"   : "1"
            },
            "included": [
                {
                    "type" : "comments",
                    "id"   : "1",
                    "attributes": {
                        "body": "One!"
                    },
                    "relationships": {
                        "author": {
                            "links": {
                                "self"    : "http://example.com/comments/1/relationships/author",
                                "related" : "http://example.com/comments/1/author"
                            },
                            "data": {
                                "type" : "people",
                                "id"   : "9"
                            }
                        }
                    },
                    "links": {
                        "self": "http://example.com/comments/1"
                    }
                }, {
                    "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" : "comments", "id"   : "1" }
                            ]
                        }
                    },
                    "links": {
                        "self": "http://example.com/people/9"
                    }
                }
            ]
        }
EOL;
        self::assertJsonStringEqualsJsonString($expected, $actual);
    }
public function testEncodeObjectAsResourceIdentityWithIncludes ( ) : void { $ comment = Comment :: instance ( 1 , 'One!' ) ; $ author = Author :: instance ( 9 , 'Dan' , 'Gebhardt' , [ $ comment ] ) ; $ comment -> { Comment :: LINK_AUTHOR } = $ author ; $ actual = Encoder :: instance ( [ Author :: class => AuthorSchema :: class , Comment :: class => CommentSchema :: class , ] ) -> withUrlPrefix ( 'http://example.com' ) -> withIncludedPaths ( [ Comment :: LINK_AUTHOR ] ) -> encodeIdentifiers ( $ comment ) ; $ expected = <<< EOL         {             "data" : {                 "type" : "comments",                 "id"   : "1"             },             "included": [                 {                     "type" : "comments",                     "id"   : "1",                     "attributes": {                         "body": "One!"                     },                     "relationships": {                         "author": {                             "links": {                                 "self"    : "http://example.com/comments/1/relationships/author",                                 "related" : "http://example.com/comments/1/author"                             },                             "data": {                                 "type" : "people",                                 "id"   : "9"                             }                         }                     },                     "links": {                         "self": "http://example.com/comments/1"                     }                 }, {                     "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" : "comments", "id"   : "1" }                             ]                         }                     },                     "links": {                         "self": "http://example.com/people/9"                     }                 }             ]         } 
EOL ; self :: assertJsonStringEqualsJsonString ( $ expected , $ actual ) ; }
Test encode simple object as resource identity with included resources.
Test encode simple object as resource identity with included resources .