I have a few hundred tests in my project. My test environment is simply a different MySQL schema in the homestead database.
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="MAIL_DRIVER" value="array"/>
<env name="DB_DATABASE" value="homestead_test"/>
</php>
The trait RefreshDatabase is touted to make sure that each tests do not interfere with one an other, however in roughly 30% of the cases, the database is not completely rollbacked between tests, as the following test throws an error (30% of the times it is ran):
/**
* Checks that the seenIndicators() function does not returns the seen indicators of a task sharing the same ID as this VEM.
* @group seenIndicators
*/
public function testSeenIndicators_withTaskWithSameId()
{
$task = factory(Task::class)->create(['id' => $this->vem->id]);
factory(SeenIndicator::class, 3)->create(['checkable_type' => Task::class, 'checkable_id' => $task->id]);
$this->assertEquals($this->vem->seenIndicators->count(), $this->seenIndicators->count());
}
With an error that there is a duplicate primary key (3/4/5) for the tasks table.
This error demonstrates that the RefreshDatabase does not always completely empty the database of data between each tests when using a database that is not in-memory.
Is there a way to sidestep this issue?
Note that for me using an in-memory sqlite database could cause issues since some of my policies use a MySQL function to delve into JSON data.