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

tenzan's avatar

Call to a member function connection() on null

Hi,

Full error message

   FAIL  Tests\Unit\ConcertTest
  ⨯ can get formatted date

   FAIL  Tests\Feature\ViewConcertListingTest
  ⨯ user can view a concert listing

  ---

  • Tests\Unit\ConcertTest > can get formatted date
   Error 

  Call to a member function connection() on null

  at vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1705
    1701▕      * @return \Illuminate\Database\Connection
    1702▕      */
    1703▕     public static function resolveConnection($connection = null)
    1704▕     {
  ➜ 1705▕         return static::$resolver->connection($connection);
    1706▕     }
    1707▕ 
    1708▕     /**
    1709▕      * Get the connection resolver instance.

      +7 vendor frames 
  8   tests/Unit/ConcertTest.php:16
      Illuminate\Database\Eloquent\Model::__callStatic()

  • Tests\Feature\ViewConcertListingTest > user can view a concert listing
  Failed asserting that '<h1>Sample Title</h1>\n
  <h2>Sample Subtitle</h2>\n
  <p></p>\n
  <p>Doors at 8:00pm</p>\n
  <p>32.50</p>\n
  <p>Sample Venue</p>\n
  <p>123 Sample Address</p>\n
  <p>Some City, ON 12345</p>\n
  <p>For tickets, call (555) 555-5555.</p>\n
  ' contains "December 13, 2016".

  at tests/Feature/ViewConcertListingTest.php:44
     40▕         // Assert
     41▕         // see the concert details
     42▕         $view->assertSee('Sample Title');
     43▕         $view->assertSee('Sample Subtitle');
  ➜  44▕         $view->assertSee('December 13, 2016');
     45▕         $view->assertSee('8:00pm');
     46▕         $view->assertSee('32.50');
     47▕         $view->assertSee('Sample Venue',);
     48▕         $view->assertSee('123 Sample Address');


  Tests:  2 failed
  Time:   1.51s

Line 16 is 'date' => Carbon::parse('2016-12-01 8:00pm') of the following ConcertTest.php

<?php

namespace Tests\Unit;

use App\Models\Concert;
use Carbon\Carbon;
use PHPUnit\Framework\TestCase;

class ConcertTest extends TestCase
{
    /** @test */
    function can_get_formatted_date()
    {
        // Create concert with a known date
        $concert = Concert::create([
            'date' => Carbon::parse('2016-12-01 8:00pm')
        ]);

        // Retrieve the formatted date
        $date = $concert->formatted_date;

        // Verify the date formatted as expected
        $this->assertEquals('December 1, 2016', $date);
    }
}
0 likes
13 replies
tykus's avatar

What does your phpunit.xml file look like; especially the DB_ environment variables?

1 like
tenzan's avatar

@tykus

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <coverage processUncoveredFiles="true">
        <include>
            <directory suffix=".php">./app</directory>
        </include>
    </coverage>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="BCRYPT_ROUNDS" value="4"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="DB_DATABASE" value="testing"/>
        <env name="MAIL_MAILER" value="array"/>
        <env name="QUEUE_CONNECTION" value="sync"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="TELESCOPE_ENABLED" value="false"/>
    </php>
</phpunit>
mvd's avatar
mvd
Best Answer
Level 48

@tenzan

What if you replace use PHPUnit\Framework\TestCase; with use Tests\TestCase; ?

1 like
tenzan's avatar

@mvd I replaced so the whole file looks like

<?php

namespace Tests\Unit;

use App\Models\Concert;
use Carbon\Carbon;
//use PHPUnit\Framework\TestCase;
use Tests\TestCase;

class ConcertTest extends TestCase
{
    /** @test */
    function can_get_formatted_date()
    {
        // Create concert with a known date
        $concert = Concert::create([
            'date' => Carbon::parse('2016-12-01 8:00pm')
        ]);

        // Retrieve the formatted date
        $date = $concert->formatted_date;

        // Verify the date formatted as expected
        $this->assertEquals('December 1, 2016', $date);
    }
}

and when I ran test again I now have

   FAIL  Tests\Unit\ConcertTest
  ⨯ can get formatted date

   FAIL  Tests\Feature\ViewConcertListingTest
  ⨯ user can view a concert listing

  ---

  • Tests\Unit\ConcertTest > can get formatted date
   Illuminate\Database\QueryException 

  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'testing.concerts' doesn't exist (SQL: insert into `concerts` (`date`, `updated_at`, `created_at`) values (2016-12-01 20:00:00, 2022-08-19 16:58:41, 2022-08-19 16:58:41))

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:759
    755▕         // If an exception occurs when attempting to run a query, we'll format the error
    756▕         // message to include the bindings with SQL, which will make this exception a
    757▕         // lot more helpful to the developer instead of just the database's errors.
    758▕         catch (Exception $e) {
  ➜ 759▕             throw new QueryException(
    760▕                 $query, $this->prepareBindings($bindings), $e
    761▕             );
    762▕         }
    763▕     }

      +18 vendor frames 
  19  tests/Unit/ConcertTest.php:17
      Illuminate\Database\Eloquent\Model::__callStatic()

  • Tests\Feature\ViewConcertListingTest > user can view a concert listing
  Failed asserting that '<h1>Sample Title</h1>\n
  <h2>Sample Subtitle</h2>\n
  <p></p>\n
  <p>Doors at 8:00pm</p>\n
  <p>32.50</p>\n
  <p>Sample Venue</p>\n
  <p>123 Sample Address</p>\n
  <p>Some City, ON 12345</p>\n
  <p>For tickets, call (555) 555-5555.</p>\n
  ' contains "December 13, 2016".

  at tests/Feature/ViewConcertListingTest.php:44
     40▕         // Assert
     41▕         // see the concert details
     42▕         $view->assertSee('Sample Title');
     43▕         $view->assertSee('Sample Subtitle');
  ➜  44▕         $view->assertSee('December 13, 2016');
     45▕         $view->assertSee('8:00pm');
     46▕         $view->assertSee('32.50');
     47▕         $view->assertSee('Sample Venue',);
     48▕         $view->assertSee('123 Sample Address');


  Tests:  2 failed
  Time:   1.79s

tenzan's avatar

@mvd I think, I need to add use DatabaseMigrations; to be like

class ConcertTest extends TestCase
{
    use DatabaseMigrations;
    /** @test */
    function can_get_formatted_date()
tenzan's avatar

@mvd So, do I have to always use use Tests\TestCase; from now one?

tykus's avatar

@tenzan Only whenever you need framework features (like Database connections) - you can use the PHPUnit TestCase whenever you're unit testing (which should not rely on the underlying framework features)

1 like
mvd's avatar

@tenzan yes, @tykus is correct.

If you don't use the database you can just use PHPUnit\Framework\TestCase;

1 like

Please or to participate in this conversation.