savinmikhail / enforce-aaa-pattern-rector
Rector rule to enforce the presence of 'arrange', 'act', 'assert' structure of phpunit tests
Installs: 38
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Type:rector-extension
Requires
- php: >=8.2
- rector/rector: ^2.1.4
Requires (Dev)
- ergebnis/composer-normalize: ^2.45
- friendsofphp/php-cs-fixer: ^3.68.5
- icanhazstring/composer-unused: ^0.8.11
- maglnet/composer-require-checker: ^4.15
- phpunit/phpunit: ^10.5.45
- phpyh/coding-standard: ^2.6.2
- savinmikhail/add_named_arguments_rector: ^0.1.18
- savinmikhail/dist-size-optimizer: ^0.2.0
README
EnforceAaaPatternRector
The EnforceAaaPatternRector rule helps maintain consistent Arrange–Act–Assert (AAA) structure in PHPUnit tests. It automatically annotates your test methods with // Arrange
, // Act
, and // Assert
comments, improving readability and making the test structure explicit.
Example
final class FooTest extends PHPUnit\Framework\TestCase { public function testFoo(): void { - $date = new DateTimeImmutable('2025-01-01'); - $formatted = $date->format('Y-m-d'); - $this->assertEquals('2025-01-01', $formatted); + // Arrange + $date = new DateTimeImmutable('2025-01-01'); + // Act + $formatted = $date->format('Y-m-d'); + // Assert + $this->assertEquals('2025-01-01', $formatted); } }
This feature works for:
- Any PHPUnit test method containing at least one
$this->assert*()
call - Idempotent: existing comments are preserved
- Safe: does not reorder statements, only annotates
Installation
You can install the package via Composer:
composer require --dev savinmikhail/enforce-aaa-pattern-rector
Usage
To enable the rule, add it to your Rector configuration (rector.php
):
<?php declare(strict_types=1); use Rector\Config\RectorConfig; use SavinMikhail\EnforceAaaPatternRector\EnforceAaaPatternRector; return static function (RectorConfig $rectorConfig): void { $rectorConfig->rule(EnforceAaaPatternRector::class); };
Tests as Documentation
The package includes PHPUnit-based Rector tests demonstrating the rule in action.
Check the tests/Fixture
directory to see examples of AAA annotation in various scenarios.
Contributing
Contributions, feedback, and suggestions are welcome! Feel free to open issues or submit pull requests.