PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/cache/tests/fixtures/lib.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 285 lines | 100 code | 20 blank | 165 comment | 3 complexity | 7e646c7da33a2f134fef89773d4c4d7d MD5 | raw file
  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/>.
  16. /**
  17. * Support library for the cache PHPUnit tests.
  18. *
  19. * This file is part of Moodle's cache API, affectionately called MUC.
  20. * It contains the components that are requried in order to use caching.
  21. *
  22. * @package core
  23. * @category cache
  24. * @copyright 2012 Sam Hemelryk
  25. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26. */
  27. defined('MOODLE_INTERNAL') || die();
  28. /**
  29. * Override the default cache configuration for our own maniacle purposes.
  30. *
  31. * @copyright 2012 Sam Hemelryk
  32. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33. */
  34. class cache_config_phpunittest extends cache_config_writer {
  35. /**
  36. * Adds a definition to the stack
  37. * @param string $area
  38. * @param array $properties
  39. */
  40. public function phpunit_add_definition($area, array $properties) {
  41. if (!array_key_exists('overrideclass', $properties)) {
  42. switch ($properties['mode']) {
  43. case cache_store::MODE_APPLICATION:
  44. $properties['overrideclass'] = 'cache_phpunit_application';
  45. break;
  46. case cache_store::MODE_SESSION:
  47. $properties['overrideclass'] = 'cache_phpunit_session';
  48. break;
  49. case cache_store::MODE_REQUEST:
  50. $properties['overrideclass'] = 'cache_phpunit_request';
  51. break;
  52. }
  53. }
  54. $this->configdefinitions[$area] = $properties;
  55. }
  56. /**
  57. * Removes the configured stores so that there are none available.
  58. */
  59. public function phpunit_remove_stores() {
  60. $this->configstores = array();
  61. }
  62. /**
  63. * Forcefully adds a file store.
  64. *
  65. * @param string $name
  66. */
  67. public function phpunit_add_file_store($name) {
  68. $this->configstores[$name] = array(
  69. 'name' => $name,
  70. 'plugin' => 'file',
  71. 'configuration' => array(
  72. 'path' => ''
  73. ),
  74. 'features' => 6,
  75. 'modes' => 3,
  76. 'mappingsonly' => false,
  77. 'class' => 'cachestore_file',
  78. 'default' => false,
  79. 'lock' => 'cachelock_file_default'
  80. );
  81. }
  82. /**
  83. * Forcefully injects a definition => store mapping.
  84. *
  85. * This function does no validation, you should only be calling if it you know
  86. * exactly what to expect.
  87. *
  88. * @param string $definition
  89. * @param string $store
  90. * @param int $sort
  91. */
  92. public function phpunit_add_definition_mapping($definition, $store, $sort) {
  93. $this->configdefinitionmappings[] = array(
  94. 'store' => $store,
  95. 'definition' => $definition,
  96. 'sort' => (int)$sort
  97. );
  98. }
  99. /**
  100. * Overrides the default site identifier used by the Cache API so that we can be sure of what it is.
  101. *
  102. * @return string
  103. */
  104. public function get_site_identifier() {
  105. global $CFG;
  106. return $CFG->wwwroot.'phpunit';
  107. }
  108. }
  109. /**
  110. * Dummy object for testing cacheable object interface and interaction
  111. *
  112. * @copyright 2012 Sam Hemelryk
  113. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  114. */
  115. class cache_phpunit_dummy_object extends stdClass implements cacheable_object {
  116. /**
  117. * Test property 1
  118. * @var string
  119. */
  120. public $property1;
  121. /**
  122. * Test property 1
  123. * @var string
  124. */
  125. public $property2;
  126. /**
  127. * Constructor
  128. * @param string $property1
  129. * @param string $property2
  130. */
  131. public function __construct($property1, $property2) {
  132. $this->property1 = $property1;
  133. $this->property2 = $property2;
  134. }
  135. /**
  136. * Prepares this object for caching
  137. * @return array
  138. */
  139. public function prepare_to_cache() {
  140. return array($this->property1.'_ptc', $this->property2.'_ptc');
  141. }
  142. /**
  143. * Returns this object from the cache
  144. * @param array $data
  145. * @return cache_phpunit_dummy_object
  146. */
  147. public static function wake_from_cache($data) {
  148. return new cache_phpunit_dummy_object(array_shift($data).'_wfc', array_shift($data).'_wfc');
  149. }
  150. }
  151. /**
  152. * Dummy data source object for testing data source interface and implementation
  153. *
  154. * @copyright 2012 Sam Hemelryk
  155. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  156. */
  157. class cache_phpunit_dummy_datasource implements cache_data_source {
  158. /**
  159. * Returns an instance of this object for use with the cache.
  160. *
  161. * @param cache_definition $definition
  162. * @return cache_phpunit_dummy_datasource
  163. */
  164. public static function get_instance_for_cache(cache_definition $definition) {
  165. return new cache_phpunit_dummy_datasource();
  166. }
  167. /**
  168. * Loads a key for the cache.
  169. *
  170. * @param string $key
  171. * @return string
  172. */
  173. public function load_for_cache($key) {
  174. return $key.' has no value really.';
  175. }
  176. /**
  177. * Loads many keys for the cache
  178. *
  179. * @param array $keys
  180. * @return array
  181. */
  182. public function load_many_for_cache(array $keys) {
  183. $return = array();
  184. foreach ($keys as $key) {
  185. $return[$key] = $key.' has no value really.';
  186. }
  187. return $return;
  188. }
  189. }
  190. /**
  191. * PHPUnit application cache loader.
  192. *
  193. * Used to expose things we could not otherwise see within an application cache.
  194. *
  195. * @copyright 2012 Sam Hemelryk
  196. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  197. */
  198. class cache_phpunit_application extends cache_application {
  199. /**
  200. * Returns the class of the store immediately associated with this cache.
  201. * @return string
  202. */
  203. public function phpunit_get_store_class() {
  204. return get_class($this->get_store());
  205. }
  206. }
  207. /**
  208. * PHPUnit session cache loader.
  209. *
  210. * Used to expose things we could not otherwise see within an session cache.
  211. *
  212. * @copyright 2012 Sam Hemelryk
  213. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  214. */
  215. class cache_phpunit_session extends cache_session {
  216. /**
  217. * Returns the class of the store immediately associated with this cache.
  218. * @return string
  219. */
  220. public function phpunit_get_store_class() {
  221. return get_class($this->get_store());
  222. }
  223. }
  224. /**
  225. * PHPUnit request cache loader.
  226. *
  227. * Used to expose things we could not otherwise see within an request cache.
  228. *
  229. * @copyright 2012 Sam Hemelryk
  230. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  231. */
  232. class cache_phpunit_request extends cache_request {
  233. /**
  234. * Returns the class of the store immediately associated with this cache.
  235. * @return string
  236. */
  237. public function phpunit_get_store_class() {
  238. return get_class($this->get_store());
  239. }
  240. }
  241. /**
  242. * Dummy overridden cache loader class that we can use to test overriding loader functionality.
  243. *
  244. * @copyright 2012 Sam Hemelryk
  245. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  246. */
  247. class cache_phpunit_dummy_overrideclass extends cache_application {
  248. // Satisfying the code pre-checker is just part of my day job.
  249. }
  250. /**
  251. * Cache PHPUnit specific factory.
  252. *
  253. * @copyright 2012 Sam Hemelryk
  254. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  255. */
  256. class cache_phpunit_factory extends cache_factory {
  257. /**
  258. * Exposes the cache_factory's disable method.
  259. *
  260. * Perhaps one day that method will be made public, for the time being it is protected.
  261. */
  262. public static function phpunit_disable() {
  263. parent::disable();
  264. }
  265. }