-
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 13 commits
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,30 @@ | ||
| name: Checks | ||
| on: | ||
| pull_request: | ||
| push: | ||
| branches: | ||
| - "master" | ||
| - "v[0-9]" | ||
| jobs: | ||
| checks: | ||
| runs-on: ubuntu-latest | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| php-version: ['7.4', '8.0', '8.1', '8.2', '8.3', '8.4'] | ||
| steps: | ||
| - | ||
| name: Checkout code | ||
| uses: actions/checkout@v4 | ||
| - | ||
| name: Setup PHP | ||
| uses: shivammathur/setup-php@v2 | ||
| with: | ||
| php-version: ${{ matrix.php-version }} | ||
| - | ||
| 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@v4 | ||
|
|
||
| - | ||
| 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,6 @@ | ||
| /vendor | ||
| /cache/* | ||
| !/cache/.gitkeep | ||
| /.idea | ||
| /phpstan.neon | ||
| composer.lock |
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,21 @@ | ||
| MIT License | ||
|
|
||
| Copyright (c) 2025 Bedabox, LLC dba ShipMonk | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
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,206 @@ | ||
| # 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 | ||
| - ✅ 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 | ||
| ``` | ||
|
|
||
| ## 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. |
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 @@ | ||
|
|
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,10 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| use ShipMonk\ComposerDependencyAnalyser\Config\Configuration; | ||
|
|
||
| $pharFile = __DIR__ . '/vendor/phpstan/phpstan/phpstan.phar'; | ||
| Phar::loadPhar($pharFile, 'phpstan.phar'); | ||
|
|
||
| require_once('phar://phpstan.phar/preload.php'); // prepends PHPStan's PharAutolaoder to composer's autoloader | ||
|
|
||
| return (new Configuration()); |
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,69 @@ | ||
| { | ||
| "name": "shipmonk/phpstan-dev", | ||
| "description": "Development utilities for PHPStan rules testing, extracted from shipmonk/phpstan-rules", | ||
| "license": [ | ||
| "MIT" | ||
| ], | ||
| "type": "library", | ||
| "keywords": [ | ||
| "phpstan", | ||
| "testing", | ||
| "static analysis" | ||
| ], | ||
| "require": { | ||
| "php": "^7.4 || ^8.0", | ||
| "phpstan/phpstan": "^2.1.8" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe it works just fine even on much lower versions. Maybe even 2.* |
||
| }, | ||
| "require-dev": { | ||
| "editorconfig-checker/editorconfig-checker": "^10.6.0", | ||
| "ergebnis/composer-normalize": "^2.45.0", | ||
| "phpstan/phpstan-deprecation-rules": "^2.0.1", | ||
| "phpstan/phpstan-phpunit": "^2.0.4", | ||
| "phpstan/phpstan-strict-rules": "^2.0.3", | ||
| "phpunit/phpunit": "^9.6.22", | ||
| "shipmonk/composer-dependency-analyser": "^1.8.1", | ||
| "shipmonk/dead-code-detector": "^0.9.0", | ||
| "shipmonk/name-collision-detector": "^2.1.1", | ||
| "slevomat/coding-standard": "^8.16.0" | ||
| }, | ||
| "suggest": { | ||
| "phpunit/phpunit": "^9.6.22 for running tests that use RuleTestCase" | ||
| }, | ||
| "autoload": { | ||
| "psr-4": { | ||
| "ShipMonk\\PHPStanDev\\": "src/" | ||
| } | ||
| }, | ||
| "autoload-dev": { | ||
| "psr-4": { | ||
| "ShipMonkTests\\PHPStanDev\\": "tests/" | ||
| } | ||
| }, | ||
| "config": { | ||
| "allow-plugins": { | ||
| "dealerdirect/phpcodesniffer-composer-installer": false, | ||
| "ergebnis/composer-normalize": true | ||
| }, | ||
| "sort-packages": true | ||
| }, | ||
| "scripts": { | ||
| "check": [ | ||
| "@check:composer", | ||
| "@check:ec", | ||
| "@check:cs", | ||
| "@check:types", | ||
| "@check:dependencies", | ||
| "@check:collisions" | ||
| ], | ||
| "check:collisions": "detect-collisions src", | ||
| "check:composer": [ | ||
| "composer normalize --dry-run --no-check-lock --no-update-lock", | ||
| "composer validate --strict" | ||
| ], | ||
| "check:cs": "phpcs", | ||
| "check:dependencies": "composer-dependency-analyser", | ||
| "check:ec": "ec src", | ||
| "check:types": "phpstan analyse -vv --ansi", | ||
| "fix:cs": "phpcbf" | ||
| } | ||
| } | ||
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.