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

Jonjie's avatar
Level 12

actingAs in Pest PHP returns error in VSCode

I am currently using actingAs in pest php but I am getting this error in my vscode, but no error when running the test. But then I use the $this->actingAs(), the error doesnt persists.

Error

Expected type 'Illuminate\Contracts\Auth\Authenticatable'. Found 'Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>|Illuminate\Database\Eloquent\Model'.

Screenshot screenshot

Setup

  • PHP8.3
  • Laravel11 (Laravel Breeze with Inertia)
  • Laravel Herd
  • VSCode (with PHP Intelephense)
1 like
2 replies
LaryAI's avatar
Level 58

The issue you're encountering is related to type hinting and static analysis provided by PHP Intelephense in VSCode. The actingAs method expects an instance of Illuminate\Contracts\Auth\Authenticatable, but it seems like the static analysis tool is confused about the type being passed.

Here's a solution to ensure that the type is correctly recognized by Intelephense:

  1. Ensure Correct Type Hinting: Make sure that the user instance you are passing to actingAs is correctly typed as Illuminate\Contracts\Auth\Authenticatable.

  2. Explicit Type Casting: You can explicitly cast the user to Illuminate\Contracts\Auth\Authenticatable to help the static analysis tool understand the type.

Here is an example of how you can modify your test code:

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use App\Models\User;

it('can perform some action', function () {
    // Create a user instance
    $user = User::factory()->create();

    // Explicitly cast the user to Authenticatable
    $this->actingAs($user);

    // Your test logic here
});

If the above solution does not resolve the issue, you can also try adding a PHPDoc block to help Intelephense understand the type:

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use App\Models\User;

it('can perform some action', function () {
    /** @var \App\Models\User $user */
    $user = User::factory()->create();

    $this->actingAs($user);

    // Your test logic here
});

By adding the PHPDoc block, you provide additional type information to the static analysis tool, which should help it understand that $user is an instance of User and implements Illuminate\Contracts\Auth\Authenticatable.

If you continue to face issues, consider checking for updates to your extensions or even trying a different PHP extension for VSCode, such as PHPStan or Psalm, which might provide better type inference in some cases.

lpinheiro's avatar

Hey, I do not think it's an error, it just the way Intelephense understands the code. You don't get this error when using PhpStorm.

Here's a workaround: Add this line to your VSCode settings.json to get rid of the error: "intelephense.diagnostics.typeErrors": false.

Please or to participate in this conversation.