Skip to content

Commit 77d4758

Browse files
authored
Merge pull request #282 from wp-cli/copilot/add-given-wp-install-step
2 parents ad66177 + 29b8d54 commit 77d4758

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed

features/testing.feature

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,21 @@ Feature: Test that WP-CLI loads.
6262
"""
6363
sqlite
6464
"""
65+
66+
Scenario: WP installation with specific version
67+
Given a WP 6.4.2 installation
68+
69+
When I run `wp core version`
70+
Then STDOUT should be:
71+
"""
72+
6.4.2
73+
"""
74+
75+
Scenario: WP installation in subdirectory with specific version
76+
Given a WP 6.3.1 installation in 'wordpress'
77+
78+
When I run `wp core version --path=wordpress`
79+
Then STDOUT should be:
80+
"""
81+
6.3.1
82+
"""

src/Context/FeatureContext.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -621,10 +621,12 @@ private static function configure_sqlite( $dir ): void {
621621
/**
622622
* We cache the results of `wp core download` to improve test performance.
623623
* Ideally, we'd cache at the HTTP layer for more reliable tests.
624+
*
625+
* @param string $version
624626
*/
625-
private static function cache_wp_files(): void {
626-
$wp_version = getenv( 'WP_VERSION' );
627-
$wp_version_suffix = ( false !== $wp_version ) ? "-$wp_version" : '';
627+
private static function cache_wp_files( $version = '' ): void {
628+
$wp_version = $version ?: getenv( 'WP_VERSION' );
629+
$wp_version_suffix = $wp_version ? "-$wp_version" : '';
628630
self::$cache_dir = sys_get_temp_dir() . '/wp-cli-test-core-download-cache' . $wp_version_suffix;
629631
self::$sqlite_cache_dir = sys_get_temp_dir() . '/wp-cli-test-sqlite-integration-cache';
630632

@@ -1288,10 +1290,15 @@ public function add_line_to_wp_config( &$wp_config_code, $line ): void {
12881290

12891291
/**
12901292
* @param string $subdir
1293+
* @param string $version
12911294
*/
1292-
public function download_wp( $subdir = '' ): void {
1293-
if ( ! self::$cache_dir ) {
1294-
self::cache_wp_files();
1295+
public function download_wp( $subdir = '', $version = '' ): void {
1296+
$wp_version = $version ?: getenv( 'WP_VERSION' );
1297+
$wp_version_suffix = $wp_version ? "-$wp_version" : '';
1298+
$expected_cache_dir = sys_get_temp_dir() . '/wp-cli-test-core-download-cache' . $wp_version_suffix;
1299+
1300+
if ( ! self::$cache_dir || self::$cache_dir !== $expected_cache_dir ) {
1301+
self::cache_wp_files( $version );
12951302

12961303
$result = Process::create( Utils\esc_cmd( 'wp core version --debug --path=%s', self::$cache_dir ), null, self::get_process_env_variables() )->run_check();
12971304
echo "[Debug messages]\n";
@@ -1365,10 +1372,11 @@ public function create_config( $subdir = '', $extra_php = false ): void {
13651372

13661373
/**
13671374
* @param string $subdir
1375+
* @param string $version
13681376
*/
1369-
public function install_wp( $subdir = '' ): void {
1370-
$wp_version = getenv( 'WP_VERSION' );
1371-
$wp_version_suffix = ( false !== $wp_version ) ? "-$wp_version" : '';
1377+
public function install_wp( $subdir = '', $version = '' ): void {
1378+
$wp_version = $version ?: getenv( 'WP_VERSION' );
1379+
$wp_version_suffix = $wp_version ? "-$wp_version" : '';
13721380
self::$install_cache_dir = sys_get_temp_dir() . '/wp-cli-test-core-install-cache' . $wp_version_suffix;
13731381
if ( ! file_exists( self::$install_cache_dir ) ) {
13741382
mkdir( self::$install_cache_dir );
@@ -1383,7 +1391,7 @@ public function install_wp( $subdir = '' ): void {
13831391
$this->create_db();
13841392
}
13851393
$this->create_run_dir();
1386-
$this->download_wp( $subdir );
1394+
$this->download_wp( $subdir, $version );
13871395
$this->create_config( $subdir, $config_extra_php );
13881396

13891397
$install_args = [

src/Context/GivenStepDefinitions.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -391,14 +391,20 @@ public function given_a_database(): void {
391391
* Scenario: My other scenario
392392
* Given a WP install
393393
* ...
394+
*
395+
* Scenario: My version-specific scenario
396+
* Given a WP 6.4.2 installation
397+
* ...
394398
* ```
395399
*
396400
* @access public
397401
*
398-
* @Given a WP install(ation)
402+
* @Given /^a WP( [^\s]+)? install(?:ation)?$/
403+
*
404+
* @param string $version Optional version number (may include leading space)
399405
*/
400-
public function given_a_wp_installation(): void {
401-
$this->install_wp();
406+
public function given_a_wp_installation( $version = '' ): void {
407+
$this->install_wp( '', trim( $version ) );
402408
}
403409

404410
/**
@@ -412,16 +418,21 @@ public function given_a_wp_installation(): void {
412418
* Scenario: My other scenario
413419
* Given a WP install in 'bar'
414420
* ...
421+
*
422+
* Scenario: My version-specific scenario
423+
* Given a WP 6.4.2 installation in 'subdir'
424+
* ...
415425
* ```
416426
*
417427
* @access public
418428
*
419-
* @Given a WP install(ation) in :subdir
429+
* @Given /^a WP( [^\s]+)? install(?:ation)? in ['"]?([^'"]+)['"]?$/
420430
*
431+
* @param string $version Optional version number (may include leading space)
421432
* @param string $subdir
422433
*/
423-
public function given_a_wp_installation_in_a_specific_folder( $subdir ): void {
424-
$this->install_wp( $subdir );
434+
public function given_a_wp_installation_in_a_specific_folder( $version = '', $subdir = '' ): void {
435+
$this->install_wp( $subdir, trim( $version ) );
425436
}
426437

427438
/**

0 commit comments

Comments
 (0)