Rector rules for PestPHP to improve code quality and help with version upgrades.
See all available Pest rules here.
composer require --dev mrpunyapal/rector-pestImprove your Pest tests with better readability and expressiveness.
// rector.php
use RectorPest\Set\PestSetList;
use Rector\Config\RectorConfig;
return RectorConfig::configure()
->withPaths([
__DIR__ . '/tests',
])
->withSets([
PestSetList::PEST_CODE_QUALITY,
]);| Set | Description |
|---|---|
PestSetList::PEST_CODE_QUALITY |
Converts expect() assertions to use Pest's built-in matchers for better readability |
PestSetList::PEST_CHAIN |
Merges multiple expect() calls into chained expectations and optimizes their order. |
PestSetList::PEST_LARAVEL |
Laravel-specific rules (requires illuminate/support): converts Str:: equality checks to Pest string case matchers |
PestSetList::PEST_MIGRATION |
PHPUnit → Pest migration rules (opt-in): converts assertions, data providers, and test structure |
Use PestLevelSetList to automatically upgrade to a specific Pest version. Sets for higher versions include sets for lower versions.
// rector.php
use RectorPest\Set\PestLevelSetList;
use Rector\Config\RectorConfig;
return RectorConfig::configure()
->withPaths([
__DIR__ . '/tests',
])
->withSets([
PestLevelSetList::UP_TO_PEST_40,
]);| Set | Description |
|---|---|
PestLevelSetList::UP_TO_PEST_30 |
Upgrade from Pest v2 to v3 |
PestLevelSetList::UP_TO_PEST_40 |
Upgrade from Pest v2/v3 to v4 (includes v3 changes) |
Use PestSetList if you only want changes for a specific version:
// rector.php
use RectorPest\Set\PestSetList;
use Rector\Config\RectorConfig;
return RectorConfig::configure()
->withPaths([
__DIR__ . '/tests',
])
->withSets([
PestSetList::PEST_30, // Only v2→v3 changes
]);| Set | Description |
|---|---|
PestSetList::PEST_30 |
Pest v2 → v3 migration rules |
PestSetList::PEST_40 |
Pest v3 → v4 migration rules |
The PEST_CHAIN set automatically merges multiple expect() calls into a single chained expression.
// rector.php
use RectorPest\Set\PestSetList;
use Rector\Config\RectorConfig;
return RectorConfig::configure()
->withPaths([
__DIR__ . '/tests',
])
->withSets([
PestSetList::PEST_CODE_QUALITY,
PestSetList::PEST_CHAIN,
]);Before:
expect($value1)->toBe(10);
expect($value1)->toBeInt();
expect($value2)->toBe(20);
expect($value2)->toBeString();
expect($value3)->toBe(30);After:
expect($value1)->toBe(10)
->toBeInt()
->and($value2)->toBe(20)
->toBeString()
->and($value3)->toBe(30);Formatting rules (requires rector/rector 2.4.1+):
- The first matcher after
expect()stays on the same line asexpect() - The first matcher after
->and()stays on the same line as->and() - Every additional matcher in a segment goes on its own indented line
->not->toBeX()is treated as a single unit and stays inline
Note: On
rector/rectorversions older than 2.4.1, chaining still works but all method calls are printed inline on a single line.
The PEST_MIGRATION set helps convert PHPUnit test patterns to Pest equivalents. This is an opt-in set — review changes carefully after applying.
// rector.php
use RectorPest\Set\PestSetList;
use Rector\Config\RectorConfig;
return RectorConfig::configure()
->withPaths([
__DIR__ . '/tests',
])
->withSets([
PestSetList::PEST_MIGRATION,
]);Included rules:
| Rule | Description |
|---|---|
ConvertAssertToExpectRector |
Converts $this->assert*() calls to expect()-> chains |
You can also use individual rules instead of sets:
// rector.php
use RectorPest\Rules\ChainExpectCallsRector;
use Rector\Config\RectorConfig;
return RectorConfig::configure()
->withPaths([
__DIR__ . '/tests',
])
->withRules([
ChainExpectCallsRector::class,
]);# Preview changes
vendor/bin/rector process --dry-run
# Apply changes
vendor/bin/rector process- PHP 8.2+
- Rector 2.0+
Contributions are welcome! Please feel free to submit a Pull Request.
The MIT License (MIT). Please see License File for more information.