Skip to content

Commit 93b00e7

Browse files
committed
Merge branch 'main' into claude/wp-cli-ai-contributor-experience-2d3ces
Conflict in bin/run-phpstan-tests: main split the runner into two numbered phases and collects an exit code across them, while this branch added the NO_COLOR and WP_CLI_TEST_QUIET flags to the analyse call. Kept both, so the first phase still takes the optional flags and now also feeds EXIT_CODE. The second phase, which runs PHPStan over the PHP blocks in feature files, needs nothing here: it already passes --no-progress and reports through its own formatter, which emits no ANSI sequences.
2 parents 68fe99f + d8c33f5 commit 93b00e7

14 files changed

Lines changed: 2162 additions & 9 deletions

‎.github/workflows/copilot-setup-steps.yml‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ jobs:
1919
permissions:
2020
contents: read
2121

22+
env:
23+
MYSQL_HOST: 127.0.0.1
24+
MYSQL_TCP_PORT: 3306
25+
WP_CLI_TEST_DBROOTUSER: root
26+
WP_CLI_TEST_DBROOTPASS: root
27+
WP_CLI_TEST_DBNAME: wp_cli_test
28+
WP_CLI_TEST_DBUSER: wp_cli_test
29+
WP_CLI_TEST_DBPASS: password1
30+
WP_CLI_TEST_DBHOST: 127.0.0.1:3306
31+
2232
steps:
2333
- name: Checkout code
2434
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
@@ -45,3 +55,28 @@ jobs:
4555
uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # 4.0.0
4656
env:
4757
COMPOSER_ROOT_VERSION: dev-${{ github.event.repository.default_branch }}
58+
59+
# Without a database the Behat runner silently falls back to SQLite, so the
60+
# agent's test runs are not the ones CI will do. Mirrors the setup in
61+
# reusable-functional.yml.
62+
- name: Check existence of behat.yml file
63+
id: check_behat_file
64+
run: echo "files_exists=$(test -f behat.yml && echo true || echo false)" >> "$GITHUB_OUTPUT"
65+
66+
- name: Setup MySQL Server
67+
if: steps.check_behat_file.outputs.files_exists == 'true'
68+
uses: shogo82148/actions-setup-mysql@62da9377d83991fce27b6ed2a397d3306121e41d # v1
69+
with:
70+
mysql-version: '8.0' # Standard MySQL version for these tests
71+
auto-start: true
72+
root-password: ${{ env.WP_CLI_TEST_DBROOTPASS }}
73+
user: ${{ env.WP_CLI_TEST_DBUSER }}
74+
password: ${{ env.WP_CLI_TEST_DBPASS }}
75+
76+
- name: Remove system MySQL binary
77+
if: steps.check_behat_file.outputs.files_exists == 'true'
78+
run: sudo rm -f /usr/bin/mysql /usr/bin/mysqldump
79+
80+
- name: Prepare test database
81+
if: steps.check_behat_file.outputs.files_exists == 'true'
82+
run: composer prepare-tests

‎.readme-partials/USING.md‎

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ To make use of the WP-CLI testing framework, you need to complete the following
1414
"lint-gherkin": "run-gherkin-lint-tests",
1515
"phpcs": "run-phpcs-tests",
1616
"phpcbf": "run-phpcbf-cleanup",
17+
"phpstan": "run-phpstan-tests",
1718
"phpunit": "run-php-unit-tests",
1819
"prepare-tests": "install-package-tests",
1920
"test": [
2021
"@lint",
2122
"@lint-gherkin",
2223
"@phpcs",
24+
"@phpstan",
2325
"@phpunit",
2426
"@behat"
2527
]
@@ -78,7 +80,9 @@ To make use of the WP-CLI testing framework, you need to complete the following
7880
```
7981
8082
All other [PHPCS configuration options](https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Annotated-Ruleset) are, of course, available.
81-
6. Update your composer dependencies and regenerate your autoloader and binary folders:
83+
6. Optionally add a `phpstan-feature-files.neon.dist` file to the package root to also run PHPStan over the PHP snippets embedded in your feature files. See [Analysing the PHP blocks in feature files](#analysing-the-php-blocks-in-feature-files) below.
84+
85+
7. Update your composer dependencies and regenerate your autoloader and binary folders:
8286
```bash
8387
composer update
8488
```
@@ -95,9 +99,75 @@ You can use the following commands to control the tests:
9599
* `composer lint-gherkin` - Run only the Gherkin linter over the feature files.
96100
* `composer phpcs` - Run only the code sniffer test suite.
97101
* `composer phpcbf` - Run only the code sniffer cleanup.
102+
* `composer phpstan` - Run only the static analysis.
98103
* `composer phpunit` - Run only the unit test suite.
99104
* `composer behat` - Run only the functional test suite.
100105
106+
### Analysing the PHP blocks in feature files
107+
108+
Feature files embed PHP snippets in docstrings, which none of the static analysis tools normally
109+
look at:
110+
111+
```gherkin
112+
Given a wp-content/mu-plugins/test-harness.php file:
113+
"""
114+
<?php
115+
WP_CLI::add_command( 'test-harness', 'Test_Harness' );
116+
"""
117+
```
118+
119+
Adding a `phpstan-feature-files.neon.dist` file to the package root makes `composer phpstan` analyse
120+
those snippets as well. The blocks are extracted into standalone PHP files that are padded so their
121+
line numbers match the feature file, which is what allows errors to be reported against the feature
122+
file itself:
123+
124+
```text
125+
features/command.feature
126+
438 Parameter #1 $message of static method WP_CLI::log() expects string, int<0, max> given.
127+
🪪 argument.type
128+
```
129+
130+
The defaults in `phpstan/feature-files.neon` are applied first, so the file only needs to hold what
131+
it wants to change. An empty file is enough to run with the defaults, and a level of its own looks
132+
like this:
133+
134+
```neon
135+
parameters:
136+
level: 4
137+
```
138+
139+
Do note that snippets in feature files are fixtures, not production code, and that they run inside a
140+
WordPress installation the analysis knows nothing about. Expect to have to ignore errors that are
141+
not actually wrong, such as functions a scenario deliberately leaves undefined.
142+
143+
An ignore matches against the extracted file rather than the feature file it came from. Those files
144+
are named `<feature file>_L<first line>_E<last line>.php`, with the feature file relative to the
145+
`features` directory, so ignoring an error for a whole feature file takes a pattern:
146+
147+
```neon
148+
parameters:
149+
ignoreErrors:
150+
-
151+
identifier: function.notFound
152+
path: */shutdown-handler.feature_L*.php
153+
```
154+
155+
Since the blocks are analysed in more than one run (see below), an ignore that no run matches is not
156+
reported. A pattern that matches nothing at all therefore goes unnoticed, so it is worth checking
157+
that the error it targets is really gone.
158+
159+
Two kinds of blocks are left out of the analysis, and are listed at the end of the run:
160+
161+
* Blocks that are not standalone PHP, such as snippets holding a placeholder that Behat substitutes
162+
(`get_the_title( {POST_ID} )`) or code that is deliberately broken to test error handling. PHPStan
163+
stops analysing altogether when a single file fails to parse, so these have to be skipped.
164+
* Docstrings that neither belong to a step creating a `.php` file nor open with `<?php`, since those
165+
are not necessarily PHP at all. The second rule covers PHP files that are not named `*.php`, such
166+
as the `.maintenance` file of a WordPress installation.
167+
168+
Blocks that declare the same class or function as another block are analysed separately from each
169+
other, so that PHPStan does not resolve a name to the wrong block's declaration.
170+
101171
### Controlling what to test
102172

103173
To send one or more arguments to one of the test tools, prepend the argument(s) with a double dash. As an example, here's how to run the functional tests for a specific feature file only:
@@ -205,6 +275,20 @@ WP_VERSION=6.4.2 WP_CLI_TEST_CORE_ZIP=~/Downloads/wordpress-6.4.2.zip composer b
205275
Note that steps requesting an explicit version, such as `Given a WP 6.4.2 installation`, keep
206276
downloading that version from WordPress.org and ignore the archive.
207277

278+
#### Environment-specific scenarios
279+
280+
Some scenarios can only run in certain environments. Tagging them makes the test framework
281+
filter them out everywhere else, rather than having them fail for reasons unrelated to what
282+
they test.
283+
284+
* `@require-wp-stable` — the scenario needs a version of WordPress that WordPress.org knows
285+
about, such as one verifying an installation against the published checksums. It is
286+
skipped when `WP_CLI_TEST_CORE_ZIP` is set, and when `WP_VERSION` is `trunk` or `nightly`.
287+
* `@require-mysql-socket` — the scenario connects to the database through a socket. It is
288+
skipped when there is none, which is the case when the database server runs in a
289+
container and is only reachable over TCP. Set `WP_CLI_TEST_DBSOCKET` to point at the
290+
socket if it lives somewhere unusual.
291+
208292
#### WP-CLI Binary
209293

210294
You can run the tests against a specific WP-CLI binary, instead of using the one that has been built in your project's `vendor/bin` folder.

‎AGENTS.md‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,27 @@ When contributing to this package, please adhere to the following guidelines:
1313

1414
### Building and running
1515

16-
Before submitting any changes, it is crucial to validate them by running the full suite of static code analysis and tests. To run the full suite of checks, execute the following command: `composer test`.
16+
Run the narrowest check that covers your change:
1717

18-
This single command ensures that your changes meet all the quality gates of the project. While you can run the individual steps separately, it is highly recommended to use this single command to ensure a comprehensive validation.
18+
| You changed | Run |
19+
| --- | --- |
20+
| Any PHP file | `composer phpcs -- <file>` and `composer phpstan` |
21+
| Logic with unit test coverage | `composer phpunit -- --filter <TestName>` |
22+
| One feature file | `composer behat -- features/<name>.feature` |
23+
| One scenario | `composer behat -- features/<name>.feature:<line>` |
24+
25+
After a failure, `composer behat-rerun` re-runs only the scenarios that failed, and `composer behat -- --stop-on-failure` bails out at the first one instead of working through the rest.
26+
27+
`composer test` runs every suite, the whole Behat suite included. In most packages that means installing WordPress from scratch once per scenario, which takes tens of minutes and needs `jq`, a MySQL or MariaDB client with a prepared test database, and network access to WordPress.org. Run it before opening a pull request when you have touched something cross-cutting. Do not run it to check a two-line change.
28+
29+
Set these first, so the tools report problems compactly instead of drawing progress bars and ANSI color into output you are going to read back:
30+
31+
```bash
32+
export NO_COLOR=1
33+
export WP_CLI_TEST_QUIET=1
34+
```
35+
36+
Note that a green `composer test` is not the same as a green CI. `actionlint` and `typos` also run on every pull request and are not part of it.
1937

2038
### Useful Composer Commands
2139

@@ -24,6 +42,7 @@ The project uses Composer to manage dependencies and run scripts. The following
2442
* `composer install`: Install dependencies.
2543
* `composer test`: Run the full test suite, including linting, code style checks, static analysis, and unit/behavior tests.
2644
* `composer lint`: Check for syntax errors.
45+
* `composer lint-gherkin`: Check the Behat feature files for style violations.
2746
* `composer phpcs`: Check for code style violations.
2847
* `composer phpcbf`: Automatically fix code style violations.
2948
* `composer phpstan`: Run static analysis.

‎README.md‎

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ wp-cli/wp-cli-tests
33

44
WP-CLI testing framework
55

6-
[![Testing](https://github.com/wp-cli/wp-cli-tests/actions/workflows/testing.yml/badge.svg)](https://github.com/wp-cli/wp-cli-tests/actions/workflows/testing.yml)
6+
[![Testing](https://github.com/wp-cli/wp-cli-tests/actions/workflows/testing.yml/badge.svg)](https://github.com/wp-cli/wp-cli-tests/actions/workflows/testing.yml) [![Code Coverage](https://codecov.io/gh/wp-cli/wp-cli-tests/branch/main/graph/badge.svg)](https://codecov.io/gh/wp-cli/wp-cli-tests/tree/main)
77

88
Quick links: [Using](#using) | [Contributing](#contributing) | [Support](#support)
99

@@ -25,12 +25,14 @@ To make use of the WP-CLI testing framework, you need to complete the following
2525
"lint-gherkin": "run-gherkin-lint-tests",
2626
"phpcs": "run-phpcs-tests",
2727
"phpcbf": "run-phpcbf-cleanup",
28+
"phpstan": "run-phpstan-tests",
2829
"phpunit": "run-php-unit-tests",
2930
"prepare-tests": "install-package-tests",
3031
"test": [
3132
"@lint",
3233
"@lint-gherkin",
3334
"@phpcs",
35+
"@phpstan",
3436
"@phpunit",
3537
"@behat"
3638
]
@@ -89,7 +91,9 @@ To make use of the WP-CLI testing framework, you need to complete the following
8991
```
9092
9193
All other [PHPCS configuration options](https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Annotated-Ruleset) are, of course, available.
92-
6. Update your composer dependencies and regenerate your autoloader and binary folders:
94+
6. Optionally add a `phpstan-feature-files.neon.dist` file to the package root to also run PHPStan over the PHP snippets embedded in your feature files. See [Analysing the PHP blocks in feature files](#analysing-the-php-blocks-in-feature-files) below.
95+
96+
7. Update your composer dependencies and regenerate your autoloader and binary folders:
9397
```bash
9498
composer update
9599
```
@@ -106,9 +110,75 @@ You can use the following commands to control the tests:
106110
* `composer lint-gherkin` - Run only the Gherkin linter over the feature files.
107111
* `composer phpcs` - Run only the code sniffer test suite.
108112
* `composer phpcbf` - Run only the code sniffer cleanup.
113+
* `composer phpstan` - Run only the static analysis.
109114
* `composer phpunit` - Run only the unit test suite.
110115
* `composer behat` - Run only the functional test suite.
111116
117+
### Analysing the PHP blocks in feature files
118+
119+
Feature files embed PHP snippets in docstrings, which none of the static analysis tools normally
120+
look at:
121+
122+
```gherkin
123+
Given a wp-content/mu-plugins/test-harness.php file:
124+
"""
125+
<?php
126+
WP_CLI::add_command( 'test-harness', 'Test_Harness' );
127+
"""
128+
```
129+
130+
Adding a `phpstan-feature-files.neon.dist` file to the package root makes `composer phpstan` analyse
131+
those snippets as well. The blocks are extracted into standalone PHP files that are padded so their
132+
line numbers match the feature file, which is what allows errors to be reported against the feature
133+
file itself:
134+
135+
```text
136+
features/command.feature
137+
438 Parameter #1 $message of static method WP_CLI::log() expects string, int<0, max> given.
138+
🪪 argument.type
139+
```
140+
141+
The defaults in `phpstan/feature-files.neon` are applied first, so the file only needs to hold what
142+
it wants to change. An empty file is enough to run with the defaults, and a level of its own looks
143+
like this:
144+
145+
```neon
146+
parameters:
147+
level: 4
148+
```
149+
150+
Do note that snippets in feature files are fixtures, not production code, and that they run inside a
151+
WordPress installation the analysis knows nothing about. Expect to have to ignore errors that are
152+
not actually wrong, such as functions a scenario deliberately leaves undefined.
153+
154+
An ignore matches against the extracted file rather than the feature file it came from. Those files
155+
are named `<feature file>_L<first line>_E<last line>.php`, with the feature file relative to the
156+
`features` directory, so ignoring an error for a whole feature file takes a pattern:
157+
158+
```neon
159+
parameters:
160+
ignoreErrors:
161+
-
162+
identifier: function.notFound
163+
path: */shutdown-handler.feature_L*.php
164+
```
165+
166+
Since the blocks are analysed in more than one run (see below), an ignore that no run matches is not
167+
reported. A pattern that matches nothing at all therefore goes unnoticed, so it is worth checking
168+
that the error it targets is really gone.
169+
170+
Two kinds of blocks are left out of the analysis, and are listed at the end of the run:
171+
172+
* Blocks that are not standalone PHP, such as snippets holding a placeholder that Behat substitutes
173+
(`get_the_title( {POST_ID} )`) or code that is deliberately broken to test error handling. PHPStan
174+
stops analysing altogether when a single file fails to parse, so these have to be skipped.
175+
* Docstrings that neither belong to a step creating a `.php` file nor open with `<?php`, since those
176+
are not necessarily PHP at all. The second rule covers PHP files that are not named `*.php`, such
177+
as the `.maintenance` file of a WordPress installation.
178+
179+
Blocks that declare the same class or function as another block are analysed separately from each
180+
other, so that PHPStan does not resolve a name to the wrong block's declaration.
181+
112182
### Controlling what to test
113183

114184
To send one or more arguments to one of the test tools, prepend the argument(s) with a double dash. As an example, here's how to run the functional tests for a specific feature file only:
@@ -216,6 +286,20 @@ WP_VERSION=6.4.2 WP_CLI_TEST_CORE_ZIP=~/Downloads/wordpress-6.4.2.zip composer b
216286
Note that steps requesting an explicit version, such as `Given a WP 6.4.2 installation`, keep
217287
downloading that version from WordPress.org and ignore the archive.
218288

289+
#### Environment-specific scenarios
290+
291+
Some scenarios can only run in certain environments. Tagging them makes the test framework
292+
filter them out everywhere else, rather than having them fail for reasons unrelated to what
293+
they test.
294+
295+
* `@require-wp-stable` — the scenario needs a version of WordPress that WordPress.org knows
296+
about, such as one verifying an installation against the published checksums. It is
297+
skipped when `WP_CLI_TEST_CORE_ZIP` is set, and when `WP_VERSION` is `trunk` or `nightly`.
298+
* `@require-mysql-socket` — the scenario connects to the database through a socket. It is
299+
skipped when there is none, which is the case when the database server runs in a
300+
container and is only reachable over TCP. Set `WP_CLI_TEST_DBSOCKET` to point at the
301+
socket if it lives somewhere unusual.
302+
219303
#### WP-CLI Binary
220304

221305
You can run the tests against a specific WP-CLI binary, instead of using the one that has been built in your project's `vendor/bin` folder.

0 commit comments

Comments
 (0)