You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 93b00e7
Browse filesBrowse the repository at this point in the historyBrowse files
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.
Copy file name to clipboardExpand all lines: .readme-partials/USING.md
+85-1Lines changed: 85 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,12 +14,14 @@ To make use of the WP-CLI testing framework, you need to complete the following
14
14
"lint-gherkin": "run-gherkin-lint-tests",
15
15
"phpcs": "run-phpcs-tests",
16
16
"phpcbf": "run-phpcbf-cleanup",
17
+
"phpstan": "run-phpstan-tests",
17
18
"phpunit": "run-php-unit-tests",
18
19
"prepare-tests": "install-package-tests",
19
20
"test": [
20
21
"@lint",
21
22
"@lint-gherkin",
22
23
"@phpcs",
24
+
"@phpstan",
23
25
"@phpunit",
24
26
"@behat"
25
27
]
@@ -78,7 +80,9 @@ To make use of the WP-CLI testing framework, you need to complete the following
78
80
```
79
81
80
82
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:
82
86
```bash
83
87
composer update
84
88
```
@@ -95,9 +99,75 @@ You can use the following commands to control the tests:
95
99
* `composer lint-gherkin` - Run only the Gherkin linter over the feature files.
96
100
* `composer phpcs` - Run only the code sniffer test suite.
97
101
* `composer phpcbf` - Run only the code sniffer cleanup.
102
+
* `composer phpstan` - Run only the static analysis.
98
103
* `composer phpunit` - Run only the unit test suite.
99
104
* `composer behat` - Run only the functional test suite.
100
105
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:
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
+
101
171
### Controlling what to test
102
172
103
173
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
205
275
Note that steps requesting an explicit version, such as `Given a WP 6.4.2 installation`, keep
206
276
downloading that version from WordPress.org and ignore the archive.
207
277
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
+
208
292
#### WP-CLI Binary
209
293
210
294
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.
Copy file name to clipboardExpand all lines: AGENTS.md
+21-2Lines changed: 21 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,9 +13,27 @@ When contributing to this package, please adhere to the following guidelines:
13
13
14
14
### Building and running
15
15
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:
17
17
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.
19
37
20
38
### Useful Composer Commands
21
39
@@ -24,6 +42,7 @@ The project uses Composer to manage dependencies and run scripts. The following
24
42
*`composer install`: Install dependencies.
25
43
*`composer test`: Run the full test suite, including linting, code style checks, static analysis, and unit/behavior tests.
26
44
*`composer lint`: Check for syntax errors.
45
+
*`composer lint-gherkin`: Check the Behat feature files for style violations.
27
46
*`composer phpcs`: Check for code style violations.
@@ -25,12 +25,14 @@ To make use of the WP-CLI testing framework, you need to complete the following
25
25
"lint-gherkin": "run-gherkin-lint-tests",
26
26
"phpcs": "run-phpcs-tests",
27
27
"phpcbf": "run-phpcbf-cleanup",
28
+
"phpstan": "run-phpstan-tests",
28
29
"phpunit": "run-php-unit-tests",
29
30
"prepare-tests": "install-package-tests",
30
31
"test": [
31
32
"@lint",
32
33
"@lint-gherkin",
33
34
"@phpcs",
35
+
"@phpstan",
34
36
"@phpunit",
35
37
"@behat"
36
38
]
@@ -89,7 +91,9 @@ To make use of the WP-CLI testing framework, you need to complete the following
89
91
```
90
92
91
93
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:
93
97
```bash
94
98
composer update
95
99
```
@@ -106,9 +110,75 @@ You can use the following commands to control the tests:
106
110
* `composer lint-gherkin` - Run only the Gherkin linter over the feature files.
107
111
* `composer phpcs` - Run only the code sniffer test suite.
108
112
* `composer phpcbf` - Run only the code sniffer cleanup.
113
+
* `composer phpstan` - Run only the static analysis.
109
114
* `composer phpunit` - Run only the unit test suite.
110
115
* `composer behat` - Run only the functional test suite.
111
116
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:
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
+
112
182
### Controlling what to test
113
183
114
184
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
216
286
Note that steps requesting an explicit version, such as `Given a WP 6.4.2 installation`, keep
217
287
downloading that version from WordPress.org and ignore the archive.
218
288
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
+
219
303
#### WP-CLI Binary
220
304
221
305
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