Reputation: 19695
I'm coding functional tests with sqlite in memory for faster results.
Everything seems ok, but I have several tables that needs to be filled so the tests works. They are small tables, and almost never change, so I would like to seed them once on the beginning of the phpunit command only.
Is it posible???
The only thing I fought is adding: Artisan::call('db:seed');
to createApplication()
but this will seed once for every tests, and I don't need it..
Any Idea how should I do it?
Upvotes: 1
Views: 451
Reputation: 2604
You can use environment variable for refreshing database whenever you want. I don't know if it's the best way but it works for me.
Add variable to .env file
DB_REFRESH=true
and change CeatesApplication.php trait method:
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
if(getenv('DB_REFRESH') === 'true') {
\Artisan::call('migrate:refresh');
\Artisan::call('db:seed');
putenv('DB_REFRESH=false');
}
return $app;
}
and then try in ExampleTest.php
public function testBasicTest()
{
$this->assertTrue(true);
}
public function testChangeDatabase()
{
//if database changes here set variable to true
//and it will be refreshed before the next test
putenv("DB_REFRESH=true");
}
public function testRefresh()
{
//you have fresh database here
}
Upvotes: 1