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

Kodandweb's avatar

Sanctum, global scopes and testing

Hi,

I'm facing an issue with Sanctum and global scope for my automated test.

The project is a standalone API accessed by a Vuejs application. I use Sanctum with the standard session cookie for authentication. Some models have global scope based on the connected user to add some security to the Eloquent queries.

For exemple, one small test:

it ('should .... whatever', function() {
	$user = User::factory()->create();

	$modelWithGlobalScope = ModelWithGlobalScope::factory()->create();

	... assertions ...
}

I get an error that Auth::user() is null. Off course I did not logged the user, but I just want to create some data, not making some queries.

I tried to add the method withoutGlobalScopes but it never work, if I put it first factory() is undefined method, and if I try after factory(), it's withoutGlobalScopes that is undefined.

I just can't get over it.

Does anyone have an idea ?

Thanks Fred

0 likes
0 replies

Please or to participate in this conversation.