|
| 1 | +--- |
| 2 | +tags: |
| 3 | + - PHPStan |
| 4 | + - PHPUnit |
| 5 | + |
| 6 | +image: "images/og-images/new-and-noteworthy-phpstan-phpunit-integration.jpg" |
| 7 | + |
| 8 | +ogImage: |
| 9 | + title: "New and noteworthy: PHPStan and PHPUnit integration" |
| 10 | + imageUrl: "https://staabm.github.io/staabm.svg" |
| 11 | + fileName: "new-and-noteworthy-phpstan-phpunit-integration" |
| 12 | +--- |
| 13 | + |
| 14 | +### New and noteworthy: PHPStan and PHPUnit integration |
| 15 | + |
| 16 | +In this article we will have a brief look into the latest update to [`phpstan/phpstan-phpunit`](https://github.com/phpstan/phpstan-phpunit) 2.0.8. |
| 17 | + |
| 18 | +### PHPStan validates PHPUnit data providers |
| 19 | + |
| 20 | +On of the features, which I am most proud of is the data provider validation. |
| 21 | +It was requested by several people years ago, but we did not yet have a good idea how to make it happen without major changes in the PHPStan core. |
| 22 | + |
| 23 | +Starting with this release, we take each data-set of a data-provider and check it against the signature of the corresponding test-case. |
| 24 | + |
| 25 | +At the time of writing we support multiple kind of data-providers: |
| 26 | +- `@test` |
| 27 | +- `#[Test]` |
| 28 | +- "test*" method name prefix |
| 29 | +- `@dataProvider` |
| 30 | +- `#[DataProvider]` |
| 31 | +- `static` data-provider |
| 32 | +- non-`static` data-provider |
| 33 | +- `return []` data-providers |
| 34 | +- `yield []` data-providers |
| 35 | +- `yield from []` data-providers |
| 36 | +- named arguments in data-providers |
| 37 | + |
| 38 | +```php |
| 39 | + #[DataProvider('aProvider')] |
| 40 | + public function testTrim(string $expectedResult, string $input): void |
| 41 | + { |
| 42 | + } |
| 43 | + |
| 44 | + public function aProvider(): array /** @phpstan-ignore missingType.iterableValue */ |
| 45 | + { |
| 46 | + return [ |
| 47 | + [ |
| 48 | + 'Hello World', |
| 49 | + " Hello World \n", |
| 50 | + ], |
| 51 | + [ |
| 52 | + // Parameter #2 $input of method FooTest::testTrim() expects string, int given. |
| 53 | + 'Hello World', |
| 54 | + 123, |
| 55 | + ], |
| 56 | + [ |
| 57 | + // Parameter #2 $input of method FooTest::testTrim() expects string, false given. |
| 58 | + 'Hello World', |
| 59 | + false, |
| 60 | + ], |
| 61 | + [ |
| 62 | + // Method FooTest::testTrim() invoked with 1 parameter, 2 required. |
| 63 | + 'Hello World', |
| 64 | + ], |
| 65 | + ]; |
| 66 | + } |
| 67 | +``` |
| 68 | + |
| 69 | +For this to happen we re-use existing rules for method call validation via the newly introduced [`NodeCallbackInvoker`](https://github.com/phpstan/phpstan-src/blob/2.1.x/src/Analyser/NodeCallbackInvoker.php). |
| 70 | +This new interface allows us to create virtual made-up AST nodes, which are handled like regular method calls. |
| 71 | + |
| 72 | +Related pull requests: |
| 73 | +- [Implement DataProviderDataRule](https://github.com/phpstan/phpstan-phpunit/pull/238) |
| 74 | +- [NodeCallbackInvoker](https://github.com/phpstan/phpstan-src/pull/4429) |
| 75 | +- [CompositeRule](https://github.com/phpstan/phpstan-src/pull/4438) |
| 76 | + |
| 77 | + |
| 78 | +### Ignore `missingType.iterableValue` for data-providers |
| 79 | + |
| 80 | +You likely have been haunted by this error in your test-suite: |
| 81 | + |
| 82 | +> Method DataProviderIterableValueTest\Foo::dataProvider() return type has no value type specified in iterable type iterable. |
| 83 | +
|
| 84 | +Even in the PHPStan-src codebase this error was ignored by NEON config in the past, as it was really not that useful to repeat all types in every data-provider, |
| 85 | +which were already present in the test-case method signatures. |
| 86 | + |
| 87 | +As you already learned in the above paragraph we learned how to validate data-providers with this release. |
| 88 | +We went one step further and re-used the existing validation logic to omit the `missingType.iterableValue` error only for those data-providers which we are able to validate. |
| 89 | +This is possible by [implementing a new `IgnoreErrorExtension`](https://github.com/phpstan/phpstan-phpunit/pull/246). |
| 90 | + |
| 91 | + |
| 92 | +### Improved `assertArrayHasKey` inference |
| 93 | + |
| 94 | +Based on a [fix in the PHPStan core](https://github.com/phpstan/phpstan-src/pull/4473), we are now able to properly narrow types after a call to `assertArrayHasKey`. |
| 95 | +This will help to prevent false positive errors you may have experienced in the past. |
| 96 | + |
| 97 | + |
| 98 | +### PHPUnit version detector |
| 99 | + |
| 100 | +With the addition of [`PHPUnitVersionDetector`](https://github.com/phpstan/phpstan-phpunit/pull/248) |
| 101 | +we will be able to easily implement rules or extensions tailored to certain PHPUnit versions. |
| 102 | + |
| 103 | +This will be useful in the future, so we can for example assist in PHPUnit migrations and pave the way for a smoother upgrade path. |
| 104 | + |
| 105 | + |
| 106 | +### Performance improvements |
| 107 | + |
| 108 | +People reading my blog or social media posts know [my obsession in making things faster](https://staabm.github.io/archive.html#performance). |
| 109 | +This release is no difference, as some changes have been done to make most PHPUnit specific rules [more efficient by reducing unnecessary work](https://github.com/phpstan/phpstan-phpunit/pull/247). |
| 110 | + |
| 111 | + |
| 112 | +### Easter eggs included |
| 113 | + |
| 114 | +There is even more magic under the hood. |
| 115 | + |
| 116 | +We have a experimental feature in PHPStan which allows us to not just report errors, but also `--fix` some of them. |
| 117 | +This new ability was also added to a few `assert*` rules. |
| 118 | + |
| 119 | + |
| 120 | +### Summary |
| 121 | + |
| 122 | +I spent a lot of time over a few weeks to make the PHPUnit integration shine. I feel we are on a totally new level and even more new cool stuff is getting possible. |
| 123 | + |
| 124 | +Make sure your boss considers [sponsoring my open source work](https://github.com/sponsors/staabm), so I can spent more time no your beloved code quality tooling. |
| 125 | + |
0 commit comments