moongod's avatar

How to send multiple requests in laravel phpunit tests?

I have a Test class extended from Illuminate\Foundation\Testing\TestCase. I use DatabaseTransactions trait. In setUp() method i create models using factories. In one of tests i need to make two calls: first to retrieve offset param and second to retrieve data with offset. Like this:

$response = $this
    ->get('/api/v1/companies')
;

$response = $this->get('/api/v1/companies?' . http_build_query([
        'offset' => $response['new_offset'],
    ]))
;

I tried to make two requests in one method. It dont work, it seems like sended two equal requests. I tried not to use DatabaseTransactions, but with sqlite dont work my migrations. I tried to refreshApplication(), but it ruins my transaction. I tried to write dependent tests, but it dont works because offset param depends on created models (and they are recreating with each test in setUp() method).

So, how can i do two (or more) requests in one test method?

0 likes
2 replies
martinbean's avatar

How to send multiple requests in laravel phpunit tests?

@moongod You don’t. The framework is not refreshed after making a request in a test case, so you may get unintended side effects.

moongod's avatar

@martinbean thank you for the answer! May be there are other ways to do what i described in post? How people usually do it?

Please or to participate in this conversation.