|
| 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. |
0 commit comments