samedi 11 mai 2019

Laravel Testing SQLite DB in Memory

I am using L5.8.16. I have an in-memory SQLite DB for testing. I register my app ACL authentication on the boot method on AuthServiceProvider as outlined below.

//AuthServiceProvider

if (Schema::hasTable('permissions') && Permission::all()->isNotEmpty()) {
        foreach (Permission::all() as $permission) {
            Gate::define($permission->name, function ($user) use ($permission) {
                return $user->hasPermission($permission) || $user->company->hasPermission($permission);
            });
        }
    }

I check with Schema::hasTable to see if the permissions table exists before defining any permissions, but the Schema::hasTable method always returns false on my in-memory SQLite testing DB and works fine on the dev MySQL DB, so my ACL does not work on my testing environment.

On my Tests\TestCase.php class I redefined the refreshInMemoryDatabase() method and seed the database with the roles and permissions. If I manually do dd($user->hasPermission($permission)) on my testing environment it returns true, so the database appears to be migrated and seeded correctly.

/**
 * Refresh the in-memory database.
 *
 * @return void
 */
protected function refreshInMemoryDatabase()
{
    $this->artisan('migrate');

    $this->artisan('db:seed');

    $this->app[Kernel::class]->setArtisan(null);
}

Is Schema::hasTable() only working on mysql and not sqlite connections expected behavior?

Aucun commentaire:

Enregistrer un commentaire