Hello,
I have this logout method.
public function logout(Request $request)
{
$request->user()->currentAccessToken()->delete();
return response()->json(null, 200);
}
And I test it like this.
public function user_can_logout(): void
{
$response = $this->postJson('/api/v1/auth/login', [
'email' => '[email protected]',
'password' => 'musique',
]);
$response->assertValid()->assertOk();
$this->assertDatabaseHas('personal_access_tokens', [
'tokenable_id' => $this->user->id,
]);
Sanctum::actingAs($this->user);
$response = $this->postJson('/api/v1/auth/logout');
$response->assertValid()->assertOk();
$this->assertDatabaseMissing('personal_access_tokens', [
'tokenable_id' => $this->user->id,
]);
}
I get an error saying that the tokenable_id is still in the database, whereas it should have been deleted.
Out of any test, when I login and then logout, it works the token is really deleted from the database, so the logout function works fine. But the test seems to not work properly given that it fails whereas it should success.
The test passes if I replace $request->user()->currentAccessToken()->delete(); by $request->user()->tokens()->delete();.
Can you help me understand why ?
Thanks for your help.
V