PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/cache/tests/fixtures/lib.php

https://github.com/jamiepratt/moodle
PHP | 346 lines | 128 code | 26 blank | 192 comment | 3 complexity | 8af8027d45b156e11684b430159a5e1a 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 a definition.
  58. * @param string $name
  59. */
  60. public function phpunit_remove_definition($name) {
  61. unset($this->configdefinitions[$name]);
  62. }
  63. /**
  64. * Removes the configured stores so that there are none available.
  65. */
  66. public function phpunit_remove_stores() {
  67. $this->configstores = array();
  68. }
  69. /**
  70. * Forcefully adds a file store.
  71. *
  72. * @param string $name
  73. */
  74. public function phpunit_add_file_store($name) {
  75. $this->configstores[$name] = array(
  76. 'name' => $name,
  77. 'plugin' => 'file',
  78. 'configuration' => array(
  79. 'path' => ''
  80. ),
  81. 'features' => 6,
  82. 'modes' => 3,
  83. 'mappingsonly' => false,
  84. 'class' => 'cachestore_file',
  85. 'default' => false,
  86. 'lock' => 'cachelock_file_default'
  87. );
  88. }
  89. /**
  90. * Forcefully adds a session store.
  91. *
  92. * @param string $name
  93. */
  94. public function phpunit_add_session_store($name) {
  95. $this->configstores[$name] = array(
  96. 'name' => $name,
  97. 'plugin' => 'session',
  98. 'configuration' => array(),
  99. 'features' => 14,
  100. 'modes' => 2,
  101. 'default' => true,
  102. 'class' => 'cachestore_session',
  103. 'lock' => 'cachelock_file_default',
  104. );
  105. }
  106. /**
  107. * Forcefully injects a definition => store mapping.
  108. *
  109. * This function does no validation, you should only be calling if it you know
  110. * exactly what to expect.
  111. *
  112. * @param string $definition
  113. * @param string $store
  114. * @param int $sort
  115. */
  116. public function phpunit_add_definition_mapping($definition, $store, $sort) {
  117. $this->configdefinitionmappings[] = array(
  118. 'store' => $store,
  119. 'definition' => $definition,
  120. 'sort' => (int)$sort
  121. );
  122. }
  123. /**
  124. * Overrides the default site identifier used by the Cache API so that we can be sure of what it is.
  125. *
  126. * @return string
  127. */
  128. public function get_site_identifier() {
  129. global $CFG;
  130. return $CFG->wwwroot.'phpunit';
  131. }
  132. }
  133. /**
  134. * Dummy object for testing cacheable object interface and interaction
  135. *
  136. * @copyright 2012 Sam Hemelryk
  137. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  138. */
  139. class cache_phpunit_dummy_object extends stdClass implements cacheable_object {
  140. /**
  141. * Test property 1
  142. * @var string
  143. */
  144. public $property1;
  145. /**
  146. * Test property 1
  147. * @var string
  148. */
  149. public $property2;
  150. /**
  151. * Constructor
  152. * @param string $property1
  153. * @param string $property2
  154. */
  155. public function __construct($property1, $property2) {
  156. $this->property1 = $property1;
  157. $this->property2 = $property2;
  158. }
  159. /**
  160. * Prepares this object for caching
  161. * @return array
  162. */
  163. public function prepare_to_cache() {
  164. return array($this->property1.'_ptc', $this->property2.'_ptc');
  165. }
  166. /**
  167. * Returns this object from the cache
  168. * @param array $data
  169. * @return cache_phpunit_dummy_object
  170. */
  171. public static function wake_from_cache($data) {
  172. return new cache_phpunit_dummy_object(array_shift($data).'_wfc', array_shift($data).'_wfc');
  173. }
  174. }
  175. /**
  176. * Dummy data source object for testing data source interface and implementation
  177. *
  178. * @copyright 2012 Sam Hemelryk
  179. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  180. */
  181. class cache_phpunit_dummy_datasource implements cache_data_source {
  182. /**
  183. * Returns an instance of this object for use with the cache.
  184. *
  185. * @param cache_definition $definition
  186. * @return cache_phpunit_dummy_datasource
  187. */
  188. public static function get_instance_for_cache(cache_definition $definition) {
  189. return new cache_phpunit_dummy_datasource();
  190. }
  191. /**
  192. * Loads a key for the cache.
  193. *
  194. * @param string $key
  195. * @return string
  196. */
  197. public function load_for_cache($key) {
  198. return $key.' has no value really.';
  199. }
  200. /**
  201. * Loads many keys for the cache
  202. *
  203. * @param array $keys
  204. * @return array
  205. */
  206. public function load_many_for_cache(array $keys) {
  207. $return = array();
  208. foreach ($keys as $key) {
  209. $return[$key] = $key.' has no value really.';
  210. }
  211. return $return;
  212. }
  213. }
  214. /**
  215. * PHPUnit application cache loader.
  216. *
  217. * Used to expose things we could not otherwise see within an application cache.
  218. *
  219. * @copyright 2012 Sam Hemelryk
  220. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  221. */
  222. class cache_phpunit_application extends cache_application {
  223. /**
  224. * Returns the class of the store immediately associated with this cache.
  225. * @return string
  226. */
  227. public function phpunit_get_store_class() {
  228. return get_class($this->get_store());
  229. }
  230. /**
  231. * Returns all the interfaces the cache store implements.
  232. * @return array
  233. */
  234. public function phpunit_get_store_implements() {
  235. return class_implements($this->get_store());
  236. }
  237. /**
  238. * Returns the given key directly from the static acceleration array.
  239. *
  240. * @param string $key
  241. * @return false|mixed
  242. */
  243. public function phpunit_static_acceleration_get($key) {
  244. $key = $this->parse_key($key);
  245. return $this->static_acceleration_get($key);
  246. }
  247. }
  248. /**
  249. * PHPUnit session cache loader.
  250. *
  251. * Used to expose things we could not otherwise see within an session cache.
  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_session extends cache_session {
  257. /**
  258. * Returns the class of the store immediately associated with this cache.
  259. * @return string
  260. */
  261. public function phpunit_get_store_class() {
  262. return get_class($this->get_store());
  263. }
  264. /**
  265. * Returns all the interfaces the cache store implements.
  266. * @return array
  267. */
  268. public function phpunit_get_store_implements() {
  269. return class_implements($this->get_store());
  270. }
  271. }
  272. /**
  273. * PHPUnit request cache loader.
  274. *
  275. * Used to expose things we could not otherwise see within an request cache.
  276. *
  277. * @copyright 2012 Sam Hemelryk
  278. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  279. */
  280. class cache_phpunit_request extends cache_request {
  281. /**
  282. * Returns the class of the store immediately associated with this cache.
  283. * @return string
  284. */
  285. public function phpunit_get_store_class() {
  286. return get_class($this->get_store());
  287. }
  288. /**
  289. * Returns all the interfaces the cache store implements.
  290. * @return array
  291. */
  292. public function phpunit_get_store_implements() {
  293. return class_implements($this->get_store());
  294. }
  295. }
  296. /**
  297. * Dummy overridden cache loader class that we can use to test overriding loader functionality.
  298. *
  299. * @copyright 2012 Sam Hemelryk
  300. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  301. */
  302. class cache_phpunit_dummy_overrideclass extends cache_application {
  303. // Satisfying the code pre-checker is just part of my day job.
  304. }
  305. /**
  306. * Cache PHPUnit specific factory.
  307. *
  308. * @copyright 2012 Sam Hemelryk
  309. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  310. */
  311. class cache_phpunit_factory extends cache_factory {
  312. /**
  313. * Exposes the cache_factory's disable method.
  314. *
  315. * Perhaps one day that method will be made public, for the time being it is protected.
  316. */
  317. public static function phpunit_disable() {
  318. parent::disable();
  319. }
  320. }