Answer the question
In order to leave comments, you need to log in
Laravel. How to clear application state during test?
Hello!
I am using Laravel 8 + sanctum api token authentication. I write apish route tests like "black box". My tests don't know anything about the application other than the routes they have available to act on.
Here is the problem. When a test builds an instance of the application, it accumulates state over time that I need to clean up. I "legitimately" create users by actions, log them in, perform various actions, and so on. Then I need to test the application on behalf of the guest, but there is a problem. Sanctum api guard does not have a logout method under the hood. And clearing the token does not help log out the user. Lara's test contains an intermediate state, incl. and the logged in user. I tried to use the refreshApllication method, but then my database is cleared along with the entire application.
Are there any ways to clear the application's intermediate state, or force an authenticated user to be nulled? I used facade overloading, that didn't help either.
PS I know about actingAs but then it breaks my black box and the whole architecture of such tests.
PPS if I remove the RefreshDatabase trait, then I will lose the isolation of each test, but the database will not be updated when using refreshApplication. And connecting the refreshDatabase method to setUp without a trait seems to be impossible in this lara.
Answer the question
In order to leave comments, you need to log in
You need to create an environment for the test in the setup, not in the test.
Then I need to test the app as a guest
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question