Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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  namespace cachestore_redis;
  18  
  19  use cache_store;
  20  use cache_definition;
  21  use cachestore_redis;
  22  
  23  defined('MOODLE_INTERNAL') || die();
  24  
  25  require_once (__DIR__.'/../../../tests/fixtures/stores.php');
  26  require_once (__DIR__.'/../lib.php');
  27  
  28  /**
  29   * Redis cache test.
  30   *
  31   * If you wish to use these unit tests all you need to do is add the following definition to
  32   * your config.php file.
  33   *
  34   * define('TEST_CACHESTORE_REDIS_TESTSERVERS', '127.0.0.1');
  35   *
  36   * @package   cachestore_redis
  37   * @copyright Copyright (c) 2015 Moodlerooms Inc. (http://www.moodlerooms.com)
  38   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class store_test extends \cachestore_tests {
  41      /**
  42       * @var cachestore_redis
  43       */
  44      protected $store;
  45  
  46      /**
  47       * Returns the MongoDB class name
  48       *
  49       * @return string
  50       */
  51      protected function get_class_name() {
  52          return 'cachestore_redis';
  53      }
  54  
  55      public function setUp(): void {
  56          if (!cachestore_redis::are_requirements_met() || !defined('TEST_CACHESTORE_REDIS_TESTSERVERS')) {
  57              $this->markTestSkipped('Could not test cachestore_redis. Requirements are not met.');
  58          }
  59          parent::setUp();
  60      }
  61      protected function tearDown(): void {
  62          parent::tearDown();
  63  
  64          if ($this->store instanceof cachestore_redis) {
  65              $this->store->purge();
  66          }
  67      }
  68  
  69      /**
  70       * Creates the required cachestore for the tests to run against Redis.
  71       *
  72       * @return cachestore_redis
  73       */
  74      protected function create_cachestore_redis() {
  75          /** @var cache_definition $definition */
  76          $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_redis', 'phpunit_test');
  77          $store = new cachestore_redis('Test', cachestore_redis::unit_test_configuration());
  78          $store->initialise($definition);
  79  
  80          $this->store = $store;
  81  
  82          if (!$store) {
  83              $this->markTestSkipped();
  84          }
  85  
  86          return $store;
  87      }
  88  
  89      public function test_has() {
  90          $store = $this->create_cachestore_redis();
  91  
  92          $this->assertTrue($store->set('foo', 'bar'));
  93          $this->assertTrue($store->has('foo'));
  94          $this->assertFalse($store->has('bat'));
  95      }
  96  
  97      public function test_has_any() {
  98          $store = $this->create_cachestore_redis();
  99  
 100          $this->assertTrue($store->set('foo', 'bar'));
 101          $this->assertTrue($store->has_any(array('bat', 'foo')));
 102          $this->assertFalse($store->has_any(array('bat', 'baz')));
 103      }
 104  
 105      public function test_has_all() {
 106          $store = $this->create_cachestore_redis();
 107  
 108          $this->assertTrue($store->set('foo', 'bar'));
 109          $this->assertTrue($store->set('bat', 'baz'));
 110          $this->assertTrue($store->has_all(array('foo', 'bat')));
 111          $this->assertFalse($store->has_all(array('foo', 'bat', 'this')));
 112      }
 113  
 114      public function test_lock() {
 115          $store = $this->create_cachestore_redis();
 116  
 117          $this->assertTrue($store->acquire_lock('lock', '123'));
 118          $this->assertTrue($store->check_lock_state('lock', '123'));
 119          $this->assertFalse($store->check_lock_state('lock', '321'));
 120          $this->assertNull($store->check_lock_state('notalock', '123'));
 121          $this->assertFalse($store->release_lock('lock', '321'));
 122          $this->assertTrue($store->release_lock('lock', '123'));
 123      }
 124  }