Skip to content

Commit 9c36668

Browse files
authored
Support Behat 4 alongside Behat 3 (#428)
1 parent db43a80 commit 9c36668

19 files changed

Lines changed: 1016 additions & 129 deletions

‎.readme-partials/USING.md‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,40 @@ 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, with anything that variable already holds
119+
merged over it. Other profiles and `imports` are not carried over; a package that needs those
120+
adds a `behat.php` next to its `behat.yml`, or names a configuration file with `--config`.
121+
* Behat 4 only reads step definitions and hooks from attributes (`#[Given( '...' )]`), not from
122+
annotations (`@Given ...`) anymore. This matters to packages with a context class of their own.
123+
Behat 3 reads attributes as well, but only on PHP 8, and it defines a step twice, which is an
124+
error, when a method has both. A context that has to run on PHP 7 too can do what
125+
`WP_CLI\Tests\Context\FeatureContext` does: declare everything with attributes, and repeat the
126+
declarations as annotations on an interface that the context only implements on PHP 7. Behat
127+
3.15, the last release for PHP 8.0, has no attributes for the `BeforeSuite` and `AfterSuite`
128+
hooks yet. There the context implements a second interface instead, which only carries the
129+
annotations of those two hooks; implementing the full one would define every step twice.
130+
* A class extending `WP_CLI\Tests\Context\FeatureContext` that overrides one of its steps or hooks
131+
keeps the definition without repeating the attribute, except with Behat 3.15 to 3.17 on PHP 8,
132+
which do not look at the method that is overridden. In practice that is PHP 8.0.
133+
* Behat 4 fails a step whose regular expression captures more values than its method has
134+
parameters, where Behat 3 dropped the extra ones. Use non-capturing groups (`(?:...)`) for those.
135+
136+
To keep a package on Behat 3 for the time being, require it explicitly:
137+
138+
```bash
139+
composer require --dev behat/behat:^3.15
140+
```
141+
108142
### Analysing the PHP blocks in feature files
109143
110144
Feature files embed PHP snippets in docstrings, which none of the static analysis tools normally

‎README.md‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,40 @@ 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, with anything that variable already holds
130+
merged over it. Other profiles and `imports` are not carried over; a package that needs those
131+
adds a `behat.php` next to its `behat.yml`, or names a configuration file with `--config`.
132+
* Behat 4 only reads step definitions and hooks from attributes (`#[Given( '...' )]`), not from
133+
annotations (`@Given ...`) anymore. This matters to packages with a context class of their own.
134+
Behat 3 reads attributes as well, but only on PHP 8, and it defines a step twice, which is an
135+
error, when a method has both. A context that has to run on PHP 7 too can do what
136+
`WP_CLI\Tests\Context\FeatureContext` does: declare everything with attributes, and repeat the
137+
declarations as annotations on an interface that the context only implements on PHP 7. Behat
138+
3.15, the last release for PHP 8.0, has no attributes for the `BeforeSuite` and `AfterSuite`
139+
hooks yet. There the context implements a second interface instead, which only carries the
140+
annotations of those two hooks; implementing the full one would define every step twice.
141+
* A class extending `WP_CLI\Tests\Context\FeatureContext` that overrides one of its steps or hooks
142+
keeps the definition without repeating the attribute, except with Behat 3.15 to 3.17 on PHP 8,
143+
which do not look at the method that is overridden. In practice that is PHP 8.0.
144+
* Behat 4 fails a step whose regular expression captures more values than its method has
145+
parameters, where Behat 3 dropped the extra ones. Use non-capturing groups (`(?:...)`) for those.
146+
147+
To keep a package on Behat 3 for the time being, require it explicitly:
148+
149+
```bash
150+
composer require --dev behat/behat:^3.15
151+
```
152+
119153
### Analysing the PHP blocks in feature files
120154
121155
Feature files embed PHP snippets in docstrings, which none of the static analysis tools normally

‎bin/run-behat-tests‎

Lines changed: 20 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,25 @@ 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 the run names a configuration
297+
# file, hand Behat the default profile of behat.yml through BEHAT_PARAMS, which it reads in every
298+
# version. What BEHAT_PARAMS already holds takes precedence over the file, as it does in Behat 3.
299+
BEHAT_CONFIG_ARG=""
300+
for arg in "$@"; do
301+
case "$arg" in
302+
-c|-c*|--config|--config=*) BEHAT_CONFIG_ARG=1;;
303+
esac
304+
done
305+
306+
if [ -f "behat.yml" ] && [ ! -f "behat.php" ] && [ ! -f "behat.dist.php" ] && [ -z "${BEHAT_CONFIG_ARG}" ]; then
307+
if ! BEHAT_YML_PARAMS=$(php "$WP_CLI_TESTS_ROOT"/utils/behat-config.php); then
308+
exit 1
309+
fi
310+
if [ -n "${BEHAT_YML_PARAMS}" ]; then
311+
export BEHAT_PARAMS="${BEHAT_YML_PARAMS}"
312+
fi
313+
fi
314+
296315
# Generate the tags to apply environment-specific filters.
297316
BEHAT_TAGS=$(php "$WP_CLI_TESTS_ROOT"/utils/behat-tags.php)
298317

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