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

JayD's avatar
Level 11

PHPUnit infinite loop after upgrading to PHP 8.0

Hello All,

I have an issue where a test will infinite loop.

This test isdisabling all authentication middleware via withoutMiddleware() and followingRedirects().

The custom implementation

     $this->withoutMiddleware([
            \App\Http\Middleware\Authenticate::class,
            \App\Http\Middleware\RedirectIfAuthenticated::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Auth\Middleware\Authorize::class,
        ]);

It seems that the test is now instant looping and throwing the following error in the logs:

Error with message: Call to a member function can() on null. .

The null is thrown on the following line of code:

if (auth()->user()->can('view', Model::class)) { ...

I wonder why I get this issue on PHP 8.0, while on PHP 7.4 this exception was not thrown at all (as auth()->user() was also null on php 7.4).

I hope someone can help me out!

Kind regards.

0 likes
3 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Not sure why it happens in php 8, but try this

 if (auth()->check() && auth()->user()->can('view', Model::class)) { ...
1 like
JayD's avatar
Level 11

@Sinnbeck This does indeed resolve the infinite loop error, but is mentioning that the actual test has failed:

Failed asserting that two strings are equal.
Expected :'account.edit'
Actual   :'dashboard.index'

I think that PHP8.0 has become more strict on specific exceptions (which were ignored in php7.4).

Thanks for pointing this out.

Please or to participate in this conversation.