Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 13... Línea 13...
13
//
13
//
14
// You should have received a copy of the GNU General Public License
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/>.
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
Línea 16... Línea 16...
16
 
16
 
17
/**
-
 
18
 * Cache store test fixtures.
-
 
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
 
-
 
26
defined('MOODLE_INTERNAL') || die();
-
 
27
 
-
 
28
/**
17
/**
29
 * An abstract class to make writing unit tests for cache stores very easy.
18
 * An abstract class to make writing unit tests for cache stores very easy.
30
 *
19
 *
31
 * @package    core
20
 * @package    core
32
 * @category   cache
21
 * @category   cache
33
 * @copyright  2013 Sam Hemelryk
22
 * @copyright  2013 Sam Hemelryk
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
24
 */
36
abstract class cachestore_tests extends advanced_testcase {
-
 
37
 
25
abstract class cachestore_tests extends advanced_testcase {
38
    /**
26
    /**
39
     * Returns the class name for the store.
27
     * Returns the class name for the store.
40
     *
28
     *
41
     * @return string
29
     * @return string
Línea 47... Línea 35...
47
     * This method is called before a test is executed.
35
     * This method is called before a test is executed.
48
     */
36
     */
49
    public function setUp(): void {
37
    public function setUp(): void {
50
        $class = $this->get_class_name();
38
        $class = $this->get_class_name();
51
        if (!class_exists($class) || !$class::are_requirements_met()) {
39
        if (!class_exists($class) || !$class::are_requirements_met()) {
52
            $this->markTestSkipped('Could not test '.$class.'. Requirements are not met.');
40
            $this->markTestSkipped('Could not test ' . $class . '. Requirements are not met.');
53
        }
41
        }
54
        parent::setUp();
42
        parent::setUp();
55
    }
43
    }
56
    /**
44
    /**
57
     * Run the unit tests for the store.
45
     * Run the unit tests for the store.
Línea 60... Línea 48...
60
        $class = $this->get_class_name();
48
        $class = $this->get_class_name();
Línea 61... Línea 49...
61
 
49
 
62
        $modes = $class::get_supported_modes();
50
        $modes = $class::get_supported_modes();
63
        if ($modes & cache_store::MODE_APPLICATION) {
51
        if ($modes & cache_store::MODE_APPLICATION) {
64
            $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, $class, 'phpunit_test');
52
            $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, $class, 'phpunit_test');
Línea 65... Línea 53...
65
            $instance = new $class($class.'_test', $class::unit_test_configuration());
53
            $instance = new $class($class . '_test', $class::unit_test_configuration());
66
 
54
 
67
            if (!$instance->is_ready()) {
55
            if (!$instance->is_ready()) {
68
                $this->markTestSkipped('Could not test '.$class.'. No test instance configured for application caches.');
56
                $this->markTestSkipped('Could not test ' . $class . '. No test instance configured for application caches.');
69
            } else {
57
            } else {
70
                $instance->initialise($definition);
58
                $instance->initialise($definition);
71
                $this->run_tests($instance);
59
                $this->run_tests($instance);
72
            }
60
            }
73
        }
61
        }
74
        if ($modes & cache_store::MODE_SESSION) {
62
        if ($modes & cache_store::MODE_SESSION) {
Línea 75... Línea 63...
75
            $definition = cache_definition::load_adhoc(cache_store::MODE_SESSION, $class, 'phpunit_test');
63
            $definition = cache_definition::load_adhoc(cache_store::MODE_SESSION, $class, 'phpunit_test');
76
            $instance = new $class($class.'_test', $class::unit_test_configuration());
64
            $instance = new $class($class . '_test', $class::unit_test_configuration());
77
 
65
 
78
            if (!$instance->is_ready()) {
66
            if (!$instance->is_ready()) {
79
                $this->markTestSkipped('Could not test '.$class.'. No test instance configured for session caches.');
67
                $this->markTestSkipped('Could not test ' . $class . '. No test instance configured for session caches.');
80
            } else {
68
            } else {
81
                $instance->initialise($definition);
69
                $instance->initialise($definition);
82
                $this->run_tests($instance);
70
                $this->run_tests($instance);
83
            }
71
            }
84
        }
72
        }
Línea 85... Línea 73...
85
        if ($modes & cache_store::MODE_REQUEST) {
73
        if ($modes & cache_store::MODE_REQUEST) {
86
            $definition = cache_definition::load_adhoc(cache_store::MODE_REQUEST, $class, 'phpunit_test');
74
            $definition = cache_definition::load_adhoc(cache_store::MODE_REQUEST, $class, 'phpunit_test');
87
            $instance = new $class($class.'_test', $class::unit_test_configuration());
75
            $instance = new $class($class . '_test', $class::unit_test_configuration());
88
 
76
 
89
            if (!$instance->is_ready()) {
77
            if (!$instance->is_ready()) {
90
                $this->markTestSkipped('Could not test '.$class.'. No test instance configured for request caches.');
78
                $this->markTestSkipped('Could not test ' . $class . '. No test instance configured for request caches.');
91
            } else {
79
            } else {
Línea 97... Línea 85...
97
 
85
 
98
    /**
86
    /**
99
     * Test the store for basic functionality.
87
     * Test the store for basic functionality.
100
     */
88
     */
101
    public function run_tests(cache_store $instance) {
89
    public function run_tests(cache_store $instance) {
102
        $object = new stdClass;
90
        $object = new stdClass();
Línea 103... Línea 91...
103
        $object->data = 1;
91
        $object->data = 1;
104
 
92
 
105
        // Test set with a string.
93
        // Test set with a string.
Línea 114... Línea 102...
114
        $this->assertSame('3', $instance->get('test3'));
102
        $this->assertSame('3', $instance->get('test3'));
Línea 115... Línea 103...
115
 
103
 
116
        // Test find and find with prefix if this class implements the searchable interface.
104
        // Test find and find with prefix if this class implements the searchable interface.
117
        if ($instance->is_searchable()) {
105
        if ($instance->is_searchable()) {
118
            // Extra settings here ignore the return order of the array.
106
            // Extra settings here ignore the return order of the array.
Línea 119... Línea 107...
119
            $this->assertEqualsCanonicalizing(['test3', 'test1', 'test2', 'other3'], $instance->find_all());
107
            $this->assertEqualsCanonicalizing(['test3', 'test1', 'test2', 'other3'], array_values($instance->find_all()));
120
 
108
 
121
            // Extra settings here ignore the return order of the array.
109
            // Extra settings here ignore the return order of the array.
122
            $this->assertEqualsCanonicalizing(['test2', 'test1', 'test3'], $instance->find_by_prefix('test'));
110
            $this->assertEqualsCanonicalizing(['test2', 'test1', 'test3'], array_values($instance->find_by_prefix('test')));
123
            $this->assertEquals(['test2'], $instance->find_by_prefix('test2'));
111
            $this->assertEquals(['test2'], $instance->find_by_prefix('test2'));
124
            $this->assertEquals(['other3'], $instance->find_by_prefix('other'));
112
            $this->assertEquals(['other3'], $instance->find_by_prefix('other'));
Línea 161... Línea 149...
161
        $this->assertTrue($instance->purge());
149
        $this->assertTrue($instance->purge());
162
        $this->assertFalse($instance->get('test1'));
150
        $this->assertFalse($instance->get('test1'));
163
        $this->assertFalse($instance->get('test2'));
151
        $this->assertFalse($instance->get('test2'));
Línea 164... Línea 152...
164
 
152
 
165
        // Test set_many.
153
        // Test set_many.
166
        $outcome = $instance->set_many(array(
154
        $outcome = $instance->set_many([
167
            array('key' => 'many1', 'value' => 'many1'),
155
            ['key' => 'many1', 'value' => 'many1'],
168
            array('key' => 'many2', 'value' => 'many2'),
156
            ['key' => 'many2', 'value' => 'many2'],
169
            array('key' => 'many3', 'value' => 'many3'),
157
            ['key' => 'many3', 'value' => 'many3'],
170
            array('key' => 'many4', 'value' => 'many4'),
158
            ['key' => 'many4', 'value' => 'many4'],
171
            array('key' => 'many5', 'value' => 'many5')
159
            ['key' => 'many5', 'value' => 'many5'],
172
        ));
160
        ]);
173
        $this->assertSame(5, $outcome);
161
        $this->assertSame(5, $outcome);
174
        $this->assertSame('many1', $instance->get('many1'));
162
        $this->assertSame('many1', $instance->get('many1'));
175
        $this->assertSame('many5', $instance->get('many5'));
163
        $this->assertSame('many5', $instance->get('many5'));
Línea 176... Línea 164...
176
        $this->assertFalse($instance->get('many6'));
164
        $this->assertFalse($instance->get('many6'));
177
 
165
 
178
        // Test get_many.
166
        // Test get_many.
179
        $result = $instance->get_many(array('many1', 'many3', 'many5', 'many6'));
167
        $result = $instance->get_many(['many1', 'many3', 'many5', 'many6']);
180
        $this->assertIsArray($result);
168
        $this->assertIsArray($result);
181
        $this->assertCount(4, $result);
169
        $this->assertCount(4, $result);
182
        $this->assertSame(array(
170
        $this->assertSame([
183
            'many1' => 'many1',
171
            'many1' => 'many1',
184
            'many3' => 'many3',
172
            'many3' => 'many3',
185
            'many5' => 'many5',
173
            'many5' => 'many5',
Línea 186... Línea 174...
186
            'many6' => false,
174
            'many6' => false,
187
        ), $result);
175
        ], $result);
188
 
176
 
189
        // Test delete_many.
177
        // Test delete_many.
190
        $this->assertSame(3, $instance->delete_many(array('many2', 'many3', 'many4')));
178
        $this->assertSame(3, $instance->delete_many(['many2', 'many3', 'many4']));