PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/models/leave/LeaveQuotaTest.php

https://bitbucket.org/wildanm/orangehrm
PHP | 336 lines | 248 code | 38 blank | 50 comment | 4 complexity | 4b74e2ff86f3fdbed56ca3c7a3cce344 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. /**
  3. * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
  4. * all the essential functionalities required for any enterprise.
  5. * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
  6. *
  7. * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
  8. * the GNU General Public License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  12. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with this program;
  16. * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA
  18. *
  19. */
  20. // Call LeaveQuotaTest::main() if this source file is executed directly.
  21. if (!defined("PHPUnit_MAIN_METHOD")) {
  22. define("PHPUnit_MAIN_METHOD", "LeaveQuotaTest::main");
  23. }
  24. require_once "PHPUnit/Framework/TestCase.php";
  25. require_once "PHPUnit/Framework/TestSuite.php";
  26. require_once "testConf.php";
  27. $_SESSION['WPATH'] = WPATH;
  28. require_once "LeaveQuota.php";
  29. require_once ROOT_PATH."/lib/confs/Conf.php";
  30. /**
  31. * Test class for LeaveQuota.
  32. * Generated by PHPUnit_Util_Skeleton on 2006-10-19 at 06:19:48.
  33. */
  34. class LeaveQuotaTest extends PHPUnit_Framework_TestCase {
  35. /**
  36. * Runs the test methods of this class.
  37. *
  38. * @access public
  39. * @static
  40. */
  41. public $classLeaveQuota = null;
  42. public $connection = null;
  43. public static function main() {
  44. require_once "PHPUnit/TextUI/TestRunner.php";
  45. $suite = new PHPUnit_Framework_TestSuite("LeaveQuotaTest");
  46. $result = PHPUnit_TextUI_TestRunner::run($suite);
  47. }
  48. /**
  49. * Sets up the fixture, for example, open a network connection.
  50. * This method is called before a test is executed.
  51. *
  52. * @access protected
  53. */
  54. protected function setUp() {
  55. $this->classLeaveQuota = new LeaveQuota();
  56. $conf = new Conf();
  57. $this->connection = mysql_connect($conf->dbhost.":".$conf->dbport, $conf->dbuser, $conf->dbpass);
  58. mysql_select_db($conf->dbname);
  59. $this->_deleteTestData();
  60. $this->_runQuery("INSERT INTO `hs_hr_employee`(emp_number, emp_lastname, emp_firstname, emp_nick_name, coun_code) " .
  61. "VALUES ('011', 'Arnold', 'Subasinghe', 'Arnold', 'AF')");
  62. $this->_runQuery("INSERT INTO `hs_hr_employee`(emp_number, emp_lastname, emp_firstname, emp_middle_name, emp_nick_name) " .
  63. "VALUES ('012', 'Mohanjith', 'Sudirikku', 'Hannadige', 'MOHA')");
  64. $this->_runQuery("INSERT INTO `hs_hr_employee`(emp_number, emp_lastname, emp_firstname, emp_middle_name, emp_nick_name) " .
  65. "VALUES ('020', 'Mohanjithx', 'Sudirikkux', 'Hannadigex', 'MOHAx')");
  66. $this->_runQuery("INSERT INTO `hs_hr_employee`(emp_number, emp_lastname, emp_firstname, emp_middle_name, emp_nick_name) " .
  67. "VALUES ('015', 'Mohanjith1', 'Sudirikku1', 'Hannadige1', 'MOHA1')");
  68. $this->_runQuery("INSERT INTO `hs_hr_employee`(emp_number, emp_lastname, emp_firstname, emp_middle_name, emp_nick_name) " .
  69. "VALUES ('016', 'Gayanath', 'Wageeshwara', 'Jayarathne', 'MOHA1')");
  70. $this->_runQuery("INSERT INTO `hs_hr_employee`(emp_number, emp_lastname, emp_firstname, emp_middle_name, emp_nick_name) " .
  71. "VALUES ('017', 'Ajith', 'Kumara', 'Bandara', 'MOHA1')");
  72. mysql_query("INSERT INTO `hs_hr_leavetype` VALUES ('LTY010', 'Medical', 1)");
  73. mysql_query("INSERT INTO `hs_hr_leavetype` VALUES ('LTY011', 'Casual', 1)");
  74. mysql_query("INSERT INTO `hs_hr_leavetype` VALUES ('LTY012', 'Personal', 1)");
  75. mysql_query("INSERT INTO `hs_hr_leavetype` VALUES ('LTY013', 'Annual', 1)");
  76. mysql_query("INSERT INTO `hs_hr_leavetype` VALUES ('LTY014', 'Special', 0)");
  77. mysql_query("INSERT INTO `hs_hr_employee_leave_quota` (year, leave_type_id, employee_id, no_of_days_allotted) VALUES ('".date('Y')."', 'LTY010', '012', 10);");
  78. mysql_query("INSERT INTO `hs_hr_employee_leave_quota` (year, leave_type_id, employee_id, no_of_days_allotted) VALUES ('".date('Y')."', 'LTY011', '012', 20);");
  79. mysql_query("INSERT INTO `hs_hr_employee_leave_quota` (year, leave_type_id, employee_id, no_of_days_allotted) VALUES ('".date('Y')."', 'LTY010', '011', 10);");
  80. mysql_query("INSERT INTO `hs_hr_employee_leave_quota` (year, leave_type_id, employee_id, no_of_days_allotted) VALUES ('".date('Y')."', 'LTY011', '011', 20);");
  81. mysql_query("INSERT INTO `hs_hr_employee_leave_quota` (year, leave_type_id, employee_id, no_of_days_allotted, leave_taken) VALUES ('2009', 'LTY012', '016', 20, 10);");
  82. mysql_query("INSERT INTO `hs_hr_employee_leave_quota` (year, leave_type_id, employee_id, no_of_days_allotted, leave_taken) VALUES ('2010', 'LTY012', '016', 20, 10);");
  83. mysql_query("INSERT INTO `hs_hr_employee_leave_quota` (year, leave_type_id, employee_id, no_of_days_allotted, leave_taken, leave_brought_forward) VALUES ('2010', 'LTY014', '016', 20, 10, 5);");
  84. mysql_query("INSERT INTO `hs_hr_employee_leave_quota` (year, leave_type_id, employee_id, no_of_days_allotted, leave_taken, leave_brought_forward) VALUES ('2011', 'LTY012', '016', 10, 20 , 10);");
  85. mysql_query("INSERT INTO `hs_hr_employee_leave_quota` (year, leave_type_id, employee_id, no_of_days_allotted, leave_taken, leave_brought_forward) VALUES ('2012', 'LTY012', '016', 10, 10 , 5);");
  86. $previousYear = date('Y') - 1;
  87. mysql_query("INSERT INTO `hs_hr_employee_leave_quota` (year, leave_type_id, employee_id, no_of_days_allotted, leave_taken, leave_brought_forward) VALUES ('".$previousYear."', 'LTY013', '017', 20, 10, 5);");
  88. echo mysql_error();
  89. }
  90. /**
  91. * Tears down the fixture, for example, close a network connection.
  92. * This method is called after a test is executed.
  93. *
  94. * @access protected
  95. */
  96. protected function tearDown() {
  97. $this->_deleteTestData();
  98. $this->connection = null;
  99. }
  100. /**
  101. * Deletes test data created during test
  102. */
  103. private function _deleteTestData() {
  104. mysql_query("DELETE FROM `hs_hr_leavetype` WHERE `Leave_Type_ID` = 'LTY011'", $this->connection);
  105. mysql_query("DELETE FROM `hs_hr_employee` WHERE `emp_number` = '011'", $this->connection);
  106. mysql_query("DELETE FROM `hs_hr_employee` WHERE `emp_number` = '012'", $this->connection);
  107. mysql_query("DELETE FROM `hs_hr_employee` WHERE `emp_number` = '020'", $this->connection);
  108. mysql_query("DELETE FROM `hs_hr_employee` WHERE `emp_number` = '015'", $this->connection);
  109. mysql_query("DELETE FROM `hs_hr_employee` WHERE `emp_number` = '016'", $this->connection);
  110. mysql_query("DELETE FROM `hs_hr_employee` WHERE `emp_number` = '017'", $this->connection);
  111. mysql_query("DELETE FROM `hs_hr_employee_leave_quota` WHERE `Employee_ID` = '012'", $this->connection);
  112. mysql_query("DELETE FROM `hs_hr_employee_leave_quota` WHERE `Employee_ID` = '011'", $this->connection);
  113. mysql_query("DELETE FROM `hs_hr_employee_leave_quota` WHERE `Employee_ID` = '020'", $this->connection);
  114. mysql_query("DELETE FROM `hs_hr_employee_leave_quota` WHERE `Employee_ID` = '015'", $this->connection);
  115. mysql_query("DELETE FROM `hs_hr_employee_leave_quota` WHERE `Employee_ID` = '016'", $this->connection);
  116. mysql_query("DELETE FROM `hs_hr_employee_leave_quota` WHERE `Employee_ID` = '017'", $this->connection);
  117. mysql_query("DELETE FROM `hs_hr_leavetype` WHERE `Leave_Type_ID` = 'LTY010'", $this->connection);
  118. mysql_query("DELETE FROM `hs_hr_leavetype` WHERE `Leave_Type_ID` = 'LTY012'", $this->connection);
  119. mysql_query("DELETE FROM `hs_hr_leavetype` WHERE `Leave_Type_ID` = 'LTY013'", $this->connection);
  120. mysql_query("DELETE FROM `hs_hr_leavetype` WHERE `Leave_Type_ID` = 'LTY014'", $this->connection);
  121. }
  122. /**
  123. * Run given sql query
  124. */
  125. private function _runQuery($sql) {
  126. $this->assertTrue(mysql_query($sql), mysql_error());
  127. }
  128. public function testAddLeaveQuotaAccuracy1() {
  129. $expected[] = array(date('Y'), "LTY011", "Casual", "20");
  130. $expected[] = array(date('Y'), "LTY010", "Medical", "10");
  131. for ($i=0; $i < count($expected); $i++) {
  132. $this->classLeaveQuota->setYear($expected[$i][0]);
  133. $this->classLeaveQuota->setLeaveTypeId($expected[$i][1]);
  134. $this->classLeaveQuota->setNoOfDaysAllotted($expected[$i][3]);
  135. $res = $this->classLeaveQuota->addLeaveQuota("020");
  136. $this->assertNotNull($res, "Addition failed - $i ");
  137. }
  138. $res = $this->classLeaveQuota->fetchLeaveQuota("020");
  139. $this->assertNotNull($res, "No record found ");
  140. $this->assertEquals(count($res), 2, "Number of records found is not accurate ");
  141. for ($i=0; $i < count($res); $i++) {
  142. $this->assertEquals($res[$i]->getYear(), $expected[$i][0], "Didn't return expected result ");
  143. $this->assertEquals($res[$i]->getLeaveTypeId(), $expected[$i][1], "Didn't return expected result ");
  144. $this->assertEquals($res[$i]->getLeaveTypeName(), $expected[$i][2], "Didn't return expected result ");
  145. $this->assertEquals($res[$i]->getNoOfDaysAllotted(), $expected[$i][3], "Didn't return expected result ");
  146. }
  147. }
  148. /**
  149. * @todo Implement testEditLeaveQuota().
  150. */
  151. public function testEditLeaveQuota() {
  152. $expected[] = array(date('Y'), "LTY010", "Medical", "15");
  153. $expected[] = array(date('Y'), "LTY011", "Casual", "18");
  154. for ($i=0; $i<count($expected); $i++) {
  155. $this->classLeaveQuota->setYear($expected[$i][0]);
  156. $this->classLeaveQuota->setLeaveTypeId($expected[$i][1]);
  157. $this->classLeaveQuota->setNoOfDaysAllotted($expected[$i][3]);
  158. $this->classLeaveQuota->setEmployeeId("015");
  159. $res = $this->classLeaveQuota->editLeaveQuota();
  160. $this->assertNotNull($res, "Didn't add non exsistant record - $i ");
  161. }
  162. }
  163. public function testEditLeaveQuota1() {
  164. $expected[] = array(date('Y'), "LTY011", "Casual", "18");
  165. $expected[] = array(date('Y'), "LTY010", "Medical", "15");
  166. for ($i=0; $i < count($expected); $i++) {
  167. $this->classLeaveQuota->setYear($expected[$i][0]);
  168. $this->classLeaveQuota->setLeaveTypeId($expected[$i][1]);
  169. $this->classLeaveQuota->setNoOfDaysAllotted($expected[$i][3]);
  170. $this->classLeaveQuota->setEmployeeId("011");
  171. $res = $this->classLeaveQuota->editLeaveQuota();
  172. $this->assertNotNull($res, "Addition failed - $i ");
  173. }
  174. $res = $this->classLeaveQuota->fetchLeaveQuota("011");
  175. $this->assertNotNull($res, "No record found ");
  176. $this->assertEquals(count($res), 2, "Number of records found is not accurate ");
  177. for ($i=0; $i < count($res); $i++) {
  178. $this->assertEquals($res[$i]->getYear(), $expected[$i][0], "Didn't return expected result ");
  179. $this->assertEquals($res[$i]->getLeaveTypeId(), $expected[$i][1], "Didn't return expected result ");
  180. $this->assertEquals($res[$i]->getLeaveTypeName(), $expected[$i][2], "Didn't return expected result ");
  181. $this->assertEquals($res[$i]->getNoOfDaysAllotted(), $expected[$i][3], "Didn't return expected result ");
  182. }
  183. }
  184. /**
  185. * @todo Implement testDeleteLeaveQuota().
  186. */
  187. /*public function testDeleteLeaveQuota() {
  188. // Remove the following line when you implement this test.
  189. $this->markTestIncomplete(
  190. "This test has not been implemented yet."
  191. );
  192. }*/
  193. /**
  194. * @todo Implement testFetchLeaveQuota().
  195. */
  196. public function testFetchLeaveQuota() {
  197. $res = $this->classLeaveQuota->fetchLeaveQuota("015");
  198. $this->assertNull($res, "Retured non exsistant record ");
  199. }
  200. public function testFetchLeaveQuotaAccuracy() {
  201. $res = $this->classLeaveQuota->fetchLeaveQuota("012");
  202. $this->assertNotNull($res, "No record found ");
  203. $this->assertEquals(count($res), 2, "Number of records found is not accurate ");
  204. $expected[] = array(date('Y'), "LTY011", "Casual", "20");
  205. $expected[] = array(date('Y'), "LTY010", "Medical", "10");
  206. for ($i=0; $i < count($res); $i++) {
  207. $this->assertEquals($res[$i]->getYear(), $expected[$i][0], "Didn't return expected result ");
  208. $this->assertEquals($res[$i]->getLeaveTypeId(), $expected[$i][1], "Didn't return expected result ");
  209. $this->assertEquals($res[$i]->getLeaveTypeName(), $expected[$i][2], "Didn't return expected result ");
  210. $this->assertEquals($res[$i]->getNoOfDaysAllotted(), $expected[$i][3], "Didn't return expected result ");
  211. }
  212. }
  213. /**
  214. * Test for copyLeaveBroughtForward()
  215. */
  216. public function testCopyLeaveBroughtForward() {
  217. $broughtForward = new LeaveQuota();
  218. $res = $broughtForward->copyLeaveBroughtForward(2009,2010);
  219. $this->assertTrue($res);
  220. $query = "SELECT * FROM hs_hr_employee_leave_quota WHERE year = '2010' AND leave_type_id = 'LTY012' AND employee_id = '16'";
  221. $result = mysql_query($query);
  222. $row = mysql_fetch_array($result);
  223. $this->assertEquals(10, $row['leave_brought_forward'], $row['leave_brought_forward']);
  224. }
  225. public function testCheckLeaveBroughtForward() {
  226. $broughtForward = new LeaveQuota();
  227. $year = date('Y') - 1;
  228. $this->assertTrue($broughtForward->checkBroughtForward($year));
  229. }
  230. public function testIsLeaveQuotaDeleted() {
  231. $quota = $this->classLeaveQuota->fetchLeaveQuota(16);
  232. $result = $quota[0]->isLeaveQuotaDeleted();
  233. $this->assertFalse($result);
  234. $result = $quota[1]->isLeaveQuotaDeleted();
  235. $this->assertFalse($result);
  236. $result = $quota[4]->isLeaveQuotaDeleted();
  237. $this->assertTrue($result);
  238. }
  239. public function testIsBalanceZero1(){
  240. $this->classLeaveQuota->setYear("2011");
  241. $this->classLeaveQuota->setLeaveTypeId("LTY012");
  242. $this->classLeaveQuota->setEmployeeId("016");
  243. $res = $this->classLeaveQuota->isBalanceZero();
  244. $this->assertTrue($res);
  245. }
  246. public function testIsBalanceZero2(){
  247. $this->classLeaveQuota->setYear("2012");
  248. $this->classLeaveQuota->setLeaveTypeId("LTY012");
  249. $this->classLeaveQuota->setEmployeeId("016");
  250. $res = $this->classLeaveQuota->isBalanceZero();
  251. $this->assertFalse($res);
  252. }
  253. }
  254. // Call LeaveQuotaTest::main() if this source file is executed directly.
  255. if (PHPUnit_MAIN_METHOD == "LeaveQuotaTest::main") {
  256. LeaveQuotaTest::main();
  257. }
  258. ?>