Level 1
Okay, for some reason I had added the WithoutMiddleware on TestCase.php, all good now
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
It may be how I am running the test but for some reason my Middleware isn't reaching when I run the test.
beforeEach I have a user that belongs to company_id 1 as it refreshes the database.
So I created a middleware empty one, just dd()
class EnsureCustomerBelongToCompany
{
/**
* Handle an incoming request.
*
* @param Closure(Request): (Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
dd($customer);
return $next($request);
}
}
On my routes:
Route::middleware(EnsureCustomerBelongToCompany::class)->group(function () {
.. rest of routes
Route::put('/{id}', [CustomerController::class, 'update'])->name('customers.update');
});
The test:
it("should redirect if the user tries to update a customer that belongs to another company", function () {
// create a new company other than the beForeEach company, this is probably the company id 2
Company::factory()->create();
$customerOfCompany2 = Customer::factory()->create(['company_id' => 2]);
// This user has company_1 middleware is supposed to check if the $customerOfCompany2->id belogns to logged in user ( before controller )
actingAs($this->user)->put(route('customers.update', $customerOfCompany2->id), $customerOfCompany2->toArray())
->assertRedirect(route('customers.index'));
});
It is passing on test due to Controller logic, but I am looking to make some further validation even before controller / formRequest, however, whenever I run the test the dd() is never reached.
Any idea on what I am failing?
Please or to participate in this conversation.