tests/Encoder/EncoderTest.php
/**
     * Test encode resource with single item array relationship.
     */
Test encode resource with single item array relationship.
Test encode resource with single item array relationship .
public function testEncodeRelationshipWithSingleItem(): void
    {
        $post = Post::instance(1, 'Title', 'Body', null, [Comment::instance(5, 'First!')]);
        $actual = Encoder::instance(
            [
                Comment::class => CommentSchema::class,
                Post::class    => function ($factory) {
                    $schema = new PostSchema($factory);
                    $schema->removeRelationship(Post::LINK_AUTHOR);
                    $schema->hideDefaultLinksInRelationship(Post::LINK_COMMENTS);
                    return $schema;
                },
            ]
        )->withUrlPrefix('http://example.com')->encodeData($post);
        $expected = <<<EOL
        {
            "data" : {
                "type"  : "posts",
                "id"    : "1",
                "attributes" : {
                    "title" : "Title",
                    "body"  : "Body"
                },
                "relationships" : {
                    "comments" : {
                        "data" : [
                            { "type":"comments", "id":"5" }
                        ]
                    }
                },
                "links" : {
                    "self" : "http://example.com/posts/1"
                }
            }
        }
EOL;
        self::assertJsonStringEqualsJsonString($expected, $actual);
    }
public function testEncodeRelationshipWithSingleItem ( ) : void { $ post = Post :: instance ( 1 , 'Title' , 'Body' , null , [ Comment :: instance ( 5 , 'First!' ) ] ) ; $ actual = Encoder :: instance ( [ Comment :: class => CommentSchema :: class , Post :: class => function ( $ factory ) { $ schema = new PostSchema ( $ factory ) ; $ schema -> removeRelationship ( Post :: LINK_AUTHOR ) ; $ schema -> hideDefaultLinksInRelationship ( Post :: LINK_COMMENTS ) ; return $ schema ; } , ] ) -> withUrlPrefix ( 'http://example.com' ) -> encodeData ( $ post ) ; $ expected = <<< EOL         {             "data" : {                 "type"  : "posts",                 "id"    : "1",                 "attributes" : {                     "title" : "Title",                     "body"  : "Body"                 },                 "relationships" : {                     "comments" : {                         "data" : [                             { "type":"comments", "id":"5" }                         ]                     }                 },                 "links" : {                     "self" : "http://example.com/posts/1"                 }             }         } 
EOL ; self :: assertJsonStringEqualsJsonString ( $ expected , $ actual ) ; }
Test encode resource with single item array relationship.
Test encode resource with single item array relationship .