Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
/**
18
 * An abstract class to make writing unit tests for cache stores very easy.
19
 *
20
 * @package    core
21
 * @category   cache
22
 * @copyright  2013 Sam Hemelryk
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
abstract class cachestore_tests extends advanced_testcase {
26
    /**
27
     * Returns the class name for the store.
28
     *
29
     * @return string
30
     */
31
    abstract protected function get_class_name();
32
 
33
    /**
34
     * Sets up the fixture, for example, open a network connection.
35
     * This method is called before a test is executed.
36
     */
37
    public function setUp(): void {
38
        $class = $this->get_class_name();
39
        if (!class_exists($class) || !$class::are_requirements_met()) {
1441 ariadna 40
            $this->markTestSkipped('Could not test ' . $class . '. Requirements are not met.');
1 efrain 41
        }
42
        parent::setUp();
43
    }
44
    /**
45
     * Run the unit tests for the store.
46
     */
47
    public function test_test_instance() {
48
        $class = $this->get_class_name();
49
 
50
        $modes = $class::get_supported_modes();
51
        if ($modes & cache_store::MODE_APPLICATION) {
52
            $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, $class, 'phpunit_test');
1441 ariadna 53
            $instance = new $class($class . '_test', $class::unit_test_configuration());
1 efrain 54
 
55
            if (!$instance->is_ready()) {
1441 ariadna 56
                $this->markTestSkipped('Could not test ' . $class . '. No test instance configured for application caches.');
1 efrain 57
            } else {
58
                $instance->initialise($definition);
59
                $this->run_tests($instance);
60
            }
61
        }
62
        if ($modes & cache_store::MODE_SESSION) {
63
            $definition = cache_definition::load_adhoc(cache_store::MODE_SESSION, $class, 'phpunit_test');
1441 ariadna 64
            $instance = new $class($class . '_test', $class::unit_test_configuration());
1 efrain 65
 
66
            if (!$instance->is_ready()) {
1441 ariadna 67
                $this->markTestSkipped('Could not test ' . $class . '. No test instance configured for session caches.');
1 efrain 68
            } else {
69
                $instance->initialise($definition);
70
                $this->run_tests($instance);
71
            }
72
        }
73
        if ($modes & cache_store::MODE_REQUEST) {
74
            $definition = cache_definition::load_adhoc(cache_store::MODE_REQUEST, $class, 'phpunit_test');
1441 ariadna 75
            $instance = new $class($class . '_test', $class::unit_test_configuration());
1 efrain 76
 
77
            if (!$instance->is_ready()) {
1441 ariadna 78
                $this->markTestSkipped('Could not test ' . $class . '. No test instance configured for request caches.');
1 efrain 79
            } else {
80
                $instance->initialise($definition);
81
                $this->run_tests($instance);
82
            }
83
        }
84
    }
85
 
86
    /**
87
     * Test the store for basic functionality.
88
     */
89
    public function run_tests(cache_store $instance) {
1441 ariadna 90
        $object = new stdClass();
1 efrain 91
        $object->data = 1;
92
 
93
        // Test set with a string.
94
        $this->assertTrue($instance->set('test1', 'test1'));
95
        $this->assertTrue($instance->set('test2', 'test2'));
96
        $this->assertTrue($instance->set('test3', '3'));
97
        $this->assertTrue($instance->set('other3', '3'));
98
 
99
        // Test get with a string.
100
        $this->assertSame('test1', $instance->get('test1'));
101
        $this->assertSame('test2', $instance->get('test2'));
102
        $this->assertSame('3', $instance->get('test3'));
103
 
104
        // Test find and find with prefix if this class implements the searchable interface.
105
        if ($instance->is_searchable()) {
106
            // Extra settings here ignore the return order of the array.
1441 ariadna 107
            $this->assertEqualsCanonicalizing(['test3', 'test1', 'test2', 'other3'], array_values($instance->find_all()));
1 efrain 108
 
109
            // Extra settings here ignore the return order of the array.
1441 ariadna 110
            $this->assertEqualsCanonicalizing(['test2', 'test1', 'test3'], array_values($instance->find_by_prefix('test')));
1 efrain 111
            $this->assertEquals(['test2'], $instance->find_by_prefix('test2'));
112
            $this->assertEquals(['other3'], $instance->find_by_prefix('other'));
113
            $this->assertEquals([], $instance->find_by_prefix('nothere'));
114
        }
115
 
116
        // Test set with an int.
117
        $this->assertTrue($instance->set('test1', 1));
118
        $this->assertTrue($instance->set('test2', 2));
119
 
120
        // Test get with an int.
121
        $this->assertSame(1, $instance->get('test1'));
122
        $this->assertIsInt($instance->get('test1'));
123
        $this->assertSame(2, $instance->get('test2'));
124
        $this->assertIsInt($instance->get('test2'));
125
 
126
        // Test set with a bool.
127
        $this->assertTrue($instance->set('test1', true));
128
 
129
        // Test get with an bool.
130
        $this->assertSame(true, $instance->get('test1'));
131
        $this->assertIsBool($instance->get('test1'));
132
 
133
        // Test with an object.
134
        $this->assertTrue($instance->set('obj', $object));
135
        if ($instance::get_supported_features() & cache_store::DEREFERENCES_OBJECTS) {
136
            $this->assertNotSame($object, $instance->get('obj'), 'Objects must be dereferenced when returned.');
137
        }
138
        $this->assertEquals($object, $instance->get('obj'));
139
 
140
        // Test delete.
141
        $this->assertTrue($instance->delete('test1'));
142
        $this->assertTrue($instance->delete('test3'));
143
        $this->assertFalse($instance->delete('test3'));
144
        $this->assertFalse($instance->get('test1'));
145
        $this->assertSame(2, $instance->get('test2'));
146
        $this->assertTrue($instance->set('test1', 'test1'));
147
 
148
        // Test purge.
149
        $this->assertTrue($instance->purge());
150
        $this->assertFalse($instance->get('test1'));
151
        $this->assertFalse($instance->get('test2'));
152
 
153
        // Test set_many.
1441 ariadna 154
        $outcome = $instance->set_many([
155
            ['key' => 'many1', 'value' => 'many1'],
156
            ['key' => 'many2', 'value' => 'many2'],
157
            ['key' => 'many3', 'value' => 'many3'],
158
            ['key' => 'many4', 'value' => 'many4'],
159
            ['key' => 'many5', 'value' => 'many5'],
160
        ]);
1 efrain 161
        $this->assertSame(5, $outcome);
162
        $this->assertSame('many1', $instance->get('many1'));
163
        $this->assertSame('many5', $instance->get('many5'));
164
        $this->assertFalse($instance->get('many6'));
165
 
166
        // Test get_many.
1441 ariadna 167
        $result = $instance->get_many(['many1', 'many3', 'many5', 'many6']);
1 efrain 168
        $this->assertIsArray($result);
169
        $this->assertCount(4, $result);
1441 ariadna 170
        $this->assertSame([
1 efrain 171
            'many1' => 'many1',
172
            'many3' => 'many3',
173
            'many5' => 'many5',
174
            'many6' => false,
1441 ariadna 175
        ], $result);
1 efrain 176
 
177
        // Test delete_many.
1441 ariadna 178
        $this->assertSame(3, $instance->delete_many(['many2', 'many3', 'many4']));
179
        $this->assertSame(2, $instance->delete_many(['many1', 'many5', 'many6']));
1 efrain 180
    }
181
}