MDL-41106 cache: several fixes for the session cache.
[moodle.git] / cache / tests / fixtures / lib.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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.
13 //
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/>.
17 /**
18  * Support library for the cache PHPUnit tests.
19  *
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.
22  *
23  * @package    core
24  * @category   cache
25  * @copyright  2012 Sam Hemelryk
26  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27  */
29 defined('MOODLE_INTERNAL') || die();
31 /**
32  * Override the default cache configuration for our own maniacle purposes.
33  *
34  * @copyright  2012 Sam Hemelryk
35  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36  */
37 class cache_config_phpunittest extends cache_config_writer {
39     /**
40      * Adds a definition to the stack
41      * @param string $area
42      * @param array $properties
43      */
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';
49                     break;
50                 case cache_store::MODE_SESSION:
51                     $properties['overrideclass'] = 'cache_phpunit_session';
52                     break;
53                 case cache_store::MODE_REQUEST:
54                     $properties['overrideclass'] = 'cache_phpunit_request';
55                     break;
56             }
57         }
58         $this->configdefinitions[$area] = $properties;
59     }
61     /**
62      * Removes a definition.
63      * @param string $name
64      */
65     public function phpunit_remove_definition($name) {
66         unset($this->configdefinitions[$name]);
67     }
69     /**
70      * Removes the configured stores so that there are none available.
71      */
72     public function phpunit_remove_stores() {
73         $this->configstores = array();
74     }
76     /**
77      * Forcefully adds a file store.
78      *
79      * @param string $name
80      */
81     public function phpunit_add_file_store($name) {
82         $this->configstores[$name] = array(
83             'name' => $name,
84             'plugin' => 'file',
85             'configuration' => array(
86                 'path' => ''
87             ),
88             'features' => 6,
89             'modes' => 3,
90             'mappingsonly' => false,
91             'class' => 'cachestore_file',
92             'default' => false,
93             'lock' => 'cachelock_file_default'
94         );
95     }
97     /**
98      * Forcefully adds a session store.
99      *
100      * @param string $name
101      */
102     public function phpunit_add_session_store($name) {
103         $this->configstores[$name] = array(
104             'name' => $name,
105             'plugin' => 'session',
106             'configuration' => array(),
107             'features' => 14,
108             'modes' => 2,
109             'default' => true,
110             'class' => 'cachestore_session',
111             'lock' => 'cachelock_file_default',
112         );
113     }
115     /**
116      * Forcefully injects a definition => store mapping.
117      *
118      * This function does no validation, you should only be calling if it you know
119      * exactly what to expect.
120      *
121      * @param string $definition
122      * @param string $store
123      * @param int $sort
124      */
125     public function phpunit_add_definition_mapping($definition, $store, $sort) {
126         $this->configdefinitionmappings[] = array(
127             'store' => $store,
128             'definition' => $definition,
129             'sort' => (int)$sort
130         );
131     }
133     /**
134      * Overrides the default site identifier used by the Cache API so that we can be sure of what it is.
135      *
136      * @return string
137      */
138     public function get_site_identifier() {
139         global $CFG;
140         return $CFG->wwwroot.'phpunit';
141     }
144 /**
145  * Dummy object for testing cacheable object interface and interaction
146  *
147  * @copyright  2012 Sam Hemelryk
148  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
149  */
150 class cache_phpunit_dummy_object extends stdClass implements cacheable_object {
151     /**
152      * Test property 1
153      * @var string
154      */
155     public $property1;
156     /**
157      * Test property 1
158      * @var string
159      */
160     public $property2;
161     /**
162      * Constructor
163      * @param string $property1
164      * @param string $property2
165      */
166     public function __construct($property1, $property2) {
167         $this->property1 = $property1;
168         $this->property2 = $property2;
169     }
170     /**
171      * Prepares this object for caching
172      * @return array
173      */
174     public function prepare_to_cache() {
175         return array($this->property1.'_ptc', $this->property2.'_ptc');
176     }
177     /**
178      * Returns this object from the cache
179      * @param array $data
180      * @return cache_phpunit_dummy_object
181      */
182     public static function wake_from_cache($data) {
183         return new cache_phpunit_dummy_object(array_shift($data).'_wfc', array_shift($data).'_wfc');
184     }
187 /**
188  * Dummy data source object for testing data source interface and implementation
189  *
190  * @copyright  2012 Sam Hemelryk
191  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
192  */
193 class cache_phpunit_dummy_datasource implements cache_data_source {
194     /**
195      * Returns an instance of this object for use with the cache.
196      *
197      * @param cache_definition $definition
198      * @return cache_phpunit_dummy_datasource
199      */
200     public static function get_instance_for_cache(cache_definition $definition) {
201         return new cache_phpunit_dummy_datasource();
202     }
204     /**
205      * Loads a key for the cache.
206      *
207      * @param string $key
208      * @return string
209      */
210     public function load_for_cache($key) {
211         return $key.' has no value really.';
212     }
214     /**
215      * Loads many keys for the cache
216      *
217      * @param array $keys
218      * @return array
219      */
220     public function load_many_for_cache(array $keys) {
221         $return = array();
222         foreach ($keys as $key) {
223             $return[$key] = $key.' has no value really.';
224         }
225         return $return;
226     }
229 /**
230  * PHPUnit application cache loader.
231  *
232  * Used to expose things we could not otherwise see within an application cache.
233  *
234  * @copyright  2012 Sam Hemelryk
235  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
236  */
237 class cache_phpunit_application extends cache_application {
239     /**
240      * Returns the class of the store immediately associated with this cache.
241      * @return string
242      */
243     public function phpunit_get_store_class() {
244         return get_class($this->get_store());
245     }
247     /**
248      * Returns all the interfaces the cache store implements.
249      * @return array
250      */
251     public function phpunit_get_store_implements() {
252         return class_implements($this->get_store());
253     }
256 /**
257  * PHPUnit session cache loader.
258  *
259  * Used to expose things we could not otherwise see within an session cache.
260  *
261  * @copyright  2012 Sam Hemelryk
262  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
263  */
264 class cache_phpunit_session extends cache_session {
266     /**
267      * Returns the class of the store immediately associated with this cache.
268      * @return string
269      */
270     public function phpunit_get_store_class() {
271         return get_class($this->get_store());
272     }
274     /**
275      * Returns all the interfaces the cache store implements.
276      * @return array
277      */
278     public function phpunit_get_store_implements() {
279         return class_implements($this->get_store());
280     }
283 /**
284  * PHPUnit request cache loader.
285  *
286  * Used to expose things we could not otherwise see within an request cache.
287  *
288  * @copyright  2012 Sam Hemelryk
289  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
290  */
291 class cache_phpunit_request extends cache_request {
293     /**
294      * Returns the class of the store immediately associated with this cache.
295      * @return string
296      */
297     public function phpunit_get_store_class() {
298         return get_class($this->get_store());
299     }
301     /**
302      * Returns all the interfaces the cache store implements.
303      * @return array
304      */
305     public function phpunit_get_store_implements() {
306         return class_implements($this->get_store());
307     }
310 /**
311  * Dummy overridden cache loader class that we can use to test overriding loader functionality.
312  *
313  * @copyright  2012 Sam Hemelryk
314  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
315  */
316 class cache_phpunit_dummy_overrideclass extends cache_application {
317     // Satisfying the code pre-checker is just part of my day job.
320 /**
321  * Cache PHPUnit specific factory.
322  *
323  * @copyright  2012 Sam Hemelryk
324  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
325  */
326 class cache_phpunit_factory extends cache_factory {
327     /**
328      * Exposes the cache_factory's disable method.
329      *
330      * Perhaps one day that method will be made public, for the time being it is protected.
331      */
332     public static function phpunit_disable() {
333         parent::disable();
334     }