diff --git a/classes/cache_config.php b/classes/cache_config.php index c68e7bc..b21e0a8 100644 --- a/classes/cache_config.php +++ b/classes/cache_config.php @@ -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)); } @@ -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). diff --git a/tests/cache_config_test.php b/tests/cache_config_test.php index 81e6294..068de60 100644 --- a/tests/cache_config_test.php +++ b/tests/cache_config_test.php @@ -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(); @@ -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'])); } /**