Skip to content

Commit ed5ed1d

Browse files
committed
Add DB name as env variable and runtime variable
1 parent f480315 commit ed5ed1d

File tree

3 files changed

+61
-49
lines changed

3 files changed

+61
-49
lines changed

.github/workflows/testing.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ jobs:
159159
export MYSQL_TCP_PORT=${{ job.services.mysql.ports['3306'] }}
160160
echo "WP_CLI_TEST_DBROOTUSER=root" >> $GITHUB_ENV
161161
echo "WP_CLI_TEST_DBROOTPASS=root" >> $GITHUB_ENV
162+
echo "WP_CLI_TEST_DBNAME=wp_cli_test" >> $GITHUB_ENV
162163
echo "WP_CLI_TEST_DBUSER=wp_cli_test" >> $GITHUB_ENV
163164
echo "WP_CLI_TEST_DBPASS=password1" >> $GITHUB_ENV
164165
echo "WP_CLI_TEST_DBHOST=$MYSQL_HOST:$MYSQL_TCP_PORT" >> $GITHUB_ENV

bin/install-package-tests

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# - WP_CLI_TEST_DBHOST is the host to use and can include a port, i.e "127.0.0.1:33060" (defaults to "localhost")
55
# - WP_CLI_TEST_DBROOTUSER is the user that has permission to administer databases and users (defaults to "root").
66
# - WP_CLI_TEST_DBROOTPASS is the password to use for the above user (defaults to an empty password).
7+
# - WP_CLI_TEST_DBNAME is the database that the tests run under (defaults to "wp_cli_test").
78
# - WP_CLI_TEST_DBUSER is the user that the tests run under (defaults to "wp_cli_test").
89
# - WP_CLI_TEST_DBPASS is the password to use for the above user (defaults to "password1").
910

@@ -31,6 +32,11 @@ if [ -n "$WP_CLI_TEST_DBROOTPASS" ]; then
3132
PASSWORD_STRING="-p$WP_CLI_TEST_DBROOTPASS"
3233
fi
3334

35+
TEST_DB=wp_cli_test
36+
if [ -n "$WP_CLI_TEST_DBNAME" ]; then
37+
TEST_DB="$WP_CLI_TEST_DBNAME"
38+
fi
39+
3440
TEST_USER=wp_cli_test
3541
if [ -n "$WP_CLI_TEST_DBUSER" ]; then
3642
TEST_USER="$WP_CLI_TEST_DBUSER"
@@ -44,18 +50,18 @@ fi
4450
# Prepare the database for running the tests with a MySQL version 8.0 or higher.
4551
install_mysql_db_8_0_plus() {
4652
set -ex
47-
mysql -e "CREATE DATABASE IF NOT EXISTS \`wp_cli_test\`;" $HOST_STRING -u"$USER" "$PASSWORD_STRING"
48-
mysql -e "CREATE USER IF NOT EXISTS \`wp_cli_test\`@'%' IDENTIFIED WITH mysql_native_password BY '$TEST_PASSWORD'" $HOST_STRING -u"$USER" "$PASSWORD_STRING"
49-
mysql -e "GRANT ALL PRIVILEGES ON \`wp_cli_test\`.* TO '$TEST_USER'@'%'" $HOST_STRING -u"$USER" "$PASSWORD_STRING"
50-
mysql -e "GRANT ALL PRIVILEGES ON \`wp_cli_test_scaffold\`.* TO '$TEST_USER'@'%'" $HOST_STRING -u"$USER" "$PASSWORD_STRING"
53+
mysql -e "CREATE DATABASE IF NOT EXISTS \`$TEST_DB\`;" $HOST_STRING -u"$USER" "$PASSWORD_STRING"
54+
mysql -e "CREATE USER IF NOT EXISTS \`$TEST_DB\`@'%' IDENTIFIED WITH mysql_native_password BY '$TEST_PASSWORD'" $HOST_STRING -u"$USER" "$PASSWORD_STRING"
55+
mysql -e "GRANT ALL PRIVILEGES ON \`$TEST_DB\`.* TO '$TEST_USER'@'%'" $HOST_STRING -u"$USER" "$PASSWORD_STRING"
56+
mysql -e "GRANT ALL PRIVILEGES ON \`$TEST_DB_scaffold\`.* TO '$TEST_USER'@'%'" $HOST_STRING -u"$USER" "$PASSWORD_STRING"
5157
}
5258

5359
# Prepare the database for running the tests with a MySQL version lower than 8.0.
5460
install_mysql_db_lower_than_8_0() {
5561
set -ex
56-
mysql -e "CREATE DATABASE IF NOT EXISTS \`wp_cli_test\`;" $HOST_STRING -u"$USER" "$PASSWORD_STRING"
57-
mysql -e "GRANT ALL ON \`wp_cli_test\`.* TO '$TEST_USER'@'%' IDENTIFIED BY '$TEST_PASSWORD'" $HOST_STRING -u"$USER" "$PASSWORD_STRING"
58-
mysql -e "GRANT ALL ON \`wp_cli_test_scaffold\`.* TO '$TEST_USER'@'%' IDENTIFIED BY '$TEST_PASSWORD'" $HOST_STRING -u"$USER" "$PASSWORD_STRING"
62+
mysql -e "CREATE DATABASE IF NOT EXISTS \`$TEST_DB\`;" $HOST_STRING -u"$USER" "$PASSWORD_STRING"
63+
mysql -e "GRANT ALL ON \`$TEST_DB\`.* TO '$TEST_USER'@'%' IDENTIFIED BY '$TEST_PASSWORD'" $HOST_STRING -u"$USER" "$PASSWORD_STRING"
64+
mysql -e "GRANT ALL ON \`$TEST_DB_scaffold\`.* TO '$TEST_USER'@'%' IDENTIFIED BY '$TEST_PASSWORD'" $HOST_STRING -u"$USER" "$PASSWORD_STRING"
5965
}
6066

6167
VERSION_STRING=$(mysql -e "SELECT VERSION()" --skip-column-names $HOST_STRING -u"$USER" "$PASSWORD_STRING")

src/Context/FeatureContext.php

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,24 @@ class FeatureContext implements SnippetAcceptingContext {
5151
/**
5252
* The test database settings. All but `dbname` can be set via environment variables. The database is dropped at the start of each scenario and created on a "Given a WP installation" step.
5353
*/
54-
private static $db_settings = array(
54+
private static $db_settings = [
5555
'dbname' => 'wp_cli_test',
5656
'dbuser' => 'wp_cli_test',
5757
'dbpass' => 'password1',
5858
'dbhost' => '127.0.0.1',
59-
);
59+
];
6060

6161
/**
6262
* Array of background process ids started by the current scenario. Used to terminate them at the end of the scenario.
6363
*/
64-
private $running_procs = array();
64+
private $running_procs = [];
6565

6666
/**
6767
* Array of variables available as {VARIABLE_NAME}. Some are always set: CORE_CONFIG_SETTINGS, DB_USER, DB_PASSWORD, DB_HOST, SRC_DIR, CACHE_DIR, WP_VERSION-version-latest.
6868
* Some are step-dependent: RUN_DIR, SUITE_CACHE_DIR, COMPOSER_LOCAL_REPOSITORY, PHAR_PATH. One is set on use: INVOKE_WP_CLI_WITH_PHP_ARGS-args.
6969
* Scenarios can define their own variables using "Given save" steps. Variables are reset for each scenario.
7070
*/
71-
public $variables = array();
71+
public $variables = [];
7272

7373
/**
7474
* The current feature file and scenario line number as '<file>.<line>'. Used in RUN_DIR and SUITE_CACHE_DIR directory names. Set at the start of each scenario.
@@ -84,9 +84,9 @@ class FeatureContext implements SnippetAcceptingContext {
8484
private static $num_top_processes; // Number of processes/methods to output by longest run times. Set on `@BeforeSuite`.
8585
private static $num_top_scenarios; // Number of scenarios to output by longest run times. Set on `@BeforeSuite`.
8686

87-
private static $scenario_run_times = array(); // Scenario run times (top `self::$num_top_scenarios` only).
87+
private static $scenario_run_times = []; // Scenario run times (top `self::$num_top_scenarios` only).
8888
private static $scenario_count = 0; // Scenario count, incremented on `@AfterScenario`.
89-
private static $proc_method_run_times = array(); // Array of run time info for proc methods, keyed by method name and arg, each a 2-element array containing run time and run count.
89+
private static $proc_method_run_times = []; // Array of run time info for proc methods, keyed by method name and arg, each a 2-element array containing run time and run count.
9090

9191
/**
9292
* Get the path to the Composer vendor folder.
@@ -211,11 +211,11 @@ private static function get_process_env_variables() {
211211
}
212212

213213
$path_separator = Utils\is_windows() ? ';' : ':';
214-
$env = array(
214+
$env = [
215215
'PATH' => $bin_path . $path_separator . getenv( 'PATH' ),
216216
'BEHAT_RUN' => 1,
217217
'HOME' => sys_get_temp_dir() . '/wp-cli-home',
218-
);
218+
];
219219

220220
$config_path = getenv( 'WP_CLI_CONFIG_PATH' );
221221
if ( false !== $config_path ) {
@@ -490,16 +490,28 @@ public function __construct() {
490490
$this->variables['DB_ROOT_PASSWORD'] = getenv( 'WP_CLI_TEST_DBROOTPASS' );
491491
}
492492

493+
if ( getenv( 'WP_CLI_TEST_DBNAME' ) ) {
494+
$this->variables['DB_NAME'] = getenv( 'WP_CLI_TEST_DBNAME' );
495+
} else {
496+
$this->variables['DB_NAME'] = 'wp_cli_test';
497+
}
498+
493499
if ( getenv( 'WP_CLI_TEST_DBUSER' ) ) {
494500
$this->variables['DB_USER'] = getenv( 'WP_CLI_TEST_DBUSER' );
501+
} else {
502+
$this->variables['DB_USER'] = 'wp_cli_test';
495503
}
496504

497505
if ( false !== getenv( 'WP_CLI_TEST_DBPASS' ) ) {
498506
$this->variables['DB_PASSWORD'] = getenv( 'WP_CLI_TEST_DBPASS' );
507+
} else {
508+
$this->variables['DB_PASSWORD'] = 'password1';
499509
}
500510

501511
if ( getenv( 'WP_CLI_TEST_DBHOST' ) ) {
502512
$this->variables['DB_HOST'] = getenv( 'WP_CLI_TEST_DBHOST' );
513+
} else {
514+
$this->variables['DB_HOST'] = 'localhost';
503515
}
504516

505517
if ( getenv( 'MYSQL_TCP_PORT' ) ) {
@@ -510,17 +522,10 @@ public function __construct() {
510522
$this->variables['MYSQL_HOST'] = getenv( 'MYSQL_HOST' );
511523
}
512524

513-
self::$db_settings['dbuser'] = array_key_exists( 'DB_USER', $this->variables )
514-
? $this->variables['DB_USER']
515-
: 'wp_cli_test';
516-
517-
self::$db_settings['dbpass'] = array_key_exists( 'DB_PASSWORD', $this->variables )
518-
? $this->variables['DB_PASSWORD']
519-
: 'password1';
520-
521-
self::$db_settings['dbhost'] = array_key_exists( 'DB_HOST', $this->variables )
522-
? $this->variables['DB_HOST']
523-
: 'localhost';
525+
self::$db_settings['dbname'] = $this->variables['DB_NAME'];
526+
self::$db_settings['dbuser'] = $this->variables['DB_USER'];
527+
self::$db_settings['dbpass'] = $this->variables['DB_PASSWORD'];
528+
self::$db_settings['dbhost'] = $this->variables['DB_HOST'];
524529

525530
$this->variables['CORE_CONFIG_SETTINGS'] = Utils\assoc_args_to_str( self::$db_settings );
526531

@@ -536,7 +541,7 @@ public function replace_variables( $str ) {
536541
if ( false !== strpos( $str, '{INVOKE_WP_CLI_WITH_PHP_ARGS-' ) ) {
537542
$str = $this->replace_invoke_wp_cli_with_php_args( $str );
538543
}
539-
$str = preg_replace_callback( '/\{([A-Z_][A-Z_0-9]*)\}/', array( $this, 'replace_var' ), $str );
544+
$str = preg_replace_callback( '/\{([A-Z_][A-Z_0-9]*)\}/', [ $this, 'replace_var' ], $str );
540545
if ( false !== strpos( $str, '{WP_VERSION-' ) ) {
541546
$str = $this->replace_wp_versions( $str );
542547
}
@@ -600,9 +605,9 @@ private function replace_var( $matches ) {
600605
private function replace_wp_versions( $str ) {
601606
static $wp_versions = null;
602607
if ( null === $wp_versions ) {
603-
$wp_versions = array();
608+
$wp_versions = [];
604609

605-
$response = Requests::get( 'https://api.wordpress.org/core/version-check/1.7/', null, array( 'timeout' => 30 ) );
610+
$response = Requests::get( 'https://api.wordpress.org/core/version-check/1.7/', null, [ 'timeout' => 30 ] );
606611
if ( 200 === $response->status_code ) {
607612
$body = json_decode( $response->body );
608613
if ( is_object( $body ) && isset( $body->offers ) && is_array( $body->offers ) ) {
@@ -724,12 +729,12 @@ private function set_cache_dir() {
724729
* @param array $assoc_args Optional. Associative array of options. Default empty.
725730
* @param bool $add_database Optional. Whether to add dbname to the $sql_cmd. Default false.
726731
*/
727-
private static function run_sql( $sql_cmd, $assoc_args = array(), $add_database = false ) {
728-
$default_assoc_args = array(
732+
private static function run_sql( $sql_cmd, $assoc_args = [], $add_database = false ) {
733+
$default_assoc_args = [
729734
'host' => self::$db_settings['dbhost'],
730735
'user' => self::$db_settings['dbuser'],
731736
'pass' => self::$db_settings['dbpass'],
732-
);
737+
];
733738
if ( $add_database ) {
734739
$sql_cmd .= ' ' . escapeshellarg( self::$db_settings['dbname'] );
735740
}
@@ -742,15 +747,15 @@ private static function run_sql( $sql_cmd, $assoc_args = array(), $add_database
742747

743748
public function create_db() {
744749
$dbname = self::$db_settings['dbname'];
745-
self::run_sql( 'mysql --no-defaults', array( 'execute' => "CREATE DATABASE IF NOT EXISTS $dbname" ) );
750+
self::run_sql( 'mysql --no-defaults', [ 'execute' => "CREATE DATABASE IF NOT EXISTS $dbname" ] );
746751
}
747752

748753
public function drop_db() {
749754
$dbname = self::$db_settings['dbname'];
750-
self::run_sql( 'mysql --no-defaults', array( 'execute' => "DROP DATABASE IF EXISTS $dbname" ) );
755+
self::run_sql( 'mysql --no-defaults', [ 'execute' => "DROP DATABASE IF EXISTS $dbname" ] );
751756
}
752757

753-
public function proc( $command, $assoc_args = array(), $path = '' ) {
758+
public function proc( $command, $assoc_args = [], $path = '' ) {
754759
if ( ! empty( $assoc_args ) ) {
755760
$command .= Utils\assoc_args_to_str( $assoc_args );
756761
}
@@ -773,11 +778,11 @@ public function proc( $command, $assoc_args = array(), $path = '' ) {
773778
* Start a background process. Will automatically be closed when the tests finish.
774779
*/
775780
public function background_proc( $cmd ) {
776-
$descriptors = array(
781+
$descriptors = [
777782
0 => STDIN,
778-
1 => array( 'pipe', 'w' ),
779-
2 => array( 'pipe', 'w' ),
780-
);
783+
1 => [ 'pipe', 'w' ],
784+
2 => [ 'pipe', 'w' ],
785+
];
781786

782787
$proc = proc_open( $cmd, $descriptors, $pipes, $this->variables['RUN_DIR'], self::get_process_env_variables() );
783788

@@ -883,14 +888,14 @@ public function install_wp( $subdir = '' ) {
883888
$this->download_wp( $subdir );
884889
$this->create_config( $subdir, $config_extra_php );
885890

886-
$install_args = array(
891+
$install_args = [
887892
'url' => 'http://example.com',
888893
'title' => 'WP CLI Site',
889894
'admin_user' => 'admin',
890895
'admin_email' => '[email protected]',
891896
'admin_password' => 'password1',
892897
'skip-email' => true,
893-
);
898+
];
894899

895900
$install_cache_path = '';
896901
if ( self::$install_cache_dir ) {
@@ -900,7 +905,7 @@ public function install_wp( $subdir = '' ) {
900905

901906
if ( $install_cache_path && file_exists( $install_cache_path ) ) {
902907
self::copy_dir( $install_cache_path, $run_dir );
903-
self::run_sql( 'mysql --no-defaults', array( 'execute' => "source {$install_cache_path}.sql" ), true /*add_database*/ );
908+
self::run_sql( 'mysql --no-defaults', [ 'execute' => "source {$install_cache_path}.sql" ], true /*add_database*/ );
904909
} else {
905910
$this->proc( 'wp core install', $install_args, $subdir )->run_check();
906911
if ( $install_cache_path ) {
@@ -914,7 +919,7 @@ public function install_wp( $subdir = '' ) {
914919
$command .= ' --skip-column-statistics';
915920
}
916921

917-
self::run_sql( $command, array( 'result-file' => "{$install_cache_path}.sql" ), true /*add_database*/ );
922+
self::run_sql( $command, [ 'result-file' => "{$install_cache_path}.sql" ], true /*add_database*/ );
918923
}
919924
}
920925
}
@@ -938,14 +943,14 @@ public function install_wp_with_composer( $vendor_directory = 'vendor' ) {
938943

939944
$this->create_config( 'WordPress', $config_extra_php );
940945

941-
$install_args = array(
946+
$install_args = [
942947
'url' => 'http://localhost:8080',
943948
'title' => 'WP CLI Site with both WordPress and wp-cli as Composer dependencies',
944949
'admin_user' => 'admin',
945950
'admin_email' => '[email protected]',
946951
'admin_password' => 'password1',
947952
'skip-email' => true,
948-
);
953+
];
949954

950955
$this->proc( 'wp core install', $install_args )->run_check();
951956
}
@@ -1065,7 +1070,7 @@ private static function dir_diff_copy( $upd_dir, $src_dir, $cop_dir ) {
10651070
$error = error_get_last();
10661071
throw new RuntimeException( sprintf( "Failed to open updated directory '%s': %s. " . __FILE__ . ':' . __LINE__, $upd_dir, $error['message'] ) );
10671072
}
1068-
foreach ( array_diff( $files, array( '.', '..' ) ) as $file ) {
1073+
foreach ( array_diff( $files, [ '.', '..' ] ) as $file ) {
10691074
$upd_file = $upd_dir . '/' . $file;
10701075
$src_file = $src_dir . '/' . $file;
10711076
$cop_file = $cop_dir . '/' . $file;
@@ -1129,10 +1134,10 @@ private static function log_run_times_after_suite( AfterSuiteScope $scope ) {
11291134
// Process and proc method run times.
11301135
$run_times = array_merge( Process::$run_times, self::$proc_method_run_times );
11311136
$reduce_callback = function ( $carry, $item ) {
1132-
return array( $carry[0] + $item[0], $carry[1] + $item[1] );
1137+
return [ $carry[0] + $item[0], $carry[1] + $item[1] ];
11331138
};
11341139

1135-
list( $ptime, $calls ) = array_reduce( $run_times, $reduce_callback, array( 0, 0 ) );
1140+
list( $ptime, $calls ) = array_reduce( $run_times, $reduce_callback, [ 0, 0 ] );
11361141

11371142
$overhead = $time - $ptime;
11381143
$pct = round( ( $overhead / $time ) * 100 );
@@ -1208,7 +1213,7 @@ private static function log_run_times_after_suite( AfterSuiteScope $scope ) {
12081213
private static function log_proc_method_run_time( $key, $start_time ) {
12091214
$run_time = microtime( true ) - $start_time;
12101215
if ( ! isset( self::$proc_method_run_times[ $key ] ) ) {
1211-
self::$proc_method_run_times[ $key ] = array( 0, 0 );
1216+
self::$proc_method_run_times[ $key ] = [ 0, 0 ];
12121217
}
12131218
self::$proc_method_run_times[ $key ][0] += $run_time;
12141219
self::$proc_method_run_times[ $key ][1]++;

0 commit comments

Comments
 (0)