PageRenderTime 73ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/common/ConfigTest.php

https://bitbucket.org/wildanm/orangehrm
PHP | 258 lines | 162 code | 57 blank | 39 comment | 22 complexity | b7a9a4d2bf571f4b62ab69fd672ffd9f MD5 | raw file
Possible License(s): CC-BY-SA-3.0, AGPL-3.0, BSD-3-Clause, AGPL-1.0, GPL-2.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. // Call ConfigTest::main() if this source file is executed directly.
  3. if (!defined('PHPUnit_MAIN_METHOD')) {
  4. define('PHPUnit_MAIN_METHOD', 'ConfigTest::main');
  5. }
  6. require_once 'PHPUnit/Framework.php';
  7. require_once "testConf.php";
  8. require_once ROOT_PATH."/lib/confs/Conf.php";
  9. require_once 'Config.php';
  10. /**
  11. * Test class for Config.
  12. * Generated by PHPUnit on 2008-02-18 at 18:37:33.
  13. */
  14. class ConfigTest extends PHPUnit_Framework_TestCase {
  15. private $oldTimesheetSetValue;
  16. private $leaveBroughtForwardSet;
  17. private $leaveBroughtForwardGet;
  18. /**
  19. * Runs the test methods of this class.
  20. *
  21. * @access public
  22. * @static
  23. */
  24. public static function main() {
  25. require_once 'PHPUnit/TextUI/TestRunner.php';
  26. $suite = new PHPUnit_Framework_TestSuite('ConfigTest');
  27. $result = PHPUnit_TextUI_TestRunner::run($suite);
  28. }
  29. /**
  30. * Sets up the fixture, for example, opens a network connection.
  31. * This method is called before a test is executed.
  32. *
  33. * @access protected
  34. */
  35. protected function setUp() {
  36. $conf = new Conf();
  37. $this->connection = mysql_connect($conf->dbhost.":".$conf->dbport, $conf->dbuser, $conf->dbpass);
  38. mysql_select_db($conf->dbname);
  39. $this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `nilai` = '0000-00-00' WHERE `key` = 'hsp_accrued_last_updated'"));
  40. $this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `nilai` = '0000-00-00' WHERE `key` = 'hsp_used_last_updated'"));
  41. $result = mysql_query("SELECT `value` FROM `hs_hr_config` WHERE `nilai` = 'timesheet_period_set'");
  42. if (mysql_num_rows($result) == 0) {
  43. $this->oldTimesheetSetValue = false;
  44. } else {
  45. $row = mysql_fetch_array($result, MYSQL_NUM);
  46. $oldValue = $row[0];
  47. }
  48. // For LeaveBroughtForward Setter
  49. $result = mysql_query("SELECT `key` FROM `hs_hr_config` WHERE `nilai` = 'LeaveBroughtForward".date('Y')."'");
  50. if (mysql_num_rows($result) == 0) {
  51. $this->leaveBroughtForwardSet = false;
  52. } else {
  53. $this->assertTrue(mysql_query("DELETE FROM `hs_hr_config` WHERE `nilai` = 'LeaveBroughtForward".date('Y')."'"), mysql_error());
  54. $this->leaveBroughtForwardSet = true;
  55. }
  56. // For Leave BroughtForward Getter
  57. $result = mysql_query("SELECT `key` FROM `hs_hr_config` WHERE `nilai` = 'LeaveBroughtForward".(date('Y')+1)."'");
  58. if (mysql_num_rows($result) == 0) {
  59. $this->assertTrue(mysql_query("INSERT INTO `hs_hr_config` (`nilai`, `value`) VALUES('LeaveBroughtForward".(date('Y')+1)."', 'set')"), mysql_error());
  60. $this->leaveBroughtForwardGet = false;
  61. } else {
  62. $this->leaveBroughtForwardGet = true;
  63. }
  64. }
  65. /**
  66. * Tears down the fixture, for example, closes a network connection.
  67. * This method is called after a test is executed.
  68. *
  69. * @access protected
  70. */
  71. protected function tearDown() {
  72. $this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `nilai` = '0000-00-00' WHERE `key` = 'hsp_accrued_last_updated'"));
  73. $this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `nilai` = '0000-00-00' WHERE `key` = 'hsp_used_last_updated'"));
  74. if ($this->oldTimesheetSetValue === false) {
  75. $this->assertTrue(mysql_query("DELETE FROM `hs_hr_config` WHERE `nilai` = 'timesheet_period_set'"));
  76. } else {
  77. $this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `nilai` = '$this->oldTimesheetSetValue' WHERE `key` = 'timesheet_period_set'"));
  78. }
  79. // For LeaveBroghtForward Setter
  80. if ($this->leaveBroughtForwardSet === false) {
  81. $this->assertTrue(mysql_query("DELETE FROM `hs_hr_config` WHERE `nilai` = 'LeaveBroughtForward".date('Y')."'"), mysql_error());
  82. } else {
  83. $result = mysql_query("SELECT `key` FROM `hs_hr_config` WHERE `nilai` = 'LeaveBroughtForward".date('Y')."'");
  84. if (mysql_num_rows($result) == 0) {
  85. $this->assertTrue(mysql_query("INSERT INTO `hs_hr_config` (`nilai`, `value`) VALUES('LeaveBroughtForward".date('Y')."', 'set')"), mysql_error());
  86. }
  87. }
  88. // For LeaveBroughtForward Getter
  89. if ($this->leaveBroughtForwardGet === false) {
  90. $this->assertTrue(mysql_query("DELETE FROM `hs_hr_config` WHERE `nilai` = 'LeaveBroughtForward".(date('Y')+1)."'"), mysql_error());
  91. }
  92. }
  93. public function testSetHspAccruedLastUpdated() {
  94. // Testing for invalid date formats.
  95. try {
  96. Config::setHspAccruedLastUpdated("dsereregg");
  97. $this->fail("Invalid dates are allowed");
  98. } catch (Exception $e) {}
  99. // Testing without giving a key, 'key' value already exists.
  100. Config::setHspAccruedLastUpdated("2008-02-18");
  101. $result = mysql_query("SELECT `value` FROM `hs_hr_config` WHERE `nilai`='hsp_accrued_last_updated'");
  102. $resultArray = mysql_fetch_array($result);
  103. $this->assertEquals($resultArray[0], "2008-02-18");
  104. // Testing by giving a key, 'key' value already exists.
  105. Config::setHspAccruedLastUpdated("2008-02-27", "hsp_accrued_last_updated");
  106. $result = mysql_query("SELECT `value` FROM `hs_hr_config` WHERE `nilai`='hsp_accrued_last_updated'");
  107. $resultArray = mysql_fetch_array($result);
  108. $this->assertEquals($resultArray[0], "2008-02-27");
  109. // Testing by giving a key, 'key' doesn't exist.
  110. Config::setHspAccruedLastUpdated("1000-01-01", "this_should_not_exist");
  111. $result = mysql_query("SELECT `value` FROM `hs_hr_config` WHERE `nilai`='this_should_not_exist'");
  112. $resultArray = mysql_fetch_array($result);
  113. $this->assertEquals($resultArray[0], "1000-01-01");
  114. $this->assertTrue(mysql_query("DELETE FROM `hs_hr_config` WHERE `nilai`='this_should_not_exist'"));
  115. }
  116. public function testGetHspAccruedLastUpdated() {
  117. // Testing normal usage.
  118. $this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `nilai` = '2008-02-25' WHERE `key` = 'hsp_accrued_last_updated'"));
  119. $this->assertNotNull(Config::getHspAccruedLastUpdated());
  120. $this->assertEquals(Config::getHspAccruedLastUpdated(), "2008-02-25");
  121. }
  122. public function testSetHspUsedLastUpdated() {
  123. // Testing for invalid date formats.
  124. try {
  125. Config::setHspUsedLastUpdated("dsereregg");
  126. $this->fail("Invalid dates are allowed");
  127. } catch (Exception $e) {}
  128. // Testing without giving a key, 'key' value already exists.
  129. Config::setHspUsedLastUpdated("2008-03-18");
  130. $result = mysql_query("SELECT `value` FROM `hs_hr_config` WHERE `nilai`='hsp_used_last_updated'");
  131. $resultArray = mysql_fetch_array($result);
  132. $this->assertEquals($resultArray[0], "2008-03-18");
  133. // Testing by giving a key, 'key' value already exists.
  134. Config::setHspUsedLastUpdated("2008-03-27", "hsp_used_last_updated");
  135. $result = mysql_query("SELECT `value` FROM `hs_hr_config` WHERE `nilai`='hsp_used_last_updated'");
  136. $resultArray = mysql_fetch_array($result);
  137. $this->assertEquals($resultArray[0], "2008-03-27");
  138. // Testing by giving a key, 'key' doesn't exist.
  139. Config::setHspUsedLastUpdated("1000-01-01", "this_should_not_exist");
  140. $result = mysql_query("SELECT `value` FROM `hs_hr_config` WHERE `nilai`='this_should_not_exist'");
  141. $resultArray = mysql_fetch_array($result);
  142. $this->assertEquals($resultArray[0], "1000-01-01");
  143. $this->assertTrue(mysql_query("DELETE FROM `hs_hr_config` WHERE `nilai`='this_should_not_exist'"));
  144. }
  145. public function testGetHspUsedLastUpdated() {
  146. // Testing normal usage.
  147. $this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `nilai` = '2008-03-25' WHERE `key` = 'hsp_used_last_updated'"));
  148. $this->assertNotNull(Config::getHspUsedLastUpdated());
  149. $this->assertEquals(Config::getHspUsedLastUpdated(), "2008-03-25");
  150. }
  151. public function testSetTimePeriodSet() {
  152. Config::setTimePeriodSet('Yes');
  153. $result = mysql_query("SELECT `value` FROM `hs_hr_config` WHERE `nilai` = 'timesheet_period_set'");
  154. $row = mysql_fetch_array($result, MYSQL_NUM);
  155. $value = $row[0];
  156. $this->assertEquals('Yes', $value);
  157. Config::setTimePeriodSet('No');
  158. $result = mysql_query("SELECT `value` FROM `hs_hr_config` WHERE `nilai` = 'timesheet_period_set'");
  159. $row = mysql_fetch_array($result, MYSQL_NUM);
  160. $value = $row[0];
  161. $this->assertEquals('No', $value);
  162. try {
  163. Config::setTimePeriodSet('ABC');
  164. $this->fail('Invalid arguments are allowed. Did not throw an exception');
  165. } catch(Exception $e) {
  166. $this->assertEquals("Given value for TimeSheetPeriodSet should be 'Yes' or 'No'", $e->getMessage());
  167. }
  168. }
  169. public function testGetTimePeriodSet() {
  170. $this->assertFalse(Config::getTimePeriodSet());
  171. if ($this->oldTimesheetSetValue === false) {
  172. $this->assertTrue(mysql_query("INSERT INTO `hs_hr_config` VALUES ('timesheet_period_set', 'Yes')"));
  173. } else {
  174. $this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `nilai` = 'Yes' WHERE `key` = 'timesheet_period_set'"));
  175. }
  176. $this->assertTrue(Config::getTimePeriodSet());
  177. $this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `nilai` = 'No' WHERE `key` = 'timesheet_period_set'"));
  178. $this->assertFalse(Config::getTimePeriodSet());
  179. }
  180. public function testSetLeaveBroughtForward() {
  181. Config::setLeaveBroughtForward(date('Y'));
  182. $result = mysql_query("SELECT `key`, `value` FROM `hs_hr_config` WHERE `nilai` = 'LeaveBroughtForward".date('Y')."'");
  183. $row = mysql_fetch_array($result);
  184. $this->assertEquals("LeaveBroughtForward".date('Y'), $row['key'], "Key is incorrect");
  185. $this->assertEquals("set", $row['value'], "Value is incorrect");
  186. // Setting LeaveBroughtForward should not be allowed more than once
  187. try {
  188. Config::setLeaveBroughtForward(date('Y'));
  189. $this->fail("Setting LeaveBroughtForward is allowed more than once");
  190. } catch (Exception $e) {}
  191. }
  192. public function testGetLeaveBroughtForward() {
  193. $this->assertTrue(Config::getLeaveBroughtForward((date('Y')+1)));
  194. $this->assertFalse(Config::getLeaveBroughtForward('4000'));
  195. }
  196. }
  197. // Call ConfigTest::main() if this source file is executed directly.
  198. if (PHPUnit_MAIN_METHOD == 'ConfigTest::main') {
  199. ConfigTest::main();
  200. }
  201. ?>