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

LiamA's avatar
Level 1

Testing a cookie added via middleware

Hi,

Is the only way to test cookies is using the withCookie() method?

I mean, I have a middleware defined where I check if a cookie is defined. If it's not, then I'm attaching a cookie to the request:

public function handle(Request $request, Closure $next)
{
    if(!$request->cookie('cart_id')){
    	$cart_id = "some value";
		return $next($request)->cookie(cookie()->forever('cart_id', $cart_id));
	}  
	return $next($request);
}     

It doesn't matter how many get requests I run in a test, then cookie remains undefined.

If I use browsers the cookie is there.

Is there no way to run this kind of test to confirm that the middleware is working properly?

0 likes
5 replies
martinbean's avatar

@liama This works for me:

<?php

namespace Tests\Feature;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
use Ramsey\Uuid\Uuid;
use Tests\TestCase;

class MiddlewareTest extends TestCase
{
    public function testMiddleware(): void
    {
        Route::get('/foo', function () {
            return response('Hello, world.');
        })->middleware(CartIdMiddleware::class);

        Str::createUuidsUsing(function () {
            return Uuid::fromString('35398f1e-a101-42b8-8200-232c915b51fb');
        });

        $this
            ->get('/foo')
            ->assertOk()
            ->assertPlainCookie('cart_id', '35398f1e-a101-42b8-8200-232c915b51fb');
    }
}

class CartIdMiddleware
{
    public function handle(Request $request, Closure $next)
    {
        /** @var \Illuminate\Http\Response $response */
        $response = $next($request);

        if (! $request->cookie('cart_id')) {
            $response = $response->cookie('cart_id', Str::uuid()->toString());
        }

        return $response;
    }
}
LiamA's avatar
Level 1

@martinbean Thank you! Your code works, all I had to do is use assertPlainCookie() instead of trying to fetch the cookie value using the Cookie facade with Cookie::get().

If I add the cookie to the request using the withCookie() method, Cookie::get() CAN access the cookie.

Does this make sense??

LiamA's avatar
Level 1

@martinbean In reference to my previous reply, while assertPlainCookie() does confirm the cookie is set, there is not way to access it and do the actual testing, for functionality that is dependent upon the cookie value!

Any idea how to resolve this??

martinbean's avatar

@liama I think you need to work out what you’re testing: are you testing the middleware adds the cookie, or are you testing that some code does something based on the presence and value of the cookie.

LiamA's avatar
Level 1

Well, I need to test both.

First I was testing that the cookie was getting set (globally) via the middleware.

Now I want to test some specific functionality that depends on the value of the cookie.

Why is there a conflict between the two?

Please or to participate in this conversation.