PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/models/leave/WeekendsTest.php

https://bitbucket.org/wildanm/orangehrm
PHP | 194 lines | 116 code | 30 blank | 48 comment | 6 complexity | a9d82cbd41402ea059f92f73538246e2 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 WeekendsTest::main() if this source file is executed directly.
  21. if (!defined("PHPUnit_MAIN_METHOD")) {
  22. define("PHPUnit_MAIN_METHOD", "WeekendsTest::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 'Weekends.php';
  29. require_once ROOT_PATH.'/lib/dao/DMLFunctions.php';
  30. require_once ROOT_PATH."/lib/confs/Conf.php";
  31. /**
  32. * Test class for Weekends.
  33. * Generated by PHPUnit_Util_Skeleton on 2007-01-05 at 05:42:38.
  34. */
  35. class WeekendsTest extends PHPUnit_Framework_TestCase {
  36. public $classWeekend = null;
  37. public $connection = null;
  38. /**
  39. * Runs the test methods of this class.
  40. *
  41. * @access public
  42. * @static
  43. */
  44. public static function main() {
  45. require_once "PHPUnit/TextUI/TestRunner.php";
  46. $suite = new PHPUnit_Framework_TestSuite("WeekendsTest");
  47. $result = PHPUnit_TextUI_TestRunner::run($suite);
  48. }
  49. /**
  50. * Sets up the fixture, for example, open a network connection.
  51. * This method is called before a test is executed.
  52. *
  53. * @access protected
  54. */
  55. protected function setUp() {
  56. $this->classWeekend = new Weekends();
  57. $conf = new Conf();
  58. $this->connection = mysql_connect($conf->dbhost.":".$conf->dbport, $conf->dbuser, $conf->dbpass);
  59. mysql_select_db($conf->dbname);
  60. mysql_query("TRUNCATE TABLE `".Weekends::WEEKENDS_TABLE."`", $this->connection);
  61. mysql_query("INSERT INTO `".Weekends::WEEKENDS_TABLE."` (`".Weekends::WEEKENDS_TABLE_DAY."`, `".Weekends::WEEKENDS_TABLE_LENGTH ."`) VALUES (1, ".Weekends::WEEKENDS_LENGTH_FULL_DAY.");");
  62. mysql_query("INSERT INTO `".Weekends::WEEKENDS_TABLE."` (`".Weekends::WEEKENDS_TABLE_DAY."`, `".Weekends::WEEKENDS_TABLE_LENGTH ."`) VALUES (2, ".Weekends::WEEKENDS_LENGTH_FULL_DAY.");");
  63. mysql_query("INSERT INTO `".Weekends::WEEKENDS_TABLE."` (`".Weekends::WEEKENDS_TABLE_DAY."`, `".Weekends::WEEKENDS_TABLE_LENGTH ."`) VALUES (3, ".Weekends::WEEKENDS_LENGTH_FULL_DAY.");");
  64. mysql_query("INSERT INTO `".Weekends::WEEKENDS_TABLE."` (`".Weekends::WEEKENDS_TABLE_DAY."`, `".Weekends::WEEKENDS_TABLE_LENGTH ."`) VALUES (4, ".Weekends::WEEKENDS_LENGTH_FULL_DAY.");");
  65. mysql_query("INSERT INTO `".Weekends::WEEKENDS_TABLE."` (`".Weekends::WEEKENDS_TABLE_DAY."`, `".Weekends::WEEKENDS_TABLE_LENGTH ."`) VALUES (5, ".Weekends::WEEKENDS_LENGTH_FULL_DAY.");");
  66. mysql_query("INSERT INTO `".Weekends::WEEKENDS_TABLE."` (`".Weekends::WEEKENDS_TABLE_DAY."`, `".Weekends::WEEKENDS_TABLE_LENGTH ."`) VALUES (6, ".Weekends::WEEKENDS_LENGTH_HALF_DAY.");");
  67. mysql_query("INSERT INTO `".Weekends::WEEKENDS_TABLE."` (`".Weekends::WEEKENDS_TABLE_DAY."`, `".Weekends::WEEKENDS_TABLE_LENGTH ."`) VALUES (7, ".Weekends::WEEKENDS_LENGTH_WEEKEND.");");
  68. mysql_query("INSERT INTO `hs_hr_employee`(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name, emp_nick_name) " .
  69. "VALUES (18, NULL, 'Nadeeth', 'H', 'Lansakara', 'rc')");
  70. mysql_query("INSERT INTO `hs_hr_leavetype` VALUES ('LTY010', 'Medical', 1)");
  71. mysql_query("INSERT INTO `hs_hr_leave_requests` (`leave_request_id`, `leave_type_id`, `leave_type_name`, `date_applied`, `employee_id`) VALUES (11, 'LTY010', 'Medical', '".date('Y-m-d', time()+3600*24)."', '18')");
  72. mysql_query("INSERT INTO `hs_hr_leave` (`leave_id`, `employee_id`, `leave_type_id`, `leave_date`, `leave_length_hours`, `leave_length_days`, `leave_status`, `leave_comments`, `leave_request_id`) VALUES (10, '18', 'LTY010', '2009-02-07', 4, 0.5, 3, '', 11)");
  73. mysql_query("INSERT INTO `hs_hr_leave` (`leave_id`, `employee_id`, `leave_type_id`, `leave_date`, `leave_length_hours`, `leave_length_days`, `leave_status`, `leave_comments`, `leave_request_id`) VALUES (11, '18', 'LTY010', '2009-02-08', 8, 1, 3, '', 11)");
  74. mysql_query("INSERT INTO `hs_hr_leave` (`leave_id`, `employee_id`, `leave_type_id`, `leave_date`, `leave_length_hours`, `leave_length_days`, `leave_status`, `leave_comments`, `leave_request_id`) VALUES (12, '18', 'LTY010', '2009-02-09', 8, 1, 4, '', 11)");
  75. mysql_query("INSERT INTO `hs_hr_leave` (`leave_id`, `employee_id`, `leave_type_id`, `leave_date`, `leave_length_hours`, `leave_length_days`, `leave_status`, `leave_comments`, `leave_request_id`) VALUES (13, '18', 'LTY010', '2009-02-10', 8, 1, 4, '', 11)");
  76. }
  77. /**
  78. * Tears down the fixture, for example, close a network connection.
  79. * This method is called after a test is executed.
  80. *
  81. * @access protected
  82. */
  83. protected function tearDown() {
  84. mysql_query("TRUNCATE TABLE `".Weekends::WEEKENDS_TABLE."`", $this->connection);
  85. mysql_query("TRUNCATE TABLE `hs_hr_leave`", $this->connection);
  86. mysql_query("TRUNCATE TABLE `hs_hr_employee`", $this->connection);
  87. mysql_query("TRUNCATE TABLE `hs_hr_leave_requests`", $this->connection);
  88. }
  89. /**
  90. * @todo Implement testFetchWeek().
  91. */
  92. public function testFetchWeek() {
  93. $expected[0] = array(1, 0);
  94. $expected[1] = array(2, 0);
  95. $expected[2] = array(3, 0);
  96. $expected[3] = array(4, 0);
  97. $expected[4] = array(5, 0);
  98. $expected[5] = array(6, 4);
  99. $expected[6] = array(7, 8);
  100. $res = $this->classWeekend->fetchWeek();
  101. $this->assertNotNull($res, 'Unexpected behavior');
  102. $this->assertEquals(count($res), 7, 'Invalid number of records found');
  103. for ($i=0; $i<count($res); $i++) {
  104. $this->assertEquals($res[$i]->getDay(), $expected[$i][0], 'Invalid day');
  105. $this->assertEquals($res[$i]->getLength(), $expected[$i][1], 'Invalid length');
  106. }
  107. }
  108. /**
  109. * @todo Implement testEditDay().
  110. */
  111. public function testEditDay() {
  112. $expected[0] = array(1, 0);
  113. $expected[1] = array(2, 0);
  114. $expected[2] = array(3, 0);
  115. $expected[3] = array(4, 4);
  116. $expected[4] = array(5, 8);
  117. $expected[5] = array(6, 0);
  118. $expected[6] = array(7, 0);
  119. for ($i=0; $i<count($expected); $i++) {
  120. $this->classWeekend->setDay($expected[$i][0]);
  121. $this->classWeekend->setLength($expected[$i][1]);
  122. $this->classWeekend->editDay();
  123. }
  124. $res = $this->classWeekend->fetchWeek();
  125. $this->assertNotNull($res, 'Unexpected behavior');
  126. $this->assertEquals(count($res), 7, 'Invalid number of records found');
  127. for ($i=0; $i<count($res); $i++) {
  128. $this->assertEquals($res[$i]->getDay(), $expected[$i][0], 'Invalid day');
  129. $this->assertEquals($res[$i]->getLength(), $expected[$i][1], 'Invalid length');
  130. }
  131. }
  132. public function testIsWeekend() {
  133. $this->assertTrue(Weekends::isWeekend("2008-07-27"));
  134. $this->assertFalse(Weekends::isWeekend("2008-07-26"));
  135. $this->assertFalse(Weekends::isWeekend("2008-07-25"));
  136. }
  137. public function testUpdateWeekendsForLeaves(){
  138. Weekends::updateWeekendsForLeaves();
  139. $dbConnection = new DMLFunctions();
  140. $query = "SELECT leave_id, leave_status FROM hs_hr_leave ";
  141. $result = $dbConnection -> executeQuery($query);
  142. $leaves = array();
  143. while($row = $dbConnection->dbObject->getArray($result)){
  144. $leaves[$row['leave_id']] = $row['leave_status'];
  145. }
  146. $this->assertEquals(4, $leaves['10'], 'Invalid status');
  147. $this->assertEquals(4, $leaves['11'], 'Invalid status');
  148. $this->assertEquals(3, $leaves['12'], 'Invalid status');
  149. $this->assertEquals(3, $leaves['13'], 'Invalid status');
  150. }
  151. public function testGetWeekendLength(){
  152. $sunday = Weekends::getWeekendLength('2009-02-15');
  153. $saturday = Weekends::getWeekendLength('2009-02-14');
  154. $friday = Weekends::getWeekendLength('2009-02-13');
  155. $this->assertEquals(8, $sunday, 'Invalid length');
  156. $this->assertEquals(4, $saturday, 'Invalid Length');
  157. $this->assertEquals(0, $friday, 'Invalid Length');
  158. }
  159. }
  160. // Call WeekendsTest::main() if this source file is executed directly.
  161. if (PHPUnit_MAIN_METHOD == "WeekendsTest::main") {
  162. WeekendsTest::main();
  163. }
  164. ?>