Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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_definition;
20
use cache_store;
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 store 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 (c) 2015 Moodlerooms Inc. (http://www.moodlerooms.com)
38
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 *
40
 * @coversDefaultClass \cachestore_redis
41
 */
42
class cachestore_redis_test extends \cachestore_tests {
43
    /** @var cachestore_redis $store Redis Cache Store. */
44
    protected $store;
45
 
46
    /**
47
     * Returns the class name.
48
     *
49
     * @return string
50
     */
51
    protected function get_class_name(): string {
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
 
62
    protected function tearDown(): void {
63
        parent::tearDown();
64
 
65
        if ($this->store instanceof cachestore_redis) {
66
            $this->store->purge();
67
        }
68
    }
69
 
70
    /**
71
     * Creates the required cachestore for the tests to run against Redis.
72
     *
73
     * @return cachestore_redis
74
     */
75
    protected function create_cachestore_redis(): cachestore_redis {
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
        $this->store = $store;
80
        $store->purge();
81
        return $store;
82
    }
83
 
84
    /**
85
     * Test methods for various operations (set and has) in the cachestore_redis class.
86
     *
87
     * @covers ::set
88
     * @covers ::has
89
     */
90
    public function test_has(): void {
91
        $store = $this->create_cachestore_redis();
92
 
93
        $this->assertTrue($store->set('foo', 'bar'));
94
        $this->assertTrue($store->has('foo'));
95
        $this->assertFalse($store->has('bat'));
96
    }
97
 
98
    /**
99
     * Test methods for the 'has_any' operation in the cachestore_redis class.
100
     *
101
     * @covers ::set
102
     * @covers ::has_any
103
     */
104
    public function test_has_any(): void {
105
        $store = $this->create_cachestore_redis();
106
 
107
        $this->assertTrue($store->set('foo', 'bar'));
108
        $this->assertTrue($store->has_any(['bat', 'foo']));
109
        $this->assertFalse($store->has_any(['bat', 'baz']));
110
    }
111
 
112
    /**
113
     * PHPUnit test methods for the 'has_all' operation in the cachestore_redis class.
114
     *
115
     * @covers ::set
116
     * @covers ::has_all
117
     */
118
    public function test_has_all(): void {
119
        $store = $this->create_cachestore_redis();
120
 
121
        $this->assertTrue($store->set('foo', 'bar'));
122
        $this->assertTrue($store->set('bat', 'baz'));
123
        $this->assertTrue($store->has_all(['foo', 'bat']));
124
        $this->assertFalse($store->has_all(['foo', 'bat', 'this']));
125
    }
126
 
127
    /**
128
     * Test methods for the 'lock' operations in the cachestore_redis class.
129
     *
130
     * @covers ::acquire_lock
131
     * @covers ::check_lock_state
132
     * @covers ::release_lock
133
     */
134
    public function test_lock(): void {
135
        $store = $this->create_cachestore_redis();
136
 
137
        $this->assertTrue($store->acquire_lock('lock', '123'));
138
        $this->assertTrue($store->check_lock_state('lock', '123'));
139
        $this->assertFalse($store->check_lock_state('lock', '321'));
140
        $this->assertNull($store->check_lock_state('notalock', '123'));
141
        $this->assertFalse($store->release_lock('lock', '321'));
142
        $this->assertTrue($store->release_lock('lock', '123'));
143
    }
144
 
145
    /**
146
     * Test method to check if the cachestore_redis instance is ready after connecting.
147
     *
148
     * @covers ::is_ready
149
     */
150
    public function test_it_is_ready_after_connecting(): void {
151
        $store = $this->create_cachestore_redis();
152
        $this::assertTrue($store->is_ready());
153
    }
154
}