Skip to content

Commit 8afd309

Browse files
committed
Support Behat 4 alongside Behat 3
Behat 4 only reads PHP configuration and attributes, and requires PHP 8.2, so Behat 3 has to keep working for older PHP versions. - Declare the steps and hooks with attributes. PHP 7, and Behat 3.15 for its suite hooks, get the same declarations as generated annotations on an interface, so that no step is ever defined twice. - Hand the default profile of behat.yml to Behat 4 through BEHAT_PARAMS. - Use non-capturing groups where a pattern captured more than its method takes. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5oCbv1VegFDowMr3eS
1 parent 434b234 commit 8afd309

17 files changed

Lines changed: 756 additions & 129 deletions

‎.readme-partials/USING.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,33 @@ You can use the following commands to control the tests:
105105
* `composer phpunit` - Run only the unit test suite.
106106
* `composer behat` - Run only the functional test suite.
107107
108+
### Behat 3 and Behat 4
109+
110+
The testing framework works with both Behat 3 and Behat 4. Composer installs Behat 4 on PHP 8.2 and
111+
later, and Behat 3 on older PHP versions, and a package that only uses the steps of the framework
112+
does not need to change anything for either.
113+
114+
Behat 4 dropped a few things that the framework bridges, within limits:
115+
116+
* Behat 4 only reads its configuration from a `behat.php` (or `behat.dist.php`) file. For a package
117+
that only has a `behat.yml`, `composer behat` hands the `default` profile of that file to Behat 4
118+
through the `BEHAT_PARAMS` environment variable. Other profiles and `imports` are not carried
119+
over; a package that needs those adds a `behat.php` next to its `behat.yml`.
120+
* Behat 4 only reads step definitions and hooks from attributes (`#[Given( '...' )]`), not from
121+
annotations (`@Given ...`) anymore. This matters to packages with a context class of their own.
122+
Behat 3 reads attributes as well, but only on PHP 8, and it defines a step twice, which is an
123+
error, when a method has both. A context that has to run on PHP 7 too can do what
124+
`WP_CLI\Tests\Context\FeatureContext` does: declare everything with attributes, and repeat the
125+
declarations as annotations on an interface that the context only implements on PHP 7.
126+
* Behat 4 fails a step whose regular expression captures more values than its method has
127+
parameters, where Behat 3 dropped the extra ones. Use non-capturing groups (`(?:...)`) for those.
128+
129+
To keep a package on Behat 3 for the time being, require it explicitly:
130+
131+
```bash
132+
composer require --dev behat/behat:^3.15
133+
```
134+
108135
### Analysing the PHP blocks in feature files
109136
110137
Feature files embed PHP snippets in docstrings, which none of the static analysis tools normally

‎README.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,33 @@ You can use the following commands to control the tests:
116116
* `composer phpunit` - Run only the unit test suite.
117117
* `composer behat` - Run only the functional test suite.
118118
119+
### Behat 3 and Behat 4
120+
121+
The testing framework works with both Behat 3 and Behat 4. Composer installs Behat 4 on PHP 8.2 and
122+
later, and Behat 3 on older PHP versions, and a package that only uses the steps of the framework
123+
does not need to change anything for either.
124+
125+
Behat 4 dropped a few things that the framework bridges, within limits:
126+
127+
* Behat 4 only reads its configuration from a `behat.php` (or `behat.dist.php`) file. For a package
128+
that only has a `behat.yml`, `composer behat` hands the `default` profile of that file to Behat 4
129+
through the `BEHAT_PARAMS` environment variable. Other profiles and `imports` are not carried
130+
over; a package that needs those adds a `behat.php` next to its `behat.yml`.
131+
* Behat 4 only reads step definitions and hooks from attributes (`#[Given( '...' )]`), not from
132+
annotations (`@Given ...`) anymore. This matters to packages with a context class of their own.
133+
Behat 3 reads attributes as well, but only on PHP 8, and it defines a step twice, which is an
134+
error, when a method has both. A context that has to run on PHP 7 too can do what
135+
`WP_CLI\Tests\Context\FeatureContext` does: declare everything with attributes, and repeat the
136+
declarations as annotations on an interface that the context only implements on PHP 7.
137+
* Behat 4 fails a step whose regular expression captures more values than its method has
138+
parameters, where Behat 3 dropped the extra ones. Use non-capturing groups (`(?:...)`) for those.
139+
140+
To keep a package on Behat 3 for the time being, require it explicitly:
141+
142+
```bash
143+
composer require --dev behat/behat:^3.15
144+
```
145+
119146
### Analysing the PHP blocks in feature files
120147
121148
Feature files embed PHP snippets in docstrings, which none of the static analysis tools normally

‎bin/run-behat-tests‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

33
# Run the Behat tests only if a Behat config file is found.
4-
if [ ! -f "behat.yml" ]; then
4+
if [ ! -f "behat.yml" ] && [ ! -f "behat.php" ] && [ ! -f "behat.dist.php" ]; then
55
echo 'Did not detect "behat.yml" file, skipping Behat tests.'
66
exit 0;
77
fi
@@ -293,6 +293,19 @@ done
293293
WP_CLI_TESTS_ROOT="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )"
294294
export WP_CLI_TESTS_ROOT
295295

296+
# Behat 4 only reads PHP configuration. Unless the project has one, or sets BEHAT_PARAMS itself,
297+
# hand Behat the default profile of behat.yml through BEHAT_PARAMS, which it reads in every version.
298+
if [ -f "behat.yml" ] && [ ! -f "behat.php" ] && [ ! -f "behat.dist.php" ] && [ -z "${BEHAT_PARAMS-}" ]; then
299+
if ! BEHAT_PARAMS=$(php "$WP_CLI_TESTS_ROOT"/utils/behat-config.php); then
300+
exit 1
301+
fi
302+
if [ -n "${BEHAT_PARAMS}" ]; then
303+
export BEHAT_PARAMS
304+
else
305+
unset BEHAT_PARAMS
306+
fi
307+
fi
308+
296309
# Generate the tags to apply environment-specific filters.
297310
BEHAT_TAGS=$(php "$WP_CLI_TESTS_ROOT"/utils/behat-tags.php)
298311

‎composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"require": {
1212
"php": ">=7.2.24",
1313
"ext-tokenizer": "*",
14-
"behat/behat": "^v3.15.0",
14+
"behat/behat": "^v3.15.0 || ^v4.0.0",
1515
"dealerdirect/phpcodesniffer-composer-installer": "^0.4.3 || ^0.5 || ^0.6.2 || ^0.7.1 || ^1.0.0",
1616
"johnbillion/wp-compat": "^2.0 || ^3.0",
1717
"php-parallel-lint/php-console-highlighter": "^1.0",

‎phpcs.xml.dist‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161

6262
<exclude-pattern>tests/phpstan/scan-files.php</exclude-pattern>
6363

64+
<!-- Generated by utils/generate-legacy-annotations.php. -->
65+
<exclude-pattern>src/Context/legacy/*</exclude-pattern>
66+
6467
<rule ref="WordPress.NamingConventions">
6568
<exclude-pattern>tests/data/*</exclude-pattern>
6669
<exclude-pattern>src/PHPStan/*</exclude-pattern>
@@ -75,6 +78,12 @@
7578
<exclude-pattern>src/PHPStan/*</exclude-pattern>
7679
</rule>
7780

81+
<!-- The contexts declare their steps and hooks with attributes for Behat 4.
82+
PHP 7 gets them as annotations instead, see src/Context/LegacyAnnotations.php. -->
83+
<rule ref="PHPCompatibility.Attributes.NewAttributes.Found">
84+
<exclude-pattern>src/Context/*</exclude-pattern>
85+
</rule>
86+
7887
<!-- This is a procedural stand-alone file that is never loaded in a WordPress context,
7988
so this file does not have to comply with WP naming conventions. -->
8089
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">

‎phpstan.neon.dist‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ parameters:
1010
# Templates are copied into the test run directory and executed by the
1111
# child WP-CLI process, where Requests v1 classes may still exist.
1212
- src/Context/templates
13+
# Generated, and declaring the interface that src/Context/LegacyAnnotations.php
14+
# declares empty where the attributes are read.
15+
- src/Context/legacy
1316
scanDirectories:
1417
- vendor/wp-cli/wp-cli
1518
- vendor/phpunit/php-code-coverage
1619
- vendor/behat/behat
1720
scanFiles:
1821
- tests/phpstan/scan-files.php
22+
- utils/generate-legacy-annotations.php
1923
treatPhpDocTypesAsCertain: false
2024
dynamicConstantNames:
2125
- WP_DEBUG

‎src/Context/FeatureContext.php‎

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
use Behat\Behat\Hook\Scope\AfterFeatureScope;
1414
use Behat\Behat\Hook\Scope\BeforeFeatureScope;
1515
use Behat\Behat\Hook\Scope\BeforeStepScope;
16+
use Behat\Hook\AfterFeature;
17+
use Behat\Hook\AfterScenario;
18+
use Behat\Hook\AfterSuite;
19+
use Behat\Hook\BeforeFeature;
20+
use Behat\Hook\BeforeScenario;
21+
use Behat\Hook\BeforeStep;
22+
use Behat\Hook\BeforeSuite;
1623
use SebastianBergmann\CodeCoverage\Report\Clover;
1724
use SebastianBergmann\CodeCoverage\Driver\Selector;
1825
use SebastianBergmann\CodeCoverage\Driver\Xdebug;
@@ -31,7 +38,7 @@
3138
/**
3239
* Features context.
3340
*/
34-
class FeatureContext implements Context {
41+
class FeatureContext implements Context, LegacyAnnotations {
3542

3643
use GivenStepDefinitions;
3744
use ThenStepDefinitions;
@@ -261,38 +268,28 @@ class FeatureContext implements Context {
261268
*/
262269
private $step_line = 0;
263270

264-
/**
265-
* @BeforeFeature
266-
*/
271+
#[BeforeFeature]
267272
public static function store_feature( BeforeFeatureScope $scope ): void {
268273
self::$feature = $scope->getFeature();
269274
}
270275

271-
/**
272-
* @BeforeScenario
273-
*/
276+
#[BeforeScenario]
274277
public function store_scenario( BeforeScenarioScope $scope ): void {
275278
$this->scenario = $scope->getScenario();
276279
}
277280

278-
/**
279-
* @BeforeStep
280-
*/
281+
#[BeforeStep]
281282
public function store_step( BeforeStepScope $scope ): void {
282283
$this->step_line = $scope->getStep()->getLine();
283284
}
284285

285-
/**
286-
* @AfterScenario
287-
*/
286+
#[AfterScenario]
288287
public function forget_scenario( AfterScenarioScope $scope ): void {
289288
$this->step_line = 0;
290289
$this->scenario = null;
291290
}
292291

293-
/**
294-
* @AfterFeature
295-
*/
292+
#[AfterFeature]
296293
public static function forget_feature( AfterFeatureScope $scope ): void {
297294
self::$feature = null;
298295
}
@@ -1105,9 +1102,7 @@ private static function cache_wp_files( $version = '' ): void {
11051102
self::$cache_dir = $cache_dir;
11061103
}
11071104

1108-
/**
1109-
* @BeforeSuite
1110-
*/
1105+
#[BeforeSuite]
11111106
public static function prepare( BeforeSuiteScope $scope ): void {
11121107
self::bootstrap_feature_context();
11131108

@@ -1141,9 +1136,7 @@ public static function prepare( BeforeSuiteScope $scope ): void {
11411136
}
11421137
}
11431138

1144-
/**
1145-
* @AfterSuite
1146-
*/
1139+
#[AfterSuite]
11471140
public static function afterSuite( AfterSuiteScope $scope ): void {
11481141
if ( self::$composer_local_repository ) {
11491142
self::remove_dir( self::$composer_local_repository );
@@ -1157,9 +1150,7 @@ public static function afterSuite( AfterSuiteScope $scope ): void {
11571150
self::merge_coverage_reports();
11581151
}
11591152

1160-
/**
1161-
* @BeforeScenario
1162-
*/
1153+
#[BeforeScenario]
11631154
public function beforeScenario( BeforeScenarioScope $scope ): void {
11641155
if ( self::$log_run_times ) {
11651156
self::log_run_times_before_scenario( $scope );
@@ -1182,9 +1173,7 @@ public function beforeScenario( BeforeScenarioScope $scope ): void {
11821173
}
11831174
}
11841175

1185-
/**
1186-
* @AfterScenario
1187-
*/
1176+
#[AfterScenario]
11881177
public function afterScenario( AfterScenarioScope $scope ): void {
11891178

11901179
if ( self::$run_dir ) {

0 commit comments

Comments
 (0)