Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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 core_cache;
  18  
  19  /**
  20   * Unit tests for cache_store functionality.
  21   *
  22   * @package core_cache
  23   * @copyright 2021 The Open University
  24   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  class store_test extends \advanced_testcase {
  27  
  28      /**
  29       * Tests the default implementation of cache_size_details, which does some
  30       * complicated statistics.
  31       */
  32      public function test_cache_size_details(): void {
  33          // Fill a store with 100 entries of varying size.
  34          $store = self::create_static_store();
  35          for ($i = 0; $i < 100; $i++) {
  36              $store->set('key' . $i, str_repeat('x', $i));
  37          }
  38  
  39          // Do the statistics for 10 random picks.
  40          $details = $store->cache_size_details(10);
  41          $this->assertTrue($details->supported);
  42          $this->assertEquals(100, $details->items);
  43  
  44          // Min/max possible means if it picks the smallest/largest 10.
  45          $this->assertGreaterThan(22, $details->mean);
  46          $this->assertLessThan(115, $details->mean);
  47          // Min/max possible SD.
  48          $this->assertLessThan(49, $details->sd);
  49          $this->assertGreaterThan(2.8, $details->sd);
  50          // Lowest possible confidence margin is about 1.74.
  51          $this->assertGreaterThan(1.7, $details->margin);
  52  
  53          // Repeat the statistics for a pick of all 100 entries (exact).
  54          $details = $store->cache_size_details(100);
  55          $this->assertTrue($details->supported);
  56          $this->assertEquals(100, $details->items);
  57          $this->assertEqualsWithDelta(69.3, $details->mean, 0.1);
  58          $this->assertEqualsWithDelta(29.2, $details->sd, 0.1);
  59          $this->assertEquals(0, $details->margin);
  60      }
  61  
  62      /**
  63       * Creates a static store for testing.
  64       *
  65       * @return \cachestore_static Store
  66       */
  67      protected static function create_static_store(): \cachestore_static {
  68          require_once (__DIR__ . '/../stores/static/lib.php');
  69          $store = new \cachestore_static('frog');
  70          $definition = \cache_definition::load('zombie', [
  71                  'mode' => \cache_store::MODE_REQUEST,
  72                  'component' => 'phpunit',
  73                  'area' => 'store_test',
  74                  'simplekeys' => true
  75          ]);
  76          $store->initialise($definition);
  77          return $store;
  78      }
  79  }