rezafm's avatar

Dusk: Very weird behavior between running locally and in homestead

Okay, so I came across this very weird behavior. I run my computer on an ubuntu 18.04 client and I hope I can to a certain extent set permissions correctly. Also, I run Laravel in Homestead.

Here is my .env.dusk.local file:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=mykey
APP_DEBUG=true
APP_URL=https://laravel.test
APP_LANG=en

DB_CONNECTION=testing_browser

and the respective DB_CONNECTION:

          'driver' => 'sqlite',
          'url' => env('DATABASE_URL'),
          'database' => database_path('testing_browser.sqlite'),
          'prefix' => '',
          'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
      ],

Okay so that's all good. If I run in homestead it's works great. If I run the same thing locally I just have to make sure to put...

APP_DEBUG=true
APP_URL=https://laravel.test:44300

..and then I can run it locally on ubuntu which I somestimes do if I cannot get a test to pass (I use quite a bit VUE js and sometimes the browser is quicker than I want so I can only find the error by seeing what's going on).

Okay now I ll come to the unexpeted behavior.

For SEO reasons I had to do the following in my web.php

        if (Schema::hasTable('posts')) {
            Post::all()
                ->each(function (Post $post) use ($locale) {
                    Route::get($post->slug, 'BlogController@post')
                        ->name($$post->slug)
                        ->defaults('slug', $post);
            });
        }

If I run my tests with this code in Homestead, everything works as expected.

Once I had to switch back to the local version and when I then tried to run my tests it immediately stopped with the following message:

  Illuminate\Database\QueryException  : SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = blog and table_name = posts and table_type = 'BASE TABLE')

  at /home/r/www/blog/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669
    665|         // If an exception occurs when attempting to run a query, we'll format the error
    666|         // message to include the bindings with SQL, which will make this exception a
    667|         // lot more helpful to the developer instead of just the database's errors.
    668|         catch (Exception $e) {
  > 669|             throw new QueryException(
    670|                 $query, $this->prepareBindings($bindings), $e
    671|             );
    672|         }
    673| 

  Exception trace:

  1   Illuminate\Foundation\Application::Illuminate\Foundation\{closure}()
      [internal]:0

  2   Doctrine\DBAL\Driver\PDOException::("SQLSTATE[HY000] [2002] Connection refused")
      /home/r/www/blog/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:31

  Please use the argument -v to see more details.

If I know uncomment that mentioned block:

       /* if (Schema::hasTable('posts')) {
            Post::all()
                ->each(function (Post $post) use ($locale) {
                    Route::get($post->slug, 'BlogController@post')
                        ->name($$post->slug)
                        ->defaults('slug', $post);
            });
        }*/

and run it locally again, just to be sure that's how the env.dusk.local file would look like then:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=mykey
APP_DEBUG=true
APP_URL=https://laravel.test:44300
APP_LANG=en

it works again and I can run all my tests.

I tried several permission changes...I deleted the sqlite file completely and put it back, but it didn't help. The only way to get the tests running again locally is to uncomment these mentioned posts routes.

So, what's going on here? Can anyone explain what's happening here?

0 likes
2 replies
rezafm's avatar

Well, as it turns out... in homestead first mysql is used... and due to migrations it has the Schema 'posts' - also the database is available as it runs inside homstead.

From what I understood so far is that the application kind of bootstraped with the standard .env file before it seems to run in the actual dusk .env file.

Anyways...I'd be quite interested in any ideas how to set this up properly.

What could work is to simply use a different .env.dusk.testing file and run dusk by --env=testing. In this file you set the env variable to "testing" and if we are in testing, we do not register these routes.

rezafm's avatar

In case anybody ever stumbles across this topic... I finally wrapped the whole code

        if (Schema::hasTable('posts')) {
            Post::all()
                ->each(function (Post $post) use ($locale) {
                    Route::get($post->slug, 'BlogController@post')
                        ->name($$post->slug)
                        ->defaults('slug', $post);
            });
        }

into a try and catch statement that checks if a database connection can be established.

It then fails for the mysql connection (on a local none homestead environment) but then works for the sqlite connections.

Please or to participate in this conversation.