trogne's avatar

dusk/phpunit test using homestead db instead of sqlite

Running the test below, with phpunit ./tests/Browser/NotesTest.php --filter a_user_can_delete_notes

I see my homestead db being changed, instead of sqlite db ! Why is that ?

    public function a_user_can_delete_notes()
    {
        $user = factory(User::class)->create();

        $notes = factory(Note::class, 2)->create([
            'user_id' => $user->id
        ]);

        $this->browse(function (Browser $browser) use ($user, $notes) {
            $browser->loginAs($user)
                ->visit(new NotesPage)
                ->pause(500);

            foreach ($notes as $note) {
                $browser->click('.notes .list-group-item:nth-child(2) a:nth-child(2)')
                    ->pause(500)
                    ->assertSeeIn('.alert', 'Your note has been deleted.')
                    ->assertDontSeeIn('.notes', $note->title);
            }

            $browser->pause(500)
                ->assertSeeIn('.notes', 'No notes yet');

        });
    }
0 likes
7 replies
trogne's avatar

Even with this trick in "CreatesApplication.php", it will still go in the homestead db :

    public function createApplication()
    {
        $app = require __DIR__.'/../bootstrap/app.php';

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

        return $app;
    }
trogne's avatar

I do have my "testing_browser" sqlite db selected in phpunit.xml :

<env name="DB_CONNECTION" value="testing_browser"/>
trogne's avatar

But it is still using my homestead database too !!

I see my testing_browser.sqlite growing, but also my "notes" in my homestead db being deleted !!

trogne's avatar

Laravel is messing with ".env" and ".env.dusk.local".

Of course if I replace the content of ".env" with the one of ".env.dusk.local", it will not mess with my homestead db.

How can I prevent dusk/phpunit from reading my ".env" file's db ?

trogne's avatar

I'd say the problem is more phpunit , not dusk... Does replaces the ".env" file correctly while running. Then put the original back.

phpunit is reading the db from ".env"... How can I prevent that ?

trogne's avatar

It has nothing to do with Dusk.

It's phpunit not persisting the overridden ".env" value in phpunit.xml :

    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="DB_CONNECTION" value="testing_browser"/>
    </php>

But why ?

It starts my "testing_browser" sqlite db, and somewhere it switch to my homestead .env db.

trogne's avatar

Finally I understand everything. There was a conflict with phpunit and laravel dusk. This test is using "Laravel\Dusk\Browser", so I guess that by this time it switches to .env which have not been overridden by the ".env.dusk.local" content, like "php artisan dusk" do.

Please or to participate in this conversation.