JHWelch/intellipest

IntelliPest - Enhanced coding assistance for PestPHP

โ˜… 0Forks 0GitHub โ†—Compare

README

IntelliPest Header

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

IntelliPest - Enhanced coding assistance for PestPHP.

Features

  • ๐Ÿง  Smart helper file generation for PestPHP
  • ๐Ÿซต๐Ÿป Supports your custom test cases
  • ๐Ÿงฉ Pure PHP & framework agnostic

Important

This project is currently in beta. Features are subject to change.


Introduction

IntelliPest is a CLI tool that generates a helper file based on your own Pest setup.

This assists your IDE or coding agent to better understand your tests, enabling full autocompletion and error checking for compatible language servers.

Editors/IDEs and Coding Agents which can benefit from this include VS Code, Cursor, Zed and OpenCode.

Quick Start

For standard Pest setups with the Pest.php configuration file located in ./tests/Pest.php, you can follow these steps. If your Pest.php is located somewhere else, see the configuration section below.

Prerequisites

Make sure your project meets the following requirements:

  • PHP 8.3+
  • Pest 4.x

Setup

  1. Install the package via composer:
composer require --dev ace-of-aces/intellipest
  1. Run this command to generate the helper file:
./vendor/bin/intellipest

terminal screenshot

  1. If the command ran successfully, you should be all set! You may have to restart your LSP for it to register the helper file.

Configuration

IntelliPest can be configured through flags to the intellipest command.

--config / -c

Specify the path to the Pest.php configuration file. Default: tests/Pest.php.

--output / -o

Specify the path to write the generated IDE helper file. Default: .intellipest/_pest-helper.php.

Note

You may also set the output directory via the INTELLIPEST_OUTPUT_DIR environment variable.

--no-expectation-helpers

Don't generate helper methods for Pest's built-in expectations in the helper file.

Note

Some LSPs like Intelephense Premium support the @mixin PHPDoc tag, which is used in Pest's source code, making these helper methods redundant for those users.

--shush / -s

Don't show the (beautiful) header and footer in the console output ๐Ÿ˜”

--watch / -w

Watch the Pest.php configuration file and automatically regenerate the helper file when changes are detected. Checks for changes every half second.

Note

The watch process continues running until manually stopped (Ctrl+C). Syntax errors in the config file are reported but don't stop the watcher.

--quiet / -q

Don't output any console messages (useful for CI).

Compatibility

LSP compatibility

Currently, compatibility has only been tested with the popular Intelephense PHP LSP.

The main requirement for an LSP to benefit from IntelliPest is support for the @param-closure-this PHPDoc tag specified by PHPStan, which enables type hinting the $this variable inside of test cases.

Note

For PHPStorm users, we recommend just using the first-party Pest plugin by JetBrains.

Pest Version compatibility

Exact API compatibility for minor Pest versions has not been thoroughly tested as of right now. This may improve in future releases.

Important

IntelliPest currently only supports projects using Pest 4.x

Known Limitations

IntelliPest is currently unable to accurately reflect the dynamic Test class association of the $this variable in test cases determined by the ->in(...) method in configuration call chains.

Concrete Example

For a Pest.php configuration with different TestCase classes for the Feature and Unit folders, like this:

pest()->extends(Tests\TestCase::class)
    ->extend(Illuminate\Foundation\Testing\RefreshDatabase::class)
    ->in('Feature');

pest()->extend(Tests\UnitTestCase::class)
    ->in('Unit');

The resulting type hint in all test cases will always be a union of both TestCase classes, regardless of whether the test is located in Feature or Unit:

$this type hint screenshot

Important

It's important to be aware of this while writing tests, as suggestions from both TestCase classes will appear on the $this variable inside of tests, even though they might not be available (possibly resulting in a runtime exception when called from the wrong context).

Technical Details

This is due to IntelliPest's helper file approach only being able to override the @param-closure-this PHPDoc tag for Pest's global testing function declarations.

Under The Hood

IntelliPest leverages the nikic/PHP-Parser package in order to parse your Pest.php as an AST (Abstract Syntax Tree).

This enables it to extract call chains to Pest's configuration API (namely pest(), uses(), expect()).

Based on these function calls and the arguments you pass to them (mainly TestCase classes and Traits), IntelliPest maps those to an internal data structure.

As a final step, IntelliPest takes all of this analyzed data and generates a PHP helper file with the help of templates.


Contributing

Whether it's reporting or fixing bugs, contributing new features, or enhancing the documentation, your help is always appreciated. ๐Ÿ™๐Ÿป

โ†’ Read the Contribution Guidelines

โ†’ Open an Issue

โ†’ Submit a Pull Request

Credits

License

Made with โค๏ธ under the MIT License

Contributors

ace-of-aces

Issues