Hello Fran,
The error you're encountering, "Call to a member function connection() on null," typically indicates that Laravel's model can't resolve the database connection. This is often due to the database setup not being correctly initialized or recognized in the testing environment. Here are a few steps to troubleshoot and potentially resolve this issue:
-
Ensure Database Configuration for Testing: Make sure that your
.env.testingfile (or the environment configuration used for testing) is correctly set up with the appropriate database connection settings. Since you're using Laravel Sail, ensure that theDB_DATABASEand other related settings match the database service configurations in yourdocker-compose.yml. -
Database Migrations: Ensure that the database migrations are running as part of your test setup. You can use the
RefreshDatabasetrait, which you're already using, but make sure it's effectively resetting your database state before tests run. You can explicitly run migrations by adding the following in your test setup:$this->artisan('migrate', ['--database' => 'testing']); -
Check Database Connection in Test: Before performing operations in your test, check if the database connection is available. You can add a simple check like this at the beginning of your test function:
it('checks database connection', function () { $this->assertNotNull(DB::connection()->getPdo()); }); -
Configuration Caching: Sometimes, configuration caching might cause issues during testing. Make sure to clear your configuration cache when switching environments. You can run:
php artisan config:clearAlternatively, ensure that your testing environment does not cache configurations or that it uses a separate cache file.
-
Inspect PHPUnit Configuration: Review your
phpunit.xmlfile settings. Ensure that the environment variables under the<php>tag are correctly set and are not being overridden somewhere else in your configuration. -
Debugging Further: If the above steps don't resolve the issue, you might want to add more logging around the point of failure to understand what exactly is null. Modify the
resolveConnectionmethod temporarily in your local vendor files to log more details or to throw a more descriptive exception.
Here's a quick example of how you might modify a test to include a connection check and explicit migration:
uses(RefreshDatabase::class);
it('has the right attribute and association', function () {
$this->artisan('migrate', ['--database' => 'testing']);
$this->assertNotNull(DB::connection()->getPdo());
$user = User::create([
'name' => 'John Doe',
'firstname' => 'John',
'lastname' => 'Doe',
'email' => '[email protected]',
'role_id' => 2,
'uuid' => '3820202',
'user_id' => 4
]);
$this->assertDatabaseHas('users', [
'name' => 'John Doe',
'firstname' => 'John',
'lastname' => 'Doe',
'email' => '[email protected]',
'role_id' => 2,
'uuid' => '3820202',
'user_id' => 4
]);
});
Try these steps and see if they help resolve the issue. If the problem persists, it might be helpful to compare the configurations between the project that works and the one that doesn't, focusing especially on any differences in the database and environment settings.