How to create .env file for test with Laravel Dusk
Asked Answered
A

6

9

I'm using Dusk to do a simple login test.

I created a .env.dusk file so that the test uses an alternate database and does not delete the data that was registered on the platform.

Archive .env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_dusk
DB_USERNAME=root
DB_PASSWORD=123456

Archive .env.dusk

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_dusk_test
DB_USERNAME=root
DB_PASSWORD=123456

LoginTest.php

namespace Tests\Browser;

use Tests\DuskTestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class LoginTest extends DuskTestCase
{
    use DatabaseMigrations;
    /**
     * A Dusk test example.
     *
     * @return void
     */
    public function testLogin()
    {
        $user = factory(\App\User::class)->create(['email' => '[email protected]']);

        $this->browse(function ($browser) use ($user) {
            $browser->visit('/login')
                    ->type('email', $user->email)
                    ->type('password', 'secret')
                    ->press('Login')
                    ->assertPathIs('/home');
        });
    }
}

But when I run the tests it does not change the database and it deletes all data from the database used in the application.

How can I solve this problem?

Advertent answered 27/3, 2017 at 12:40 Comment(0)
D
7

You have to append an environment value, (which matches the environment you'll be initializing Dusk in), to the end of your .env.dusk file name, (e.g. - .env.dusk.local). For reference check the documentation on Dusk Environment Handling.

Update: If you're still having issues per your comments, put the following at the top of your testLogin function and report back what it says dd(env('APP_ENV'));

Directoire answered 27/3, 2017 at 12:49 Comment(1)
Change the env.dusk.testing file name to env.dusk.localDirectoire
A
2

@alaric

I changed the .env.dusk.testing file to .env.dusk.local

I ran the php artisan serve and created a new user in the laravel_dusk database.

I ran the php artisan serve again and then the php artisan dusk to run the tests and create a new user with the same email but in the database laravel_dusk_test and it continues registering in laravel_dusk.

Advertent answered 27/3, 2017 at 13:23 Comment(0)
T
0

I had similar issue when i was using \App\User::truncate() in my dusk file.

It truncates the dev database instead of the test database. I don't know exactly why because php artisan dusk replace the .env file with the .env.dusk.local for the test. Adding the whole configuration of the DB in the .env.dusk.local did the job for me.

So first, if your file .env have APP_ENV=local then rename your dusk file to .env.dusk.local

Second, in your .env.dusk.local be sure to use the whole configuration of your database:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=tdd_test
DB_USERNAME=your_username
DB_PASSWORD=your_password

instead of

DB_CONNECTION=test // configuration of test in the /config/database.php

then run your test with

php arisan dusk
Tani answered 30/4, 2019 at 10:55 Comment(0)
O
0

pass your dusk env to laravel serve:

-- php artisan serve --env=dusk.local
Octant answered 23/12, 2020 at 23:3 Comment(1)
What is the additional insight this provide in addtion to very similar but more extensively explained answer by Lucas Lopes? Please highlight the difference, explain the effect it has and point out the advantage gained by it.Admonitory
C
0
sail dusk --env=dusk.local

I am using Sail. I think

php artisan dusk --env=dusk.local

will work also.

Caracul answered 17/10, 2023 at 11:8 Comment(0)
B
-1

php artisan caches data on the onset.. so you have to direct it to the environment in start-up by:

php artisan serve --env=dusk.local
Borscht answered 2/11, 2022 at 18:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.