2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
20 * Unit tests for cache_store functionality.
23 * @copyright 2021 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 class store_test
extends \advanced_testcase
{
29 * Tests the default implementation of cache_size_details, which does some
30 * complicated statistics.
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));
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
);
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
);
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
);
63 * Creates a static store for testing.
65 * @return \cachestore_static Store
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',
76 $store->initialise($definition);