Skip to content

Commit c5e051b

Browse files
committed
Merge BEHAT_PARAMS into behat.yml the way Behat reads suites
- Replace lists as a whole instead of index by index, so that an override can shorten or empty a list of the file. - Bring the settings of a suite into one form before merging, as Behat lets the ones listed next to its reserved keys win. - Fail the generator when a file cannot be written. - Document the second interface of Behat 3.15, and that Behat 3.15 to 3.17 do not look at an overridden method for its attributes. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5oCbv1VegFDowMr3eS
1 parent 2e6ce92 commit c5e051b

7 files changed

Lines changed: 227 additions & 4 deletions

File tree

‎.readme-partials/USING.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,11 @@ Behat 4 dropped a few things that the framework bridges, within limits:
125125
`WP_CLI\Tests\Context\FeatureContext` does: declare everything with attributes, and repeat the
126126
declarations as annotations on an interface that the context only implements on PHP 7. Behat
127127
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.
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.
129133
* Behat 4 fails a step whose regular expression captures more values than its method has
130134
parameters, where Behat 3 dropped the extra ones. Use non-capturing groups (`(?:...)`) for those.
131135

‎README.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ Behat 4 dropped a few things that the framework bridges, within limits:
136136
`WP_CLI\Tests\Context\FeatureContext` does: declare everything with attributes, and repeat the
137137
declarations as annotations on an interface that the context only implements on PHP 7. Behat
138138
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.
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.
140144
* Behat 4 fails a step whose regular expression captures more values than its method has
141145
parameters, where Behat 3 dropped the extra ones. Use non-capturing groups (`(?:...)`) for those.
142146

‎tests/data/overriding_context.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/**
4+
* Test data for TestLegacyAnnotations: a context overriding a hook and a step without attributes.
5+
*/
6+
7+
namespace WP_CLI\Tests\Tests;
8+
9+
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
10+
use WP_CLI\Tests\Context\FeatureContext;
11+
12+
class Overriding_Context extends FeatureContext {
13+
14+
public function beforeScenario( BeforeScenarioScope $scope ): void { // phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod.Found
15+
parent::beforeScenario( $scope );
16+
}
17+
18+
public function given_an_empty_directory(): void { // phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod.Found
19+
parent::given_an_empty_directory();
20+
}
21+
}

‎tests/tests/TestBehatConfig.php‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
namespace WP_CLI\Tests\Tests;
4+
5+
use WP_CLI\Tests\TestCase;
6+
use WP_CLI\Utils;
7+
8+
class TestBehatConfig extends TestCase {
9+
10+
/**
11+
* @var string
12+
*/
13+
private $config_file = '';
14+
15+
protected function set_up(): void {
16+
parent::set_up();
17+
18+
$this->config_file = Utils\get_temp_dir() . uniqid( 'wp-cli-test-behat-config-', true ) . '.yml';
19+
20+
file_put_contents(
21+
$this->config_file,
22+
"default:\n suites:\n default:\n contexts: [ FeatureContext ]\n paths: [ features/a, features/b ]\n formatters:\n pretty: true\n"
23+
);
24+
}
25+
26+
protected function tear_down(): void {
27+
if ( file_exists( $this->config_file ) ) {
28+
unlink( $this->config_file );
29+
}
30+
31+
parent::tear_down();
32+
}
33+
34+
/**
35+
* Run the converter the way run-behat-tests does, from the root of this package.
36+
*
37+
* @param string|null $params Value of BEHAT_PARAMS, if any.
38+
* @return array<mixed>
39+
*/
40+
private function convert( $params = null ): array {
41+
$root = dirname( dirname( __DIR__ ) );
42+
$command = 'cd ' . escapeshellarg( $root ) . ' && '
43+
. ( null === $params ? 'unset BEHAT_PARAMS; ' : 'BEHAT_PARAMS=' . escapeshellarg( $params ) . ' ' )
44+
. escapeshellarg( PHP_BINARY ) . ' utils/behat-config.php ' . escapeshellarg( $this->config_file );
45+
46+
$output = (string) shell_exec( $command );
47+
48+
if ( '' === $output ) {
49+
$this->markTestSkipped( 'The installed Behat reads behat.yml by itself.' );
50+
}
51+
52+
$config = json_decode( $output, true );
53+
$this->assertIsArray( $config );
54+
55+
return $config;
56+
}
57+
58+
public function test_default_profile_is_passed_on(): void {
59+
$this->assertSame(
60+
[
61+
'suites' => [
62+
'default' => [
63+
'contexts' => [ 'FeatureContext' ],
64+
'paths' => [ 'features/a', 'features/b' ],
65+
],
66+
],
67+
'formatters' => [ 'pretty' => true ],
68+
],
69+
$this->convert()
70+
);
71+
}
72+
73+
public function test_existing_params_are_merged_over_the_profile(): void {
74+
$config = $this->convert( '{"formatters":{"progress":true}}' );
75+
76+
$this->assertSame(
77+
[
78+
'pretty' => true,
79+
'progress' => true,
80+
],
81+
$config['formatters']
82+
);
83+
$this->assertSame( [ 'FeatureContext' ], $config['suites']['default']['settings']['contexts'] );
84+
}
85+
86+
public function test_lists_are_replaced_as_a_whole(): void {
87+
$config = $this->convert( '{"suites":{"default":{"paths":["features/smoke"]}}}' );
88+
$this->assertSame( [ 'features/smoke' ], $config['suites']['default']['settings']['paths'] );
89+
90+
$config = $this->convert( '{"suites":{"default":{"paths":[]}}}' );
91+
$this->assertSame( [], $config['suites']['default']['settings']['paths'] );
92+
}
93+
94+
public function test_both_forms_of_suite_settings_are_reconciled(): void {
95+
$config = $this->convert( '{"suites":{"default":{"settings":{"paths":["features/smoke"]}}}}' );
96+
97+
$this->assertSame(
98+
[
99+
'settings' => [
100+
'contexts' => [ 'FeatureContext' ],
101+
'paths' => [ 'features/smoke' ],
102+
],
103+
],
104+
$config['suites']['default']
105+
);
106+
}
107+
}

‎tests/tests/TestLegacyAnnotations.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace WP_CLI\Tests\Tests;
44

5+
use WP_CLI\Tests\Context\FeatureContext;
56
use WP_CLI\Tests\TestCase;
67

78
class TestLegacyAnnotations extends TestCase {
@@ -23,6 +24,26 @@ public function test_annotations_match_the_attributes(): void {
2324
}
2425
}
2526

27+
/**
28+
* Behat reads the attributes of the method an override replaces, from 3.18 on. That only finds
29+
* them as long as no interface declares the method where the attributes are read.
30+
*/
31+
public function test_overrides_keep_their_definitions(): void {
32+
if ( PHP_VERSION_ID < 80100 || ! class_exists( 'Behat\Hook\BeforeSuite' ) ) {
33+
$this->markTestSkipped( 'Reading the attributes needs PHP 8.1 and a Behat that has all of them.' );
34+
}
35+
36+
require_once dirname( __DIR__ ) . '/data/overriding_context.php';
37+
38+
foreach ( [ 'beforeScenario', 'given_an_empty_directory' ] as $name ) {
39+
$method = new \ReflectionMethod( 'WP_CLI\Tests\Tests\Overriding_Context', $name );
40+
41+
$this->assertCount( 0, $method->getAttributes() );
42+
$this->assertSame( FeatureContext::class, $method->getPrototype()->getDeclaringClass()->getName() );
43+
$this->assertCount( 1, $method->getPrototype()->getAttributes() );
44+
}
45+
}
46+
2647
/**
2748
* PHP 7 reads an attribute as a comment, which ends with the line, or with a closing PHP tag.
2849
*/

‎utils/behat-config.php‎

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,67 @@
1616
use Composer\InstalledVersions;
1717
use Symfony\Component\Yaml\Yaml;
1818

19+
/**
20+
* Move the settings a suite lists next to its reserved keys into its "settings", as Behat does.
21+
*
22+
* Behat accepts both forms, and lets the first one win. Merging two profiles that each use another
23+
* form would otherwise keep both, and the one that is meant to be overridden could be that winner.
24+
*
25+
* @param array<mixed> $profile
26+
* @return array<mixed>
27+
*/
28+
function wp_cli_tests_normalize_suites( array $profile ) {
29+
if ( ! isset( $profile['suites'] ) || ! is_array( $profile['suites'] ) ) {
30+
return $profile;
31+
}
32+
33+
foreach ( $profile['suites'] as $name => $suite ) {
34+
if ( ! is_array( $suite ) ) {
35+
continue;
36+
}
37+
38+
$settings = isset( $suite['settings'] ) && is_array( $suite['settings'] ) ? $suite['settings'] : [];
39+
40+
foreach ( $suite as $key => $value ) {
41+
if ( ! in_array( $key, [ 'enabled', 'type', 'settings' ], true ) ) {
42+
$settings[ $key ] = $value;
43+
unset( $suite[ $key ] );
44+
}
45+
}
46+
47+
if ( $settings ) {
48+
$suite['settings'] = $settings;
49+
}
50+
51+
$profile['suites'][ $name ] = $suite;
52+
}
53+
54+
return $profile;
55+
}
56+
57+
/**
58+
* Merge overrides into a profile: maps key by key, anything else, lists included, as a whole.
59+
*
60+
* @param array<mixed> $base
61+
* @param array<mixed> $overrides
62+
* @return array<mixed>
63+
*/
64+
function wp_cli_tests_merge_params( array $base, array $overrides ) {
65+
foreach ( $overrides as $key => $value ) {
66+
$is_map = static function ( $candidate ) {
67+
return is_array( $candidate ) && $candidate && array_keys( $candidate ) !== range( 0, count( $candidate ) - 1 );
68+
};
69+
70+
if ( isset( $base[ $key ] ) && $is_map( $base[ $key ] ) && $is_map( $value ) ) {
71+
$base[ $key ] = wp_cli_tests_merge_params( $base[ $key ], $value );
72+
} else {
73+
$base[ $key ] = $value;
74+
}
75+
}
76+
77+
return $base;
78+
}
79+
1980
$wp_cli_tests_autoloader = getcwd() . '/vendor/autoload.php';
2081

2182
if ( ! file_exists( $wp_cli_tests_autoloader ) ) {
@@ -65,7 +126,10 @@
65126
exit( 1 );
66127
}
67128

68-
$wp_cli_tests_params = array_replace_recursive( $wp_cli_tests_params, $wp_cli_tests_overrides );
129+
$wp_cli_tests_params = wp_cli_tests_merge_params(
130+
wp_cli_tests_normalize_suites( $wp_cli_tests_params ),
131+
wp_cli_tests_normalize_suites( $wp_cli_tests_overrides )
132+
);
69133
}
70134

71135
echo json_encode( $wp_cli_tests_params, JSON_UNESCAPED_SLASHES );

‎utils/generate-legacy-annotations.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,10 @@ function wp_cli_tests_legacy_files() {
144144

145145
if ( $wp_cli_tests_check ) {
146146
fwrite( STDERR, "{$wp_cli_tests_file} is out of date, run `php utils/generate-legacy-annotations.php`.\n" );
147+
} elseif ( strlen( $wp_cli_tests_contents ) !== file_put_contents( $wp_cli_tests_file, $wp_cli_tests_contents ) ) {
148+
fwrite( STDERR, "Error: Could not write {$wp_cli_tests_file}.\n" );
149+
exit( 1 );
147150
} else {
148-
file_put_contents( $wp_cli_tests_file, $wp_cli_tests_contents );
149151
echo "Wrote {$wp_cli_tests_file}\n";
150152
}
151153
}

0 commit comments

Comments
 (0)