H
H
Holmess882021-12-07 21:11:38
Laravel
Holmess88, 2021-12-07 21:11:38

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

1 answer(s)
J
jazzus, 2021-12-08
@jazzus

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

This is a separate test. The test should have a short script. Shot - killed. But I didn’t learn how to shoot, I got permission, I bought a gun, etc. No need to lead a user from registration to deleting an account with interaction with other entities and users. That's bullshit. Lots of small tests, all repeating through dataProvider iteration and clearing before the test, not during.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question