1<?php 2 3 4 5/** 6 * ConfigService Test Class 7 * @group Core 8 */ 9class ConfigServiceTest extends PHPUnit_Framework_TestCase { 10 11 private $configService; 12 13 /** 14 * Set up method 15 */ 16 protected function setUp() { 17 $this->configService = new ConfigService(); 18 } 19 20 /** 21 * Test the getConfigDao() and setConfigDao() method 22 */ 23 public function testGetSetConfigDao() { 24 $dao = $this->configService->getConfigDao(); 25 $this->assertTrue($dao instanceof ConfigDao); 26 27 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 28 $this->configService->setConfigDao($mockDao); 29 $dao = $this->configService->getConfigDao(); 30 $this->assertEquals($dao, $mockDao); 31 } 32 33 /** 34 * Test the setIsLeavePeriodDefined() method 35 */ 36 public function testSetIsLeavePeriodDefined() { 37 38 $value = 'Yes'; 39 40 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 41 $mockDao->expects($this->once()) 42 ->method('setValue') 43 ->with(ConfigService::KEY_LEAVE_PERIOD_DEFINED, $value); 44 45 $this->configService->setConfigDao($mockDao); 46 47 $this->configService->setIsLeavePeriodDefined($value); 48 49 // with invalid parameters 50 try { 51 $this->configService->setIsLeavePeriodDefined('test'); 52 $this->fail("Exception expected when invalid value passed to setisLeavePeriodDefined()"); 53 } catch (Exception $e) { 54 // expected 55 } 56 } 57 58 /** 59 * Test isLeavePeriodDefined() 60 */ 61 public function testIsLeavePeriodDefined() { 62 $value = true; 63 64 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 65 $mockDao->expects($this->once()) 66 ->method('getValue') 67 ->with(ConfigService::KEY_LEAVE_PERIOD_DEFINED) 68 ->will($this->returnValue($value)); 69 70 $this->configService->setConfigDao($mockDao); 71 72 $returnVal = $this->configService->isLeavePeriodDefined(); 73 $this->assertEquals($value, $returnVal); 74 } 75 76 /** 77 * Test setShowPimDeprecatedFields() method 78 */ 79 public function testSetShowPimDeprecatedFields() { 80 81 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 82 $mockDao->expects($this->once()) 83 ->method('setValue') 84 ->with(ConfigService::KEY_PIM_SHOW_DEPRECATED, 1); 85 86 $this->configService->setConfigDao($mockDao); 87 88 $this->configService->setShowPimDeprecatedFields(true); 89 90 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 91 $mockDao->expects($this->once()) 92 ->method('setValue') 93 ->with(ConfigService::KEY_PIM_SHOW_DEPRECATED, 0); 94 95 $this->configService->setConfigDao($mockDao); 96 97 $this->configService->setShowPimDeprecatedFields(false); 98 99 } 100 101 /** 102 * Test showPimDeprecatedFields() method 103 */ 104 public function testShowPimDeprecatedFields() { 105 106 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 107 $mockDao->expects($this->once()) 108 ->method('getValue') 109 ->with(ConfigService::KEY_PIM_SHOW_DEPRECATED) 110 ->will($this->returnValue('1')); 111 112 $this->configService->setConfigDao($mockDao); 113 114 $returnVal = $this->configService->showPimDeprecatedFields(); 115 $this->assertTrue($returnVal); 116 117 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 118 $mockDao->expects($this->once()) 119 ->method('getValue') 120 ->with(ConfigService::KEY_PIM_SHOW_DEPRECATED) 121 ->will($this->returnValue('0')); 122 123 $this->configService->setConfigDao($mockDao); 124 125 $returnVal = $this->configService->showPimDeprecatedFields(); 126 $this->assertFalse($returnVal); 127 128 } 129 130 public function testSetShowPimSSN() { 131 132 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 133 $mockDao->expects($this->once()) 134 ->method('setValue') 135 ->with(ConfigService::KEY_PIM_SHOW_SSN, 1); 136 137 $this->configService->setConfigDao($mockDao); 138 139 $this->configService->setShowPimSSN(true); 140 141 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 142 $mockDao->expects($this->once()) 143 ->method('setValue') 144 ->with(ConfigService::KEY_PIM_SHOW_SSN, 0); 145 146 $this->configService->setConfigDao($mockDao); 147 148 $this->configService->setShowPimSSN(false); 149 } 150 151 public function testShowPimSSN() { 152 153 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 154 $mockDao->expects($this->once()) 155 ->method('getValue') 156 ->with(ConfigService::KEY_PIM_SHOW_SSN) 157 ->will($this->returnValue('1')); 158 159 $this->configService->setConfigDao($mockDao); 160 161 $returnVal = $this->configService->showPimSSN(); 162 $this->assertTrue($returnVal); 163 164 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 165 $mockDao->expects($this->once()) 166 ->method('getValue') 167 ->with(ConfigService::KEY_PIM_SHOW_SSN) 168 ->will($this->returnValue('0')); 169 170 $this->configService->setConfigDao($mockDao); 171 172 $returnVal = $this->configService->showPimSSN(); 173 $this->assertFalse($returnVal); 174 } 175 176 public function testSetShowPimSIN() { 177 178 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 179 $mockDao->expects($this->once()) 180 ->method('setValue') 181 ->with(ConfigService::KEY_PIM_SHOW_SIN, 1); 182 183 $this->configService->setConfigDao($mockDao); 184 185 $this->configService->setShowPimSIN(true); 186 187 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 188 $mockDao->expects($this->once()) 189 ->method('setValue') 190 ->with(ConfigService::KEY_PIM_SHOW_SIN, 0); 191 192 $this->configService->setConfigDao($mockDao); 193 194 $this->configService->setShowPimSIN(false); 195 } 196 197 public function testShowPimSIN() { 198 199 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 200 $mockDao->expects($this->once()) 201 ->method('getValue') 202 ->with(ConfigService::KEY_PIM_SHOW_SIN) 203 ->will($this->returnValue('1')); 204 205 $this->configService->setConfigDao($mockDao); 206 207 $returnVal = $this->configService->showPimSIN(); 208 $this->assertTrue($returnVal); 209 210 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 211 $mockDao->expects($this->once()) 212 ->method('getValue') 213 ->with(ConfigService::KEY_PIM_SHOW_SIN) 214 ->will($this->returnValue('0')); 215 216 $this->configService->setConfigDao($mockDao); 217 218 $returnVal = $this->configService->showPimSIN(); 219 $this->assertFalse($returnVal); 220 } 221 222 public function testSetShowPimTaxExemptions() { 223 224 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 225 $mockDao->expects($this->once()) 226 ->method('setValue') 227 ->with(ConfigService::KEY_PIM_SHOW_TAX_EXEMPTIONS, 1); 228 229 $this->configService->setConfigDao($mockDao); 230 231 $this->configService->setShowPimTaxExemptions(true); 232 233 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 234 $mockDao->expects($this->once()) 235 ->method('setValue') 236 ->with(ConfigService::KEY_PIM_SHOW_TAX_EXEMPTIONS, 0); 237 238 $this->configService->setConfigDao($mockDao); 239 240 $this->configService->setShowPimTaxExemptions(false); 241 242 // Exception 243 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 244 $mockDao->expects($this->once()) 245 ->method('setValue') 246 ->with(ConfigService::KEY_PIM_SHOW_TAX_EXEMPTIONS, 0) 247 ->will($this->throwException(new DaoException())); 248 249 $this->configService->setConfigDao($mockDao); 250 251 try { 252 $this->configService->setShowPimTaxExemptions(false); 253 $this->fail("Exception expected"); 254 } catch (Exception $e) { 255 $this->assertTrue($e instanceof CoreServiceException); 256 } 257 258 } 259 260 public function testShowPimTaxExemptions() { 261 262 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 263 $mockDao->expects($this->once()) 264 ->method('getValue') 265 ->with(ConfigService::KEY_PIM_SHOW_TAX_EXEMPTIONS) 266 ->will($this->returnValue('1')); 267 268 $this->configService->setConfigDao($mockDao); 269 270 $returnVal = $this->configService->showPimTaxExemptions(); 271 $this->assertTrue($returnVal); 272 273 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 274 $mockDao->expects($this->once()) 275 ->method('getValue') 276 ->with(ConfigService::KEY_PIM_SHOW_TAX_EXEMPTIONS) 277 ->will($this->returnValue('0')); 278 279 $this->configService->setConfigDao($mockDao); 280 281 $returnVal = $this->configService->showPimTaxExemptions(); 282 $this->assertFalse($returnVal); 283 284 // Exception 285 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 286 $mockDao->expects($this->once()) 287 ->method('getValue') 288 ->with(ConfigService::KEY_PIM_SHOW_TAX_EXEMPTIONS) 289 ->will($this->throwException(new DaoException())); 290 291 $this->configService->setConfigDao($mockDao); 292 293 try { 294 $returnVal = $this->configService->showPimTaxExemptions(); 295 $this->fail("Exception expected"); 296 } catch (Exception $e) { 297 $this->assertTrue($e instanceof CoreServiceException); 298 } 299 300 301 } 302 303 public function testSetSupervisorChainSuported() { 304 305 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 306 $mockDao->expects($this->once()) 307 ->method('setValue') 308 ->with(ConfigService::KEY_INCLUDE_SUPERVISOR_CHAIN, 'Yes'); 309 310 $this->configService->setConfigDao($mockDao); 311 312 $this->configService->setSupervisorChainSuported(true); 313 314 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 315 $mockDao->expects($this->once()) 316 ->method('setValue') 317 ->with(ConfigService::KEY_INCLUDE_SUPERVISOR_CHAIN, 'No'); 318 319 $this->configService->setConfigDao($mockDao); 320 321 $this->configService->setSupervisorChainSuported(false); 322 323 } 324 325 public function testIsSupervisorChainSuported() { 326 327 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 328 $mockDao->expects($this->once()) 329 ->method('getValue') 330 ->with(ConfigService::KEY_INCLUDE_SUPERVISOR_CHAIN) 331 ->will($this->returnValue('Yes')); 332 333 $this->configService->setConfigDao($mockDao); 334 335 $returnVal = $this->configService->isSupervisorChainSuported(); 336 $this->assertTrue($returnVal); 337 338 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 339 $mockDao->expects($this->once()) 340 ->method('getValue') 341 ->with(ConfigService::KEY_INCLUDE_SUPERVISOR_CHAIN) 342 ->will($this->returnValue('No')); 343 344 $this->configService->setConfigDao($mockDao); 345 346 $returnVal = $this->configService->isSupervisorChainSuported(); 347 $this->assertFalse($returnVal); 348 } 349 350 public function testGetDefaultWorkShiftStartTime() { 351 $startTime = '09:30'; 352 $this->validateGetMethod('getDefaultWorkShiftStartTime', ConfigService::KEY_ADMIN_DEFAULT_WORKSHIFT_START_TIME, $startTime); 353 } 354 355 public function testSetDefaultWorkShiftStartTime() { 356 $startTime = '11:30'; 357 $this->validateSetMethod('setDefaultWorkShiftStartTime', ConfigService::KEY_ADMIN_DEFAULT_WORKSHIFT_START_TIME, $startTime); 358 359 } 360 361 public function testGetDefaultWorkShiftEndTime() { 362 $startTime = '09:30'; 363 $this->validateGetMethod('getDefaultWorkShiftEndTime', ConfigService::KEY_ADMIN_DEFAULT_WORKSHIFT_END_TIME, $startTime); 364 } 365 366 public function testSetDefaultWorkShiftEndTime() { 367 $startTime = '11:30'; 368 $this->validateSetMethod('setDefaultWorkShiftEndTime', ConfigService::KEY_ADMIN_DEFAULT_WORKSHIFT_END_TIME, $startTime); 369 370 } 371 372 public function testGetAllValues() { 373 $allValues = array('k1' => 'v1', 'k2' => 'v2'); 374 $mockDao = $this->getMockBuilder('ConfigDao') 375 ->setMethods( array('getAllValues')) 376 ->getMock(); 377 $mockDao->expects($this->once()) 378 ->method('getAllValues') 379 ->will($this->returnValue($allValues)); 380 381 $this->configService->setConfigDao($mockDao); 382 $this->assertEquals($allValues, $this->configService->getAllValues()); 383 } 384 385 protected function validateGetMethod($method, $key, $expected) { 386 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 387 $mockDao->expects($this->once()) 388 ->method('getValue') 389 ->with($key) 390 ->will($this->returnValue($expected)); 391 392 $this->configService->setConfigDao($mockDao); 393 394 $returnVal = $this->configService->$method(); 395 $this->assertEquals($returnVal, $expected); 396 } 397 398 protected function validateSetMethod($method, $key, $value) { 399 $mockDao = $this->getMockBuilder('ConfigDao')->getMock(); 400 $mockDao->expects($this->once()) 401 ->method('setValue') 402 ->with($key, $value); 403 404 $this->configService->setConfigDao($mockDao); 405 406 $this->configService->$method($value); 407 } 408 409 public function testSetOpenIdProviderAdded(){ 410 $value = 'on'; 411 $this->validateSetMethod('setOpenIdProviderAdded', ConfigService::KEY_OPENID_PROVIDER_ADDED, $value); 412 413 } 414 415 public function testGetOpenIdProviderAdded(){ 416 $value = 'off'; 417 $this->validateGetMethod('getOpenIdProviderAdded', ConfigService::KEY_OPENID_PROVIDER_ADDED, $value); 418 } 419 420} 421 422