MDL-36363 cache: added unit tests for muliple loaders
authorSam Hemelryk <sam@moodle.com>
Wed, 30 Jan 2013 23:16:08 +0000 (12:16 +1300)
committerSam Hemelryk <sam@moodle.com>
Wed, 30 Jan 2013 23:16:08 +0000 (12:16 +1300)
cache/tests/cache_test.php
cache/tests/fixtures/lib.php

index 6ad2152..98d9c5b 100644 (file)
@@ -744,4 +744,26 @@ class cache_phpunit_tests extends advanced_testcase {
         $config = $factory->create_config_instance();
         $this->assertEquals('cache_config_phpunittest', get_class($config));
     }
+
+    /**
+     * Test that multiple loaders work ok.
+     */
+    public function test_multiple_loaders() {
+        $instance = cache_config_phpunittest::instance(true);
+        $instance->phpunit_add_file_store('phpunittest1');
+        $instance->phpunit_add_file_store('phpunittest2');
+        $instance->phpunit_add_definition('phpunit/multi_loader', array(
+            'mode' => cache_store::MODE_APPLICATION,
+            'component' => 'phpunit',
+            'area' => 'multi_loader'
+        ));
+        $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3);
+        $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2);
+
+        $cache = cache::make('phpunit', 'multi_loader');
+        $this->assertInstanceOf('cache_application', $cache);
+        $this->assertFalse($cache->get('test'));
+        $this->assertTrue($cache->set('test', 'test'));
+        $this->assertEquals('test', $cache->get('test'));
+    }
 }
\ No newline at end of file
index 180d768..d5e5297 100644 (file)
@@ -63,6 +63,45 @@ class cache_config_phpunittest extends cache_config_writer {
     public function phpunit_remove_stores() {
         $this->configstores = array();
     }
+
+    /**
+     * Forcefully adds a file store.
+     *
+     * @param string $name
+     */
+    public function phpunit_add_file_store($name) {
+        $this->configstores[$name] = array(
+            'name' => $name,
+            'plugin' => 'file',
+            'configuration' => array(
+                'path' => ''
+            ),
+            'features' => 6,
+            'modes' => 3,
+            'mappingsonly' => false,
+            'class' => 'cachestore_file',
+            'default' => false,
+            'lock' => 'cachelock_file_default'
+        );
+    }
+
+    /**
+     * Forcefully injects a definition => store mapping.
+     *
+     * This function does no validation, you should only be calling if it you know
+     * exactly what to expect.
+     *
+     * @param string $definition
+     * @param string $store
+     * @param int $sort
+     */
+    public function phpunit_add_definition_mapping($definition, $store, $sort) {
+        $this->configdefinitionmappings[] = array(
+            'store' => $store,
+            'definition' => $definition,
+            'sort' => (int)$sort
+        );
+    }
 }
 
 /**