Search code examples
laraveleloquentphpunitlumen

ModelFactory: Error: Call to a member function connection() on null


While trying to populate database using ModelFactory I am coming across this bit of error, I have been looking up since the last six hours, can not seem to fix it.

My Test runs this:

$books = factory('App\Book', 2)->create();

and the error message is this:

There was 1 error:

1) Tests\App\Http\Controllers\BooksControllerTest::testIndexReturnsCollection
Error: Call to a member function connection() on null

D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\database\Eloquent\Model.php:1234
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\database\Eloquent\Model.php:1200
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\database\Eloquent\Model.php:1030
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\database\Eloquent\Model.php:945
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\database\Eloquent\Model.php:983
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\database\Eloquent\FactoryBuilder.php:203
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\support\Collection.php:407
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\database\Eloquent\FactoryBuilder.php:207
D:\Code\Server\htdocs\rlfp.localhost.com\vendor\illuminate\database\Eloquent\FactoryBuilder.php:185
D:\Code\Server\htdocs\rlfp.localhost.com\tests\app\Http\Controllers\BooksControllerTest.php:35

On my bootstrap/app.php I have on line 26 and 28, like so:

$app->withFacades();

$app->withEloquent();

While my phpunit.xml is like so:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/app.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory suffix="Test.php">./tests</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="DB_DATABASE" value="rlp_rlfpsfactory"/>
    </php>
</phpunit>

Do I need to put anything else in the php part of phpunit xml file?


Solution

  • After spending one whole day with no luck, thanks to @ChrisSprague I downgraded Lumen to 5.6 and it worked.

    Steps:

    1. In composer.json file change

      "laravel/lumen-framework": "5.7.*"
      

      to

      "laravel/lumen-framework": "5.6.*"
      
    2. Delete your vendor folder

    3. Do composer install
    4. Run the test again!

    If it still does not work, make sure you have correct test database details in your phpunit.xml file

    Mine looks like:

    <?xml version="1.0" encoding="UTF-8"?>
    <phpunit backupGlobals="false"
             backupStaticAttributes="false"
             bootstrap="bootstrap/app.php"
             colors="true"
             convertErrorsToExceptions="true"
             convertNoticesToExceptions="true"
             convertWarningsToExceptions="true"
             processIsolation="false"
             stopOnFailure="false">
        <testsuites>
            <testsuite name="Application Test Suite">
                <directory suffix="Test.php">./tests</directory>
            </testsuite>
        </testsuites>
        <filter>
            <whitelist processUncoveredFilesFromWhitelist="true">
                <directory suffix=".php">./app</directory>
            </whitelist>
        </filter>
        <php>
            <env name="APP_ENV" value="testing"/>
            <env name="CACHE_DRIVER" value="array"/>
            <env name="QUEUE_DRIVER" value="sync"/>
            <env name="SESSION_DRIVER" value="array"/>
            <env name="DB_DATABASE" value="rlp_rlfpsfactory"/> <!-- this is testing database -->
            <env name="DB_USERNAME" value="root"/>
            <env name="DB_PASSWORD" value=""/>
        </php>
    </phpunit>