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

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

require_once "testConf.php";

require_once 'HspPayPeriod.php';

require_once ROOT_PATH."/lib/confs/Conf.php";
require_once ROOT_PATH."/lib/common/UniqueIDGenerator.php";

/**
 * Test class for HspPayPeriod.
 * Generated by PHPUnit_Util_Skeleton on 2007-10-14 at 23:17:10.
 */
class HspPayPeriodTest extends PHPUnit_Framework_TestCase {
    /**
     * 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("HspPayPeriodTest");
        $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() {
    	$conf = new Conf();
    	$this->connection = mysql_connect($conf->dbhost.":".$conf->dbport, $conf->dbuser, $conf->dbpass);
        mysql_select_db($conf->dbname);

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

        $this->assertTrue(mysql_query("INSERT INTO hs_hr_pay_period (id, start_date, end_date, close_date, check_date, timesheet_aproval_due_date) " .
        			"VALUES(1, '".date('Y-m-d')."', '".date('Y-m-d', time()+3600*24)."', '".date('Y-m-d', time()+3600*24*2)."', '".date('Y-m-d', time()+3600*24*3)."', '".date('Y-m-d', time()+3600*24*4)."')"));

        // For testGetYears()
        $this->assertTrue(mysql_query("INSERT INTO hs_hr_pay_period (id, start_date, end_date, close_date, check_date, timesheet_aproval_due_date) VALUES(45, '".(date('Y')+20)."-01-01', '".(date('Y')+20)."-01-15', '".(date('Y')+20)."-01-15', '".(date('Y')+20)."-01-16', '".(date('Y')+20)."-01-17')"));

    	UniqueIDGenerator::getInstance()->resetIDs();
    }

    /**
     * Tears down the fixture, for example, close 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_pay_period`", $this->connection));

    	UniqueIDGenerator::getInstance()->resetIDs();
    }

    public function testCountPayPeriods() {
    	$payPeriodsCount = HspPayPeriod::countPayPeriods(date('Y', time()-3600*24*750));

    	$this->assertEquals(0, $payPeriodsCount);
    }

    public function testCountPayPeriods1() {
    	$payPeriodsCount = HspPayPeriod::countPayPeriods(date('Y'));

    	$this->assertEquals(1, $payPeriodsCount);
    }

    public function testCountPayPeriods2() {
    	$payPeriodsCount = HspPayPeriod::countPayPeriods(date('Y'), true);

    	$this->assertEquals(1, $payPeriodsCount);
    }


    public function testListPayPeriods() {
		$payPeriods = HspPayPeriod::listPayPeriods(date('Y', time()-3600*24*750));

		$this->assertTrue(is_array($payPeriods));
		$this->assertEquals(0, count($payPeriods));
    }

    public function testListPayPeriods1() {
    	$expected[0] = array(1, date('Y-m-d'), date('Y-m-d', time()+3600*24), date('Y-m-d', time()+3600*24*2), date('Y-m-d', time()+3600*24*3), date('Y-m-d', time()+3600*24*4));
    	//$expected[1] = array(2, date('Y-m-d', time()+3600*24*370), date('Y-m-d', time()+3600*24*371), date('Y-m-d', time()+3600*24*372), date('Y-m-d', time()+3600*24*373), date('Y-m-d', time()+3600*24*374));

		$payPeriods = HspPayPeriod::listPayPeriods(date('Y'));

		$this->assertTrue(is_array($payPeriods));
		$this->assertEquals(1, count($payPeriods));

		for ($i=0; $i<count($payPeriods); $i++) {
			$this->assertEquals($expected[$i][0], $payPeriods[$i]->getId());
			$this->assertEquals($expected[$i][1], $payPeriods[$i]->getStartDate());
			$this->assertEquals($expected[$i][2], $payPeriods[$i]->getEndDate());
			$this->assertEquals($expected[$i][3], $payPeriods[$i]->getCloseDate());
			$this->assertEquals($expected[$i][4], $payPeriods[$i]->getCheckDate());
			$this->assertEquals($expected[$i][5], $payPeriods[$i]->getTimesheetAprovalDueDate());
		}
    }

    public function testAdd() {
		$expected[0] = array(1, date('Y-m-d'), date('Y-m-d', time()+3600*24), date('Y-m-d', time()+3600*24*2), date('Y-m-d', time()+3600*24*3), date('Y-m-d', time()+3600*24*4));
		$expected[1] = array(2, date('Y-m-d', time()+3600*24*2), date('Y-m-d', time()+3600*24*3), date('Y-m-d', time()+3600*24*4), date('Y-m-d', time()+3600*24*5), date('Y-m-d', time()+3600*24*6));

		$payPeriod = new HspPayPeriod();
		$payPeriod->setStartDate($expected[1][1]);
		$payPeriod->setEndDate($expected[1][2]);
		$payPeriod->setCloseDate($expected[1][3]);
		$payPeriod->setCheckDate($expected[1][4]);
		$payPeriod->setTimesheetAprovalDueDate($expected[1][5]);

		$payPeriod->add();

		$payPeriods = HspPayPeriod::listPayPeriods(date('Y'));

		$this->assertTrue(is_array($payPeriods));
		//$this->assertEquals(2, count($payPeriods));

		for ($i=0; $i<count($payPeriods); $i++) {
			$this->assertEquals($expected[$i][0], $payPeriods[$i]->getId());
			$this->assertEquals($expected[$i][1], $payPeriods[$i]->getStartDate());
			$this->assertEquals($expected[$i][2], $payPeriods[$i]->getEndDate());
			$this->assertEquals($expected[$i][3], $payPeriods[$i]->getCloseDate());
			$this->assertEquals($expected[$i][4], $payPeriods[$i]->getCheckDate());
			$this->assertEquals($expected[$i][5], $payPeriods[$i]->getTimesheetAprovalDueDate());
		}
    }

    public function testUpdate() {
    	$expected[0] = array(1, date('Y-m-d'), date('Y-m-d', time()+3600*24), date('Y-m-d', time()+3600*24*2), date('Y-m-d', time()+3600*24*3), date('Y-m-d', time()+3600*24*5));

		$payPeriod = new HspPayPeriod();
		$payPeriod->setId($expected[0][0]);
		$payPeriod->setStartDate($expected[0][1]);
		$payPeriod->setEndDate($expected[0][2]);
		$payPeriod->setCloseDate($expected[0][3]);
		$payPeriod->setCheckDate($expected[0][4]);
		$payPeriod->setTimesheetAprovalDueDate($expected[0][5]);

		$payPeriod->update();

		$payPeriods = HspPayPeriod::listPayPeriods(date('Y'));

		$this->assertTrue(is_array($payPeriods));
		$this->assertEquals(1, count($payPeriods));

		for ($i=0; $i<count($payPeriods); $i++) {
			$this->assertEquals($expected[$i][0], $payPeriods[$i]->getId());
			$this->assertEquals($expected[$i][1], $payPeriods[$i]->getStartDate());
			$this->assertEquals($expected[$i][2], $payPeriods[$i]->getEndDate());
			$this->assertEquals($expected[$i][3], $payPeriods[$i]->getCloseDate());
			$this->assertEquals($expected[$i][4], $payPeriods[$i]->getCheckDate());
			$this->assertEquals($expected[$i][5], $payPeriods[$i]->getTimesheetAprovalDueDate());
		}
    }

    public function testDelete() {
		$payPeriod = new HspPayPeriod();
		$payPeriod->setId(1);

		$payPeriod->delete();

		$payPeriods = HspPayPeriod::listPayPeriods(date('Y'));

		$this->assertTrue(is_array($payPeriods));
		$this->assertEquals(0, count($payPeriods));
    }

    public function testGetYears() {

    	$years = HspPayPeriod::getYears();

		$expected = array(date('Y'), date('Y')+1, date('Y')+20);

    	$this->assertEquals($expected, $years);
    }

	public function testCountCheckDates() {

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

		$this->assertTrue(mysql_query("INSERT INTO hs_hr_pay_period VALUES(1, '2007-12-01', '2007-12-31', '2007-12-31', '2007-12-25', '2007-12-20')"));
		$this->assertTrue(mysql_query("INSERT INTO hs_hr_pay_period VALUES(2, '2008-01-01', '2008-01-31', '2008-01-31', '2008-01-25', '2008-01-20')"));
		$this->assertTrue(mysql_query("INSERT INTO hs_hr_pay_period VALUES(3, '2008-02-01', '2008-02-29', '2008-02-29', '2008-02-25', '2008-02-20')"));
		$this->assertTrue(mysql_query("INSERT INTO hs_hr_pay_period VALUES(4, '2008-03-01', '2008-03-31', '2008-03-31', '2008-03-25', '2008-03-20')"));

		$this->assertEquals(HspPayPeriod::countCheckDates("2008-01-01", "2008-04-01"), 3);
		$this->assertEquals(HspPayPeriod::countCheckDates("2007-12-01", "2008-04-01"), 3);
		$this->assertEquals(HspPayPeriod::countCheckDates("2005-12-01", "2008-04-01"), 3);
		$this->assertEquals(HspPayPeriod::countCheckDates("2008-01-25", "2008-03-31"), 2);
		$this->assertEquals(HspPayPeriod::countCheckDates("2008-03-31", "2008-05-31"), 0);

    }

}

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