Skip to content

Commit 0328764

Browse files
committed
Merge branch 'main' into try/phpcs-in-feature-files
2 parents 9c79335 + d8c33f5 commit 0328764

24 files changed

Lines changed: 2930 additions & 154 deletions

‎.github/actionlint.yml‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Configuration for actionlint, run by the `actionlint` job in
2+
# `.github/workflows/reusable-code-quality.yml`.
3+
paths:
4+
.github/workflows/**/*.{yml,yaml}:
5+
ignore:
6+
# `copilot-requests` is a real permission scope - it is what lets a
7+
# workflow authenticate the Copilot CLI with the built-in `GITHUB_TOKEN`
8+
# instead of a personal access token - but actionlint's hard-coded scope
9+
# list has not caught up with it yet, so it reports every use as unknown.
10+
# Drop this entry once actionlint ships the scope, and it will go back to
11+
# catching genuine typos in permission names.
12+
# See https://docs.github.com/en/copilot/how-tos/copilot-cli/use-copilot-cli-in-actions
13+
- 'unknown permission scope "copilot-requests"'

‎.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

‎.github/workflows/issue-triage.yml‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,17 @@ name: Issue and PR Triage
1616
permissions:
1717
issues: write
1818
pull-requests: write
19-
actions: write
2019
contents: read
21-
models: read
20+
# A caller can only cap a reusable workflow's permissions, so the scope the
21+
# Copilot CLI needs has to be granted here too. `models: read` was for the
22+
# GitHub Models provider, which `actions/ai-inference` v3 removed.
23+
copilot-requests: write
24+
# A caller can only cap a reusable workflow's permissions, never raise them,
25+
# so `actions: write` has to be granted here for the dispatch job downstream
26+
# to work at all. The reusable workflow narrows it to that single job, so the
27+
# `pull_request_target` job does not receive it. Removing it here breaks
28+
# dispatching (see #271/#272); narrow it there instead.
29+
actions: write
2230

2331
jobs:
2432
issue-triage:

‎.readme-partials/USING.md‎

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

100170
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:
@@ -119,6 +189,49 @@ Here's how to run your tests against the latest trunk version of WordPress:
119189
WP_VERSION=trunk composer behat
120190
```
121191

192+
#### WordPress Archive
193+
194+
Instead of downloading WordPress from WordPress.org, you can run the tests against an arbitrary
195+
WordPress ZIP archive by setting the `WP_CLI_TEST_CORE_ZIP` environment variable. It accepts either
196+
a path to a local archive or an HTTP(S) URL.
197+
198+
This is useful to test against a WordPress build that has not been released, such as the ZIP file
199+
produced by the WordPress core build process.
200+
201+
```bash
202+
WP_CLI_TEST_CORE_ZIP=~/Downloads/wordpress.zip composer behat
203+
```
204+
205+
The archive may contain WordPress at its root, or wrapped in a single folder — both `wordpress/`
206+
(as used by WordPress.org releases) and `build/` (as used by some WordPress core build artifacts)
207+
work. Archives are extracted once and then cached, keyed by their contents.
208+
209+
`WP_VERSION` still determines which version-specific tags (`@require-wp-6.4`, `@less-than-wp-6.4`)
210+
are filtered out, since the version of a development build cannot be compared meaningfully. It
211+
defaults to `trunk` when an archive is set, which runs every scenario. Set it explicitly when the
212+
archive holds a specific release:
213+
214+
```bash
215+
WP_VERSION=6.4.2 WP_CLI_TEST_CORE_ZIP=~/Downloads/wordpress-6.4.2.zip composer behat
216+
```
217+
218+
Note that steps requesting an explicit version, such as `Given a WP 6.4.2 installation`, keep
219+
downloading that version from WordPress.org and ignore the archive.
220+
221+
#### Environment-specific scenarios
222+
223+
Some scenarios can only run in certain environments. Tagging them makes the test framework
224+
filter them out everywhere else, rather than having them fail for reasons unrelated to what
225+
they test.
226+
227+
* `@require-wp-stable` — the scenario needs a version of WordPress that WordPress.org knows
228+
about, such as one verifying an installation against the published checksums. It is
229+
skipped when `WP_CLI_TEST_CORE_ZIP` is set, and when `WP_VERSION` is `trunk` or `nightly`.
230+
* `@require-mysql-socket` — the scenario connects to the database through a socket. It is
231+
skipped when there is none, which is the case when the database server runs in a
232+
container and is only reachable over TCP. Set `WP_CLI_TEST_DBSOCKET` to point at the
233+
socket if it lives somewhere unusual.
234+
122235
#### WP-CLI Binary
123236

124237
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.

0 commit comments

Comments
 (0)