Skip to content

Commit

Permalink
Add support for using SQLite in tests (opt-in) (#393)
Browse files Browse the repository at this point in the history
* Add support for sqlite drop-in

* Finish support for sqlite dropin

* Remove the temp branch name

* Dont use sqlite with the framework

* Testing CI

* Exclude db.php from rsync
  • Loading branch information
srtfisher committed May 31, 2023
1 parent 9755d09 commit 584d284
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
20 changes: 20 additions & 0 deletions src/mantle/testing/class-installation-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ public function theme( string $theme ) {
return $this->loaded( fn () => switch_theme( $theme ) );
}

/**
* Alias for `theme()`.
*
* @param string $theme Theme name.
* @return static
*/
public function with_theme( string $theme ) {
return $this->theme( $theme );
}

/**
* Define the active plugins to be set after the installation is loaded.
*
Expand All @@ -129,6 +139,16 @@ public function plugins( array $plugins ) {
return $this->loaded( fn () => update_option( 'active_plugins', $plugins ) );
}

/**
* Alias for `plugins()`.
*
* @param array<int, string> $plugins Plugin files.
* @return static
*/
public function with_plugins( array $plugins ) {
return $this->plugins( $plugins );
}

/**
* Install the Mantle Testing Framework.
*
Expand Down
15 changes: 11 additions & 4 deletions src/mantle/testing/class-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,16 +275,23 @@ public static function shell_safe( string|bool $string ): string {
* not install the WordPress database.
*
* @param string $directory Directory to install WordPress in.
* @param bool $install_vip_mu_plugins Whether to install VIP MU plugins.
* @param bool $install_object_cache Whether to install the object cache drop-in.
* @param bool $install_vip_mu_plugins Whether to install VIP MU plugins, defaults to false.
* @param bool $install_object_cache Whether to install the object cache drop-in, defaults to false.
* @param bool $use_sqlite_db Whether to use SQLite for the database, defaults to false.
*/
public static function install_wordpress( string $directory, bool $install_vip_mu_plugins = false, bool $install_object_cache = false ) {
public static function install_wordpress(
string $directory,
bool $install_vip_mu_plugins = false,
bool $install_object_cache = false,
bool $use_sqlite_db = false,
): void {
$branch = static::env( 'MANTLE_CI_BRANCH', 'HEAD' );

$command = sprintf(
'export WP_CORE_DIR=%s WP_MULTISITE=%s INSTALL_WP_TEST_DEBUG=%s && curl -s %s | bash -s %s',
'export WP_CORE_DIR=%s WP_MULTISITE=%s WP_USE_SQLITE=%s INSTALL_WP_TEST_DEBUG=%s && curl -s %s | bash -s %s',
$directory,
static::shell_safe( static::env( 'WP_MULTISITE', '0' ) ),
static::shell_safe( $use_sqlite_db ),
static::shell_safe( static::is_debug_mode() ),
"https://raw.githubusercontent.com/alleyinteractive/mantle-ci/{$branch}/install-wp-tests.sh",
collect(
Expand Down
29 changes: 29 additions & 0 deletions src/mantle/testing/concerns/trait-rsync-installation.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ trait Rsync_Installation {
*/
protected bool $install_object_cache = false;

/**
* Flag to use a SQLite db.php drop-in when rsyncing the codebase.
*
* @var boolean
*/
protected bool $use_sqlite_db = false;

/**
* Exclusions to be used when rsyncing the codebase.
*
Expand Down Expand Up @@ -197,6 +204,27 @@ public function with_object_cache( bool $install = true ): static {
return $this;
}

/**
* Install SQLite db.php drop-in into the codebase.
*
* This will only be applied if the codebase is being rsync-ed to a WordPress
* installation.
*
* @param bool $install Install the SQLite db.php drop-in into the codebase.
* @return static
*/
public function with_sqlite( bool $install = true ): static {
if ( $this->is_within_wordpress_install() ) {
return $this;
}

$this->rsync_exclusions[] = 'db.php';

$this->use_sqlite_db = $install;

return $this;
}

/**
* Maybe rsync the codebase as a plugin within WordPress.
*
Expand Down Expand Up @@ -306,6 +334,7 @@ protected function perform_rsync_testsuite() {
directory: $base_install_path,
install_vip_mu_plugins: $this->install_vip_mu_plugins,
install_object_cache: $this->install_object_cache,
use_sqlite_db: $this->use_sqlite_db,
);

Utils::success(
Expand Down

0 comments on commit 584d284

Please sign in to comment.