rezafm
1 month ago
179
2
Testing

Dusk: Very weird behavior between running locally and in homestead

Posted 1 month ago by rezafm

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, '[email protected]')
                        ->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, '[email protected]')
                        ->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?

Please sign in or create an account to participate in this conversation.