tests/Encoder/EncodeSimpleObjectsTest.php
/**
     * Test encode array-based objects.
     *
     * @see https://github.com/neomerx/json-api/pull/214
     */
Test encode array-based objects.
Test encode array - based objects .
public function testEncodeArrayBasedObject(): void
    {
        $author  = new AuthorCModel(9, 'Dan', 'Gebhardt');
        $encoder = Encoder::instance(
            [
                AuthorCModel::class => AuthorCModelSchema::class,
            ]
        );
        $actual = $encoder->withUrlPrefix('http://example.com')->encodeData($author);
        $expected = <<<EOL
        {
            "data" : {
                "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"
                        }
                    }
                },
                "links" : {
                    "self" : "http://example.com/people/9"
                }
            }
        }
EOL;
        self::assertJsonStringEqualsJsonString($expected, $actual);
        $actual = $encoder->withUrlPrefix('http://example.com')->encodeData([$author]);
        $expected = <<<EOL
        {
            "data" : [{
                "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"
                        }
                    }
                },
                "links" : {
                    "self" : "http://example.com/people/9"
                }
            }]
        }
EOL;
        self::assertJsonStringEqualsJsonString($expected, $actual);
    }
public function testEncodeArrayBasedObject ( ) : void { $ author = new AuthorCModel ( 9 , 'Dan' , 'Gebhardt' ) ; $ encoder = Encoder :: instance ( [ AuthorCModel :: class => AuthorCModelSchema :: class , ] ) ; $ actual = $ encoder -> withUrlPrefix ( 'http://example.com' ) -> encodeData ( $ author ) ; $ expected = <<< EOL         {             "data" : {                 "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"                         }                     }                 },                 "links" : {                     "self" : "http://example.com/people/9"                 }             }         } 
EOL ; self :: assertJsonStringEqualsJsonString ( $ expected , $ actual ) ; $ actual = $ encoder -> withUrlPrefix ( 'http://example.com' ) -> encodeData ( [ $ author ] ) ; $ expected = <<< EOL         {             "data" : [{                 "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"                         }                     }                 },                 "links" : {                     "self" : "http://example.com/people/9"                 }             }]         } 
EOL ; self :: assertJsonStringEqualsJsonString ( $ expected , $ actual ) ; }
Test encode array-based objects.
Test encode array - based objects .
// same but as array