Use to transform a resource to an associative array:
abstract class TestCase extends BaseTestCase
{
//...
/**
* Convert the json resource instance to an array.
* @param JsonResource $resource
* @return array
*/
public function resourceToArray(JsonResource $resource): array
{
return json_decode($resource->toJson(), 1);
}
}
$post = Post::factory()->create();
$resource = $this->resourceToArray(PostResource::make($post));
$this->assertEquals($resource, [
'id' => $post->id,
'title' => $post->title,
'content' => $post->content,
]);