Reputation: 437
I have a Laravel 5.4 project and are trying to use Dusk to run some test. I want to reset, migrate and seed before I run my test. I've got it setup to use SQLite and ideally want to run it in memory, but a physical file is also fine.
I was able to get what I want by changing Illuminate\Foundation\Testing\DatabaseMigrations; Originial:
public function runDatabaseMigrations()
{
$this->artisan('migrate');
$this->app[Kernel::class]->setArtisan(null);
$this->beforeApplicationDestroyed(function () {
$this->artisan('migrate:rollback');
});
}
My version:
public function runDatabaseMigrations()
{
$this->artisan('migrate:refresh');
$this->artisan('db:seed');
$this->app[Kernel::class]->setArtisan(null);
$this->beforeApplicationDestroyed(function () {
$this->artisan('migrate:rollback');
});
}
But of course I will loose this with some future update, so I need to override my trait.
I copied the trait to App\Traits\DatabaseMigrations.php and changed the namespace.
<?php
namespace App\Traits;
use Illuminate\Contracts\Console\Kernel;
trait DatabaseMigrations
{
/**
* Define hooks to migrate the database before and after each test.
*
* @return void
*/
public function runDatabaseMigrations()
{
$this->artisan('migrate:refresh');
$this->artisan('db:seed');
$this->app[Kernel::class]->setArtisan(null);
$this->beforeApplicationDestroyed(function () {
$this->artisan('migrate:rollback');
});
}
}
MyTest.php is as follows:
<?php
namespace Tests\Browser;
use App\User;
use Tests\Browser\Pages\Dashboard;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
//use Illuminate\Foundation\Testing\DatabaseMigrations;
use App\Traits\DatabaseMigrations;
class MyTest extends DuskTestCase
{
use DatabaseMigrations;
but this doesn't work. I get an error: PDOException: SQLSTATE[HY000]: General error: 1 no such table: users
So somehow the migrations don't run without an error about that, but the result is that my test fails because there are no tables.
What does work is this:
<?php
namespace Tests\Browser;
use App\User;
use Tests\Browser\Pages\Dashboard;
use Illuminate\Contracts\Console\Kernel;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;
// use App\Traits\DatabaseMigrations;
class WorkshopRegistrationTest extends DuskTestCase
{
public function runDatabaseMigrations()
{
$this->artisan('migrate:refresh');
$this->artisan('db:seed');
$this->app[Kernel::class]->setArtisan(null);
$this->beforeApplicationDestroyed(function () {
$this->artisan('migrate:rollback');
});
}
use DatabaseMigrations;
but I don't like to have to put that method into every Test file. Does anybody know why one method works, but the other doesn't? The first method seems to me, with little knowledge of OOP, completely normal.
Upvotes: 5
Views: 1011
Reputation: 390
after reading some SO question i found out that it needed the same answer:
protected static $migrationRun = false;
public function setUp(): void{
parent::setUp();
if(!static::$migrationRun){
$this->artisan('migrate:refresh');
$this->artisan('db:seed');
static::$migrationRun = true;
}
}
Include this in your dusk test class. setUp
method runs before each test method, If migration has run once, It won't run it again.
Why the late answer: I dont like unanswered questions.
Upvotes: 0