Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

naghal's avatar

RefreshDatabase trait with sql server

I am trying to use a real Sql Server connection to run my php unit test in Laravel because SqLite does not have the function I am trying to test. The database name is "Test" and the connection details have all been added to database.php.

I have changed my phpunit.xml env variables to refer to the new test database.

<env name="DB_CONNECTION" value="test_sqlserver"/>
<env name="DB_DATABASE" value="Test"/>

Now, when I try to run a simple test with a class that use the RefreshDatabase trait, or even the DatabaseMigrations trait, it result in the following error: Symfony\Component\Console\Exception\InvalidOptionException: The "--drop-views" option does not exist.

Now, as a workaround I can add the following function in my test case. The error goes away, but now no test works correctly because the trait does not migrate the database.

protected function migrateFreshUsing()
 {
    $seeder = $this->seeder();
    
    return array_merge([
        //    '--drop-views' => $this->shouldDropViews(),
        //    '--drop-types' => $this->shouldDropTypes(),
        ],
        // $seeder ? ['--seeder' => $seeder] : ['--seed' => $this->shouldSeed()]
    );
}

Since the seeder works for the real database, I wonder why it doesnt in phpunit? How can I make test run with the RefreshDatabase trait with sql server?

0 likes
8 replies
Sinnbeck's avatar

Does it need to drop views? Are you using view? If not, just remove that property from your TestCase

//protected $dropViews = true; //remove this
naghal's avatar

@Sinnbeck I do not need view, commenting the --drop-views part in the migrateFreshUsing removed the error, but then it does the same with --drop-types, and then the same with --seed|--seeder. I need the seed compatibility however, so commenting it soolve the error, but after, tests doesnt work properly.

Sinnbeck's avatar

@naghal No need to change that. Just make sure you dont have those properties on the TestCase

This is the source code.. It just checks for those properties

protected function shouldDropViews()
    {
        return property_exists($this, 'dropViews') ? $this->dropViews : false;
    }

    protected function shouldDropTypes()
    {
        return property_exists($this, 'dropTypes') ? $this->dropTypes : false;
    }
Sinnbeck's avatar

@naghal How are you running the tests? Seems strange that it cannot resolve the --seed option.

naghal's avatar

@Sinnbeck Sure, here it is:

<?php

namespace Tests;

use App\Models\User;
use Database\Seeders\DatabaseSeeder;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;

    protected $shouldSeed = true;

    public function setUp(): void
    {
        // first include all the normal setUp operations
        parent::setUp();

        if ($this->shouldSeed) {
            $this->seed(DatabaseSeeder::class);
        }

        // now re-register all the roles and permissions (clears cache and reloads relations)
        $this->app->make(\Spatie\Permission\PermissionRegistrar::class)->registerPermissions();

        $this->withoutMiddleware(\App\Http\Middleware\SetLocaleMiddleware::class);
    }

    protected function authenticate($user = null)
    {
        $userToAuthenticate = $user ?: User::factory()->withResource()->create();
        $this->actingAs($userToAuthenticate);

        return $userToAuthenticate;
    }

    protected function createRequest(string $method, string $uri): Request
    {
        $symfonyRequest = SymfonyRequest::create($uri, $method);

        return Request::createFromBase($symfonyRequest);
    }
}

omer_hamza's avatar

@naghal maybe it's too late but I got the same problem and found the solution, I just disabled migration commands from the console.php file and that is why it gives this error, maybe you need to check it too

Please or to participate in this conversation.