Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

futurefuture's avatar

Testing With API Resource as assertion

Hey there!

Looking for some help here if anyone has it.

Let's say I have an API Resource such as this:

class Resource extends JsonResource
{
    public function toArray($request)
    {
        return [
           'foo' => $this->foo,
		   'bar' => $this->bar
        ];
    }
}

And a test like this:

class ProductTest extends TestCase
{
   /**
     * @test
     */
    public function test_api()
    {
        $response = $this->get($this->some_url);

        $response->assertJsonStructure([
            'foo',
            'bar'
        ]);
   }
}

Is it possible to do something like this to test for the param returned:

class ProductTest extends TestCase
{
   /**
     * @test
     */
    public function test_api()
    {
        $response = $this->get($this->some_url);

        $response->assertResponse($response, Resource::class);
   }
}

I'm constantly writing out everything expected in the JSON response of my tests and would love a way to just test it against the expected response of the API resource.

Thanks in advance!

0 likes
0 replies

Please or to participate in this conversation.