Warm tip: This article is reproduced from serverfault.com, please click

Trying to access array offset on value of type null when using RefreshDatabase in Laravel

发布于 2020-02-19 09:50:03

I am writing tests in a fresh installation of Laravel 6.15 (with Spark 9). My PHP version is PHP 7.4.2. My testing environment is Laravel Valet on MacOS.

I am facing an issue where whenever I use the RefreshDatabase trait as part of a test, all tests fail with the trying to access array offset on value of type null error message.

From reading around the suggestion seems to be to downgrade to PHP 7.3 whenever this error is encountered, however there is no mention in the official documentation that PHP 7.4 isn't supported - is there any other way to resolve the error?

It triggers even if the trait is interested into the Laravel example test:


namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ExampleTest extends TestCase
{

    Use RefreshDatabase;

    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
}

Gives


PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

.EE                                                                 3 / 3 (100%)

Time: 212 ms, Memory: 22.00 MB

There were 2 errors:

1) Tests\Feature\ExampleTest::testBasicTest
ErrorException: Trying to access array offset on value of type null

/Users/rory/sites/landlord/vendor/laravel/telescope/src/Watchers/QueryWatcher.php:46
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:369
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:218
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Database/Connection.php:833
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Database/Connection.php:687
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Database/Connection.php:640
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Database/Connection.php:338
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Database/Connection.php:309
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php:75
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Database/Migrations/DatabaseMigrationRepository.php:169
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:590
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:91
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:63
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:32
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Container/Util.php:36
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:90
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:34
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Container/Container.php:590
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Console/Command.php:134
/Users/rory/sites/landlord/vendor/symfony/console/Command/Command.php:255
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Console/Command.php:121
/Users/rory/sites/landlord/vendor/symfony/console/Application.php:1012
/Users/rory/sites/landlord/vendor/symfony/console/Application.php:272
/Users/rory/sites/landlord/vendor/symfony/console/Application.php:148
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Console/Application.php:93
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Console/Application.php:185
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:273
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Foundation/Testing/PendingCommand.php:139
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Foundation/Testing/PendingCommand.php:238
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php:56
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Foundation/Testing/RefreshDatabase.php:40
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Foundation/Testing/RefreshDatabase.php:17
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:115
/Users/rory/sites/landlord/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:82

phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <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="sqlite"/>
        <server name="DB_DATABASE" value=":memory:"/>
        <server name="MAIL_DRIVER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
    </php>
</phpunit>
Questioner
Rory
Viewed
0
Rory 2020-02-25 00:04:38

I have finally resolved this issue - which it turns out is related to Telescope being installed and running in tests.

To resolve this issue (and ReflectionException: Class env does not exist related issue) add the following line to the phpunit.xml file between the <php> nodes:

<env name="TELESCOPE_ENABLED" value="false"/>