Skip to content

Commit

Permalink
Adding support for installing redis object cache (#550)
Browse files Browse the repository at this point in the history
* Adding support for installing redis object cache

* Changelog
  • Loading branch information
srtfisher committed May 10, 2024
1 parent 22e0d5b commit 03836b4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Added a `classname`/`the_classname` helper to generate complex class names.
- Added support for installing the Redis `object-cache.php` drop-in during
testing with `with_object_cache()`.

## Fixed

Expand Down
2 changes: 2 additions & 0 deletions src/mantle/testing/class-installation-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public function __construct() {

if ( Utils::env_bool( 'MANTLE_INSTALL_OBJECT_CACHE', false ) ) {
$this->with_object_cache();
} elseif ( $object_cache = Utils::env( 'MANTLE_INSTALL_OBJECT_CACHE', false ) ) {
$this->with_object_cache( $object_cache );
}

if ( Utils::env_bool( 'MANTLE_USE_SQLITE', false ) ) {
Expand Down
16 changes: 14 additions & 2 deletions src/mantle/testing/class-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,21 @@ public static function shell_safe( string|bool $string ): string {
*/
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 );

// Handle the legacy values for MANTLE_INSTALL_OBJECT_CACHE.
if ( static::env_bool( 'MANTLE_INSTALL_OBJECT_CACHE', false ) ) {
$install_object_cache = 'memcached';
} else {
$install_object_cache = static::env( 'MANTLE_INSTALL_OBJECT_CACHE', false );

if ( $install_object_cache && ! in_array( $install_object_cache, [ 'memcached', 'redis' ], true ) ) {
static::error( '🚨 Invalid value for MANTLE_INSTALL_OBJECT_CACHE (' . $install_object_cache . '). Ignoring...' );

$install_object_cache = false;
}
}

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

// Compile the variables to pass to the shell script.
Expand Down Expand Up @@ -334,7 +346,7 @@ public static function install_wordpress( string $directory ): void {
static::shell_safe( static::env( 'WP_VERSION', 'latest' ) ),
static::shell_safe( static::env( 'WP_SKIP_DB_CREATE', 'false' ) ),
static::shell_safe( $install_vip_mu_plugins ? 'true' : 'false' ),
static::shell_safe( $install_object_cache ? 'true' : 'false' ),
static::shell_safe( $install_object_cache ),
]
)->implode( ' ' ),
);
Expand Down
32 changes: 25 additions & 7 deletions src/mantle/testing/concerns/trait-rsync-installation.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function with_default_exclusions(): static {
'.phpunit.result.cache',
'node_modules',
'phpstan.neon',
]
]
);
}

Expand Down Expand Up @@ -173,23 +173,41 @@ public function with_vip_mu_plugins( bool $install = true ): static {
* Will only be applied if the codebase is not already within a WordPress and
* is being rsync-ed to one.
*
* @param bool $install Install the object cache drop-in into the codebase.
* @param bool|string $install The object cache provider to install (redis/memcached)
* or true to install the default Memcached object cache (legacy).
*/
public function with_object_cache( bool $install = true ): static {
public function with_object_cache( bool|string $install = true ): static {
if ( $this->is_within_wordpress_install() ) {
return $this;
}

// Check if Memcached is installed.
if ( ! class_exists( \Memcached::class ) && ! Utils::env( 'MANTLE_REQUIRE_OBJECT_CACHE', false ) ) {
Utils::error( 'Memcached is not installed. Cannot install object cache. Skipping...' );
// Allow object cache to be disabled.
if ( ! $install ) {
putenv( 'MANTLE_INSTALL_OBJECT_CACHE=' );

return $this;
} elseif ( true === $install ) {
$install = 'memcached';
}

if ( ! in_array( $install, [ 'redis', 'memcached' ], true ) ) {
Utils::error( 'Invalid object cache provider. Must be either "redis" or "memcached". Skipping...' );

return $this;
}

if ( 'memcached' === $install ) {
// Check if Memcached is installed before proceeding.
if ( ! class_exists( \Memcached::class ) && ! Utils::env( 'MANTLE_REQUIRE_OBJECT_CACHE', false ) ) {
Utils::error( 'Memcached is not installed. Cannot install object cache. Skipping...' );

return $this;
}
}

$this->add_exclusion( 'object-cache.php' );

putenv( 'MANTLE_INSTALL_OBJECT_CACHE=' . ( $install ? '1' : '0' ) );
putenv( 'MANTLE_INSTALL_OBJECT_CACHE=' . $install );

return $this;
}
Expand Down

0 comments on commit 03836b4

Please sign in to comment.