tests/Encoder/EncoderTest.php
/**
     * Test encode with 'self' and 'related' URLs in main document and relationships.
     */
Test encode with 'self' and 'related' URLs in main document and relationships.
Test encode with ' self ' and ' related ' URLs in main document and relationships .
public function testEncodeLinkWithMeta(): void
    {
        $comments = [
            Comment::instance(5, 'First!'),
            Comment::instance(12, 'I like XML better'),
        ];
        $author   = Author::instance(9, 'Dan', 'Gebhardt', $comments);
        $actual   = Encoder::instance(
            [
                Author::class  => function ($factory) {
                    $schema = new AuthorSchema($factory);
                    $schema->hideRelatedLinkInRelationship(Author::LINK_COMMENTS);
                    $schema->addToRelationship(
                        Author::LINK_COMMENTS,
                        AuthorSchema::RELATIONSHIP_LINKS,
                        [
                            LinkInterface::SELF => function (AuthorSchema $schema, Author $author) {
                                return new Link(
                                    true,
                                    $schema->getSelfSubUrl($author) . '/relationships/comments',
                                    true,
                                    ['some' => 'meta']
                                );
                            },
                        ]
                    );
                    return $schema;
                },
                Comment::class => CommentSchema::class,
            ]
        )->withUrlPrefix('http://example.com')->encodeData($author);
        $expected = <<<EOL
        {
            "data":{
                "type" : "people",
                "id"   : "9",
                "attributes" : {
                    "first_name" : "Dan",
                    "last_name"  : "Gebhardt"
                },
                "relationships" : {
                    "comments"  : {
                        "links" : {
                            "self" : {
                                "href" : "http://example.com/people/9/relationships/comments",
                                "meta" : { "some" : "meta" }
                            }
                        },
                        "data":[
                            { "type" : "comments", "id" : "5" },
                            { "type" : "comments", "id" : "12" }
                        ]
                    }
                },
                "links":{
                    "self":"http://example.com/people/9"
                }
            }
        }
EOL;
        self::assertJsonStringEqualsJsonString($expected, $actual);
    }
public function testEncodeLinkWithMeta ( ) : void { $ comments = [ Comment :: instance ( 5 , 'First!' ) , Comment :: instance ( 12 , 'I like XML better' ) , ] ; $ author = Author :: instance ( 9 , 'Dan' , 'Gebhardt' , $ comments ) ; $ actual = Encoder :: instance ( [ Author :: class => function ( $ factory ) { $ schema = new AuthorSchema ( $ factory ) ; $ schema -> hideRelatedLinkInRelationship ( Author :: LINK_COMMENTS ) ; $ schema -> addToRelationship ( Author :: LINK_COMMENTS , AuthorSchema :: RELATIONSHIP_LINKS , [ LinkInterface :: SELF => function ( AuthorSchema $ schema , Author $ author ) { return new Link ( true , $ schema -> getSelfSubUrl ( $ author ) . '/relationships/comments' , true , [ 'some' => 'meta' ] ) ; } , ] ) ; return $ schema ; } , Comment :: class => CommentSchema :: class , ] ) -> withUrlPrefix ( 'http://example.com' ) -> encodeData ( $ author ) ; $ expected = <<< EOL         {             "data":{                 "type" : "people",                 "id"   : "9",                 "attributes" : {                     "first_name" : "Dan",                     "last_name"  : "Gebhardt"                 },                 "relationships" : {                     "comments"  : {                         "links" : {                             "self" : {                                 "href" : "http://example.com/people/9/relationships/comments",                                 "meta" : { "some" : "meta" }                             }                         },                         "data":[                             { "type" : "comments", "id" : "5" },                             { "type" : "comments", "id" : "12" }                         ]                     }                 },                 "links":{                     "self":"http://example.com/people/9"                 }             }         } 
EOL ; self :: assertJsonStringEqualsJsonString ( $ expected , $ actual ) ; }
Test encode with 'self' and 'related' URLs in main document and relationships.
Test encode with ' self ' and ' related ' URLs in main document and relationships .