<?php
// Call HspTest::main() if this source file is executed directly.
if (!defined('PHPUnit_MAIN_METHOD')) {
    define('PHPUnit_MAIN_METHOD', 'HspTest::main');
}



require_once "testConf.php";
require_once ROOT_PATH."/lib/confs/Conf.php";

require_once 'Hsp.php';

/**
 * Test class for Hsp.
 * Generated by PHPUnit on 2008-02-19 at 11:29:52.
 */
class HspTest extends PHPUnit_Framework_TestCase {
	private $paymentRequestFields;
	private $employeeFields;

    /**
     * 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('HspTest');
        $result = PHPUnit_TextUI_TestRunner::run($suite);
    }

    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     *
     * @access protected
     */
    protected function setUp() {
    	$this->paymentRequestFields = "`id`, `hsp_id`, `employee_id`, `date_incurred`, `provider_name`, `person_incurring_expense`, " .
								"`expense_description`, `expense_amount`, `payment_made_to`, `third_party_account_number`, " .
								"`mail_address`, `comments`, `date_paid`, `check_number`,	`status`";
		$this->employeeFields = "`emp_number`, `employee_id`, `emp_lastname`, `emp_firstname`, `emp_middle_name`, `emp_nick_name`, " .
								"`emp_smoker`, `ethnic_race_code`, `emp_birthday`, `nation_code`, `emp_gender`, `emp_marital_status`, " .
								"`emp_ssn_num`, `emp_sin_num`, `emp_other_id`, `emp_dri_lice_num`, `emp_dri_lice_exp_date`, `emp_military_service`, " .
								"`emp_status`, `job_title_code`, `eeo_cat_code`, `work_station`, `emp_street1`, `emp_street2`, " .
								"`city_code`, `coun_code`, `provin_code`, `emp_zipcode`, `emp_hm_telephone`, `emp_mobile`, " .
								"`emp_work_telephone`, `emp_work_email`, `sal_grd_code`, `joined_date`, `emp_oth_email`";

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

    }

    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     *
     * @access protected
     */
    protected function tearDown() {
    	$this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_hsp_summary`"));
    	$this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_employee`"));
    	$this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `value` = '0' WHERE `key` = 'hsp_current_plan'"));
    }

    public function testUpdateAccrued() {

		$this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_pay_period`"));
		$this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_hsp_summary`"));

		// Here 'check dates' don't mach with pay periods. They have been added to get three past check dates from current day.
		$this->assertTrue(mysql_query("INSERT INTO hs_hr_pay_period VALUES(1, '".date('Y')."-01-01', '".date('Y')."-01-31', '".date('Y')."-01-31', '".date('Y-m-d', time()-3600*24*3)."', '".date('Y')."-01-20')"));
		$this->assertTrue(mysql_query("INSERT INTO hs_hr_pay_period VALUES(2, '".date('Y')."-02-01', '".date('Y')."-02-29', '".date('Y')."-02-29', '".date('Y-m-d', time()-3600*24*2)."', '".date('Y')."-02-20')"));
		$this->assertTrue(mysql_query("INSERT INTO hs_hr_pay_period VALUES(3, '".date('Y')."-03-01', '".date('Y')."-03-31', '".date('Y')."-03-31', '".date('Y-m-d', time()-3600*24*1)."', '".date('Y')."-03-20')"));

		// Summary Insert
		$this->assertTrue(mysql_query("INSERT INTO hs_hr_hsp_summary VALUES(1, 1, 1, '".date('Y')."', 1, 1500, 50, 50, 0, 0)"), mysql_error());
		$this->assertTrue(mysql_query("INSERT INTO hs_hr_hsp_summary VALUES(2, 2, 1, '".date('Y')."', 1, 2500, 75, 75, 0, 0)"), mysql_error());

		$this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `value` = '".(date('Y')-1)."-12-31' WHERE `key` = 'hsp_accrued_last_updated'"));
		$this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `value` = '1' WHERE `key` = 'hsp_current_plan'"));

		$result = mysql_query("SELECT COUNT(*) FROM `hs_hr_pay_period` WHERE `check_date` < '".date('Y-m-d')."'");
		$resultArray = mysql_fetch_array($result);

		$expectedAccrued1 = 100*$resultArray[0]; // 100 comes from Summary Insert (50+50)
		$expectedAccrued2 = 150*$resultArray[0];

		Hsp::updateAccrued(date('Y')); // total_accrued is calculated by function and should be equal to $expectedAccrued

		$result = mysql_query("SELECT `total_accrued` FROM `hs_hr_hsp_summary` WHERE `summary_id` = 1");
		$resultArray = mysql_fetch_array($result);

		$this->assertEquals($resultArray[0], $expectedAccrued1);

		$result = mysql_query("SELECT `total_accrued` FROM `hs_hr_hsp_summary` WHERE `summary_id` = 2");
		$resultArray = mysql_fetch_array($result);

		$this->assertEquals($resultArray[0], $expectedAccrued2);

   }

    public function testUpdateAccruedHalted() {

		$this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_pay_period`"));
		$this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_hsp_summary`"));

		// Here 'check dates' don't mach with pay periods. They have been added to get three past check dates from current day.
		$this->assertTrue(mysql_query("INSERT INTO hs_hr_pay_period VALUES(1, '".date('Y')."-01-01', '".date('Y')."-01-31', '".date('Y')."-01-31', '".date('Y-m-d', time()-3600*24*3)."', '".date('Y')."-01-20')"));
		$this->assertTrue(mysql_query("INSERT INTO hs_hr_pay_period VALUES(2, '".date('Y')."-02-01', '".date('Y')."-02-29', '".date('Y')."-02-29', '".date('Y-m-d', time()-3600*24*2)."', '".date('Y')."-02-20')"));
		$this->assertTrue(mysql_query("INSERT INTO hs_hr_pay_period VALUES(3, '".date('Y')."-03-01', '".date('Y')."-03-31', '".date('Y')."-03-31', '".date('Y-m-d', time()-3600*24*1)."', '".date('Y')."-03-20')"));

		// Summary Insert
		$this->assertTrue(mysql_query("INSERT INTO hs_hr_hsp_summary VALUES(1, 1, 1, '".date('Y')."', 0, 1500, 50, 50, 823, 0)"), mysql_error());
		$this->assertTrue(mysql_query("INSERT INTO hs_hr_hsp_summary VALUES(2, 2, 1, '".date('Y')."', 0, 2500, 75, 75, 0, 0)"), mysql_error());

		$this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `value` = '".(date('Y')-1)."-12-31' WHERE `key` = 'hsp_accrued_last_updated'"));
		$this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `value` = '1' WHERE `key` = 'hsp_current_plan'"));

		$result = mysql_query("SELECT COUNT(*) FROM `hs_hr_pay_period` WHERE `check_date` < '".date('Y-m-d')."'");
		$resultArray = mysql_fetch_array($result);

		// Since `hsp_plan_status` is 0 no accrued should happen
		$expectedAccrued1 = 823;
		$expectedAccrued2 = 0;

		Hsp::updateAccrued(date('Y')); // total_accrued is calculated by function and should be equal to $expectedAccrued

		$result = mysql_query("SELECT `total_accrued` FROM `hs_hr_hsp_summary` WHERE `summary_id` = 1");
		$resultArray = mysql_fetch_array($result);

		$this->assertEquals($expectedAccrued1, $resultArray[0]);

		$result = mysql_query("SELECT `total_accrued` FROM `hs_hr_hsp_summary` WHERE `summary_id` = 2");
		$resultArray = mysql_fetch_array($result);

		$this->assertEquals($expectedAccrued2, $resultArray[0]);

   }

    public function testUpdateAccruedForTwoPlans() {

		$this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_pay_period`"));
		$this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_hsp_summary`"));

		// Here 'check dates' don't mach with pay periods. They have been added to get three past check dates from current day.
		$this->assertTrue(mysql_query("INSERT INTO hs_hr_pay_period VALUES(1, '".date('Y')."-01-01', '".date('Y')."-01-31', '".date('Y')."-01-31', '".date('Y-m-d', time()-3600*24*3)."', '".date('Y')."-01-20')"));
		$this->assertTrue(mysql_query("INSERT INTO hs_hr_pay_period VALUES(2, '".date('Y')."-02-01', '".date('Y')."-02-29', '".date('Y')."-02-29', '".date('Y-m-d', time()-3600*24*2)."', '".date('Y')."-02-20')"));
		$this->assertTrue(mysql_query("INSERT INTO hs_hr_pay_period VALUES(3, '".date('Y')."-03-01', '".date('Y')."-03-31', '".date('Y')."-03-31', '".date('Y-m-d', time()-3600*24*1)."', '".date('Y')."-03-20')"));

		// Summary Insert
		$this->assertTrue(mysql_query("INSERT INTO hs_hr_hsp_summary VALUES(1, 1, 1, '".date('Y')."', 1, 1500, 50, 50, 0, 0)"), mysql_error());
		$this->assertTrue(mysql_query("INSERT INTO hs_hr_hsp_summary VALUES(2, 1, 3, '".date('Y')."', 1, 2500, 75, 75, 0, 0)"), mysql_error());

		$this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `value` = '".(date('Y')-1)."-12-31' WHERE `key` = 'hsp_accrued_last_updated'"));

		$result = mysql_query("SELECT COUNT(*) FROM `hs_hr_pay_period` WHERE `check_date` < '".date('Y-m-d')."'");
		$resultArray = mysql_fetch_array($result);

		$expectedAccrued1 = 100*$resultArray[0]; // 100 comes from Summary Insert (50+50)
		$expectedAccrued2 = 150*$resultArray[0];

		$this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `value` = '4' WHERE `key` = 'hsp_current_plan'"));

		Hsp::updateAccrued(date('Y')); // total_accrued is calculated by function and should be equal to $expectedAccrued

		$result = mysql_query("SELECT `total_accrued` FROM `hs_hr_hsp_summary` WHERE `summary_id` = 1");
		$resultArray = mysql_fetch_array($result);

		$this->assertEquals($expectedAccrued1, $resultArray[0]);

		$result = mysql_query("SELECT `total_accrued` FROM `hs_hr_hsp_summary` WHERE `summary_id` = 2");
		$resultArray = mysql_fetch_array($result);

		$this->assertEquals($expectedAccrued2, $resultArray[0]);

    }

    public function testUpdateUsed() {

		$this->assertTrue(mysql_query("TRUNCATE `hs_hr_employee`;", $this->connection), mysql_error());
        $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_hsp_payment_request`"), mysql_error());
		$this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_hsp_summary`"), mysql_error());

		$this->assertTrue(mysql_query("INSERT INTO `hs_hr_employee` ($this->employeeFields) VALUES (1, '001', 'Arnold', 'Subasinghe', '', 'Arnold', 0, NULL, NULL, NULL, NULL, NULL, '', '', '', '', NULL, '', NULL, NULL, NULL, NULL, '', '', '', 'AF', '', '', '', '', '', '', NULL, NULL, '')"), mysql_error());
		$this->assertTrue(mysql_query("INSERT INTO `hs_hr_employee` ($this->employeeFields) VALUES (2, '002', 'Kalum', 'Kumara', '', 'Kal', 0, NULL, NULL, NULL, NULL, NULL, '', '', '', '', NULL, '', NULL, NULL, NULL, NULL, '', '', '', 'AF', '', '', '', '', '', '', NULL, NULL, '')"), mysql_error());

		// For employee1
		$this->assertTrue(mysql_query("INSERT INTO `hs_hr_hsp_payment_request` ($this->paymentRequestFields) " .
	    							  "VALUES (1, 1, 1, '".date('Y')."-02-01', 'Test provider', 'Tester', 'Just testing', '150', 'TestX', '12345GD', " .
	    							  "'1231, Test Grove, Test City', 'Test', '".date('Y')."-02-02',  '123552-55821-ff25', 1)"), mysql_error());

	    $this->assertTrue(mysql_query("INSERT INTO `hs_hr_hsp_payment_request` ($this->paymentRequestFields) " .
	    							  "VALUES (2, 1, 1, '".date('Y')."-02-10', 'Test provider', 'Tester', 'Just testing', '100', 'TestX', '12345GD', " .
	    							  "'1231, Test Grove, Test City', 'Test', '".date('Y')."-02-11',  '123552-55821-ff25', 1)"), mysql_error());

		$this->assertTrue(mysql_query("INSERT INTO `hs_hr_hsp_payment_request` ($this->paymentRequestFields) " .
	    							  "VALUES (3, 1, 1, '".date('Y')."-02-20', 'Test provider', 'Tester', 'Just testing', '127', 'TestX', '12345GD', " .
	    							  "'1231, Test Grove, Test City', 'Test', '".date('Y')."-02-21',  '123552-55821-ff25', 1)"), mysql_error());

		// For employee2
		$this->assertTrue(mysql_query("INSERT INTO `hs_hr_hsp_payment_request` ($this->paymentRequestFields) " .
	    							  "VALUES (4, 1, 2, '".date('Y')."-02-01', 'Test provider', 'Tester', 'Just testing', '122', 'TestX', '12345GD', " .
	    							  "'1231, Test Grove, Test City', 'Test', '".date('Y')."-02-02',  '123552-55821-ff25', 1)"), mysql_error());

	    $this->assertTrue(mysql_query("INSERT INTO `hs_hr_hsp_payment_request` ($this->paymentRequestFields) " .
	    							  "VALUES (5, 1, 2, '".date('Y')."-02-10', 'Test provider', 'Tester', 'Just testing', '41', 'TestX', '12345GD', " .
	    							  "'1231, Test Grove, Test City', 'Test', '".date('Y')."-02-11',  '123552-55821-ff25', 1)"), mysql_error());


		$this->assertTrue(mysql_query("INSERT INTO hs_hr_hsp_summary VALUES(1, 1, 1, '".date('Y')."', 1, 1500, 50, 50, 0, 22)"), mysql_error());
		$this->assertTrue(mysql_query("INSERT INTO hs_hr_hsp_summary VALUES(2, 2, 1, '".date('Y')."', 1, 2500, 75, 75, 0, 0)"), mysql_error());

		$this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `value` = '".(date('Y')-1)."-12-31' WHERE `key` = 'hsp_used_last_updated'"));
		$this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `value` = '1' WHERE `key` = 'hsp_current_plan'"));

		Hsp::updateUsed(date('Y')); // total_used is calculated by function and should be equal to expense sum of employee1 (150+100+127) plus existing value (22)

		$result = mysql_query("SELECT `total_used` FROM `hs_hr_hsp_summary` WHERE `summary_id` = 1");
		$resultArray = mysql_fetch_array($result);

		$this->assertEquals(399, $resultArray[0]);

		$result = mysql_query("SELECT `total_used` FROM `hs_hr_hsp_summary` WHERE `summary_id` = 2");
		$resultArray = mysql_fetch_array($result);

		$this->assertEquals(163, $resultArray[0]); // should be 163 (122+41)

    }

    public function testUpdateUsedForTwoPlans() {

		$this->assertTrue(mysql_query("TRUNCATE `hs_hr_employee`;", $this->connection), mysql_error());
        $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_hsp_payment_request`"), mysql_error());
		$this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_hsp_summary`"), mysql_error());

		$this->assertTrue(mysql_query("INSERT INTO `hs_hr_employee` ($this->employeeFields) VALUES (1, '001', 'Arnold', 'Subasinghe', '', 'Arnold', 0, NULL, NULL, NULL, NULL, NULL, '', '', '', '', NULL, '', NULL, NULL, NULL, NULL, '', '', '', 'AF', '', '', '', '', '', '', NULL, NULL, '')"), mysql_error());
		$this->assertTrue(mysql_query("INSERT INTO `hs_hr_employee` ($this->employeeFields) VALUES (2, '002', 'Kalum', 'Kumara', '', 'Kal', 0, NULL, NULL, NULL, NULL, NULL, '', '', '', '', NULL, '', NULL, NULL, NULL, NULL, '', '', '', 'AF', '', '', '', '', '', '', NULL, NULL, '')"), mysql_error());

		// For employee1
		$this->assertTrue(mysql_query("INSERT INTO `hs_hr_hsp_payment_request` ($this->paymentRequestFields) " .
	    							  "VALUES (1, 1, 1, '".date('Y')."-02-01', 'Test provider', 'Tester', 'Just testing', '150', 'TestX', '12345GD', " .
	    							  "'1231, Test Grove, Test City', 'Test', '".date('Y')."-02-02',  '123552-55821-ff25', 1)"), mysql_error());

	    $this->assertTrue(mysql_query("INSERT INTO `hs_hr_hsp_payment_request` ($this->paymentRequestFields) " .
	    							  "VALUES (2, 3, 1, '".date('Y')."-02-10', 'Test provider', 'Tester', 'Just testing', '100', 'TestX', '12345GD', " .
	    							  "'1231, Test Grove, Test City', 'Test', '".date('Y')."-02-11',  '123552-55821-ff25', 1)"), mysql_error());

		$this->assertTrue(mysql_query("INSERT INTO `hs_hr_hsp_payment_request` ($this->paymentRequestFields) " .
	    							  "VALUES (3, 1, 1, '".date('Y')."-02-20', 'Test provider', 'Tester', 'Just testing', '150', 'TestX', '12345GD', " .
	    							  "'1231, Test Grove, Test City', 'Test', '".date('Y')."-02-21',  '123552-55821-ff25', 1)"), mysql_error());

		$this->assertTrue(mysql_query("INSERT INTO `hs_hr_hsp_payment_request` ($this->paymentRequestFields) " .
	    							  "VALUES (4, 3, 1, '".date('Y')."-02-20', 'Test provider', 'Tester', 'Just testing', '100', 'TestX', '12345GD', " .
	    							  "'1231, Test Grove, Test City', 'Test', '".date('Y')."-02-21',  '123552-55821-ff25', 1)"), mysql_error());

		$this->assertTrue(mysql_query("INSERT INTO hs_hr_hsp_summary VALUES(1, 1, 1, '".date('Y')."', 1, 1500, 50, 50, 0, 22)"), mysql_error());
		$this->assertTrue(mysql_query("INSERT INTO hs_hr_hsp_summary VALUES(2, 1, 3, '".date('Y')."', 1, 2500, 75, 75, 0, 0)"), mysql_error());

		$this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `value` = '".(date('Y')-1)."-12-31' WHERE `key` = 'hsp_Used_last_updated'"));

		$this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `value` = '4' WHERE `key` = 'hsp_current_plan'"));

		Hsp::updateUsed(date('Y')); // total_used is calculated by function and should be equal to expense sum of employee1 (150+100+127) plus existed value (22)

		$result = mysql_query("SELECT `total_used` FROM `hs_hr_hsp_summary` WHERE `summary_id` = 1");
		$resultArray = mysql_fetch_array($result);

		$this->assertEquals(322, $resultArray[0]);

		$result = mysql_query("SELECT `total_used` FROM `hs_hr_hsp_summary` WHERE `summary_id` = 2");
		$resultArray = mysql_fetch_array($result);

		$this->assertEquals(200, $resultArray[0]); // should be 163 (122+41)

    }

    public function testUpdateStatus() {

		$this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_hsp_summary`"), mysql_error());

		$this->assertNotNull(mysql_query("INSERT INTO `hs_hr_employee` VALUES (1, '001', 'Arnold', 'Subasinghe', '', 'Arnold', 0, NULL, NULL, NULL, NULL, NULL, '', '', '', '', NULL, '', NULL, NULL, NULL, NULL, '', '', '', 'AF', '', '', '', '', '', '', NULL, NULL, '')"));
		$this->assertNotNull(mysql_query("INSERT INTO `hs_hr_employee` VALUES (2, '002', 'Kalum', 'Kumara', '', 'Kal', 0, NULL, NULL, NULL, NULL, NULL, '', '', '', '', NULL, '', NULL, NULL, NULL, NULL, '', '', '', 'AF', '', '', '', '', '', '', NULL, NULL, '')"));

		// For employee1

		$this->assertTrue(mysql_query("INSERT INTO hs_hr_hsp_summary VALUES(1, 1, 1, '".date('Y')."', 1, 1500, 50, 50, 0, 22)"), mysql_error());
		$this->assertTrue(mysql_query("INSERT INTO hs_hr_hsp_summary VALUES(2, 2, 1, '".date('Y')."', 1, 2500, 75, 75, 0, 0)"), mysql_error());

		$this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `value` = '".(date('Y')-1)."-12-31' WHERE `key` = 'hsp_Used_last_updated'"));


		Hsp::updateStatus(1, 0);
		$result = mysql_query("SELECT `hsp_plan_status` FROM `hs_hr_hsp_summary` WHERE `summary_id` = 1");
		$resultArray = mysql_fetch_array($result);

		$this->assertEquals($resultArray[0], 0); // should be 0 (Halted)

		Hsp::updateStatus(1, 1);
		$result = mysql_query("SELECT `hsp_plan_status` FROM `hs_hr_hsp_summary` WHERE `summary_id` = 1");
		$resultArray = mysql_fetch_array($result);

		$this->assertEquals($resultArray[0], 1); // should be 1 (Active)

    }

}

// Call HspTest::main() if this source file is executed directly.
if (PHPUnit_MAIN_METHOD == 'HspTest::main') {
    HspTest::main();
}
?>
