Summer Sale! All accounts are 50% off this week.

Maruu's avatar
Level 38

Can't jump to method definition in Pest with PhpStorm

I recently started using Pest.

It's very beautiful, but I have one problem. The problem is that PhpStorm cannot jump to the definition of $this->actingAs() and $this->get() as shown below.

     $response = $this
         ->actingAs($user)
         ->get('/profile');

Is it possible to jump to the definition of each method with some settings?

0 likes
3 replies
LaryAI's avatar
Level 58

To enable navigation to method definitions in Pest with PhpStorm, you need to ensure that PhpStorm recognizes the methods provided by Pest and its underlying framework (likely Laravel in this case). Here are the steps to improve method recognition and navigation:

  1. Install the Laravel Plugin: Ensure that you have the Laravel plugin installed in PhpStorm. This plugin enhances support for Laravel's features in PhpStorm, including navigation and code completion.

    • Go to File > Settings (or PhpStorm > Preferences on macOS).
    • Navigate to Plugins.
    • Search for "Laravel" in the Marketplace tab.
    • Install the plugin and restart PhpStorm.
  2. Enable Laravel Plugin for Your Project: After installing the plugin, you need to enable it specifically for your project.

    • Go to File > Settings > Languages & Frameworks > PHP > Laravel.
    • Check Enable plugin for this project.
  3. IDE Helper: Laravel IDE Helper can generate a file that PhpStorm can use to understand Laravel facades and magic methods, which improves navigation and code completion.

    • Install the IDE Helper package via Composer:
      composer require --dev barryvdh/laravel-ide-helper
      
    • Generate the helper file:
      php artisan ide-helper:generate
      
    • For Pest-specific methods, ensure that your Pest test cases are properly documented or that any Pest-specific plugins or extensions are recognized by PhpStorm.
  4. Regenerate PhpStorm Metadata: Sometimes PhpStorm needs to refresh its internal metadata to recognize new classes and methods.

    • Use the command File > Invalidate Caches / Restart... > Invalidate and Restart.
  5. DocBlocks: If PhpStorm still doesn't recognize methods, you can manually add DocBlocks above your test methods to hint the expected types. For example:

    /** @var \Illuminate\Foundation\Testing\TestCase $this */
    $response = $this
        ->actingAs($user)
        ->get('/profile');
    

    This tells PhpStorm that $this is an instance of the Laravel testing base class, which should help it resolve methods like actingAs() and get().

By following these steps, you should be able to improve method recognition in PhpStorm when working with Pest and Laravel, allowing you to navigate to method definitions more effectively.

Maruu's avatar
Level 38

I don't know the cause, but now I can jump without any settings. .

Please or to participate in this conversation.