MDL-45513 cache: added support to unit tests for alt cache stores

This commit is contained in:
Sam Hemelryk
2014-05-30 08:41:07 +12:00
parent 8e478c99d3
commit 79a8ea653c
7 changed files with 91 additions and 9 deletions
+4 -3
View File
@@ -100,8 +100,8 @@ class cache_config {
* @return bool True if it exists
*/
public static function config_file_exists() {
// Allow for late static binding.
return file_exists(self::get_config_file_path());
// Allow for late static binding by using static.
return file_exists(static::get_config_file_path());
}
/**
@@ -324,7 +324,8 @@ class cache_config {
*/
protected function include_configuration() {
$configuration = array();
$cachefile = self::get_config_file_path();
// We need to allow for late static bindings to allow for class path mudling happending for unit tests.
$cachefile = static::get_config_file_path();
if (!file_exists($cachefile)) {
throw new cache_exception('Default cache config could not be found. It should have already been created by now.');
+14 -3
View File
@@ -325,11 +325,21 @@ class cache_factory {
public function create_config_instance($writer = false) {
global $CFG;
// Check if we need to create a config file with defaults.
$needtocreate = !cache_config::config_file_exists();
// The class to use.
$class = 'cache_config';
// Check if this is a PHPUnit test and redirect to the phpunit config classes if it is.
if (defined('PHPUNIT_TEST') && PHPUNIT_TEST) {
require_once($CFG->dirroot.'/cache/locallib.php');
require_once($CFG->dirroot.'/cache/tests/fixtures/lib.php');
// We have just a single class for PHP unit tests. We don't care enough about its
// performance to do otherwise and having a single method allows us to inject things into it
// while testing.
$class = 'cache_config_phpunittest';
}
// Check if we need to create a config file with defaults.
$needtocreate = !$class::config_file_exists();
if ($writer || $needtocreate) {
require_once($CFG->dirroot.'/cache/locallib.php');
$class .= '_writer';
@@ -350,6 +360,7 @@ class cache_factory {
// Create the default configuration.
// Update the state, we are now initialising the cache.
self::set_state(self::STATE_INITIALISING);
/** @var cache_config_writer $class */
$configuration = $class::create_default_configuration();
if ($configuration !== true) {
// Failed to create the default configuration. Disable the cache stores and update the state.