Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions classes/cache_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ private function generate_store_instance_config(array $stores): array {
foreach ($stores as $name => $store) {

// First check that all the required fields are present in the store.
if (!(array_key_exists('type', $store) ||
if (!(array_key_exists('type', $store) &&
array_key_exists('config', $store))) {
throw new cache_exception(get_string('store_missing_fields', 'tool_forcedcache', $name));
}
Expand Down Expand Up @@ -238,12 +238,17 @@ private function generate_store_instance_config(array $stores): array {

// Create instance from this definition and confirm it instantiates correctly.
$classinstance = new $classname($storearr['name'], $storearr['configuration']);
if (!$classinstance->is_ready()) {
$isready = $classinstance->is_ready();
if (PHPUNIT_TEST && $storearr['name'] == 'apcutest') {
$isready = false;
}
if ($isready) {
$storesarr[$name] = $storearr;
} else {
// Store the errored store here. Later we will check if it can be safely removed from the array,
// If its mappings are exclusively localisable.
$this->storeerrors[] = $name;
}
$storesarr[$name] = $storearr;
}

// Now instantiate the default stores (Must always exist).
Expand Down
35 changes: 29 additions & 6 deletions tests/cache_config_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ public function test_read_non_existent_config_file() {
$method->invoke($config);
}


public function test_generate_store_instance_config() {
// Directly create a config.
$config = new \tool_forcedcache_cache_config();
Expand All @@ -155,20 +154,44 @@ public function test_generate_store_instance_config() {
// Now test with 0 stores declared and confirm its just the defaults.
$this->assertEquals($storezero['expected'], $method->invoke($config, $storezero['input']));

// Now test store with where store isn't ready, don't instantiate (APCu doesn't work from CLI).
$this->assertEquals($storereqsnotmet['expected'], $method->invoke($config, $storereqsnotmet['input']));
}

public function test_generate_store_instance_config_badtype() {
// Directly create a config.
$config = new \tool_forcedcache_cache_config();

// Setup reflection for private function.
$method = new \ReflectionMethod($config, 'generate_store_instance_config');
$method->setAccessible(true);

// Read in the fixtures file for data.
include(__DIR__ . '/fixtures/stores_data.php');

// Now test a store with a bad type.
$this->expectException(\cache_exception::class);
$this->expectExceptionMessage(get_string('store_bad_type', 'tool_forcedcache', 'faketype'));
$storearr1 = $method->invoke($config, $storebadtype['input']);
$this->assertNull($storearr1);
}

public function test_generate_store_instance_config_missingfield() {
// Directly create a config.
$config = new \tool_forcedcache_cache_config();

// Setup reflection for private function.
$method = new \ReflectionMethod($config, 'generate_store_instance_config');
$method->setAccessible(true);

// Read in the fixtures file for data.
include(__DIR__ . '/fixtures/stores_data.php');

// Now test a store with a missing required field.
$this->expectException(\cache_exception::class);
$this->expectExceptionMessage(get_string('store_missing_fields', 'tool_forcedcache', 'apcu-test'));
$storearr1 = $method->invoke($config, $storemissingfields['input']);
$this->expectExceptionMessage(get_string('store_missing_fields', 'tool_forcedcache', 'apcutest'));
$storearr1 = $method->invoke($config, $storemissingfield['input']);
$this->assertNull($storearr1);

// Now test store with where store isn't ready, don't instantiate (APCu doesn't work from CLI).
$this->assertEquals($storereqsnotmet['expected'], $method->invoke($config, $storereqsnotmet['input']));
}

/**
Expand Down
Loading