Hello everybody!
I encounter a weird issue using Artisan commands (directly or indirectly) in the setUp method of my integration test classes.
In order to get test data, I want to migrate and seed data in a test database before running my tests. To do so, I use the setUp method:
public function setUp(): void
{
parent::setUp();
$this->seed();
}
I also use the RefreshDatabase trait in order to empty my database before each test.
The first test runs perfectly and works as expected. Then, all other tests fail because of a CommandNotFoundException thrown in $this->seed();. I tried to use the debugger to find out what happens and saw that only two commands are loaded during the second test execution: list and help.
I really don't understand what happens here. Am I missing something?
Here is an example test class which fails:
class TVShowAPITest extends TestCase
{
use RefreshDatabase;
public function setUp(): void
{
parent::setUp();
$this->seed();
}
public function testGetAllUnauthenticated()
{
$response = $this->get('/api/tvshows');
$response->assertStatus(200)->assertJson([
'meta' => [
'last_page' => 2,
'to' => 25,
'total' => 30
]
]);
}
public function testGetAllAuthenticatedAsSuperadmin()
{
$superAdmin = User::where('username', env('SUPERADMIN_USERNAME', 'superadmin'))->firstOrFail();
Passport::actingAs($superAdmin);
$responseWhisp = $this->get('/api/tvshows');
$responseWhisp->assertStatus(200)->assertJson([
'meta' => [
'last_page' => 6,
'to' => 25,
'total' => 120
]
]);
}
}
Thanks in advance for your help!