tests/Encoder/EncodeSimpleObjectsTest.php
/**
     * Test encode for 2 relationships that have identical name beginnings.
     *
     * @return void
     *
     * @link https://github.com/neomerx/json-api/issues/200
     */
Test encode for 2 relationships that have identical name beginnings.
@return void


Test encode for 2 relationships that have identical name beginnings . @return void
public function testEncodingSimilarRelationships(): void
    {
        $secondRelName = Author::LINK_COMMENTS . '-second-name';
        $comment1 = Comment::instance(1, 'One!');
        $comment5 = Comment::instance(5, 'Five!');
        $author   = Author::instance(9, 'Dan', 'Gebhardt', [$comment1]);
        $actual = Encoder::instance(
            [
                Author::class  => function ($factory) use ($secondRelName, $comment5) {
                    $schema = new AuthorSchema($factory);
                    $schema->addToRelationship(
                        $secondRelName,
                        AuthorSchema::RELATIONSHIP_DATA,
                        function () use ($comment5
                        ) {
                            return [$comment5];
                        }
                    );
                    $schema->hideDefaultLinksInRelationship(Author::LINK_COMMENTS);
                    $schema->hideDefaultLinksInRelationship($secondRelName);
                    return $schema;
                },
                Comment::class => CommentSchema::class,
            ]
        )->withUrlPrefix('http://example.com')->withIncludedPaths(
            [$secondRelName]
        )->encodeData($author);
        $expected = <<<EOL
        {
            "data": {
                "type": "people",
                "id": "9",
                "attributes": {
                    "first_name": "Dan",
                    "last_name": "Gebhardt"
                },
                "relationships": {
                    "comments": {
                    "data": [
                        {
                            "type": "comments",
                            "id": "1"
                        }
                    ]
                    },
                    "comments-second-name": {
                        "data": [
                            {
                                "type": "comments",
                                "id": "5"
                            }
                        ]
                    }
                },
                "links": {
                    "self": "http://example.com/people/9"
                }
            },
            "included": [
                {
                    "type": "comments",
                    "id": "5",
                    "attributes": {
                    "body": "Five!"
                    },
                    "relationships": {
                        "author": {
                            "links": {
                                "self"    : "http://example.com/comments/5/relationships/author",
                                "related" : "http://example.com/comments/5/author"
                            },
                            "data": null
                        }
                    },
                    "links": {
                        "self": "http://example.com/comments/5"
                    }
                }
            ]
        }
EOL;
        self::assertJsonStringEqualsJsonString($expected, $actual);
    }
public function testEncodingSimilarRelationships ( ) : void { $ secondRelName = Author :: LINK_COMMENTS . '-second-name' ; $ comment1 = Comment :: instance ( 1 , 'One!' ) ; $ comment5 = Comment :: instance ( 5 , 'Five!' ) ; $ author = Author :: instance ( 9 , 'Dan' , 'Gebhardt' , [ $ comment1 ] ) ; $ actual = Encoder :: instance ( [ Author :: class => function ( $ factory ) use ( $ secondRelName , $ comment5 ) { $ schema = new AuthorSchema ( $ factory ) ; $ schema -> addToRelationship ( $ secondRelName , AuthorSchema :: RELATIONSHIP_DATA , function ( ) use ( $ comment5 ) { return [ $ comment5 ] ; } ) ; $ schema -> hideDefaultLinksInRelationship ( Author :: LINK_COMMENTS ) ; $ schema -> hideDefaultLinksInRelationship ( $ secondRelName ) ; return $ schema ; } , Comment :: class => CommentSchema :: class , ] ) -> withUrlPrefix ( 'http://example.com' ) -> withIncludedPaths ( [ $ secondRelName ] ) -> encodeData ( $ author ) ; $ expected = <<< EOL         {             "data": {                 "type": "people",                 "id": "9",                 "attributes": {                     "first_name": "Dan",                     "last_name": "Gebhardt"                 },                 "relationships": {                     "comments": {                     "data": [                         {                             "type": "comments",                             "id": "1"                         }                     ]                     },                     "comments-second-name": {                         "data": [                             {                                 "type": "comments",                                 "id": "5"                             }                         ]                     }                 },                 "links": {                     "self": "http://example.com/people/9"                 }             },             "included": [                 {                     "type": "comments",                     "id": "5",                     "attributes": {                     "body": "Five!"                     },                     "relationships": {                         "author": {                             "links": {                                 "self"    : "http://example.com/comments/5/relationships/author",                                 "related" : "http://example.com/comments/5/author"                             },                             "data": null                         }                     },                     "links": {                         "self": "http://example.com/comments/5"                     }                 }             ]         } 
EOL ; self :: assertJsonStringEqualsJsonString ( $ expected , $ actual ) ; }
Test encode for 2 relationships that have identical name beginnings.
Test encode for 2 relationships that have identical name beginnings .
/**
         * It's odd to have a second comments relationship and the naming is also weird but...
         * let's check a relationship that which naming start identical to the first one.
         */ // make the author have the comment only in that odd relationship // we will emulate the new relationship with that comment // hide links // include only the new odd relationship and omit the original `comments` relationship // The issue was that comment with id 1 was also added in `included` section