Skip to content

Commit d7ea4a5

Browse files
committed
Initial commit
0 parents  commit d7ea4a5

28 files changed

+923
-0
lines changed

.github/workflows/full-checks.yml

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: "Full checks"
2+
3+
on:
4+
schedule:
5+
- cron: '0 10 * * *'
6+
pull_request:
7+
push:
8+
branches:
9+
- main
10+
11+
jobs:
12+
full-checks:
13+
name: "Full CI checks for all supported PHP versions"
14+
15+
runs-on: ${{ matrix.operating-system }}
16+
17+
strategy:
18+
fail-fast: false
19+
matrix:
20+
dependencies:
21+
- "lowest"
22+
- "highest"
23+
php-version:
24+
- "8.0"
25+
- "8.1"
26+
- "8.2"
27+
operating-system:
28+
- "ubuntu-22.04"
29+
30+
steps:
31+
- name: "Checkout"
32+
uses: "actions/checkout@v2"
33+
34+
- name: "Install PHP"
35+
uses: "shivammathur/setup-php@v2"
36+
with:
37+
coverage: Xdebug
38+
php-version: "${{ matrix.php-version }}"
39+
tools: composer
40+
41+
- name: Get composer cache directory
42+
id: composercache
43+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
44+
45+
- name: Cache dependencies
46+
uses: actions/cache@v2
47+
with:
48+
path: ${{ steps.composercache.outputs.dir }}
49+
key: "php-${{ matrix.php-version }}-${{ matrix.dependencies }}-${{ hashFiles('**/composer.lock') }}"
50+
restore-keys: "php-${{ matrix.php-version }}-${{ matrix.dependencies }}-${{ hashFiles('**/composer.lock') }}"
51+
52+
- name: "Install lowest dependencies"
53+
if: ${{ matrix.dependencies == 'lowest' }}
54+
run: "composer update --prefer-lowest --no-interaction --no-progress"
55+
56+
- name: "Install highest dependencies"
57+
if: ${{ matrix.dependencies == 'highest' }}
58+
run: "composer update --no-interaction --no-progress"
59+
60+
- name: "Full CI"
61+
run: "composer ci"

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.vagrant
2+
Vargrantfile
3+
vendor/
4+
.idea/
5+
.phpunit.cache/
6+
.php-cs-fixer.cache

.php-cs-fixer.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->exclude('tests/Rules/data')
5+
->in(__DIR__);
6+
7+
$config = new PhpCsFixer\Config();
8+
return $config
9+
->setRiskyAllowed(true)
10+
->setRules(
11+
[
12+
'@PSR1' => true,
13+
'@PSR2' => true,
14+
'@PSR12' => true,
15+
'@Symfony' => true,
16+
'@Symfony:risky' => true,
17+
'array_syntax' => ['syntax' => 'short'],
18+
'no_useless_else' => true,
19+
'no_useless_return' => true,
20+
'ordered_imports' => true,
21+
'phpdoc_order' => true,
22+
'strict_comparison' => true,
23+
'phpdoc_align' => false,
24+
'phpdoc_to_comment' => false,
25+
'native_function_invocation' => false,
26+
]
27+
)
28+
->setFinder($finder)
29+
;

LICENSE.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# The MIT License (MIT)
2+
3+
Copyright (c) 2022 Dave Liddament
4+
5+
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:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
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.

README.md

+209
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
# PHPStan rule testing helper
2+
3+
This is a helper library for slight improvement to DX for testing PHPStan rules.
4+
It allows you to write the expected error message in the fixture file.
5+
Anything after `// ERROR ` is considered the expected error message.
6+
The test classes are simplified as you now specify just the fixture files, and this library will extract the expected error and calculate the correct line number.
7+
8+
You can also use an `ErrorMessageFormatter` to further decouple tests from the actual error message.
9+
See [ErrorMessageFormatter](#error-formatter) section.
10+
11+
## Example
12+
13+
Test code extends [AbstractRuleTestCase](src/AbstractRuleTestCase.php).
14+
As with the PHPStan's `RuleTestCase` use the `getRule` method to setup the rule used by the test.
15+
For each test list the fixture file(s) needed by the test, using the `assertIssuesReported` method.
16+
17+
#### Test code:
18+
```php
19+
use DaveLiddament\PhpstanRuleTestingHelper\AbstractRuleTestCase;
20+
21+
class CallableFromRuleTest extends AbstractRuleTestCase
22+
{
23+
protected function getRule(): Rule
24+
{
25+
return new CallableFromRule($this->createReflectionProvider());
26+
}
27+
28+
public function testAllowedCall(): void
29+
{
30+
$this->assertIssuesReported(__DIR__ . '/Fixtures/SomeCode.php');
31+
}
32+
}
33+
```
34+
35+
The fixture file contains the expected error message.
36+
#### Fixture:
37+
38+
```php
39+
class SomeCode
40+
{
41+
public function go(): void
42+
{
43+
$item = new Item("hello");
44+
$item->updateName("world"); // ERROR Can not call method
45+
}
46+
}
47+
```
48+
49+
Every line that contains `// ERROR ` is considered an issue that should be picked up by the rule.
50+
The text after `// ERROR` is the expected error message.
51+
52+
With this approach you don't need to work out the line number of the error.
53+
There are further benefits by using the [ErrorMessageFormatter](#error-formatter) to decouple the error messages from the test code.
54+
55+
56+
57+
NOTE: You can pass in multiple fixture files. E.g.
58+
```php
59+
$this->assertIssuesReported(
60+
__DIR__ . '/Fixtures/SomeCode.php',
61+
__DIR__ . '/Fixtures/SomeCode2.php',
62+
// And so on...
63+
);
64+
```
65+
66+
67+
## Installation
68+
69+
```shell
70+
composer require --dev dave-liddament/phpstan-rule-test-helper
71+
```
72+
73+
## Error Formatter
74+
75+
The chances are when you developing PHPStan rules the error message for violations will change.
76+
Making any change will require you to update all the related tests.
77+
78+
### Constant string error messages
79+
80+
In the simplest case the error is a message that does provide any context, other than line number.
81+
E.g. in the example the error is `Can not call method`. No additional information (e.g. who was trying to call the method) is provided.
82+
83+
Create a class that extends `ConstantErrorFormatter` and pass the error message to the constructor.
84+
85+
```php
86+
class CallableFromRuleErrorFormatter extends ConstantStringErrorMessageFormatter
87+
{
88+
public function __construct()
89+
{
90+
parent::__construct('Can not call method');
91+
}
92+
}
93+
```
94+
95+
The next step is to update the test to tell it to use this formatter.
96+
97+
```php
98+
class CallableFromRuleTest extends AbstractRuleTestCase
99+
{
100+
// getRule method omitted for brevity
101+
// testAllowedCall method omitted for brevity
102+
103+
protected function getErrorFormatter(): ErrorMessageFormatter
104+
{
105+
return new CallableFromRuleErrorFormatter();
106+
}
107+
}
108+
```
109+
110+
Now if the error message is changed, the text only needs to be updated in one place.
111+
112+
Finally, the fixture can be simplified.
113+
There is no need to specify the error message in the fixture file, we just need to specify where the error is.
114+
115+
Updated fixture:
116+
```php
117+
class SomeCode
118+
{
119+
public function go(): void
120+
{
121+
$item = new Item("hello");
122+
$item->updateName("world"); // ERROR
123+
}
124+
}
125+
```
126+
127+
### Error messages with context
128+
129+
Good error message will provide context.
130+
For example, the error message could be improved to give the name of the calling class.
131+
The calling class is `SomeClass` so let's update the error message to `Can not call method from SomeCode`.
132+
133+
The fixture is updated to include the calling class name after `// ERROR`
134+
135+
```php
136+
class SomeCode
137+
{
138+
public function go(): void
139+
{
140+
$item = new Item("hello");
141+
$item->updateName("world"); // ERROR SomeCode
142+
}
143+
}
144+
```
145+
146+
The `CallableFromRuleErrorFormatter` is updated.
147+
Firstly it now extends `ErrorMessageFormatter` instead of `ConstantErrorFormatter`.
148+
An implementation of `getErrorMessage` is added.
149+
This is passed everything after `\\ ERROR`, with whitespace trimmed from each side, and must return the expected error message.
150+
151+
```php
152+
class CallableFromRuleErrorFormatter extends ErrorMessageFormatter
153+
{
154+
public function getErrorMessage(string $errorContext): string
155+
{
156+
return 'Can not call method from ' . $errorContext;
157+
}
158+
}
159+
```
160+
161+
### Error message helper methods
162+
163+
Sometimes the contextual error messages might have 2 or more pieces of information.
164+
Continuing the example above, the error message could be improved to give the name of the calling class and the method being called.
165+
E.g. `Can not call Item::updateName from SomeCode`.
166+
167+
The fixture is updated to include both `Item::updateName` and `SomeCode` seperated by the `|` character.
168+
169+
E.g. `// ERROR`
170+
171+
```php
172+
class SomeCode
173+
{
174+
public function go(): void
175+
{
176+
$item = new Item("hello");
177+
$item->updateName("world"); // ERROR Item::updateName|SomeCode
178+
}
179+
}
180+
```
181+
182+
Use the `getErrorMessageAsParts` helper method to do this, as shown below:
183+
184+
```php
185+
186+
class CallableFromRuleErrorFormatter extends ErrorMessageFormatter
187+
{
188+
public function getErrorMessage(string $errorContext): string
189+
{
190+
$parts = $this->getErrorMessageAsParts($errorContext, 2);
191+
return sprintf('Can not call %s from %s', $parts[0], $parts[1]);
192+
}
193+
}
194+
```
195+
196+
The signature of `getErrorMessageAsParts` is:
197+
198+
```php
199+
/**
200+
* @return list<string>
201+
*/
202+
protected function getErrorMessageAsParts(
203+
string $errorContext,
204+
int $expectedNumberOfParts,
205+
string $separator = '|',
206+
): array
207+
```
208+
209+
If you use the `getErrorMessageAsParts` and the number of parts is not as expected, the test will error with a message that tells you file and line number of the invalid error.

composer.json

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"name": "dave-liddament/phpstan-rule-test-helper",
3+
"description": "Library to help make testing of PHPStan rules easier",
4+
"type": "library",
5+
"require": {
6+
"php": ">=8.0 <8.3",
7+
"phpstan/phpstan": "^1.6"
8+
},
9+
"require-dev": {
10+
"phpunit/phpunit": "^9.0",
11+
"friendsofphp/php-cs-fixer": "^3.7",
12+
"php-parallel-lint/php-parallel-lint": "^1.3.2"
13+
},
14+
"license": "MIT",
15+
"autoload": {
16+
"psr-4": {
17+
"DaveLiddament\\PhpstanRuleTestHelper\\": "src/"
18+
}
19+
},
20+
"autoload-dev": {
21+
"psr-4": {
22+
"DaveLiddament\\PhpstanRuleTestHelper\\Tests\\": "tests/"
23+
}
24+
},
25+
"authors": [
26+
{
27+
"name": "Dave Liddament",
28+
"email": "dave@lampbristol.com"
29+
}
30+
],
31+
"scripts": {
32+
"composer-validate": "@composer validate --no-check-all --strict",
33+
"cs-fix": "php-cs-fixer fix",
34+
"cs": [
35+
"@putenv PHP_CS_FIXER_IGNORE_ENV=1",
36+
"php-cs-fixer fix --dry-run -v"
37+
],
38+
"analyse": "phpstan analyse",
39+
"lint": "parallel-lint src tests",
40+
"test": "phpunit",
41+
"ci": [
42+
"@composer-validate",
43+
"@lint",
44+
"@cs",
45+
"@test",
46+
"@analyse"
47+
]
48+
}
49+
}

fixtures/InvalidErrorMessage.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
class SomeCode
4+
{
5+
public function go(): void
6+
{
7+
$item = new Item('hello');
8+
$item->updateName('world'); // ERROR Item
9+
}
10+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
class SomeCode
4+
{
5+
public function go(): void
6+
{
7+
$item = new Item('hello');
8+
$item->updateName('world'); // ERROR Item::updateName|SomeCode
9+
}
10+
11+
public function anotherMethod(Item $item): void
12+
{
13+
$item->updateName('world'); // ERROR Item::anotherMethod|SomeCode
14+
}
15+
}

0 commit comments

Comments
 (0)