Skip to content

Commit

Permalink
Fix unit tests to pass sanity checks. Add PHPDocs
Browse files Browse the repository at this point in the history
The sanity checks require that assertEquals(), assertNull() are not called after expectException().
Also, there should be only one expectException() call per test.
  • Loading branch information
katerynadegtyariova committed Mar 10, 2022
1 parent 9ac8357 commit 4faece5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
42 changes: 30 additions & 12 deletions tests/cache_config_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@

namespace tool_forcedcache\tests;

/**
* Tests for forced cache configuration.
*
* @package tool_forcedcache
* @author Peter Burnett <[email protected]>
* @copyright Catalyst IT
*/
class tool_forcedcache_cache_config_testcase extends \advanced_testcase {

/**
Expand All @@ -41,6 +48,9 @@ public function copy_to_valid_config_location(string $source): string {
return realpath($dest);
}

/**
* Tests reading config file from invalid path.
*/
public function test_read_config_file_from_invalid_path() {
global $CFG;
$this->resetAfterTest(true);
Expand All @@ -66,6 +76,9 @@ public function test_read_config_file_from_invalid_path() {
$method->invoke($config);
}

/**
* Tests reading invalid config file.
*/
public function test_read_valid_config_file() {
global $CFG;
$this->resetAfterTest(true);
Expand All @@ -90,6 +103,9 @@ public function test_read_valid_config_file() {
$this->assertArrayHasKey('definitionoverrides', $configarr1);
}

/**
* Tests reading garbled config file.
*/
public function test_read_garbled_config_file() {
global $CFG;
$this->resetAfterTest(true);
Expand All @@ -112,6 +128,9 @@ public function test_read_garbled_config_file() {
$method->invoke($config);
}

/**
* Tests reading non-existing config file.
*/
public function test_read_non_existent_config_file() {
global $CFG;
$this->resetAfterTest(true);
Expand All @@ -134,7 +153,9 @@ public function test_read_non_existent_config_file() {
$method->invoke($config);
}


/**
* Tests bad type of a generated store instance.
*/
public function test_generate_store_instance_config() {
// Directly create a config.
$config = new \tool_forcedcache_cache_config();
Expand All @@ -155,20 +176,13 @@ 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']));

// 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);

// 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->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 Expand Up @@ -196,10 +210,11 @@ public function test_generated_mode_mappings_for_definitionmatchtopruleset() {
include(__DIR__ . '/fixtures/mode_mappings_data.php');
include(__DIR__ . '/fixtures/definition_mappings_data.php');

$expected = $generatedmodemappingagainstdefinitionmatchtoprulesetexpected;
$expected = $generatedmodemapping;
$rules = $definitionmatchtopruleset['rules'];
$this->assertEquals($expected, $method->invoke($config, $rules));
}

/**
* Tests output of mode mpapings once rules included in the mix.
*/
Expand All @@ -217,6 +232,9 @@ public function test_generated_mode_mappings_for_definitionnoruleset() {
$this->assertEquals($defaultsexpected, $method->invoke($config, $rules));
}

/**
* Tests definition mappings generated from rules.
*/
public function test_generate_definition_mappings_from_rules() {
$config = new \tool_forcedcache_cache_config();

Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/mode_mappings_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
)
);

$generatedmodemappingagainstdefinitionmatchtoprulesetexpected = array(
$generatedmodemapping = array(
array(
'mode' => cache_store::MODE_APPLICATION,
'store' => 'file-test',
Expand Down
17 changes: 16 additions & 1 deletion tests/fixtures/stores_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,21 @@
'default' => true,
'class' => 'cachestore_static',
'lock' => 'cachelock_file_default',
)
),
'apcutest' =>
array (
'name' => 'apcutest',
'plugin' => 'apcu',
'configuration' =>
array (
'prefix' => 'test_',
),
'class' => 'cachestore_apcu',
'features' => 4,
'modes' => 3,
'default' => false,
'mappingsonly' => false,
'lock' => 'cachelock_file_default',
),
)
);

0 comments on commit 4faece5

Please sign in to comment.