Trying to access array offset on value of type null when using RefreshDatabase in Laravel
When running PHPUnit tests with the RefreshDatabase trait, you might encounter a baffling error message: "Trying to access array offset on value of type null." This often happens even on a pristine Laravel installation. In this case, the stack trace points directly to /vendor/laravel/telescope/src/Watchers/QueryWatcher.php, specifically line 46.
At first glance, the error suggests that Telescope is trying to store a query log entry, but the underlying data structure it relies on has collapsed into null. This behavior typically stems from a mismatch between the PHP version and the specific version of Laravel packages installed, particularly in environments using legacy package versions like Laravel 6 or early Spark releases.
The Root Cause: Telescope and PHP 7.4
The core issue lies in how the Telescope package handles its internal caching mechanisms during test execution. When RefreshDatabase is active, Laravel drops to an in-memory SQLite database. Telescope attempts to record every SQL query executed during the test run into a cache driver to display them in the dashboard.
If you are running PHP 7.4 (or newer) with older versions of Laravel or Telescope, there is a known incompatibility where the QueryWatcher fails to initialize its internal array correctly. The code attempts to push data into an array that hasn't been instantiated, resulting in the fatal error observed in your stack trace.
The vendor path /vendor/laravel/telescope/src/Watchers/QueryWatcher.php confirms that Telescope is actively trying to watch queries. In a testing environment, this is often unnecessary overhead and can introduce exactly this kind of fragility when version compatibility isn't perfect.
Resolving the Issue Without Downgrading PHP
While downgrading to PHP 7.3 is a quick workaround, it is not a sustainable solution for modern development workflows. You should be able to resolve this without sacrificing your PHP environment by disabling Telescope during tests entirely.
Since you are using RefreshDatabase, you don't need Telescope's query logging capabilities in your test suite. The simplest fix is to ensure Telescope does not run at all when the testing environment is active.
First, check your .env file (or your phpunit.xml configuration). If Telescope is registered as a service provider or alias, it needs to be excluded from the testing context. However, a more direct approach involves checking if Telescope is actually installed in your project.
If you do not strictly need the Spark dashboard for your tests, you can disable the Telescope package by removing its service provider registration or ensuring the telescope facade is not available during test execution. But often, the issue is simply that the APP_RUNNING_IN_TEST_MODE check within Telescope isn't triggering correctly due to the version mismatch.
A more robust solution involves configuring your phpunit.xml to prevent Telescope from loading. You can add a specific environment variable or modify the service provider bootstrapping logic. However, in many cases with Laravel 6 and older Telescope versions on PHP 7.4, simply ensuring that the DB_CONNECTION is set to sqlite and DB_DATABASE is :memory: (which you already have) combined with disabling Telescope resolves the crash.
To disable Telescope programmatically without removing the package, you can run the following command before your test suite or modify your configuration:
php artisan config:clear
php artisan cache:clear
If the error persists despite clearing caches, the most reliable fix is to uninstall Telescope if it is not required for your CI/CD pipeline or local development dashboard.
Alternative: Using Dusk or Separate Configurations
If you require Telescope for debugging but cannot downgrade PHP, consider running your test suite against a different configuration where Telescope is explicitly disabled via environment variables. You can create a .env.testing file that sets TELESCOPE_ENABLED=false if the package supports such a flag in its later versions, though older versions may require removing the package entirely.
In summary, the "array offset on value of type null" error is a symptom of Telescope trying to log queries in an environment where its internal state management has failed due to PHP version incompatibility. Disabling Telescope for tests restores stability without requiring you to compromise your PHP version.