-
Notifications
You must be signed in to change notification settings - Fork 1
Extract RuleTestCase from shipmonk/phpstan-rules #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
53b81aa
Extract RuleTestCase from shipmonk/phpstan-rules
JanTvrdik d7a0419
Add MIT License
JanTvrdik 059292c
Fix deprecated PHPCS configuration syntax
JanTvrdik 40e3f40
Remove shipmonk identifier requirement from RuleTestCase
JanTvrdik 88521d5
Add PSR-4 autoload-dev configuration for tests
JanTvrdik af5b783
Add comprehensive test suite with example rule
JanTvrdik 73c2fd4
Enhance RuleTestCase to support multiple errors on same line
JanTvrdik cffc3e9
Update GitHub Actions checkout to v4
JanTvrdik c13b0b2
Add tests directory to PHPStan analysis
JanTvrdik 88eb8c1
Add PHP version matrix to checks workflow
JanTvrdik 26e630b
Update README with comprehensive testing documentation
JanTvrdik bbfb406
Add composer.lock to gitignore and remove from repository
JanTvrdik e644310
Add PHPUnit dependency for testing framework
JanTvrdik 3276bae
Refactor analyseFile to analyzeFiles accepting multiple files
JanTvrdik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| [*] | ||
| charset = utf-8 | ||
| end_of_line = lf | ||
| trim_trailing_whitespace = true | ||
| insert_final_newline = true | ||
| indent_style = space | ||
| indent_size = 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| * text=auto | ||
|
|
||
| /cache export-ignore | ||
| /.editorconfig export-ignore | ||
| /.gitattributes export-ignore | ||
| /.github export-ignore | ||
| /.gitignore export-ignore | ||
| /temp-clone export-ignore | ||
| /composer-dependency-analyser.php export-ignore | ||
| /phpcs.xml.dist export-ignore | ||
| /phpstan.neon.dist export-ignore | ||
| /phpunit.xml.dist export-ignore | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| name: Checks | ||
| on: | ||
| pull_request: | ||
| push: | ||
| branches: | ||
| - "master" | ||
| - "v[0-9]" | ||
| jobs: | ||
| checks: | ||
| runs-on: ubuntu-latest | ||
| strategy: | ||
| fail-fast: false | ||
| steps: | ||
| - | ||
| name: Checkout code | ||
| uses: actions/checkout@v4 | ||
| - | ||
| name: Setup PHP | ||
| uses: shivammathur/setup-php@v2 | ||
| with: | ||
| php-version: 8.4 | ||
JanTvrdik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - | ||
| name: Install dependencies | ||
| run: composer install --no-progress --prefer-dist --no-interaction | ||
|
|
||
| - | ||
| name: Run checks | ||
| run: composer check | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| name: phpcs.xml lint | ||
| on: | ||
| pull_request: | ||
| push: | ||
| branches: | ||
| - "master" | ||
| - "v[0-9]" | ||
|
|
||
| jobs: | ||
| xml-linter: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - | ||
| name: Checkout code | ||
| uses: actions/checkout@v3 | ||
JanTvrdik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| - | ||
| name: Install dependencies | ||
| run: composer install --no-progress --no-interaction | ||
|
|
||
| - | ||
| name: Lint | ||
| uses: ChristophWurst/xmllint-action@v1 | ||
| with: | ||
| xml-file: phpcs.xml.dist | ||
| xml-schema-file: vendor/squizlabs/php_codesniffer/phpcs.xsd | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| /vendor | ||
| /cache/* | ||
| !/cache/.gitkeep | ||
| /.idea | ||
| /phpstan.neon |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,304 @@ | ||
| # PHPStan 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. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| composer require --dev shipmonk/phpstan-dev | ||
| ``` | ||
|
|
||
| ## Quick Start | ||
|
|
||
| ### 1. Create Your Rule Test Class | ||
|
|
||
| ```php | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace YourNamespace; | ||
|
|
||
| use ShipMonk\PHPStanDev\RuleTestCase; | ||
| use PHPStan\Rules\Rule; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<YourRule> | ||
| */ | ||
| class YourRuleTest extends RuleTestCase | ||
| { | ||
| protected function getRule(): Rule | ||
| { | ||
| return new YourRule(); | ||
| } | ||
|
|
||
| public function testRule(): void | ||
| { | ||
| $this->analyseFile(__DIR__ . '/data/YourRule/code.php'); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### 2. Create Test Data File | ||
|
|
||
| Create `tests/Rule/data/YourRule/code.php`: | ||
|
|
||
| ```php | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace YourRule; | ||
|
|
||
| function test() { | ||
| $valid = 'This is valid code'; | ||
| $invalid = something(); // error: Your custom error message here | ||
| } | ||
| ``` | ||
|
|
||
| ### 3. Run Your Test | ||
|
|
||
| ```bash | ||
| vendor/bin/phpunit tests/Rule/YourRuleTest.php | ||
| ``` | ||
|
|
||
| ## Key Features | ||
|
|
||
| ### 🎯 Error Comment System | ||
|
|
||
| Use `// error: <message>` comments in test files to specify expected errors: | ||
|
|
||
| ```php | ||
| <?php | ||
|
|
||
| $validCode = 'No error expected here'; | ||
| $invalidCode = forbidden(); // error: This is forbidden | ||
| $alsoInvalid = another(); // error: Another error message | ||
| ``` | ||
|
|
||
| ### 🔧 Autofix Mode | ||
|
|
||
| During development, automatically generate error comments: | ||
|
|
||
| ```php | ||
| public function testRule(): void | ||
| { | ||
| // Set to true temporarily to generate error comments | ||
| $this->analyseFile(__DIR__ . '/data/code.php', 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 | ||
| - ✅ Identifier must start with `'shipmonk.'` | ||
| - ✅ 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->analyseFile(__DIR__ . '/data/ComplexRule/default.php'); | ||
| } | ||
|
|
||
| public function testStrict(): void | ||
| { | ||
| $this->strictMode = true; | ||
| $this->analyseFile(__DIR__ . '/data/ComplexRule/strict.php'); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### PHP Version-Specific Tests | ||
|
|
||
| ```php | ||
| public function testPhp82Features(): void | ||
| { | ||
| $this->phpVersion = $this->createPhpVersion(80_200); | ||
| $this->analyseFile(__DIR__ . '/data/Rule/php82-features.php'); | ||
| } | ||
| ``` | ||
|
|
||
| ### Custom PHPStan Configuration | ||
|
|
||
| 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 | ||
| ``` | ||
|
|
||
| ## Common Patterns | ||
|
|
||
| ### Testing Different Error Scenarios | ||
|
|
||
| ```php | ||
| <?php | ||
|
|
||
| // Valid usage - no error comment | ||
| $valid = getValue(); | ||
|
|
||
| // Invalid usage with expected error | ||
| $invalid = badFunction(); // error: Function badFunction() is not allowed | ||
|
|
||
| // Multiple errors on same line | ||
| $a = bad1(); $b = bad2(); // error: Function bad1() is not allowed // error: Function bad2() is not allowed | ||
| ``` | ||
|
|
||
| ### Testing Complex Error Messages | ||
|
|
||
| ```php | ||
| <?php | ||
|
|
||
| function test($param) { | ||
| // Error with variable content | ||
| return $param + null; // error: Null value involved in binary operation: int + null | ||
|
|
||
| // Error with specific types | ||
| $array['key'] = $value; // error: Unsafe array key access, key type: mixed | ||
| } | ||
| ``` | ||
|
|
||
| ## Error Comment Rules | ||
|
|
||
| 1. **Format**: `// error: <message>` | ||
| 2. **Placement**: At the end of the line that should trigger the error | ||
| 3. **Multiple errors**: Use separate comments: `// error: First // error: Second` | ||
| 4. **No error expected**: Don't add any comment | ||
JanTvrdik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 5. **Exact matching**: Error message must match exactly (whitespace sensitive) | ||
|
|
||
| ## Development Workflow | ||
|
|
||
| ### 1. Write the Rule | ||
|
|
||
| ```php | ||
| class YourRule implements Rule | ||
| { | ||
| public function getNodeType(): string | ||
| { | ||
| return Node\Expr\FuncCall::class; | ||
| } | ||
|
|
||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| // Your rule logic | ||
| return [ | ||
| RuleErrorBuilder::message('Error message') | ||
| ->identifier('shipmonk.yourRule.errorType') | ||
| ->build(), | ||
| ]; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### 2. Create Test with Autofix | ||
|
|
||
| ```php | ||
| public function testRule(): void | ||
| { | ||
| $this->analyseFile(__DIR__ . '/data/code.php', autofix: true); | ||
| } | ||
| ``` | ||
|
|
||
| ### 3. Run Test to Generate Comments | ||
|
|
||
| ```bash | ||
| vendor/bin/phpunit tests/Rule/YourRuleTest.php | ||
| ``` | ||
|
|
||
| ### 4. Review Generated Comments | ||
|
|
||
| Check the test data file - error comments will be automatically added. | ||
|
|
||
| ### 5. Remove Autofix and Commit | ||
|
|
||
| ```php | ||
| public function testRule(): void | ||
| { | ||
| $this->analyseFile(__DIR__ . '/data/code.php'); // autofix: true removed | ||
| } | ||
| ``` | ||
|
|
||
| ## 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 | ||
| ``` | ||
|
|
||
| ## Requirements | ||
|
|
||
| - PHP 7.4 or higher | ||
| - PHPStan 2.1.8 or higher | ||
| - PHPUnit 9.6.22 or higher (for testing) | ||
|
|
||
| ## License | ||
|
|
||
| MIT License - see [LICENSE](LICENSE) file. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.