diff --git a/README.md b/README.md index 46ccaec..19152c4 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,7 @@ -# PHPStan Development Utilities +# PHPStan Rules Development Utilities -Development utilities for PHPStan rules testing, extracted from [shipmonk/phpstan-rules](https://github.com/shipmonk-rnd/phpstan-rules). - -This package provides the `RuleTestCase` class - an enhanced testing framework specifically designed for testing PHPStan rules with additional validation and convenience features. +Inplace fixture asserts and autofix for PHPStan rules. +No more manual line adjustments in tests when new code is added to rule fixtures. ## Installation @@ -10,197 +9,67 @@ This package provides the `RuleTestCase` class - an enhanced testing framework s composer require --dev shipmonk/phpstan-dev ``` -## Quick Start - -### 1. Create Your Rule Test Class +## Usage ```php */ -class YourRuleTest extends RuleTestCase +class YourRuleTest extends \ShipMonk\PHPStanDev\RuleTestCase { - protected function getRule(): Rule - { - return new YourRule(); - } - public function testRule(): void { - $this->analyzeFiles([__DIR__ . '/Data/YourRule/code.php']); + $this->analyzeFiles([__DIR__ . '/Data/code.php']); } } ``` -### 2. Create Test Data File - -Create `tests/Rule/Data/YourRule/code.php`: +Create test fixture at `code.php`: ```php -` comments in test files to specify expected errors: +Mark expected errors directly in test files via PHP comments: ```php analyzeFiles([__DIR__ . '/Data/code.php'], autofix: true); + $this->analyzeFiles([...], autofix: true); } ``` **⚠️ Important**: Remove `autofix: true` before committing - tests will fail if autofix is enabled. -### 🛡️ Automatic Error Validation - -Every error is automatically validated: -- ✅ Must have an identifier -- ✅ Errors are matched to specific line numbers - -## Advanced Usage - -### Multiple Test Scenarios - -```php -class ComplexRuleTest extends RuleTestCase -{ - private bool $strictMode = false; - - protected function getRule(): Rule - { - return new ComplexRule($this->strictMode); - } - - public function testDefault(): void - { - $this->analyzeFiles([__DIR__ . '/Data/ComplexRule/default.php']); - } - - public function testStrict(): void - { - $this->strictMode = true; - $this->analyzeFiles([__DIR__ . '/Data/ComplexRule/strict.php']); - } -} -``` - -### PHP Version-Specific Tests - -```php -public function testPhp82Features(): void -{ - $this->phpVersion = $this->createPhpVersion(80_200); - $this->analyzeFiles([__DIR__ . '/Data/Rule/php82-features.php']); -} -``` - -### Custom PHPStan Configuration +## Contributing +- Check your code by `composer check` +- Autofix coding-style by `composer fix:cs` +- All functionality must be tested -Create `tests/Rule/Data/YourRule/config.neon`: - -```neon -parameters: - customParameter: value -``` - -Then reference it in your test: - -```php -public static function getAdditionalConfigFiles(): array -{ - return array_merge( - parent::getAdditionalConfigFiles(), - [__DIR__ . '/Data/YourRule/config.neon'], - ); -} -``` - -### Rules with Dependencies - -```php -protected function getRule(): Rule -{ - $dependency = self::getContainer()->getByType(SomeService::class); - return new RuleWithDependencies($dependency); -} -``` - -## File Organization - -Recommended directory structure: - -``` -tests/ -├── Rule/ -│ ├── YourRuleTest.php -│ ├── AnotherRuleTest.php -│ └── Data/ -│ ├── YourRule/ -│ │ ├── code.php # Main test file -│ │ ├── edge-cases.php # Additional scenarios -│ │ └── config.neon # Optional PHPStan config -│ └── AnotherRule/ -│ └── code.php -``` - -## Development - -```bash -# Install dependencies -composer install - -# Run all checks -composer check - -# Individual checks -composer check:composer # Validate composer.json -composer check:ec # Check EditorConfig compliance -composer check:cs # Check coding standards (PHPCS) -composer check:types # Run PHPStan analysis -composer check:dependencies # Analyze dependencies -composer check:collisions # Check for name collisions - -# Fix coding standards -composer fix:cs -``` ## License -MIT License - see [LICENSE](LICENSE) file. +MIT