Skip to content

Commit

Permalink
Add a db connection test before behat runs.
Browse files Browse the repository at this point in the history
  • Loading branch information
janw-me committed Jun 13, 2024
1 parent bc262e2 commit 928405c
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/Context/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Behat\Testwork\Hook\Scope\AfterSuiteScope;
use Behat\Testwork\Hook\Scope\BeforeSuiteScope;
use RuntimeException;
use WP_CLI;
use WP_CLI\Process;
use WP_CLI\Utils;

Expand Down Expand Up @@ -639,6 +640,7 @@ public function __construct() {

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

$this->test_connection();
$this->drop_db();
$this->set_cache_dir();
}
Expand Down Expand Up @@ -842,6 +844,8 @@ private function set_cache_dir() {
* @param string $sql_cmd Command to run.
* @param array $assoc_args Optional. Associative array of options. Default empty.
* @param bool $add_database Optional. Whether to add dbname to the $sql_cmd. Default false.
*
* return array.
*/
private static function run_sql( $sql_cmd, $assoc_args = [], $add_database = false ) {
$default_assoc_args = [
Expand All @@ -852,11 +856,22 @@ private static function run_sql( $sql_cmd, $assoc_args = [], $add_database = fal
if ( $add_database ) {
$sql_cmd .= ' ' . escapeshellarg( self::$db_settings['dbname'] );
}
$send_to_shell = true;
if ( isset( $assoc_args['send_to_shell'] ) ) {
$send_to_shell = (bool) $assoc_args['send_to_shell'];
unset( $assoc_args['send_to_shell'] );
}

$start_time = microtime( true );
Utils\run_mysql_command( $sql_cmd, array_merge( $assoc_args, $default_assoc_args ) );
$result = Utils\run_mysql_command( $sql_cmd, array_merge( $assoc_args, $default_assoc_args ), null, $send_to_shell );
if ( self::$log_run_times ) {
self::log_proc_method_run_time( 'run_sql ' . $sql_cmd, $start_time );
}
return [
'stdout' => $result[0],
'stderr' => $result[1],
'exit_code' => $result[2],
];
}

public function create_db() {
Expand All @@ -868,6 +883,26 @@ public function create_db() {
self::run_sql( 'mysql --no-defaults', [ 'execute' => "CREATE DATABASE IF NOT EXISTS $dbname" ] );
}

/**
* Test if the database connection is working.
*/
public function test_connection() {
$sql_result = self::run_sql(
'mysql --no-defaults',
[
'execute' => 'SELECT 1',
'send_to_shell' => false,
]
);
if ( ! empty( $sql_result['stderr'] ) ) {
# WP_CLI output functions are suppressed in behat context.
echo 'There was an error connecting to the database:' . \PHP_EOL;
echo ' ' . trim( $sql_result['stderr'] ) . \PHP_EOL;
echo 'run `composer prepare-tests` to connect to the database.' . \PHP_EOL;
die( 1 );
}
}

public function drop_db() {
if ( 'sqlite' === self::$db_type ) {
return;
Expand Down

0 comments on commit 928405c

Please sign in to comment.