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

tylernathanreed's avatar

Testing JSON API without AJAX

I'm working on a project where all of my API calls go through the backend, using Httpful, Guzzle, or whatever. I'm trying to test my API from the consumer's point of view.

However, because I'm essentially making a Curl Request, when the Request is picked up by Laravel, I've actually left the PHPUnit Environment, and built a second instance of the application. This means that traits like use DatabaseMigrations and such are effectively useless, as anything I do through the API will end up persisting.

Rather than do a ton of manual clean-up, is there any good solution to telling the API how to handle testing? I'll be working with User Authentication, Billing, Mailing, and several other concepts.

0 likes
1 reply
Thijmen's avatar

You can do something like this:

public function testWeGetTheCorrectResponse() {
    $this->get(route('api.users'))->seeJson(['success'  => true]);
}

In this way, you will remain in your testsuite and your traits will work :-)

Please or to participate in this conversation.