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 * Unit tests for lib/outputcomponents.php. 19 * 20 * @package core 21 * @category test 22 * @copyright 2011 David Mudrak <david@moodle.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 namespace core\output; 27 28 use advanced_testcase; 29 use coding_exception; 30 31 /** 32 * Unit tests for the `icon_system` class. 33 * 34 * @coversDefaultClass \core\output\icon_system 35 */ 36 class icon_system_test extends advanced_testcase { 37 /** 38 * Check whether the supplied classes are valid icon subsystems of the supplied one. 39 * 40 * @covers ::is_valid_system 41 * @dataProvider is_valid_subsystem_provider 42 * @param string $parent The class to call ::is_valid_system() on 43 * @param string $system The class to request 44 * @param bool $expected Whether the supplied relationship is valid 45 */ 46 public function test_is_valid_subsystem(string $parent, string $system, bool $expected): void { 47 $this->assertEquals($expected, $parent::is_valid_system($system)); 48 } 49 50 /** 51 * Ensure that the ::instance() function throws an appropriate Exception when an inappropriate relationship is 52 * specified. 53 * 54 * @covers ::instance 55 * @dataProvider invalid_instance_provider 56 * @param string $parent The class to call ::instance() on 57 * @param string $system The class to request 58 */ 59 public function test_invalid_instance(string $parent, string $system): void { 60 $this->expectException(coding_exception::class); 61 $this->expectExceptionMessage("Invalid icon system requested '{$system}'"); 62 63 $parent::instance($system); 64 } 65 66 /** 67 * Ensure that the ::instance() function returns an instance of the supplied system for a valid icon system 68 * relationship. 69 * 70 * @covers ::instance 71 * @dataProvider valid_instance_provider 72 * @param string $parent The class to call ::instance() on 73 * @param string $system The class to request 74 */ 75 public function test_valid_instance(string $parent, string $system): void { 76 $instance = $parent::instance($system); 77 $this->assertInstanceOf($parent, $instance); 78 $this->assertInstanceOf($system, $instance); 79 } 80 81 /** 82 * Ensure that subsequent calls without arguments to ::instance() return the exact same instance. 83 * 84 * @covers ::instance 85 */ 86 public function test_instance_singleton(): void { 87 $singleton = icon_system::instance(); 88 89 // Calling instance() again returns the same singleton. 90 $this->assertSame($singleton, icon_system::instance()); 91 } 92 93 /** 94 * Ensure thaat subsequent calls with an argument to ::instance() return the exact same instance. 95 * 96 * @covers ::instance 97 */ 98 public function test_instance_singleton_named_default(): void { 99 global $PAGE; 100 $singleton = icon_system::instance(); 101 102 $defaultsystem = $PAGE->theme->get_icon_system(); 103 $this->assertSame($singleton, icon_system::instance($defaultsystem)); 104 } 105 106 /** 107 * Ensure that ::instance() returns an instance of the correct icon system when requested on the core icon_system 108 * class. 109 * 110 * @covers ::instance 111 * @dataProvider valid_instance_provider 112 * @param string $parent The class to call ::instance() on 113 * @param string $child The class to request 114 */ 115 public function test_instance_singleton_named(string $parent, string $child): void { 116 $iconsystem = icon_system::instance($child); 117 $this->assertInstanceOf($child, $iconsystem); 118 } 119 120 /** 121 * Ensure that ::instance() returns an instance of the correct icon system when called on a named parent class. 122 * 123 * @covers ::instance 124 * @dataProvider valid_instance_provider 125 * @param string $parent The class to call ::instance() on 126 * @param string $child The class to request 127 */ 128 public function test_instance_singleton_named_child(string $parent, string $child): void { 129 $iconsystem = $parent::instance($child); 130 $this->assertInstanceOf($parent, $iconsystem); 131 $this->assertInstanceOf($child, $iconsystem); 132 } 133 134 /** 135 * Ensure that the ::reset_caches() function resets the stored instance such that ::instance() returns a new 136 * instance in subsequent calls. 137 * 138 * @covers ::instance 139 * @covers ::reset_caches 140 */ 141 public function test_instance_singleton_reset(): void { 142 $singleton = icon_system::instance(); 143 144 // Reset the cache. 145 icon_system::reset_caches(); 146 147 // Calling instance() again returns a new singleton. 148 $newsingleton = icon_system::instance(); 149 $this->assertNotSame($singleton, $newsingleton); 150 151 // Calling it again gets the new singleton. 152 $this->assertSame($newsingleton, icon_system::instance()); 153 } 154 155 /** 156 * Returns data for data providers containing: 157 * - parent icon system 158 * - child icon system 159 * - whether it is a valid child 160 * 161 * @return array 162 */ 163 public function icon_system_provider(): array { 164 return [ 165 'icon_system => icon_system_standard' => [ 166 icon_system::class, 167 icon_system_standard::class, 168 true, 169 ], 170 'icon_system => icon_system_fontawesome' => [ 171 icon_system::class, 172 icon_system_fontawesome::class, 173 true, 174 ], 175 'icon_system => \theme_classic\output\icon_system_fontawesome' => [ 176 icon_system::class, 177 \theme_classic\output\icon_system_fontawesome::class, 178 true, 179 ], 180 'icon_system => notification' => [ 181 icon_system::class, 182 notification::class, 183 false, 184 ], 185 186 'icon_system_standard => icon_system_standard' => [ 187 icon_system_standard::class, 188 icon_system_standard::class, 189 true, 190 ], 191 'icon_system_standard => icon_system_fontawesome' => [ 192 icon_system_standard::class, 193 icon_system_fontawesome::class, 194 false, 195 ], 196 'icon_system_standard => \theme_classic\output\icon_system_fontawesome' => [ 197 icon_system_standard::class, 198 \theme_classic\output\icon_system_fontawesome::class, 199 false, 200 ], 201 'icon_system_fontawesome => icon_system_standard' => [ 202 icon_system_fontawesome::class, 203 icon_system_standard::class, 204 false, 205 ], 206 ]; 207 } 208 209 /** 210 * Data provider for tests of `is_valid`. 211 * 212 * @return array 213 */ 214 public function is_valid_subsystem_provider(): array { 215 return $this->icon_system_provider(); 216 } 217 218 /** 219 * Data provider for tests of `instance` containing only invalid tests. 220 * 221 * @return array 222 */ 223 public function invalid_instance_provider(): array { 224 return array_filter( 225 $this->icon_system_provider(), 226 function($data) { 227 return !$data[2]; 228 }, 229 ARRAY_FILTER_USE_BOTH 230 ); 231 } 232 233 /** 234 * Data provider for tests of `instance` containing only valid tests. 235 * 236 * @return array 237 */ 238 public function valid_instance_provider(): array { 239 return array_filter( 240 $this->icon_system_provider(), 241 function($data) { 242 return $data[2]; 243 }, 244 ARRAY_FILTER_USE_BOTH 245 ); 246 } 247 248 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body