HTTP tests
Call endpoints
HTTP tests drive the app without a browser by calling routes directly. The get and post helpers return a response object with rich asserts, so each test reads like a short script of a real visit.
class PagesTest extends TestCase { public function test_homepage_loads(): void { $response = $this->get("/"); $response->assertStatus(200); } }
Acting as a user
Protected pages need a signed in user before the call. The actingAs helper signs in any model you hand it, and model factories build a fresh user in one line with no manual setup.
class DashboardTest extends TestCase { public function test_member_sees_dashboard(): void { $user = User::factory()->create(); $response = $this->actingAs($user)->get("/dashboard"); $response->assertStatus(200); } }
Assert JSON
API routes answer with JSON, so assert the decoded body instead of raw text. The assertJson method checks a subset of keys, which keeps tests stable when extra fields appear later.
class StatusTest extends TestCase { public function test_status_endpoint(): void { $response = $this->get("/status"); $response->assertJson(["ok" => true]); } }
Assert status
Every HTTP test should pin the status code first. Code 200 means the page rendered, 302 means a redirect fired, and 404 means the route missed, so the number alone already tells a story.
class MissingTest extends TestCase { public function test_unknown_page_missing(): void { $response = $this->get("/no-such-page"); $response->assertStatus(404); } }
Next: Collections
Article author: Arthur Isaev