L0GAN's avatar
Level 2

Dusk Writes to Correct DB but Reads from Wrong DB

I am using Dusk for browser testing. My testing database is an sqlite file (dusk.sqlite); I use mysql for the actual database. During testing, I have no issues connecting and seeding the correct database (sqlite) but whenever the application needs to read from the database, it defaults to the mysql instance instead of the sqlite file. For instance, it will properly seed a user to the sqlite database however authentication in the test will fail because it will attempt to auth the given credentials using the users table from mysql.

.env.dusk.testing

APP_NAME=Laravel
APP_ENV=testing
APP_KEY=[redacted]
APP_DEBUG=true
APP_URL=http://localhost:8001
LOG_CHANNEL=stack
LOG_LEVEL=debug
DB_CONNECTION=test

database.php

 'test' => [
            'driver' => 'sqlite',
            'database' => database_path('dusk.sqlite'),
            'prefix' => '',
        ],

AuthTest.php

class AuthTest extends DuskTestCase
{

    public function setUp(): void
    {
        parent::setUp();

        $this->artisan('migrate:refresh');

        // seed required data for testing
        $this->artisan('db:seed', ['class' => 'DuskTestingSeeder']);
    }

    /**
     * @test
     * @return void
     * @throws Throwable
     */
    public function user_can_login()
    {
        $user = User::find(1);

        $this->browse(function (Browser $browser) use ($user) {
            $browser->visit('http://localhost:8001/login/')
                ->pause(5000) // wait 5 secs for loader
                ->type('email', $user->email)
                ->type('password', 'password123')
                ->press('Sign in')
                ->pause(5000)
                ->screenshot(date("Y-m-d"));
        });
    }
}

CreatesApplication.php

trait CreatesApplication
{
    /**
     * Creates the application.
     *
     * @return \Illuminate\Foundation\Application
     */
    public function createApplication()
    {
        $app = require __DIR__.'/../bootstrap/app.php';

        $app->make(Kernel::class)->bootstrap();
        $app['config']->set('database.default','test');

        return $app;
    }
}
0 likes
1 reply
L0GAN's avatar
Level 2

In case anyone comes across this in the future: I was never able to find a real solution to this issue. I ended up reverting back to a recent version of the app and starting over with no issues.

Please or to participate in this conversation.