Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

codingstuff91's avatar

Problem with DB connection on Unit Testing

Greetings friends developers !

I'm a noob on Unit Testing and i try to follow the series on unit testing, i'm on the beggining.

I searched for someone who had the same problem but i didnt find one...

I try to do a basic UserTest to make some practise of what i learned. But when i want to fetch my Users, i ve got an error on my "php artisan test" that i dont understand.

I guess its my DB connection that doesnt work and i dont know why. What do i miss to make my DB connection work on testing purposes ?

Is there anyone that could help me please ?

There is my basic test.

** NB : i've tested my DB connection on tinker and everything works fine

`<?php

namespace Tests\Unit;

use App\User; use PHPUnit\Framework\TestCase; use Illuminate\Database\Eloquent\Factory; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\DatabaseTransactions; use Database\Factories\UserFactory;

class UserTest extends TestCase { use DatabaseTransactions;

protected function createUser()
{
    $user1 = User::create([
        'name' => "test",
        'email' => "[email protected]",
        'password' => "secret"
    ]);
}

/** @test */
public function there_are_2_users()
{
    $this->createUser();

    // $users = User::all();
    // // $count = $users->count();
    $count = 2;

    $this->assertEquals(2, $count);
}

}`

0 likes
7 replies
Sinnbeck's avatar
  1. What is the error
  2. What is phpunit.xml set to use as database?
<server name="DB_CONNECTION" value="testing"/>
<server name="DB_DATABASE" value=":memory:"/>
1 like
codingstuff91's avatar

Sorry i forgot to copy paste my error messages :

I ve got this error message on my console :

Call to a member function connection() on null

at C:\laragon\www\laravel-testing\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1342 1338| * @return \Illuminate\Database\Connection 1339| */ 1340| public static function resolveConnection($connection = null) 1341| {

1342| return static::$resolver->connection($connection); 1343| } 1344| 1345| /** 1346| * Get the connection resolver instance.

1 C:\laragon\www\laravel-testing\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1308 Illuminate\Database\Eloquent\Model::resolveConnection()

2 C:\laragon\www\laravel-testing\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1114 Illuminate\Database\Eloquent\Model::getConnection()

And there is my PHPUNIT config file :

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true"

<testsuites>
    <testsuite name="Unit">
        <directory suffix="Test.php">./tests/Unit</directory>
    </testsuite>
    <testsuite name="Feature">
        <directory suffix="Test.php">./tests/Feature</directory>
    </testsuite>
</testsuites>
<filter>
    <whitelist processUncoveredFilesFromWhitelist="true">
        <directory suffix=".php">./app</directory>
    </whitelist>
</filter>
<php>
    <server name="APP_ENV" value="testing"/>
    <server name="BCRYPT_ROUNDS" value="4"/>
    <server name="CACHE_DRIVER" value="array"/>
    <server name="DB_CONNECTION" value="mysql"/>
    <server name="DB_DATABASE" value="laravel_testing"/>
    <server name="MAIL_MAILER" value="array"/>
    <server name="QUEUE_CONNECTION" value="sync"/>
    <server name="SESSION_DRIVER" value="array"/>
    <server name="TELESCOPE_ENABLED" value="false"/>
</php>
GeordieJackson's avatar
Level 18

@codingstuff91

Instead of use PHPUnit\Framework\TestCase use use Tests\TestCase; instead.

You need to extend the Laravel TestCase to give you access to the framework.

You're also using both of these:

use Illuminate\Foundation\Testing\RefreshDatabase; 
use Illuminate\Foundation\Testing\DatabaseTransactions; 

You just need one.

3 likes
codingstuff91's avatar

Thanks a lot for your reply.

I have made the changes and everything works fine !

martinbean's avatar

@codingstuff91 The “problem” you have is: unit tests shouldn’t be interacting with a database. Unit tests should test a single unit of code only, so think testing individual methods. Unit tests shouldn’t touch other methods or classes, or rely on services such as a database being present—they’re integration tests.

3 likes

Please or to participate in this conversation.