Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

creating a user and logging in with the DatabaseMigrations trait #531

Closed
alexkb opened this issue Jul 11, 2018 · 21 comments
Closed

creating a user and logging in with the DatabaseMigrations trait #531

alexkb opened this issue Jul 11, 2018 · 21 comments

Comments

@alexkb
Copy link

alexkb commented Jul 11, 2018

Despite a lot of other users having issues with the initial setup of dusk, I've managed to get dusk running using php artisan serve --quiet & (http://localhost:8000) and then running php artisan dusk. The example Browser test works fine (after changing the ->assertSee('Laravel'); to my site title of course).

However, as per the docs - when I move onto creating a LoginTest example, using the DatabaseMigrations trait, create a user and attempt to login, the authentication never finds the user. When i do a ->dump(), I see the message "These credentials do not match our records.". If I try do a query on the database (to dump the users table contents to confirm its being created) during the test, when I run the tests it just sits there and never completes.

I tried what's in the documentation:

$user = factory(User::class)->create([
    'email' => 'taylor@laravel.com',
]);

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

and thought that that wouldn't work, due to the password not matching. It should work because the database/factories/UserFactory.php class that comes with laravel, set's this password if you don't pass it to $user = factory(User::class)->create(.

If I open up the sqlite database, I can see the taylor user there, so it looks like it's getting created ok.

@staudenmeir
Copy link
Contributor

Are you using a .env.dusk.local file?

@alexkb
Copy link
Author

alexkb commented Jul 12, 2018

@staudenmeir yes. My non-dusk setup uses MySQL and Dusk uses sqlite, which is being written to when I run dusk, as mentioned above.

@staudenmeir
Copy link
Contributor

Please post the SQLite connection from config/database.php.

@alexkb
Copy link
Author

alexkb commented Jul 12, 2018

Thanks @staudenmeir.

My config/database.php:

....
'connections' => [
    ...
    'sqlite' => [
        'driver' => 'sqlite',
        'database' => env('DB_DATABASE', database_path('database.sqlite')),
        'prefix' => '',
    ],
....

My .env.dusk.local:

APP_URL=http://localhost:8000
DB_CONNECTION=sqlite
APP_DEBUG=TRUE
CACHE_DRIVER=array
SESSION_DRIVER=array
DB_DATABASE=./database/portal.sqlite
DEBUGBAR_ENABLED=false

And the sqlite file:

root@240ba91d54e3:/var/www# ls -la database/portal.sqlite
-rwxr-xr-x 1 root root 12288 Jul 11 08:13 database/portal.sqlite

@staudenmeir
Copy link
Contributor

staudenmeir commented Jul 12, 2018

That's a known problem with .env values (can't find the issue right now).

Replace env('DB_DATABASE', database_path('database.sqlite')) with database_path('portal.sqlite').
Do you use the SQLite connection anywhere else?

@alexkb
Copy link
Author

alexkb commented Jul 12, 2018

Do you use the SQLite connection anywhere else?

No.

Replace env('DB_DATABASE', database_path('database.sqlite')) with database_path('portal.sqlite').

The sqlite database is being written to, so why would changing config/database.php not to use the env value help things?

@staudenmeir
Copy link
Contributor

Which database does your login page's controller use?

@alchermd
Copy link

alchermd commented Aug 7, 2018

@alexkb I'm experiencing a similar issue. Have you found a solution yet?

@alexkb
Copy link
Author

alexkb commented Aug 10, 2018

@staudenmeir the login controllers are the default ones that came with laravel - there's been no customization to those.

@alchermd no luck yet, but I haven't had time to try it again. Just making do with HTTP tests for now. I will try again at some point. Good to know I'm not the only one having issues.

FYI - I've re-written the OP above, as I recently realised the UserFactory sets the password correctly, so there's no need to explicitly set it to 'secret'.

@staudenmeir
Copy link
Contributor

Please put dd(config('database.default')) in one of your controllers and visit the page with Dusk. Does it correctly use the sqlite connection?

@alchermd
Copy link

alchermd commented Aug 10, 2018

@alexkb maybe it's worth trying since I got mine to work now:

  1. Create a sqlite file for testing ex: database/dusk.sqlite
  2. Create a .env.dusk.local file and customize as needed, obviously using the db from step 1.
    2.1 Might be a good idea to use the absolute path when setting DB_DATABASE
    2.2 Or just straight up create a dusk connection config on config/database.php and set DB_CONNECTION as dusk (this is what I use)
  3. Specify the environment when running the web server with php artisan serve --env=dusk.local
    3.1 I even use a dedicated port with --port=8001 just to be extra sure (update APP_URL on your .env.dusk.local if you'll do this).
  4. Same concept as step 3 when running dusk php artisan dusk --env=dusk.local

This is the only path that consistently worked for me. Other workarounds either consumes (i.e re-migrates and deletes) my main database or produce the same error as your OP. Hope it helps!

EDIT: Snippets below for clarity

// config/database.php

// ...
        'dusk' => [
            'driver' => 'sqlite',
            'database' => database_path('dusk.sqlite'),
            'prefix' => '',
        ],
// ...
# .env.dusk.local
APP_ENV=local
APP_URL=http://127.0.0.1:8001/
DB_CONNECTION=dusk

@staudenmeir
Copy link
Contributor

Custom .env.dusk files don't work with php artisan serve: #162

@alchermd
Copy link

Oh, that's good to know. I guess using --env=dusk.local when running Dusk is what did the trick. Btw, I'm also that guy on the Laracasts thread you just replied to 😄

@alchermd
Copy link

@staudenmeir errrr, my tests fail (same errors as the OP) when I run php artisan serve without the --env=dusk.local. So I guess custom Dusk env files do work?

@staudenmeir
Copy link
Contributor

Did you check dd(config('database.default'))?

@alchermd
Copy link

When using php artisan env --port=8001 --env=dusk.local, I get "dusk" which is the custom DB_CONNECTION in my .env.dusk.local file.

When using php artisan env --port=8001, I get "pgsql" which is what's inside the .env file.

@staudenmeir
Copy link
Contributor

staudenmeir commented Aug 12, 2018

This is a great solution, can you post it in #162 and on Laracasts?

@alchermd
Copy link

@staudenmeir sure thing

@driesvints
Copy link
Member

Gonna close this as this seems to be resolved. Thanks guys.

@biniyam17
Copy link

Wow, this is great. Thank you @alchermd for the suggestion. Using php artisan serve --env=dusk.local fixed my issue of using the correct testing database in my test class but not using the test database when hitting the browser via dusk but since now I am forcing the env to be the dusk local it will use the testing database for the browser as well.

@AntoscencoVladimir
Copy link

Guys if you force the --env=dusk.local that's not how it should work, in docs written that it should backup current .env file, rename the env.dusk.{env} to .env and use it. But unfortunately it not do that. Once you run php artisan dusk it use the current .env file for DB connection for ex. ....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants