<?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/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.");");

    }

    /**
     * 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);
    }

    /**
     * @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"));
    }
}

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