Skip to content

Commit

Permalink
Switch to environmental variables for installing SQLite/mu-plugins/ob…
Browse files Browse the repository at this point in the history
…ject cache (#498)

* Use env variables to set the flags to install sqlite/object cache/mu-plugins. Ensure that standalone plugins can use sqlite without any database.

* Update src/mantle/testing/class-utils.php

Co-authored-by: Damian <4309872+attackant@users.noreply.github.com>

---------

Co-authored-by: Damian <4309872+attackant@users.noreply.github.com>
  • Loading branch information
srtfisher and attackant committed Feb 21, 2024
1 parent 4b5aff2 commit 144df8b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 41 deletions.
27 changes: 18 additions & 9 deletions src/mantle/testing/class-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,19 @@ public static function env( string $variable, $default ) {
return false === $value ? $default : $value;
}

/**
* Retrieve an environment variable and check if it is truthy.
*
* @param string $variable Variable to get.
* @param bool $default Default value used as a fallback.
* @return bool
*/
public static function env_bool( string $variable, bool $default ): bool {
$value = static::env( $variable, $default );

return in_array( strtolower( $value ), [ 'true', '1', 'yes' ], true );
}

/**
* Ensure an environment variable is is a valid string that can be passed to
* to a shell script.
Expand All @@ -278,16 +291,12 @@ 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, 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,
bool $use_sqlite_db = false,
): void {
public static function install_wordpress( string $directory ): void {
$install_vip_mu_plugins = static::env_bool( 'MANTLE_INSTALL_VIP_MU_PLUGINS', false );
$install_object_cache = static::env_bool( 'MANTLE_INSTALL_OBJECT_CACHE', false );
$use_sqlite_db = static::env_bool( 'MANTLE_USE_SQLITE', false );

$branch = static::env( 'MANTLE_CI_BRANCH', 'HEAD' );

// Compile the variables to pass to the shell script.
Expand Down
39 changes: 7 additions & 32 deletions src/mantle/testing/concerns/trait-rsync-installation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Rsync_Installation trait file
*
* phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound
* phpcs:disable WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_putenv
*
* @package Mantle
*/
Expand Down Expand Up @@ -53,27 +54,6 @@ trait Rsync_Installation {
*/
protected ?string $rsync_subdir = '';

/**
* Flag to install the VIP MU plugins.
*
* @var boolean
*/
protected bool $install_vip_mu_plugins = false;

/**
* Flag to install a Memcache object cache drop-in.
*
* @var boolean
*/
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;

/**
* Plugin slugs or URLs to ZIP files to install after rsyncing the codebase.
*
Expand Down Expand Up @@ -182,7 +162,7 @@ public function with_vip_mu_plugins( bool $install = true ): static {

$this->rsync_exclusions[] = 'mu-plugins';

$this->install_vip_mu_plugins = $install;
putenv( 'MANTLE_INSTALL_VIP_MU_PLUGINS=' . ( $install ? '1' : '0' ) );

return $this;
}
Expand Down Expand Up @@ -210,7 +190,7 @@ public function with_object_cache( bool $install = true ): static {

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

$this->install_object_cache = $install;
putenv( 'MANTLE_INSTALL_OBJECT_CACHE=' . ( $install ? '1' : '0' ) );

return $this;
}
Expand All @@ -229,9 +209,10 @@ public function with_sqlite( bool $install = true ): static {
return $this;
}

$this->rsync_exclusions[] = 'db.php';
putenv( 'MANTLE_USE_SQLITE=' . ( $install ? '1' : '0' ) );
putenv( 'WP_SKIP_DB_CREATE=1' );

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

return $this;
}
Expand Down Expand Up @@ -367,13 +348,7 @@ protected function perform_rsync_testsuite() {
exit( 1 );
}

Utils::install_wordpress(
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::install_wordpress( $base_install_path );
Utils::success(
"WordPress installed at <em>{$base_install_path}</em>",
'Install Rsync'
Expand Down

0 comments on commit 144df8b

Please sign in to comment.