Skip to content

Commit 8c74ce7

Browse files
committed
Merge BEHAT_PARAMS over behat.yml for Behat 4 and honor --config
- An existing BEHAT_PARAMS no longer replaces the default profile of behat.yml under Behat 4, it is merged over it, as Behat 3 does. - A run that names a configuration file with --config is left alone. - Read the attribute values from their public properties, which Behat 3.16 to 3.21 have no getters for. - Document the suite hook annotations Behat 3.15 needs on PHP 8.0. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5oCbv1VegFDowMr3eS
1 parent 8afd309 commit 8c74ce7

5 files changed

Lines changed: 46 additions & 17 deletions

File tree

‎.readme-partials/USING.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,17 @@ Behat 4 dropped a few things that the framework bridges, within limits:
115115
116116
* Behat 4 only reads its configuration from a `behat.php` (or `behat.dist.php`) file. For a package
117117
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`.
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`.
120121
* Behat 4 only reads step definitions and hooks from attributes (`#[Given( '...' )]`), not from
121122
annotations (`@Given ...`) anymore. This matters to packages with a context class of their own.
122123
Behat 3 reads attributes as well, but only on PHP 8, and it defines a step twice, which is an
123124
error, when a method has both. A context that has to run on PHP 7 too can do what
124125
`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+
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, so there the interface still has to carry the annotations of those two.
126129
* Behat 4 fails a step whose regular expression captures more values than its method has
127130
parameters, where Behat 3 dropped the extra ones. Use non-capturing groups (`(?:...)`) for those.
128131

‎README.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,17 @@ Behat 4 dropped a few things that the framework bridges, within limits:
126126
127127
* Behat 4 only reads its configuration from a `behat.php` (or `behat.dist.php`) file. For a package
128128
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`.
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`.
131132
* Behat 4 only reads step definitions and hooks from attributes (`#[Given( '...' )]`), not from
132133
annotations (`@Given ...`) anymore. This matters to packages with a context class of their own.
133134
Behat 3 reads attributes as well, but only on PHP 8, and it defines a step twice, which is an
134135
error, when a method has both. A context that has to run on PHP 7 too can do what
135136
`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+
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, so there the interface still has to carry the annotations of those two.
137140
* Behat 4 fails a step whose regular expression captures more values than its method has
138141
parameters, where Behat 3 dropped the extra ones. Use non-capturing groups (`(?:...)`) for those.
139142

‎bin/run-behat-tests‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,16 +293,22 @@ 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
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
300308
exit 1
301309
fi
302-
if [ -n "${BEHAT_PARAMS}" ]; then
303-
export BEHAT_PARAMS
304-
else
305-
unset BEHAT_PARAMS
310+
if [ -n "${BEHAT_YML_PARAMS}" ]; then
311+
export BEHAT_PARAMS="${BEHAT_YML_PARAMS}"
306312
fi
307313
fi
308314

‎utils/behat-config.php‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* BEHAT_PARAMS environment variable, in every version. Printing the profile in that form lets a
77
* project keep a single behat.yml for all the Behat versions it is tested with.
88
*
9+
* What BEHAT_PARAMS already holds is merged over the profile, as Behat 3 does with behat.yml.
10+
*
911
* Prints nothing when Behat reads behat.yml by itself.
1012
*
1113
* Usage: php behat-config.php [<path to behat.yml>]
@@ -52,4 +54,18 @@
5254
fwrite( STDERR, "Warning: Behat {$wp_cli_tests_behat_version} does not read YAML, so only the \"default\" profile of {$wp_cli_tests_config_file} is used, not: {$wp_cli_tests_ignored}. Add a behat.php for those.\n" );
5355
}
5456

55-
echo json_encode( $wp_cli_tests_config['default'], JSON_UNESCAPED_SLASHES );
57+
$wp_cli_tests_params = $wp_cli_tests_config['default'];
58+
$wp_cli_tests_env = getenv( 'BEHAT_PARAMS' );
59+
60+
if ( is_string( $wp_cli_tests_env ) && '' !== $wp_cli_tests_env ) {
61+
$wp_cli_tests_overrides = json_decode( $wp_cli_tests_env, true );
62+
63+
if ( ! is_array( $wp_cli_tests_overrides ) ) {
64+
fwrite( STDERR, "Error: BEHAT_PARAMS should contain valid JSON, but it is set to `{$wp_cli_tests_env}`.\n" );
65+
exit( 1 );
66+
}
67+
68+
$wp_cli_tests_params = array_replace_recursive( $wp_cli_tests_params, $wp_cli_tests_overrides );
69+
}
70+
71+
echo json_encode( $wp_cli_tests_params, JSON_UNESCAPED_SLASHES );

‎utils/generate-legacy-annotations.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ function wp_cli_tests_legacy_interface( array $only = [] ) {
8989
continue;
9090
}
9191

92-
$instance = $attribute->newInstance();
93-
$argument = $instance instanceof Definition ? $instance->getPattern() : $instance->getFilterString();
92+
// Public properties in every Behat version, which the getters are not.
93+
$values = get_object_vars( $attribute->newInstance() );
94+
$argument = $values['pattern'] ?? $values['filterString'] ?? null;
9495
$name = substr( (string) strrchr( $attribute->getName(), '\\' ), 1 );
9596

9697
if ( null !== $argument && ( false !== strpos( $argument, '*/' ) || false !== strpos( $argument, "\n" ) ) ) {

0 commit comments

Comments
 (0)