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

unlikenesses's avatar

“Your session has expired” when testing with user in Laravel Spark

[Posted this in the Spark channel too.]

In Spark on Laravel 5.5, I have this test:

$this->be(factory('App\User')->create());
$thing= factory('App\Thing')->create();
$response = $this->get('/things');
$response->assertSee($thing->title);

When I run it with PHPUnit it fails, and among the HTML output is:

<div class="modal-body">
    Your session has expired. Please login again to continue.
</div>

The route it is hitting has no middleware - instead the middleware's in the controller:

public function __construct()
{
    $this->middleware('auth');
}

But I get the same error when I remove this function.

Other things I've tried:

  • Using WithoutMiddleware
  • Using MySQL instead of sqlite-in-memory as test db
  • Adding trial_ends_at field to User factory
  • Adding <env name="CASHIER_ENV" value="testing"/> to phpunit.xml, so that it matches the Spark tests config
  • Testing with a clean install of Laravel/Spark. Same error.
0 likes
4 replies
jonasd's avatar

Either of you (or anyone else? :) ) found the issue yet? It's likely something stupid, as it usually is... but we have to find the stupidity first to avoid it in the future ;)

As an added remark, in my case copying a test case from vendor (Composer) to my own tests folder caused this behaviour (which makes it even weirder; as the test is completely self-contained code-wise)

I'll update this for those who follow in case I find it before anyone replied.

jonasd's avatar

Depending on your case, this answer might solve it for you: https://stackoverflow.com/a/46558013/620141

In my case, the issue was that I was issuing the test directly to debug; instead of calling it via phpunit.xml (thus my environment wasn't fully set up for testing)

Please or to participate in this conversation.