2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Support library for the cache PHPUnit tests.
20 * This file is part of Moodle's cache API, affectionately called MUC.
21 * It contains the components that are requried in order to use caching.
25 * @copyright 2012 Sam Hemelryk
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 defined('MOODLE_INTERNAL') || die();
32 * Override the default cache configuration for our own maniacle purposes.
34 * @copyright 2012 Sam Hemelryk
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class cache_config_phpunittest extends cache_config_writer {
40 * Adds a definition to the stack
42 * @param array $properties
44 public function phpunit_add_definition($area, array $properties) {
45 if (!array_key_exists('overrideclass', $properties)) {
46 switch ($properties['mode']) {
47 case cache_store::MODE_APPLICATION:
48 $properties['overrideclass'] = 'cache_phpunit_application';
50 case cache_store::MODE_SESSION:
51 $properties['overrideclass'] = 'cache_phpunit_session';
53 case cache_store::MODE_REQUEST:
54 $properties['overrideclass'] = 'cache_phpunit_request';
58 $this->configdefinitions[$area] = $properties;
62 * Removes a definition.
65 public function phpunit_remove_definition($name) {
66 unset($this->configdefinitions[$name]);
70 * Removes the configured stores so that there are none available.
72 public function phpunit_remove_stores() {
73 $this->configstores = array();
77 * Forcefully adds a file store.
81 public function phpunit_add_file_store($name) {
82 $this->configstores[$name] = array(
85 'configuration' => array(
90 'mappingsonly' => false,
91 'class' => 'cachestore_file',
93 'lock' => 'cachelock_file_default'
98 * Forcefully adds a session store.
100 * @param string $name
102 public function phpunit_add_session_store($name) {
103 $this->configstores[$name] = array(
105 'plugin' => 'session',
106 'configuration' => array(),
110 'class' => 'cachestore_session',
111 'lock' => 'cachelock_file_default',
116 * Forcefully injects a definition => store mapping.
118 * This function does no validation, you should only be calling if it you know
119 * exactly what to expect.
121 * @param string $definition
122 * @param string $store
125 public function phpunit_add_definition_mapping($definition, $store, $sort) {
126 $this->configdefinitionmappings[] = array(
128 'definition' => $definition,
134 * Overrides the default site identifier used by the Cache API so that we can be sure of what it is.
138 public function get_site_identifier() {
140 return $CFG->wwwroot.'phpunit';
145 * Dummy object for testing cacheable object interface and interaction
147 * @copyright 2012 Sam Hemelryk
148 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
150 class cache_phpunit_dummy_object extends stdClass implements cacheable_object {
163 * @param string $property1
164 * @param string $property2
166 public function __construct($property1, $property2) {
167 $this->property1 = $property1;
168 $this->property2 = $property2;
171 * Prepares this object for caching
174 public function prepare_to_cache() {
175 return array($this->property1.'_ptc', $this->property2.'_ptc');
178 * Returns this object from the cache
180 * @return cache_phpunit_dummy_object
182 public static function wake_from_cache($data) {
183 return new cache_phpunit_dummy_object(array_shift($data).'_wfc', array_shift($data).'_wfc');
188 * Dummy data source object for testing data source interface and implementation
190 * @copyright 2012 Sam Hemelryk
191 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
193 class cache_phpunit_dummy_datasource implements cache_data_source {
195 * Returns an instance of this object for use with the cache.
197 * @param cache_definition $definition
198 * @return cache_phpunit_dummy_datasource
200 public static function get_instance_for_cache(cache_definition $definition) {
201 return new cache_phpunit_dummy_datasource();
205 * Loads a key for the cache.
210 public function load_for_cache($key) {
211 return $key.' has no value really.';
215 * Loads many keys for the cache
220 public function load_many_for_cache(array $keys) {
222 foreach ($keys as $key) {
223 $return[$key] = $key.' has no value really.';
230 * PHPUnit application cache loader.
232 * Used to expose things we could not otherwise see within an application cache.
234 * @copyright 2012 Sam Hemelryk
235 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
237 class cache_phpunit_application extends cache_application {
240 * Returns the class of the store immediately associated with this cache.
243 public function phpunit_get_store_class() {
244 return get_class($this->get_store());
248 * Returns all the interfaces the cache store implements.
251 public function phpunit_get_store_implements() {
252 return class_implements($this->get_store());
257 * PHPUnit session cache loader.
259 * Used to expose things we could not otherwise see within an session cache.
261 * @copyright 2012 Sam Hemelryk
262 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
264 class cache_phpunit_session extends cache_session {
267 * Returns the class of the store immediately associated with this cache.
270 public function phpunit_get_store_class() {
271 return get_class($this->get_store());
275 * Returns all the interfaces the cache store implements.
278 public function phpunit_get_store_implements() {
279 return class_implements($this->get_store());
284 * PHPUnit request cache loader.
286 * Used to expose things we could not otherwise see within an request cache.
288 * @copyright 2012 Sam Hemelryk
289 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
291 class cache_phpunit_request extends cache_request {
294 * Returns the class of the store immediately associated with this cache.
297 public function phpunit_get_store_class() {
298 return get_class($this->get_store());
302 * Returns all the interfaces the cache store implements.
305 public function phpunit_get_store_implements() {
306 return class_implements($this->get_store());
311 * Dummy overridden cache loader class that we can use to test overriding loader functionality.
313 * @copyright 2012 Sam Hemelryk
314 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
316 class cache_phpunit_dummy_overrideclass extends cache_application {
317 // Satisfying the code pre-checker is just part of my day job.
321 * Cache PHPUnit specific factory.
323 * @copyright 2012 Sam Hemelryk
324 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
326 class cache_phpunit_factory extends cache_factory {
328 * Exposes the cache_factory's disable method.
330 * Perhaps one day that method will be made public, for the time being it is protected.
332 public static function phpunit_disable() {