Skip to content

Commit a299ad2

Browse files
committed
DRY unit test
1 parent 0f1f277 commit a299ad2

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

tests/tests/TestBehatTags.php

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,30 @@ protected function tear_down(): void {
3535
parent::tear_down();
3636
}
3737

38+
/**
39+
* Runs the behat-tags.php script in a cross-platform way.
40+
*
41+
* @param string $env Environment variable string to set (e.g., 'WP_VERSION=4.5').
42+
* @return string|false The output of the script.
43+
*/
44+
private function run_behat_tags_script( $env = '' ) {
45+
$behat_tags = dirname( dirname( __DIR__ ) ) . '/utils/behat-tags.php';
46+
47+
if ( ! empty( $env ) ) {
48+
putenv( $env );
49+
}
50+
51+
$command = 'cd ' . escapeshellarg( $this->temp_dir ) . ' && php ' . escapeshellarg( $behat_tags );
52+
$output = exec( $command );
53+
54+
if ( ! empty( $env ) ) {
55+
list( $key ) = explode( '=', $env, 2 );
56+
putenv( $key ); // Unsets the variable.
57+
}
58+
59+
return $output;
60+
}
61+
3862
/**
3963
* @dataProvider data_behat_tags_wp_version_github_token
4064
*
@@ -50,19 +74,10 @@ public function test_behat_tags_wp_version_github_token( $env, $expected ): void
5074
putenv( 'WP_VERSION' );
5175
putenv( 'GITHUB_TOKEN' );
5276

53-
$behat_tags = dirname( dirname( __DIR__ ) ) . '/utils/behat-tags.php';
54-
5577
$contents = '@require-wp-4.6 @require-wp-4.8 @require-wp-4.9 @less-than-wp-4.6 @less-than-wp-4.8 @less-than-wp-4.9';
5678
file_put_contents( $this->temp_dir . '/features/wp_version.feature', $contents );
5779

58-
if ( ! empty( $env ) ) {
59-
putenv( $env );
60-
}
61-
$output = exec( 'cd ' . escapeshellarg( $this->temp_dir ) . ' && php ' . escapeshellarg( $behat_tags ) );
62-
if ( ! empty( $env ) ) {
63-
list( $key ) = explode( '=', $env, 2 );
64-
putenv( $key );
65-
}
80+
$output = $this->run_behat_tags_script( $env );
6681

6782
$expected .= '&&~@broken';
6883
if ( in_array( $env, array( 'WP_VERSION=trunk', 'WP_VERSION=nightly' ), true ) ) {
@@ -116,8 +131,6 @@ public function test_behat_tags_php_version(): void {
116131

117132
putenv( 'GITHUB_TOKEN' );
118133

119-
$behat_tags = dirname( dirname( __DIR__ ) ) . '/utils/behat-tags.php';
120-
121134
$php_version = substr( PHP_VERSION, 0, 3 );
122135
$contents = '';
123136
$expected = '';
@@ -172,7 +185,7 @@ public function test_behat_tags_php_version(): void {
172185

173186
file_put_contents( $this->temp_dir . '/features/php_version.feature', $contents );
174187

175-
$output = exec( "cd {$this->temp_dir}; php $behat_tags" );
188+
$output = $this->run_behat_tags_script();
176189
$this->assertSame( '--tags=' . $expected, $output );
177190

178191
putenv( false === $env_github_token ? 'GITHUB_TOKEN' : "GITHUB_TOKEN=$env_github_token" );
@@ -184,8 +197,6 @@ public function test_behat_tags_extension(): void {
184197

185198
putenv( 'GITHUB_TOKEN' );
186199

187-
$behat_tags = dirname( dirname( __DIR__ ) ) . '/utils/behat-tags.php';
188-
189200
file_put_contents( $this->temp_dir . '/features/extension.feature', '@require-extension-imagick @require-extension-curl' );
190201

191202
$expecteds = array();
@@ -215,7 +226,7 @@ public function test_behat_tags_extension(): void {
215226
}
216227

217228
$expected = '--tags=' . implode( '&&', array_merge( array( '~@github-api', '~@broken' ), $expecteds ) );
218-
$output = exec( "cd {$this->temp_dir}; php $behat_tags" );
229+
$output = $this->run_behat_tags_script();
219230
$this->assertSame( $expected, $output );
220231

221232
putenv( false === $env_github_token ? 'GITHUB_TOKEN' : "GITHUB_TOKEN=$env_github_token" );
@@ -264,7 +275,7 @@ public function test_behat_tags_db_version(): void {
264275
file_put_contents( $this->temp_dir . '/features/extension.feature', $contents );
265276

266277
$expected = '--tags=' . implode( '&&', array_merge( array( '~@github-api', '~@broken' ), $expecteds ) );
267-
$output = exec( "cd {$this->temp_dir}; php $behat_tags" );
278+
$output = $this->run_behat_tags_script();
268279
$this->assertSame( $expected, $output );
269280
}
270281
}

0 commit comments

Comments
 (0)