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

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

require_once 'testConf.php';
require_once ROOT_PATH."/lib/confs/Conf.php";
require_once ROOT_PATH."/lib/confs/sysConf.php";
require_once ROOT_PATH."/lib/models/recruitment/JobApplication.php";
require_once ROOT_PATH."/lib/common/UniqueIDGenerator.php";
require_once ROOT_PATH."/lib/models/hrfunct/EmpInfo.php";
require_once 'RecruitmentController.php';

/**
 * Test class for RecruitmentController.
 * Generated by PHPUnit_Util_Skeleton on 2008-03-03 at 08:30:30.
 */
class RecruitmentControllerTest 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("RecruitmentControllerTest");
        $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->_deleteTables();

        // Insert job titles
        $this->_runQuery("INSERT INTO hs_hr_job_title(jobtit_code, jobtit_name, jobtit_desc, jobtit_comm, sal_grd_code) " .
                "VALUES('JOB001', 'Manager', 'Manager job title', 'no comments', null)");
        $this->_runQuery("INSERT INTO hs_hr_job_title(jobtit_code, jobtit_name, jobtit_desc, jobtit_comm, sal_grd_code) " .
                "VALUES('JOB002', 'Driver', 'Driver job title', 'no comments', null)");

        // Insert employees (managers)
        $this->_runQuery("INSERT INTO hs_hr_employee(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name, job_title_code) " .
                    "VALUES(11, '0011', 'Rajasinghe', 'Saman', 'Marlon', 'JOB001')");

        // Insert Job Vacancies
        $this->_runQuery("INSERT INTO hs_hr_job_vacancy(vacancy_id, jobtit_code, manager_id, active, description) " .
                         "VALUES(1, 'JOB002', 11, " . JobVacancy::STATUS_ACTIVE . ", 'Job vacancy 1')");

        // Insert Job Applications
        $application = $this->_getJobApplication(1, 1, 'Janaka', 'T', 'Kulathunga', '111 Main Street', 'Apt X2',
                'Colombo', 'Western', '2222', 'Sri Lanka', '01121111121', '077282828282', 'janaka@example.com',
                'aaa bbb', JobApplication::STATUS_HIRED);
        $application->setHiringManagerName('Saman Rajasinghe');
        $application->setJobTitleName('Driver');
        $this->jobApplications[1] = $application;

        $this->_createJobApplications($this->jobApplications);

        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->_deleteTables();
        UniqueIDGenerator::getInstance()->resetIDs();

    }

    private function _deleteTables() {
        $this->_runQuery("DELETE FROM `hs_hr_users` WHERE id = 'USR111'");
        $this->_runQuery("TRUNCATE TABLE `hs_hr_job_application`");
        $this->_runQuery("TRUNCATE TABLE `hs_hr_job_vacancy`");
        $this->_runQuery("TRUNCATE TABLE `hs_hr_job_title`");
        $this->_runQuery("TRUNCATE TABLE `hs_hr_employee`");
    }

     /**
     * Test case for createEmployeeFromApplication
     */
    public function testCreateEmployeeFromApplication() {
        $jobApplication = JobApplication::getJobApplication(1);

        $empInfo = new EmpInfo();
        $before = $empInfo->countEmployee();

        $recController = new RecruitmentController();
        $empNum = $recController->createEmployeeFromApplication($jobApplication);
        $this->assertNotNull($empNum);

        // check employee count increased by 1
        $after = $empInfo->countEmployee();
        $this->assertEquals($before + 1, $after);

        // verify employee main details
        $empMain = $empInfo->filterEmpMain($empNum);
        $this->assertTrue(isset($empMain[0]));
        $this->assertEquals($empNum, $empMain[0][0]);
        $this->assertEquals($jobApplication->getLastName(), $empMain[0][1]);
        $this->assertEquals($jobApplication->getFirstName(), $empMain[0][2]);
        $this->assertEquals($jobApplication->getMiddleName(), $empMain[0][3]);

        // check that empId saved as well.
        $employeeId = str_pad($empNum, $empInfo->getEmployeeIdLength(), "0", STR_PAD_LEFT);
        $this->assertEquals($employeeId, $empMain[0][5]);

        // verify employee contact details
        $empContact = $empInfo->filterEmpContact($empNum);
        $this->assertTrue(isset($empContact[0]));

        $this->assertEquals($empNum, $empMain[0][0]);
        $this->assertEquals($jobApplication->getStreet1(), $empContact[0][1]);
        $this->assertEquals($jobApplication->getStreet2(), $empContact[0][2]);
        $this->assertEquals($jobApplication->getCity(), $empContact[0][3]);
        $this->assertEquals($jobApplication->getCountry(), $empContact[0][4]);
        $this->assertEquals($jobApplication->getProvince(), $empContact[0][5]);
        $this->assertEquals($jobApplication->getZip(), $empContact[0][6]);

        // Phone saved as home telephone
        $this->assertEquals($jobApplication->getPhone(), $empContact[0][7]);
        $this->assertEquals($jobApplication->getMobile(), $empContact[0][8]);

        // Email stored as other email.
        $this->assertEquals($jobApplication->getEmail(), $empContact[0][11]);

        // Job title
        $empJobInfo = $empInfo->filterEmpJobInfo($empNum);
        $this->assertTrue(isset($empJobInfo[0]));
        $this->assertEquals($empNum, $empJobInfo[0][0]);

        $vacancy = JobVacancy::getJobVacancy($jobApplication->getVacancyId());
        $this->assertEquals($vacancy->getJobTitleCode(), $empJobInfo[0][2]);

    }

    /**
     * Create a JobApplication object with the passed parameters
     */
    private function _getJobApplication($id, $vacancyId, $firstName, $middleName, $lastName, $street1, $street2,
            $city, $province, $zip, $country, $mobile, $phone, $email, $qualifications, $status = JobApplication::STATUS_SUBMITTED) {
        $application = new JobApplication($id);
        $application->setVacancyId($vacancyId);
        $application->setFirstName($firstName);
        $application->setMiddleName($middleName);
        $application->setLastName($lastName);
        $application->setStreet1($street1);
        $application->setStreet2($street2);
        $application->setCity($city);
        $application->setProvince($province);
        $application->setZip($zip);
        $application->setCountry($country);
        $application->setMobile($mobile);
        $application->setPhone($phone);
        $application->setEmail($email);
        $application->setQualifications($qualifications);
        $application->setStatus($status);
        $application->setAppliedDateTime(date(LocaleUtil::STANDARD_TIMESTAMP_FORMAT));
        return $application;
    }

    /**
     * Saves the given JobApplication objects in the database
     *
     * @param array $applications Array of JobApplication objects to save.
     */
    private function _createJobApplications($applications) {
        foreach ($applications as $application) {

            $sql = sprintf("INSERT INTO hs_hr_job_application(application_id, vacancy_id, firstname, middlename, ".
                        "lastname, street1, street2, city, country_code, province, zip, " .
                        "phone, mobile, email, qualifications, status, applied_datetime) " .
                        "VALUES(%d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, '%s')",
                        $application->getId(), $application->getVacancyId(), $application->getFirstName(),
                        $application->getMiddleName(), $application->getLastName(), $application->getStreet1(),
                        $application->getStreet2(), $application->getCity(), $application->getCountry(),
                        $application->getProvince(), $application->getZip(), $application->getPhone(),
                        $application->getMobile(), $application->getEmail(),
                        $application->getQualifications(), $application->getStatus(),
                        $application->getAppliedDateTime());
            $this->assertTrue(mysql_query($sql), mysql_error());
        }
        UniqueIDGenerator::getInstance()->initTable();
    }

    /**
     * Run given sql query, checking the return value
     */
    private function _runQuery($sql) {
        $this->assertTrue(mysql_query($sql), mysql_error());
    }
}

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