lundi 4 janvier 2021

Is it possibel to do InMemory testing with Migrating and Seed Once

I am writing functional tests with in-memory database

I recently installed a plugin, laravel permission.

before, I had a simple profile_id field in my users table. So, I just had to use refreshDatabase trait create a user and test it for each user test. It was pretty fast.

Now I have migrated to laravel permission, I have defined more that 100 permissions. I have to migrate DB and seed them for each tests. Now, each tests take up to 3 secs, and I have 112 test, and this is just the beginning. It is just too slow this way.

I tried to use a MigrateAndSeedOnce method inside my setUp() method:

trait MigrateFreshSeedOnce
{
    /**
     * If true, setup has run at least once.
     * @var boolean
     */
    protected static $setUpHasRunOnce = false;
    /**
     * After the first run of setUp "migrate:fresh --seed"
     * @return void
     */
    public function setUp() :void
    {
        parent::setUp();
        if (!static::$setUpHasRunOnce) {
            Artisan::call('migrate:fresh');
            $this->seed('SourceTableSeeder'); 

            $this->seed('RoleTableSeeder');
            $this->seed('PermissionTableSeeder');

            $this->seed('RepartitionTableSeeder'); 

            $this->company = Company::factory()->create();
            $this->operation = Operation::factory()->create(['company_id' => 1]);
            $this->user = User::factory()->create(
                [
                    'email' => 'exapmle@company.com',
                    'company_id' => 1, 
                    'operation_id' => 1,
                ]);
            $this->user->assignRole(User::SUPER_ADMIN);
            static::$setUpHasRunOnce = true;
        }
        $this->actingAs($this->user);

    }

This works great for the first test, but for the second test, $this->user is null.

Is it possible to perform this with in-memory database ( this would be much faster ), or must I switch to Sqlite DB ( slower ), or is there any other better option ?

Aucun commentaire:

Enregistrer un commentaire