tests/Encoder/EncoderTest.php
/**
     * Test add links to empty relationship.
     */
Test add links to empty relationship.
Test add links to empty relationship .
public function testAddLinksToEmptyRelationship(): void
    {
        $actual = Encoder::instance(
            [
                Author::class  => AuthorSchema::class,
                Comment::class => CommentSchema::class,
                Post::class    => function ($factory) {
                    $schema = new PostSchema($factory);
                    $schema->removeFromRelationship(Post::LINK_AUTHOR, PostSchema::RELATIONSHIP_DATA);
                    $selfLink    = new Link(false, 'http://foo.boo/custom-self', false);
                    $relatedLink = new Link(false, 'http://foo.boo/custom-related', false);
                    $schema->setSelfLinkInRelationship(Post::LINK_AUTHOR, $selfLink);
                    $schema->setRelatedLinkInRelationship(Post::LINK_AUTHOR, $relatedLink);
                    $schema->addToRelationship(
                        Post::LINK_AUTHOR,
                        PostSchema::RELATIONSHIP_LINKS,
                        ['foo' => new Link(false, '/your/link', false)]
                    );
                    $schema->removeFromRelationship(Post::LINK_COMMENTS, PostSchema::RELATIONSHIP_DATA);
                    $schema->addToRelationship(
                        Post::LINK_COMMENTS,
                        PostSchema::RELATIONSHIP_LINKS,
                        [
                            'boo' => function (PostSchema $schema, Post $post) {
                                return new Link(true, $schema->getSelfSubUrl($post) . '/another/link', false);
                            },
                        ]
                    );
                    $schema->hideRelatedLinkInRelationship(Post::LINK_COMMENTS);
                    return $schema;
                },
            ]
        )->withUrlPrefix('http://example.com')->encodeData($this->getStandardPost());
        $expected = <<<EOL
        {
            "data" : {
                "type"  : "posts",
                "id"    : "1",
                "attributes" : {
                    "title" : "JSON API paints my bikeshed!",
                    "body"  : "Outside every fat man there was an even fatter man trying to close in"
                },
                "relationships" : {
                    "author" : {
                        "links" : {
                            "self"    : "http://foo.boo/custom-self",
                            "related" : "http://foo.boo/custom-related",
                            "foo"     : "/your/link"
                        }
                    },
                    "comments" : {
                        "links" : {
                            "boo"  : "http://example.com/posts/1/another/link",
                            "self" : "http://example.com/posts/1/relationships/comments"
                        }
                    }
                },
                "links" : {
                    "self" : "http://example.com/posts/1"
                }
            }
        }
EOL;
        self::assertJsonStringEqualsJsonString($expected, $actual);
    }
public function testAddLinksToEmptyRelationship ( ) : void { $ actual = Encoder :: instance ( [ Author :: class => AuthorSchema :: class , Comment :: class => CommentSchema :: class , Post :: class => function ( $ factory ) { $ schema = new PostSchema ( $ factory ) ; $ schema -> removeFromRelationship ( Post :: LINK_AUTHOR , PostSchema :: RELATIONSHIP_DATA ) ; $ selfLink = new Link ( false , 'http://foo.boo/custom-self' , false ) ; $ relatedLink = new Link ( false , 'http://foo.boo/custom-related' , false ) ; $ schema -> setSelfLinkInRelationship ( Post :: LINK_AUTHOR , $ selfLink ) ; $ schema -> setRelatedLinkInRelationship ( Post :: LINK_AUTHOR , $ relatedLink ) ; $ schema -> addToRelationship ( Post :: LINK_AUTHOR , PostSchema :: RELATIONSHIP_LINKS , [ 'foo' => new Link ( false , '/your/link' , false ) ] ) ; $ schema -> removeFromRelationship ( Post :: LINK_COMMENTS , PostSchema :: RELATIONSHIP_DATA ) ; $ schema -> addToRelationship ( Post :: LINK_COMMENTS , PostSchema :: RELATIONSHIP_LINKS , [ 'boo' => function ( PostSchema $ schema , Post $ post ) { return new Link ( true , $ schema -> getSelfSubUrl ( $ post ) . '/another/link' , false ) ; } , ] ) ; $ schema -> hideRelatedLinkInRelationship ( Post :: LINK_COMMENTS ) ; return $ schema ; } , ] ) -> withUrlPrefix ( 'http://example.com' ) -> encodeData ( $ this -> getStandardPost ( ) ) ; $ expected = <<< EOL         {             "data" : {                 "type"  : "posts",                 "id"    : "1",                 "attributes" : {                     "title" : "JSON API paints my bikeshed!",                     "body"  : "Outside every fat man there was an even fatter man trying to close in"                 },                 "relationships" : {                     "author" : {                         "links" : {                             "self"    : "http://foo.boo/custom-self",                             "related" : "http://foo.boo/custom-related",                             "foo"     : "/your/link"                         }                     },                     "comments" : {                         "links" : {                             "boo"  : "http://example.com/posts/1/another/link",                             "self" : "http://example.com/posts/1/relationships/comments"                         }                     }                 },                 "links" : {                     "self" : "http://example.com/posts/1"                 }             }         } 
EOL ; self :: assertJsonStringEqualsJsonString ( $ expected , $ actual ) ; }
Test add links to empty relationship.
Test add links to empty relationship .