Search code examples
laravellaravel-dusklaravel-6

Cannot use right environment database with Laravel Dusk


  • I have two environment files: .env and .env.dusk.testing (Because .env gets overwritten constantly if I'm not explicit with the environment handler).
  • I've got two development servers:
    • One in port 8080 that uses .env (using command php artisan serve --port=8080)
    • One in port 8000 that uses .env.dusk.testing (using command php artisan serve --env=dusk.testing)

I'm trying a very basic test. Logging in an user. It's not working because it's not using the correct database.

.env

APP_NAME=DEV
APP_ENV=local
APP_DEBUG=true
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5433
DB_DATABASE=db
DB_USERNAME=dbuser
DB_PASSWORD=****

.env.dusk.testing

APP_NAME=DUSK
APP_ENV=testing
APP_DEBUG=true
DB_CONNECTION=sqlite

phpunit.dusk.xml

...
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="DB_CONNECTION" value="sqlite"/>
        <env name="DB_DATABASE" value=":memory:"/>
    </php>
...

I've also defined the sqlite connection in config/database.php as follows:

'sqlite' => [
    'driver' => 'sqlite',
    'database' => ':memory:',
    'prefix' => '',
],

My test uses a setup method to wipe up an user and persist it in the database.

namespace Tests\Browser;

use App\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;

class ExampleTest extends DuskTestCase
{
    use DatabaseMigrations;

    protected $user;

    public function setUp(): void
    {
        parent::setUp();
        $this->user = factory(User::class)->create([
            'nombre' => 'John Doe',
            'email' => 'john.doe@testing.com',
            'password' => bcrypt('password')
        ]);
    }

    /** @test */
    public function guest_user_can_authenticate_with_valid_credentials()
    {
        $this->browse(function (Browser $browser) {
            $browser->assertGuest()
                    ->visit('/login')
                    ->type('@email', $this->user->email)
                    ->type('@password', 'password')
                    ->click('@login-button')
                    ->assertAuthenticatedAs($this->user)
                    ->assertUrlIs('/dashboard')
                    ->logout();
        });
    }
}

Using the DatabaseMigrations trait wipes out my PostgreSQL database instead of using the SQLite one. I tried to use RefreshDatabase instead but it's the same result. My environment database gets wiped out.

And the test fails anyways in both cases. The user is persisted into the SQLite testing database but it's still looking in the other one in the tests and destroying it in the process.

I'm this close to just drop this package and browser tests altogether. There is nothing simple about it. The documentation is shallow and it just doesn't work as expected.


Solution

  • My issue was fixed by

    • Not using an in-memory SQLite database (I used a .sqlite file instead).
    • Using the DatabaseMigrations trait instead of RefreshDatabase.
    • Being explicit about the environment file when running dusk. php artisan dusk --env=dusk.testing.