[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 * 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 /** 29 * An abstract class to make writing unit tests for cache stores very easy. 30 * 31 * @package core 32 * @category cache 33 * @copyright 2013 Sam Hemelryk 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 abstract class cachestore_tests extends advanced_testcase { 37 38 /** 39 * Returns the class name for the store. 40 * 41 * @return string 42 */ 43 abstract protected function get_class_name(); 44 45 /** 46 * Run the unit tests for the store. 47 */ 48 public function test_test_instance() { 49 $class = $this->get_class_name(); 50 if (!class_exists($class) || !method_exists($class, 'initialise_test_instance') || !$class::are_requirements_met()) { 51 $this->markTestSkipped('Could not test '.$class.'. Requirements are not met.'); 52 } 53 54 $modes = $class::get_supported_modes(); 55 if ($modes & cache_store::MODE_APPLICATION) { 56 $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, $class, 'phpunit_test'); 57 $instance = $class::initialise_unit_test_instance($definition); 58 if (!$instance) { 59 $this->markTestSkipped('Could not test '.$class.'. No test instance configured for application caches.'); 60 } else { 61 $this->run_tests($instance); 62 } 63 } 64 if ($modes & cache_store::MODE_SESSION) { 65 $definition = cache_definition::load_adhoc(cache_store::MODE_SESSION, $class, 'phpunit_test'); 66 $instance = $class::initialise_unit_test_instance($definition); 67 if (!$instance) { 68 $this->markTestSkipped('Could not test '.$class.'. No test instance configured for session caches.'); 69 } else { 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'); 75 $instance = $class::initialise_unit_test_instance($definition); 76 if (!$instance) { 77 $this->markTestSkipped('Could not test '.$class.'. No test instance configured for request caches.'); 78 } else { 79 $this->run_tests($instance); 80 } 81 } 82 } 83 84 /** 85 * Test the store for basic functionality. 86 */ 87 public function run_tests(cache_store $instance) { 88 89 // Test set with a string. 90 $this->assertTrue($instance->set('test1', 'test1')); 91 $this->assertTrue($instance->set('test2', 'test2')); 92 $this->assertTrue($instance->set('test3', '3')); 93 94 // Test get with a string. 95 $this->assertSame('test1', $instance->get('test1')); 96 $this->assertSame('test2', $instance->get('test2')); 97 $this->assertSame('3', $instance->get('test3')); 98 99 // Test set with an int. 100 $this->assertTrue($instance->set('test1', 1)); 101 $this->assertTrue($instance->set('test2', 2)); 102 103 // Test get with an int. 104 $this->assertSame(1, $instance->get('test1')); 105 $this->assertInternalType('int', $instance->get('test1')); 106 $this->assertSame(2, $instance->get('test2')); 107 $this->assertInternalType('int', $instance->get('test2')); 108 109 // Test set with a bool. 110 $this->assertTrue($instance->set('test1', true)); 111 112 // Test get with an bool. 113 $this->assertSame(true, $instance->get('test1')); 114 $this->assertInternalType('boolean', $instance->get('test1')); 115 116 // Test delete. 117 $this->assertTrue($instance->delete('test1')); 118 $this->assertTrue($instance->delete('test3')); 119 $this->assertFalse($instance->delete('test3')); 120 $this->assertFalse($instance->get('test1')); 121 $this->assertSame(2, $instance->get('test2')); 122 $this->assertTrue($instance->set('test1', 'test1')); 123 124 // Test purge. 125 $this->assertTrue($instance->purge()); 126 $this->assertFalse($instance->get('test1')); 127 $this->assertFalse($instance->get('test2')); 128 129 // Test set_many. 130 $outcome = $instance->set_many(array( 131 array('key' => 'many1', 'value' => 'many1'), 132 array('key' => 'many2', 'value' => 'many2'), 133 array('key' => 'many3', 'value' => 'many3'), 134 array('key' => 'many4', 'value' => 'many4'), 135 array('key' => 'many5', 'value' => 'many5') 136 )); 137 $this->assertSame(5, $outcome); 138 $this->assertSame('many1', $instance->get('many1')); 139 $this->assertSame('many5', $instance->get('many5')); 140 $this->assertFalse($instance->get('many6')); 141 142 // Test get_many. 143 $result = $instance->get_many(array('many1', 'many3', 'many5', 'many6')); 144 $this->assertInternalType('array', $result); 145 $this->assertCount(4, $result); 146 $this->assertSame(array( 147 'many1' => 'many1', 148 'many3' => 'many3', 149 'many5' => 'many5', 150 'many6' => false, 151 ), $result); 152 153 // Test delete_many. 154 $this->assertSame(3, $instance->delete_many(array('many2', 'many3', 'many4'))); 155 $this->assertSame(2, $instance->delete_many(array('many1', 'many5', 'many6'))); 156 } 157 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |