How Laravel Handles Requests (and How Your Tests Fake It)
When you're writing a Laravel test like this:
$this->get('/subscribe');
…it looks like you're making a real HTTP request — like a browser visiting your site. But you’re not.
Laravel is doing something a little magical behind the scenes. And understanding that magic helps you become a better developer and tester.
Let’s take a look.
🛣️ What Happens When a Real User Visits Your Site
When someone visits your Laravel app in their browser (e.g. https://example.com/subscribe
), this is roughly what happens:
- The web server (like Apache or Nginx) points all requests to the
public/index.php
file. - This
index.php
file is the entry point to your Laravel app — it’s called the front controller. - From there, Laravel:
- Boots the application
- Loads up the routes and middleware
- Figures out which controller or closure should handle the request
- Runs that code and sends back a response (like an HTML page, JSON, or redirect)
So when a browser makes a request, Laravel does all the work — but it starts with the index.php
file.
🧪 What Happens When You Call $this->get('/subscribe')
in a Test
When you call $this->get()
in a test, Laravel doesn't go through index.php
at all.
Instead, it:
- Creates a test version of your app, just like it would in production — but in memory.
- Passes the request directly into the HTTP Kernel, which is the core part of Laravel that handles incoming requests.
- Skips the web server, skips the network, and simulates the whole process entirely in PHP.
- Returns a TestResponse, which you can assert on (e.g.
assertSee
,assertRedirect
, etc.).
This is called feature testing. You're testing how features of your app behave together — routes, middleware, controllers, views — without needing a browser or server.
🤯 True Story: A Lead Dev Who Didn't Believe This
A few years ago, a lead developer on a project I was working on refused to believe that $this->get()
didn’t make a real HTTP request.
He was convinced Laravel must be spinning up a tiny server in the background and hitting it through curl
.
Spoiler: it wasn't.
Now you understand how this really works — and you officially know more than that lead dev. 😎
(And probably quite a few others!)
0 comments