📄 projectreporttest.php
字号:
$this->assertFalse($this->_verifyEmployeeTime($results, 3, $activityId + 1, array(1), array(250))); // Wrong activity $this->assertFalse($this->_verifyEmployeeTime($results, 1, $activityId + 1, array(1), array(250))); // Wrong time $this->assertFalse($this->_verifyEmployeeTime($results, 1, $activityId, array(1), array(252))); // Activity ID and name not matching in results $results = array(new EmployeeActivityTime(1, "John", "Rajasinghe", $activityId, "Programming", 250 * 60)); $this->assertFalse($this->_verifyEmployeeTime($results, 1, $activityId , array(1), array(250))); // EmployeeId and first name does not match in results $results = array(new EmployeeActivityTime(1, "Ravi", "Rajasinghe", $activityId, "QA", 250 * 60)); $this->assertFalse($this->_verifyEmployeeTime($results, 1, $activityId , array(1), array(250))); // EmployeeId and last name does not match in results $results = array(new EmployeeActivityTime(1, "John", "Rajapaksha", $activityId, "QA", 250 * 60)); $this->assertFalse($this->_verifyEmployeeTime($results, 1, $activityId , array(1), array(250))); $activity = $this->activities[1]["Programming"]; $activityId = $activity->getId(); $results = array(); $results[] = new EmployeeActivityTime(1, "John", "Rajasinghe", $activityId, "Programming", 480 * 60); $results[] = new EmployeeActivityTime(2, "Ravi", "Wickramasinghe", $activityId, "Programming", 400 * 60); $results[] = new EmployeeActivityTime(3, "Rasanga", "Kumarasinghe", $activityId, "Programming", 800 * 60); $this->assertTrue($this->_verifyEmployeeTime($results, 1, $activityId, array(1,2,3), array(480, 400, 800))); // One time incorrect $this->assertFalse($this->_verifyEmployeeTime($results, 1, $activityId, array(1,2,3), array(480, 400, 801))); // Extra employee in results $this->_addEmployee(4, "004", "Sam", "Samarasinghe"); $this->assertFalse($this->_verifyEmployeeTime($results, 1, $activityId, array(1,2,3,4), array(480, 400, 800, 200))); // Wrong activity id $this->assertFalse($this->_verifyEmployeeTime($results, 1, $activityId + 1, array(1,2,3), array(480, 400, 800))); // One employee missing $this->assertFalse($this->_verifyEmployeeTime($results, 1, $activityId, array(1,2), array(480, 400))); // One activity wrong $results = array(); $results[] = new EmployeeActivityTime(1, "John", "Rajasinghe", $activityId, "Programming", 480 * 60); $results[] = new EmployeeActivityTime(2, "Ravi", "Wickramasinghe", $activityId, "QA", 400 * 60); $results[] = new EmployeeActivityTime(3, "Rasanga", "Kumarasinghe", $activityId, "Programming", 800 * 60); $this->assertFalse($this->_verifyEmployeeTime($results, 1, $activityId, array(1,2,3), array(480, 400, 800))); // Wrong name for one employee in results $results = array(); $results[] = new EmployeeActivityTime(1, "John", "Rajasinghe", $activityId, "Programming", 480 * 60); $results[] = new EmployeeActivityTime(2, "Ravi", "Ranasinghe", $activityId, "QA", 400 * 60); $results[] = new EmployeeActivityTime(3, "Rasanga", "Kumarasinghe", $activityId, "Programming", 800 * 60); $this->assertFalse($this->_verifyEmployeeTime($results, 1, $activityId, array(1,2,3), array(480, 400, 800))); } /** * Add customer used by unit test */ private function _addCustomer() { $template = "INSERT INTO hs_hr_customer(customer_id, name, description, deleted) VALUES(%d, '%s', '%s', 0)"; $sql = sprintf($template, $this->customerId, $this->customerName, "Desc. " . $this->customerName); $this->_runQuery($sql); } /** * Add projects used by unit test. */ private function _addProjects() { $template = "INSERT INTO hs_hr_project(project_id, customer_id, name, description, deleted) " . "VALUES(%d, %d, '%s', '%s', 0)"; $id = $this->_count("hs_hr_project") + 1; foreach ($this->projects as $project) { $sql = sprintf($template, $id, $this->customerId, $project, "Desc. " . $project); $this->_runQuery($sql); $id++; } } /** * Add an activity to the database and saves it in the $this->activities * array * * @param int $projectId The project Id * @param string $name The Activity name * @param bool $deleted Create activity in deleted state * * @return ProjectActivity The activity object that was created. */ private function _addActivity($projectId, $name, $deleted = false) { $activity = new ProjectActivity(); $activity->setName($name); $activity->setProjectId($projectId); $activity->setDeleted($deleted); $activity->save(); $this->activities[$projectId][$name] = $activity; return $activity; } /** * Add employee */ private function _addEmployee($empNumber, $empId, $lastName, $firstName) { $sql = sprintf("INSERT INTO hs_hr_employee(emp_number, employee_id, emp_lastname, emp_firstname) " . "VALUES(%d, '%s', '%s', '%s')", $empNumber, $empId, $lastName, $firstName); $this->_runQuery($sql); } /** * Insert event into database. */ private function _addEvent($projectId, $employeeId, $activityName, $startTime, $duration) { // Get activity Id $this->assertTrue(isset($this->activities[$projectId])); $this->assertTrue(isset($this->activities[$projectId][$activityName])); $activity = $this->activities[$projectId][$activityName]; $timeEventId = $this->_getNextId("hs_hr_time_event", "time_event_id"); $durationInSec = $duration * 60; $endTime = $this->_getEndTime($startTime, $durationInSec); $activityId = $activity->getId(); $timesheetId = $this->_getTimesheetId($employeeId, $startTime, $endTime); $reportedDate = date("Y-m-d"); $description = "Desc $activityName"; $sql = sprintf("INSERT INTO hs_hr_time_event(time_event_id, project_id, activity_id, employee_id, timesheet_id, " . "start_time, end_time, reported_date, duration, description) " . "VALUES(%d, %d, %d, %d, %d, '%s', '%s', '%s', %d, '%s')", $timeEventId, $projectId, $activityId, $employeeId, $timesheetId, $startTime, $endTime, $reportedDate, $durationInSec, "activity $activityName"); $this->_runQuery($sql); } /** * Calculate the duration between the given dates in seconds * * @param string start date * @param string end date * * @return int duration in seconds */ private function _calculateDuration($startDate, $endDate) { $start = strtotime($startDate); $end = strtotime($endDate); $duration = $end - $start; return $duration; } /** * Calculates the end time * @param string $startTime The start time * @param int $duration The duration (in seconds) * @return string The end time. */ private function _getEndTime($startTime, $duration) { $start = strtotime($startTime); $end = $start + $duration; $endTime = date('Y-m-d H:i', $end); return $endTime; } /** * Gets the timesheet id for the given employee for the given period. * If no timesheet is present, creates one * * @param int $employeeId Employee id * @param string $startTime Start time * @param string $endTime End time * * @return int Timesheet id */ private function _getTimesheetId($employeeId, $startTime, $endTime) { $start = strtotime($startTime); $periodStart = strtotime("last Monday", $start); $periodStartDay = date('Y-m-d', $periodStart); $id = null; $sql = "SELECT timesheet_id FROM hs_hr_timesheet WHERE employee_id = $employeeId AND " . " DATE(start_date) = DATE('$periodStartDay')"; $result = $this->_runQuery($sql); if (mysql_num_rows($result) > 0) { $row = mysql_fetch_array($result, MYSQL_NUM); $id = $row[0]; } else { $periodEndDay = date('Y-m-d', strtotime("next Sunday", $start)); $id = $this->_getNextId("hs_hr_timesheet", "timesheet_id"); $sql = sprintf("INSERT INTO hs_hr_timesheet(timesheet_id, employee_id, timesheet_period_id, start_date, " . "end_date, status, comment) VALUES(%d, %d, %d, '%s', '%s', %d, '%s')", $id, $employeeId, 1, $periodStartDay, $periodEndDay, Timesheet::TIMESHEET_STATUS_NOT_SUBMITTED, "comment"); $this->_runQuery($sql); } return $id; } /** * Verifies that the results contain the given activities and times * * @param array $results Array ProjectActivityTime objects * @param string $projectId project id * @param array $activities array of activity names expected * @param array $times array of times values expected (in minutes) * * @return bool false if verification failed, true otherwise */ private function _verifyActivityTime($results, $projectId, $activities, $times) { // Verify number if (count($results) != count($activities)) { return false; } foreach ($results as $activityTime) { // find in activities. $activityName = $activityTime->getActivityName(); $index = array_search($activityName, $activities); // if not found, return false if (($index === false) || ($index === null)) { return false; } // if found, check time $time = $times[$index]; // if time matches, remove from activities if ($activityTime->getActivityTime() == ($time * 60)) { unset($activities[$index]); unset($times[$index]); } else { return false; } } // see if any activities left, if so return false if ((count($activities) != 0) || (count($times) != 0)) { return false; } return true; } /** * Verifies the results contain the given employees and times * * @param array $results The results array * @param string $projectId The project Id * @param string $activityId Activity Id * @param array $empIds Employee Ids * @param array $times Activity times * * @return true if results match or false if not */ private function _verifyEmployeeTime($results, $projectId, $activityId, $empIds, $times) { // Verify number if (count($results) != count($empIds)) { return false; } foreach ($results as $empActivityTime) { // find emp number in list $empNumber = $empActivityTime->getEmpNumber(); $index = array_search($empNumber, $empIds); // if not found, return false if (($index === false) || ($index === null)) { return false; } // check activity id if ($activityId != $empActivityTime->getActivityId()) { return false; } // check activity name match $activityName = $empActivityTime->getActivityName(); if (!isset($this->projects[$projectId][$activityName])) { return false; } $activity = $this->activities[$projectId][$activityName]; if ($activity->getId() != $activityId) { return false; } // Check employee details $empCountWhere = sprintf("emp_number=%d AND emp_firstname='%s' AND emp_lastname='%s'", $empActivityTime->getEmpNumber(), $empActivityTime->getFirstName(), $empActivityTime->getLastName()); if ($this->_count("hs_hr_employee", $empCountWhere) != 1) { return false; } // Check time $time = $times[$index]; if ($empActivityTime->getActivityTime() == ($time * 60)) { unset($empIds[$index]); unset($times[$index]); } else { return false; } } // see if any activities left, if so return false if ((count($empIds) != 0) || (count($times) != 0)) { return false; } return true; } /** * Truncates tables used in test. */ private function _truncateTables() { $this->_runQuery("TRUNCATE TABLE `hs_hr_project_activity`"); $this->_runQuery("TRUNCATE TABLE `hs_hr_time_event`"); $this->_runQuery("TRUNCATE TABLE `hs_hr_project`"); $this->_runQuery("TRUNCATE TABLE `hs_hr_customer`"); $this->_runQuery("TRUNCATE TABLE `hs_hr_timesheet`"); $this->_runQuery("TRUNCATE TABLE `hs_hr_employee`"); } /** * Counts rows in the given table with optional where clause * * @param string $table The table name * @param string $where The where clause * @return int number of rows */ private function _count($table, $where = null) { $sql = "SELECT COUNT(*) FROM " . $table; if (!empty($where)) { $sql .= " WHERE " . $where; } $result = $this->_runQuery($sql); $row = mysql_fetch_array($result, MYSQL_NUM); $count = $row[0]; return $count; } /** * Return the next Id for the given field in * the given table * * @param string $table Table name * @param string $field Field name * @return int Next Id */ private function _getNextId($table, $field) { $sql = "SELECT MAX($field) FROM $table"; $result = $this->_runQuery($sql); $row = mysql_fetch_array($result, MYSQL_NUM); $nextId = $row[0] + 1; return $nextId; } /** * Runs the given sql query verifies and returns the * result. */ private function _runQuery($sql) { $result = mysql_query($sql, $this->connection); $this->assertTrue($result !== false, mysql_error()); return $result; }}// Call ProjectReportTest::main() if this source file is executed directly.if (PHPUnit_MAIN_METHOD == "ProjectReportTest::main") { ProjectReportTest::main();}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -