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

5

10

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>
Bridging answered 19/2, 2020 at 9:50 Comment(5)
Can you post your phpunit.xml? It's in the root directory of the project.Gaylor
@KristianVasilev have added it inBridging
How do you run the tests? phpunit or vendor/bin/phpunit? Can you try vendor/bin/phpunit if you haven't ? What is your phpunit version?Gaylor
@KristianVasilev - I use vendor/bin/phpunit as from my project root directory phpunit returns command not found. Using PHPUnit 8.5.2Bridging
If the problem is in the version of the PHP, the only way you can work around it that I can think of is using DatabaseTransactions trait in your test and have your database structure already in the database and just use transaction to prevent the tests from insterting data in your database so you can start always from a fresh databases without using the RefreshDatabase trait.Gaylor
B
9

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"/>
Bridging answered 24/2, 2020 at 16:4 Comment(0)
E
1

Adding this to my TestCase class in the Laravel tests root folder solves the issue:

protected function setUp(): void
{
    parent::setUp();
}

seems to be an issue with laravel telescope: https://github.com/laravel/telescope/issues/804

Emelineemelita answered 21/2, 2020 at 14:46 Comment(0)
T
1

I faced quite a similar issue when i upgraded my php version to 7.4.3. I typed this command

composer update

in my console and the error disappeared.

Turpin answered 7/4, 2020 at 10:58 Comment(0)
D
0

I had also faced the similar issue.

I upgraded my PHP version to 7.4 and run composer update and the issue was fixed.

Denby answered 30/11, 2020 at 13:17 Comment(0)
K
0

This fixed the issue for me

composer selfupdate

I'm using an old Symfony 4 but the behavior should be close for some.
Source

Kan answered 10/2, 2022 at 15:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.