Skip to content

Commit 06295e3

Browse files
authored
Allow object-cache.php to not be being loaded locally (#617)
* Test disabling object-cache locally * Finish up disabling object cache locally * CHANGELOG
1 parent 92f299d commit 06295e3

File tree

6 files changed

+48
-6
lines changed

6 files changed

+48
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
### Added
11+
12+
- Add a `without_local_object_cache()` method to prevent the `object-cache.php` drop-in from being loaded locally.
13+
1014
### Changed
1115

1216
- Disable `spatie/once`'s cache if found during unit testing.

src/mantle/testing/class-utils.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,19 @@ public static function is_debug_mode(): bool {
437437
);
438438
}
439439

440+
/**
441+
* Check if we're running in a CI (Continuous Integration) environment.
442+
*/
443+
public static function is_ci(): bool {
444+
return (
445+
! empty( $_SERVER['GITHUB_ENV'] )
446+
|| ( ! empty( $_SERVER['CI'] ) && in_array( $_SERVER['CI'], [ 'true', '1' ], true ) )
447+
|| ! empty( $_SERVER['GITHUB_REPOSITORY_OWNER'] )
448+
|| ! empty( $_SERVER['GITHUB_WORKFLOW'] )
449+
|| ! empty( $_SERVER['GITHUB_EVENT_NAME'] )
450+
);
451+
}
452+
440453
/**
441454
* Run a system command and return the output.
442455
*

src/mantle/testing/concerns/trait-rsync-installation.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,20 @@ public function with_object_cache( bool|string $install = true ): static {
212212
return $this;
213213
}
214214

215+
/**
216+
* Skip the local object cache when running the tests.
217+
*
218+
* Useful for local development where the object cache may be causing issues.
219+
* Will not disable the object cache drop-in when run in GitHub Actions.
220+
*
221+
* @param bool $skip Whether to skip the local object cache.
222+
*/
223+
public function without_local_object_cache( bool $skip = true ): static {
224+
putenv( 'MANTLE_SKIP_LOCAL_OBJECT_CACHE=' . ( $skip ? '1' : '0' ) );
225+
226+
return $this;
227+
}
228+
215229
/**
216230
* Install SQLite db.php drop-in into the codebase.
217231
*

src/mantle/testing/install-wordpress.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php // phpcs:disable
22
/**
3-
* Installs WordPress for the purpose of the unit-tests
4-
*
5-
* @todo Reuse the init/load code in init.php
3+
* Installs WordPress for the purpose of the unit-tests.
64
*/
75

86
use Mantle\Testing\Utils;

src/mantle/testing/wordpress-bootstrap.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,20 @@
210210
remove_filter( 'pre_wp_load_alloptions', 'Automattic\\VIP\\Core\\OptionsAPI\\pre_wp_load_alloptions_protections', 999 );
211211
} );
212212

213+
// Disable the object-cache.php drop if the MANTLE_SKIP_LOCAL_OBJECT_CACHE
214+
// environment variable is set.
215+
tests_add_filter( 'enable_loading_object_cache_dropin', function ( $enable_object_cache ) {
216+
if ( Utils::env_bool( 'MANTLE_SKIP_LOCAL_OBJECT_CACHE', false ) && ! Utils::is_ci() ) {
217+
if ( Utils::is_debug_mode() ) {
218+
Utils::info( 'Skipping local object cache drop-in.' );
219+
}
220+
221+
return false;
222+
}
223+
224+
return $enable_object_cache;
225+
} );
226+
213227
// Load WordPress.
214228
require_once ABSPATH . '/wp-settings.php';
215229

@@ -223,7 +237,7 @@
223237
$_SERVER['REQUEST_TIME_FLOAT'] = (float) $_SERVER['REQUEST_TIME_FLOAT'];
224238
}
225239

226-
// Disable VIP's alloptions protections during unit testing.
240+
// Remove the disabling of VIP's alloptions protections during unit testing.
227241
remove_filter( 'pre_wp_load_alloptions', 'Automattic\\VIP\\Core\\OptionsAPI\\pre_wp_load_alloptions_protections', 999 );
228242

229243
// Delete any default posts & related data.

tests/bootstrap.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
// Enable debugging flag for local development on the testing framework.
1515
// define( 'MANTLE_TESTING_DEBUG', true );
1616

17-
( new \NunoMaduro\Collision\Provider() )->register();
18-
1917
\Mantle\Testing\manager()
2018
->maybe_rsync_plugin()
2119
->with_vip_mu_plugins()
2220
->install_plugin( 'logger', 'https://github.com/alleyinteractive/logger/archive/refs/heads/develop.zip' )
2321
->install_plugin( 'jetpack', '12.4' )
22+
->without_local_object_cache()
2423
->install();

0 commit comments

Comments
 (0)