Skip to content

Commit c4b144d

Browse files
committed
Build/Test Tools: Add tests for the PHPStan extensions.
The extensions in `tests/phpstan` change what PHPStan reads out of core, deriving types from hash notation and `@global` tags, typing filtered values, and checking hook documentation, so a defect in one surfaces only as a quiet change in what the analysis reports. They are now covered by a PHPUnit suite built on PHPStan's test harness, which runs them against fixtures rather than against core, with hook reference comments resolving against a small fixture tree instead of `src`. It is run with `composer phpstan:test` or `npm run typecheck:php:test`. Since the harness parses every file as an analyzed one, it cannot see whether the redefinition of the simple parser in `base.neon` still applies, which is what gives a file that is only scanned the same derived types. One test therefore runs a real analysis with `base.neon` to cover it, so that a PHPStan upgrade renaming the service it redefines fails a test rather than quietly undoing it. In CI, the suite runs in the analysis of the whole codebase, after its results are reported, so that a failing test does not also leave a pull request without its annotations. It is skipped on branches that do not have it. Developed in #15850. Follow-up to r62292, r62939, r63420, r63462. Props johnbillion, lucatume, westonruter. Fixes #66634. git-svn-id: https://develop.svn.wordpress.org/trunk@65808 602fd350-edb4-49c9-b593-d223f7449a82
1 parent d450653 commit c4b144d

35 files changed

Lines changed: 2594 additions & 2 deletions

‎.github/workflows/phpstan-static-analysis.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ on:
2121
- 'tests/phpstan/base.neon'
2222
- 'tests/phpstan/baselines/**'
2323
- 'tests/phpstan/baselines-strict/**'
24+
# These files configure the tests of the PHPStan extensions, and hold their fixtures.
25+
- 'tests/phpstan/phpunit.xml.dist'
26+
- 'tests/phpstan/Tests/**'
2427
# Confirm any changes to relevant workflow files.
2528
- '.github/workflows/phpstan-static-analysis.yml'
2629
- '.github/workflows/reusable-phpstan-static-analysis-v2.yml'

‎.github/workflows/reusable-phpstan-static-analysis-v2.yml‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ jobs:
3838
# - Runs PHPStan static analysis.
3939
# - Reports the results as annotations.
4040
# - Uploads the results as an artifact (pull requests only).
41+
# - Runs the tests of the WordPress-specific PHPStan extensions (default analysis only).
4142
# - Checks for errors that cannot be reported as warnings, such as out-of-date baselines.
4243
# - Saves the PHPStan result cache.
4344
# - Ensures version-controlled files are not modified or deleted.
@@ -277,6 +278,20 @@ jobs:
277278
name: ${{ matrix.artifact }}
278279
path: ${{ runner.temp }}/phpstan-results/
279280

281+
# The extensions in `tests/phpstan` are tested against fixtures rather than against
282+
# core, so a defect in one is reported here as a failing test rather than as a
283+
# change in what the analysis above reports.
284+
#
285+
# This runs after the results are reported and uploaded, so that a failing test
286+
# does not also keep the pull request from being annotated and commented on. It
287+
# runs even when the analysis failed, since the two are independent, but not when
288+
# the analysis was skipped: that means a step setting up the job, such as installing
289+
# the Composer dependencies, failed, and the tests could only fail along with it.
290+
# Both analyses load the same extensions, so the tests run in only one of them.
291+
- name: Run the tests of the PHPStan extensions
292+
if: ${{ !cancelled() && steps.phpstan.outcome != 'skipped' && matrix.analysis == 'default' && hashFiles('tests/phpstan/phpunit.xml.dist') != '' }}
293+
run: composer run phpstan:test
294+
280295
# Some errors cannot be ignored, so they cannot be reported as warnings either. Most often
281296
# this is an ignored error that no longer occurs, or occurs a different number of times,
282297
# which is reported under an `ignore.*` identifier. That is not something to fix in the

‎composer.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"phpstan:strict": [ "Composer\\Config::disableProcessTimeout", "@php ./vendor/bin/phpstan analyse --configuration=phpstan-strict.neon.dist --memory-limit=2G" ],
8181
"phpstan:baselines": [ "Composer\\Config::disableProcessTimeout", "@php ./tests/phpstan/generate-baselines.php" ],
8282
"phpstan:fix": [ "Composer\\Config::disableProcessTimeout", "@php ./tests/phpstan/analyse.php --memory-limit=2G --fix" ],
83+
"phpstan:test": "@php ./vendor/phpunit/phpunit/phpunit -c tests/phpstan/phpunit.xml.dist",
8384
"compat": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs --standard=phpcompat.xml.dist --report=summary,source",
8485
"format": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf --report=summary,source",
8586
"lint": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs --report=summary,source",

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
"typecheck:php": "node ./tools/local-env/scripts/docker.js run --rm php composer phpstan --",
156156
"typecheck:php:baselines": "node ./tools/local-env/scripts/docker.js run --rm php composer phpstan:baselines --",
157157
"typecheck:php:fix": "node ./tools/local-env/scripts/docker.js run --rm php composer phpstan:fix --",
158+
"typecheck:php:test": "node ./tools/local-env/scripts/docker.js run --rm php composer phpstan:test --",
158159
"gutenberg:copy": "node tools/gutenberg/copy.js",
159160
"gutenberg:verify": "node tools/gutenberg/utils.js",
160161
"gutenberg:download": "node tools/gutenberg/download.js && npm run build:gutenberg"

‎tests/phpstan/README.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,26 @@ These identifiers are specific to WordPress, and can be ignored or baselined lik
209209
| `wordpress.hookDocReferenceHookMissing` | The referenced file exists, but documents no hook of that name. Either the reference is stale, or the canonical docblock has moved. |
210210
| `wordpress.hookParamCountMismatch` | The call passes a different number of arguments than the docblock documents `@param` tags for. Passing fewer risks an `ArgumentCountError` in a callback registered for the documented count; passing more silently drops the extra argument and leaves the documentation misleading. |
211211

212+
### Testing the extensions
213+
214+
The extensions have tests of their own under [`Tests/`](Tests), built on PHPStan's test harness. The two rules are run over fixture files and the errors they report are compared with the ones expected. The visitors and the return type extension are checked through the types PHPStan infers for the `assertType()` calls in their fixtures. Reference comments resolve against a small WordPress tree of fixtures in [`Tests/data/wordpress`](Tests/data/wordpress) rather than against `src`, so a broken reference can be tested without one existing in core.
215+
216+
The harness parses every file the way an analyzed file is parsed, so one test, `ReflectionParserTest`, runs a real analysis with `base.neon` instead. It checks that a function declared in a file that is only scanned is reflected with the shape its hash notation describes, which depends on the redefinition of `currentPhpVersionSimpleParser` there.
217+
218+
Run them with:
219+
220+
```bash
221+
npm run typecheck:php:test
222+
```
223+
224+
or, outside the Docker environment:
225+
226+
```bash
227+
composer phpstan:test
228+
```
229+
230+
They also run in CI, after the analysis of the whole codebase, so that a failing test does not keep its results from being reported.
231+
212232
## Ignoring and baselining errors
213233

214234
As we adopt PHPStan iteratively, you may be faced with false positives due to legacy code, or code that is not worth changing at this time.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Tests for ApplyFiltersDynamicFunctionReturnTypeExtension.
4+
*
5+
* @package WordPress
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace WordPress\PHPStan\Tests;
11+
12+
use PHPStan\Testing\TypeInferenceTestCase;
13+
14+
/**
15+
* Checks the type PHPStan infers for a filtered value against the `assertType()`
16+
* calls in the fixtures.
17+
*/
18+
final class ApplyFiltersDynamicFunctionReturnTypeExtensionTest extends TypeInferenceTestCase {
19+
20+
/**
21+
* Gathers the assertions from the fixtures.
22+
*
23+
* @return iterable<string, mixed[]>
24+
*/
25+
public function data_file_asserts(): iterable {
26+
yield from self::gatherAssertTypes( __DIR__ . '/data/apply-filters.php' );
27+
yield from self::gatherAssertTypes( __DIR__ . '/data/ApplyFiltersTestClass.php' );
28+
}
29+
30+
/**
31+
* @dataProvider data_file_asserts
32+
*
33+
* @param string $assert_type Kind of assertion.
34+
* @param string $file Fixture file.
35+
* @param mixed ...$args Assertion arguments.
36+
*/
37+
public function test_file_asserts( string $assert_type, string $file, ...$args ): void {
38+
$this->assertFileAsserts( $assert_type, $file, ...$args );
39+
}
40+
41+
/**
42+
* Returns the configuration registering the extension and the visitors it relies on.
43+
*
44+
* @return list<string>
45+
*/
46+
public static function getAdditionalConfigFiles(): array {
47+
return array( __DIR__ . '/tests.neon' );
48+
}
49+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Tests for GlobalDocBlockVisitor.
4+
*
5+
* @package WordPress
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace WordPress\PHPStan\Tests;
11+
12+
use PHPStan\Testing\TypeInferenceTestCase;
13+
14+
/**
15+
* Checks the type PHPStan infers for an imported global against the `assertType()`
16+
* calls in the fixtures.
17+
*/
18+
final class GlobalDocBlockVisitorTest extends TypeInferenceTestCase {
19+
20+
/**
21+
* Gathers the assertions from the fixtures.
22+
*
23+
* @return iterable<string, mixed[]>
24+
*/
25+
public function data_file_asserts(): iterable {
26+
yield from self::gatherAssertTypes( __DIR__ . '/data/global-docblock.php' );
27+
yield from self::gatherAssertTypes( __DIR__ . '/data/GlobalDocBlockTestClass.php' );
28+
}
29+
30+
/**
31+
* @dataProvider data_file_asserts
32+
*
33+
* @param string $assert_type Kind of assertion.
34+
* @param string $file Fixture file.
35+
* @param mixed ...$args Assertion arguments.
36+
*/
37+
public function test_file_asserts( string $assert_type, string $file, ...$args ): void {
38+
$this->assertFileAsserts( $assert_type, $file, ...$args );
39+
}
40+
41+
/**
42+
* Returns the configuration registering the visitor.
43+
*
44+
* @return list<string>
45+
*/
46+
public static function getAdditionalConfigFiles(): array {
47+
return array( __DIR__ . '/tests.neon' );
48+
}
49+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Tests the types PHPStan infers once HashNotationVisitor has translated a hash.
4+
*
5+
* @package WordPress
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace WordPress\PHPStan\Tests;
11+
12+
use PHPStan\Testing\TypeInferenceTestCase;
13+
14+
/**
15+
* Checks the `assertType()` calls in the hash notation fixture.
16+
*/
17+
final class HashNotationTypeTest extends TypeInferenceTestCase {
18+
19+
/**
20+
* Gathers the assertions from the fixture.
21+
*
22+
* @return iterable<string, mixed[]>
23+
*/
24+
public function data_file_asserts(): iterable {
25+
yield from self::gatherAssertTypes( __DIR__ . '/data/hash-notation.php' );
26+
}
27+
28+
/**
29+
* @dataProvider data_file_asserts
30+
*
31+
* @param string $assert_type Kind of assertion.
32+
* @param string $file Fixture file.
33+
* @param mixed ...$args Assertion arguments.
34+
*/
35+
public function test_file_asserts( string $assert_type, string $file, ...$args ): void {
36+
$this->assertFileAsserts( $assert_type, $file, ...$args );
37+
}
38+
39+
/**
40+
* Returns the configuration registering the visitor.
41+
*
42+
* @return list<string>
43+
*/
44+
public static function getAdditionalConfigFiles(): array {
45+
return array( __DIR__ . '/tests.neon' );
46+
}
47+
}

0 commit comments

Comments
 (0)