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 70201d7
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: .readme-partials/USING.md
+194-1Lines changed: 194 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,13 +11,17 @@ To make use of the WP-CLI testing framework, you need to complete the following
11
11
"behat": "run-behat-tests",
12
12
"behat-rerun": "rerun-behat-tests",
13
13
"lint": "run-linter-tests",
14
+
"lint-gherkin": "run-gherkin-lint-tests",
14
15
"phpcs": "run-phpcs-tests",
15
16
"phpcbf": "run-phpcbf-cleanup",
17
+
"phpstan": "run-phpstan-tests",
16
18
"phpunit": "run-php-unit-tests",
17
19
"prepare-tests": "install-package-tests",
18
20
"test": [
19
21
"@lint",
22
+
"@lint-gherkin",
20
23
"@phpcs",
24
+
"@phpstan",
21
25
"@phpunit",
22
26
"@behat"
23
27
]
@@ -76,7 +80,11 @@ To make use of the WP-CLI testing framework, you need to complete the following
76
80
```
77
81
78
82
All other [PHPCS configuration options](https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Annotated-Ruleset) are, of course, available.
79
-
6. Update your composer dependencies and regenerate your autoloader and binary folders:
83
+
The PHP snippets embedded in your feature files are checked along with the rest of the package. See [Checking the code style of the PHP blocks in feature files](#checking-the-code-style-of-the-php-blocks-in-feature-files) below.
84
+
85
+
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.
86
+
87
+
7. Update your composer dependencies and regenerate your autoloader and binary folders:
80
88
```bash
81
89
composer update
82
90
```
@@ -90,11 +98,128 @@ You can use the following commands to control the tests:
90
98
* `composer prepare-tests` - Set up the database that is needed for running the functional tests. This is only needed once.
91
99
* `composer test` - Run all test suites.
92
100
* `composer lint` - Run only the linting test suite.
101
+
* `composer lint-gherkin` - Run only the Gherkin linter over the feature files.
93
102
* `composer phpcs` - Run only the code sniffer test suite.
94
103
* `composer phpcbf` - Run only the code sniffer cleanup.
104
+
* `composer phpstan` - Run only the static analysis.
95
105
* `composer phpunit` - Run only the unit test suite.
96
106
* `composer behat` - Run only the functional test suite.
97
107
108
+
### Analysing the PHP blocks in feature files
109
+
110
+
Feature files embed PHP snippets in docstrings, which none of the static analysis tools normally
111
+
look at:
112
+
113
+
```gherkin
114
+
Given a wp-content/mu-plugins/test-harness.php file:
The blocks are left alone when a run is narrowed down to a path, as in`composer phpcs -- src/`,
221
+
since such an argument is about the files of the package itself.
222
+
98
223
### Controlling what to test
99
224
100
225
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:
Prepending with the double dash is needed because the arguments would otherwise be sent to Composer itself, not the tool that Composer executes.
106
231
232
+
The same mechanism works for narrowing a run down further, or for bailing out early:
233
+
```bash
234
+
# A single scenario, identified by the line it starts on.
235
+
composer behat -- features/cli-info.feature:12
236
+
237
+
# Every scenario carrying a given tag.
238
+
composer behat -- --tags=@require-wp-5.0
239
+
240
+
# Stop at the first failing scenario instead of running the whole suite.
241
+
composer behat -- --stop-on-failure
242
+
243
+
# Re-run only the scenarios that failed the last time.
244
+
composer behat-rerun
245
+
```
246
+
247
+
### Linting the feature files
248
+
249
+
`composer lint-gherkin` checks `features/` with
250
+
[gherkin-lint-plus](https://www.npmjs.com/package/gherkin-lint-plus), against the
251
+
`.gherkin-lintrc` ruleset shipped with this package. A project that needs
252
+
different rules can override it by committing its own `.gherkin-lintrc`.
253
+
254
+
The linter is a Node package, so it is run through `npx` and needs Node.js 20 or
255
+
later. Where `npx` is not available the check reports that it is skipping, rather
256
+
than failing a suite that is otherwise entirely PHP. Its version is pinned in
257
+
this package's `package.json`, which exists only to hold that pin.
258
+
259
+
### Controlling the amount of output
260
+
261
+
Two environment variables make the test tools less chatty. Both are unset by default, which leaves the output exactly as it has always been.
262
+
263
+
*`NO_COLOR` (the [no-color.org](https://no-color.org/) convention) turns off the ANSI color codes in the output of every runner. Set this when capturing output to a file or a pipe, where the escape sequences are noise.
264
+
*`WP_CLI_TEST_QUIET` switches the reporters to their most compact form: PHP_CodeSniffer reports one `file:line:col` line per violation with no progress ticker, PHPStan reports one `file:line:message` line per error with no progress bar and no result table. This covers the analysis of the PHP files themselves; the checks over the PHP blocks embedded in feature files keep their own reports, which are rewritten to point back at the feature file a block came from. Behat's own output is already minimal, so it is unaffected.
265
+
266
+
`NO_COLOR` also covers the Gherkin linter, which colors its report unconditionally and has no plain output format of its own.
267
+
268
+
```bash
269
+
NO_COLOR=1 WP_CLI_TEST_QUIET=1 composer phpstan
270
+
```
271
+
272
+
This is worth setting permanently in environments that read the output back rather than display it, such as an AI coding agent's shell:
273
+
274
+
```bash
275
+
export NO_COLOR=1
276
+
export WP_CLI_TEST_QUIET=1
277
+
```
278
+
107
279
### Controlling the test environment
108
280
109
281
#### WordPress Version
@@ -119,6 +291,13 @@ Here's how to run your tests against the latest trunk version of WordPress:
119
291
WP_VERSION=trunk composer behat
120
292
```
121
293
294
+
Resolving `latest`, or a `X.Y` version without a patch number, needs the
295
+
WordPress versions data, which is fetched once and cached in the system temp
296
+
directory for a day. Repeated runs do not repeat the request, and a run without
297
+
connectivity falls back to the last known copy.
298
+
`WP_CLI_TEST_WP_VERSION_CACHE_TTL` sets the lifetime of that cache in seconds;
299
+
`0` fetches it every time.
300
+
122
301
#### WordPress Archive
123
302
124
303
Instead of downloading WordPress from WordPress.org, you can run the tests against an arbitrary
@@ -148,6 +327,20 @@ WP_VERSION=6.4.2 WP_CLI_TEST_CORE_ZIP=~/Downloads/wordpress-6.4.2.zip composer b
148
327
Note that steps requesting an explicit version, such as `Given a WP 6.4.2 installation`, keep
149
328
downloading that version from WordPress.org and ignore the archive.
150
329
330
+
#### Environment-specific scenarios
331
+
332
+
Some scenarios can only run in certain environments. Tagging them makes the test framework
333
+
filter them out everywhere else, rather than having them fail for reasons unrelated to what
334
+
they test.
335
+
336
+
* `@require-wp-stable` — the scenario needs a version of WordPress that WordPress.org knows
337
+
about, such as one verifying an installation against the published checksums. It is
338
+
skipped when `WP_CLI_TEST_CORE_ZIP` is set, and when `WP_VERSION` is `trunk` or `nightly`.
339
+
* `@require-mysql-socket` — the scenario connects to the database through a socket. It is
340
+
skipped when there is none, which is the case when the database server runs in a
341
+
container and is only reachable over TCP. Set `WP_CLI_TEST_DBSOCKET` to point at the
342
+
socket if it lives somewhere unusual.
343
+
151
344
#### WP-CLI Binary
152
345
153
346
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