orangehrm /lib/models/leave/WeekendsTest.php

Language PHP Lines 195
MD5 Hash a9d82cbd41402ea059f92f73538246e2 Estimated Cost $2,763 (why?)
Repository https://bitbucket.org/wildanm/orangehrm View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
 * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
 * all the essential functionalities required for any enterprise.
 * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
 *
 * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA
 *
 */


// Call WeekendsTest::main() if this source file is executed directly.
if (!defined("PHPUnit_MAIN_METHOD")) {
    define("PHPUnit_MAIN_METHOD", "WeekendsTest::main");
}

require_once "PHPUnit/Framework/TestCase.php";
require_once "PHPUnit/Framework/TestSuite.php";

require_once "testConf.php";

$_SESSION['WPATH'] = WPATH;

require_once 'Weekends.php';
require_once ROOT_PATH.'/lib/dao/DMLFunctions.php';
require_once ROOT_PATH."/lib/confs/Conf.php";

/**
 * Test class for Weekends.
 * Generated by PHPUnit_Util_Skeleton on 2007-01-05 at 05:42:38.
 */
class WeekendsTest extends PHPUnit_Framework_TestCase {
    public $classWeekend = null;
    public $connection = null;
	/**
     * Runs the test methods of this class.
     *
     * @access public
     * @static
     */
    public static function main() {
        require_once "PHPUnit/TextUI/TestRunner.php";

        $suite  = new PHPUnit_Framework_TestSuite("WeekendsTest");
        $result = PHPUnit_TextUI_TestRunner::run($suite);
    }

    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     *
     * @access protected
     */
    protected function setUp() {
		$this->classWeekend = new Weekends();

    	$conf = new Conf();

    	$this->connection = mysql_connect($conf->dbhost.":".$conf->dbport, $conf->dbuser, $conf->dbpass);

    	mysql_select_db($conf->dbname);
    	mysql_query("TRUNCATE TABLE `".Weekends::WEEKENDS_TABLE."`", $this->connection);
    	mysql_query("INSERT INTO `".Weekends::WEEKENDS_TABLE."` (`".Weekends::WEEKENDS_TABLE_DAY."`, `".Weekends::WEEKENDS_TABLE_LENGTH ."`) VALUES (1, ".Weekends::WEEKENDS_LENGTH_FULL_DAY.");");
    	mysql_query("INSERT INTO `".Weekends::WEEKENDS_TABLE."` (`".Weekends::WEEKENDS_TABLE_DAY."`, `".Weekends::WEEKENDS_TABLE_LENGTH ."`) VALUES (2, ".Weekends::WEEKENDS_LENGTH_FULL_DAY.");");
    	mysql_query("INSERT INTO `".Weekends::WEEKENDS_TABLE."` (`".Weekends::WEEKENDS_TABLE_DAY."`, `".Weekends::WEEKENDS_TABLE_LENGTH ."`) VALUES (3, ".Weekends::WEEKENDS_LENGTH_FULL_DAY.");");
    	mysql_query("INSERT INTO `".Weekends::WEEKENDS_TABLE."` (`".Weekends::WEEKENDS_TABLE_DAY."`, `".Weekends::WEEKENDS_TABLE_LENGTH ."`) VALUES (4, ".Weekends::WEEKENDS_LENGTH_FULL_DAY.");");
    	mysql_query("INSERT INTO `".Weekends::WEEKENDS_TABLE."` (`".Weekends::WEEKENDS_TABLE_DAY."`, `".Weekends::WEEKENDS_TABLE_LENGTH ."`) VALUES (5, ".Weekends::WEEKENDS_LENGTH_FULL_DAY.");");
    	mysql_query("INSERT INTO `".Weekends::WEEKENDS_TABLE."` (`".Weekends::WEEKENDS_TABLE_DAY."`, `".Weekends::WEEKENDS_TABLE_LENGTH ."`) VALUES (6, ".Weekends::WEEKENDS_LENGTH_HALF_DAY.");");
    	mysql_query("INSERT INTO `".Weekends::WEEKENDS_TABLE."` (`".Weekends::WEEKENDS_TABLE_DAY."`, `".Weekends::WEEKENDS_TABLE_LENGTH ."`) VALUES (7, ".Weekends::WEEKENDS_LENGTH_WEEKEND.");");

        mysql_query("INSERT INTO `hs_hr_employee`(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name, emp_nick_name) " .
                "VALUES (18, NULL, 'Nadeeth', 'H', 'Lansakara', 'rc')");
        mysql_query("INSERT INTO `hs_hr_leavetype` VALUES ('LTY010', 'Medical', 1)");
        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')");
        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)");
        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)");
        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)");
        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)");
    }

    /**
     * Tears down the fixture, for example, close a network connection.
     * This method is called after a test is executed.
     *
     * @access protected
     */
    protected function tearDown() {
    	mysql_query("TRUNCATE TABLE `".Weekends::WEEKENDS_TABLE."`", $this->connection);
        mysql_query("TRUNCATE TABLE `hs_hr_leave`", $this->connection);
        mysql_query("TRUNCATE TABLE `hs_hr_employee`", $this->connection);
        mysql_query("TRUNCATE TABLE `hs_hr_leave_requests`", $this->connection);
    }

    /**
     * @todo Implement testFetchWeek().
     */
    public function testFetchWeek() {
    	$expected[0] = array(1, 0);
    	$expected[1] = array(2, 0);
    	$expected[2] = array(3, 0);
    	$expected[3] = array(4, 0);
    	$expected[4] = array(5, 0);
    	$expected[5] = array(6, 4);
    	$expected[6] = array(7, 8);

        $res = $this->classWeekend->fetchWeek();

        $this->assertNotNull($res, 'Unexpected behavior');
        $this->assertEquals(count($res), 7, 'Invalid number of records found');

        for ($i=0; $i<count($res); $i++) {
        	$this->assertEquals($res[$i]->getDay(), $expected[$i][0], 'Invalid day');
        	$this->assertEquals($res[$i]->getLength(), $expected[$i][1], 'Invalid length');
        }

    }

    /**
     * @todo Implement testEditDay().
     */
    public function testEditDay() {

        $expected[0] = array(1, 0);
    	$expected[1] = array(2, 0);
    	$expected[2] = array(3, 0);
    	$expected[3] = array(4, 4);
    	$expected[4] = array(5, 8);
    	$expected[5] = array(6, 0);
    	$expected[6] = array(7, 0);

    	for ($i=0; $i<count($expected); $i++) {
	    	$this->classWeekend->setDay($expected[$i][0]);
	    	$this->classWeekend->setLength($expected[$i][1]);

	    	$this->classWeekend->editDay();
    	}

        $res = $this->classWeekend->fetchWeek();

        $this->assertNotNull($res, 'Unexpected behavior');
        $this->assertEquals(count($res), 7, 'Invalid number of records found');

        for ($i=0; $i<count($res); $i++) {
        	$this->assertEquals($res[$i]->getDay(), $expected[$i][0], 'Invalid day');
        	$this->assertEquals($res[$i]->getLength(), $expected[$i][1], 'Invalid length');
        }
    }

    public function testIsWeekend() {
		$this->assertTrue(Weekends::isWeekend("2008-07-27"));
		$this->assertFalse(Weekends::isWeekend("2008-07-26"));
		$this->assertFalse(Weekends::isWeekend("2008-07-25"));
    }

    public function testUpdateWeekendsForLeaves(){
        Weekends::updateWeekendsForLeaves();
        $dbConnection = new DMLFunctions();
        $query = "SELECT leave_id, leave_status FROM hs_hr_leave ";
        $result = $dbConnection -> executeQuery($query);
        $leaves = array();
        while($row = $dbConnection->dbObject->getArray($result)){
            $leaves[$row['leave_id']] = $row['leave_status'];
        }
        $this->assertEquals(4, $leaves['10'], 'Invalid status');
        $this->assertEquals(4, $leaves['11'], 'Invalid status');
        $this->assertEquals(3, $leaves['12'], 'Invalid status');
        $this->assertEquals(3, $leaves['13'], 'Invalid status');
    }

    public function testGetWeekendLength(){
        $sunday = Weekends::getWeekendLength('2009-02-15');
        $saturday = Weekends::getWeekendLength('2009-02-14');
        $friday = Weekends::getWeekendLength('2009-02-13');
        $this->assertEquals(8, $sunday, 'Invalid length');
        $this->assertEquals(4, $saturday, 'Invalid Length');
        $this->assertEquals(0, $friday, 'Invalid Length');
    }
}

// Call WeekendsTest::main() if this source file is executed directly.
if (PHPUnit_MAIN_METHOD == "WeekendsTest::main") {
    WeekendsTest::main();
}
?>
Back to Top