⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 workshifttest.php

📁 国外的人才求职招聘最新版
💻 PHP
📖 第 1 页 / 共 2 页
字号:
	}	/**	 * Test method for removeAssignedEmployees	 */	public function testRemoveAssignedEmployees() {		// Callling remove assigned employees without setting valid workshift id		$workshift = new Workshift();		try {			$workshift->removeAssignedEmployees();			$this->fail("Trying to remove assigned employees without setting workshift id should throw exception");		} catch (WorkshiftException $e) {			$this->assertEquals(WorkshiftException::INVALID_ID, $e->getCode());		}		// remove assigned employees with non-existent workshift_id, shouldn't throw error		$workshift = new Workshift();		$workshift->setWorkshiftId(4);		$count = $workshift->removeAssignedEmployees();		$this->assertEquals(0, $count);		$this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('1' , 'New Test Shift', '5')"));		$this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('2' , 'Workshift 2', '10')"));		$this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('3' , 'Workshift 3', '11')"));		$this->assertTrue(mysql_query("INSERT INTO hs_hr_employee_workshift(workshift_id, emp_number) VALUES (1, 1)"));		$this->assertTrue(mysql_query("INSERT INTO hs_hr_employee_workshift(workshift_id, emp_number) VALUES (2, 2)"));		$this->assertTrue(mysql_query("INSERT INTO hs_hr_employee_workshift(workshift_id, emp_number) VALUES (2, 3)"));		$count = $workshift->removeAssignedEmployees();		$this->assertEquals(0, $count);		// check no assignments are deleted		$this->assertEquals(3, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE));		// Remove assigned employees when no employees are assigned - check no assignments are deleted		$workshift->setWorkshiftId(3);		$count = $workshift->removeAssignedEmployees();		$this->assertEquals(0, $count);		$this->assertEquals(3, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE));		// Remove assigned employees		$workshift->setWorkshiftId(2);		$count = $workshift->removeAssignedEmployees();		$this->assertEquals(2, $count);		$this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE));		$this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "workshift_id = 1"));		$workshift->setWorkshiftId(1);		$count = $workshift->removeAssignedEmployees();		$this->assertEquals(1, $count);		$this->assertEquals(0, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE));	}	/**	 * Test method for getEmployeesWithoutWorkshift	 */	public function testGetEmployeesWithoutWorkshift() {		$employees = Workshift::getEmployeesWithoutWorkshift();		$expected[1] = array(1, '0011', 'Rajasinghe', 'Saman', 'Marlon');		$expected[2] = array(2, '0022', 'Jayasinghe', 'Aruna', 'Shantha');		$expected[3] = array(3, '0034', 'Ranasinghe', 'Nimal', 'Bandara');		$this->assertEquals(3, count($employees));		$this->_checkEmployeeList($employees, $expected);		$this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('1' , 'New Test Shift', '5')"));		$this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('2' , 'Workshift 2', '10')"));		$employees = Workshift::getEmployeesWithoutWorkshift();		$this->assertEquals(3, count($employees));		$this->_checkEmployeeList($employees, $expected);		$this->assertTrue(mysql_query("INSERT INTO hs_hr_employee_workshift(workshift_id, emp_number) VALUES (1, 1)"));		$employees = Workshift::getEmployeesWithoutWorkshift();		$this->assertEquals(2, count($employees));		unset($expected[1]);		$this->_checkEmployeeList($employees, $expected);		$this->assertTrue(mysql_query("INSERT INTO hs_hr_employee_workshift(workshift_id, emp_number) VALUES (2, 2)"));		$employees = Workshift::getEmployeesWithoutWorkshift();		$this->assertEquals(1, count($employees));		unset($expected[2]);		$this->_checkEmployeeList($employees, $expected);		$this->assertTrue(mysql_query("INSERT INTO hs_hr_employee_workshift(workshift_id, emp_number) VALUES (2, 3)"));		$employees = Workshift::getEmployeesWithoutWorkshift();		$this->assertEquals(0, count($employees));	}	/**	 * Test method for getAssignedEmployees	 */	public function testGetAssignedEmployees() {		$workshift = new Workshift();		try {			$workshift->getAssignedEmployees();			$this->fail("Trying to fetch assigned employees without setting workshift id should throw exception");		} catch (WorkshiftException $e) {			$this->assertEquals(WorkshiftException::INVALID_ID, $e->getCode());		}		// Workshift not in system		$workshift->setWorkshiftId(1);		$employees = $workshift->getAssignedEmployees();		$this->assertEquals(0, count($employees));		$this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('1' , 'New Test Shift', '5')"));		$this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('2' , 'Workshift 2', '10')"));		// Workshift with no assigned employees		$workshift->setWorkshiftId(2);		$employees = $workshift->getAssignedEmployees();		$this->assertEquals(0, count($employees));		$this->assertTrue(mysql_query("INSERT INTO hs_hr_employee_workshift(workshift_id, emp_number) VALUES (1, 1)"));		$this->assertTrue(mysql_query("INSERT INTO hs_hr_employee_workshift(workshift_id, emp_number) VALUES (2, 2)"));		$this->assertTrue(mysql_query("INSERT INTO hs_hr_employee_workshift(workshift_id, emp_number) VALUES (2, 3)"));		$expected[2] = array(2, '0022', 'Jayasinghe', 'Aruna', 'Shantha');		$expected[3] = array(3, '0034', 'Ranasinghe', 'Nimal', 'Bandara');		$employees = $workshift->getAssignedEmployees();		$this->assertEquals(2, count($employees));		$this->_checkEmployeeList($employees, $expected);		$workshift->setWorkshiftId(1);		$employees = $workshift->getAssignedEmployees();		unset($expected);		$expected[1] = array(1, '0011', 'Rajasinghe', 'Saman', 'Marlon');		$this->assertEquals(1, count($employees));		$this->_checkEmployeeList($employees, $expected);	}    /**     * Test case for testGetWorkshifts().     */    public function testGetWorkshifts() {		// No workshifts, should return empty array		$workshifts = Workshift::getWorkshifts();		$this->assertTrue(is_array($workshifts));		$this->assertEquals(0, count($workshifts));		// Only one workshift, should return array with one workshift		$sql = "INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('1' , 'New Test Shift', '5')";		$this->assertTrue(mysql_query($sql));		$workshifts = Workshift::getWorkshifts();		$this->assertTrue(is_array($workshifts));		$this->assertEquals(1, count($workshifts));		$this->assertEquals('New Test Shift', $workshifts[0]->getName());		$this->assertEquals(1, $workshifts[0]->getWorkshiftId());		$this->assertEquals(5, $workshifts[0]->getHoursPerDay());		// Many workshifts, should return array with all available workshifts		$sql = "INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('2' , 'Workshift 2', '8')";		$this->assertTrue(mysql_query($sql));		$sql = "INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('3' , 'Workshift 3', '6')";		$this->assertTrue(mysql_query($sql));		$workshifts = Workshift::getWorkshifts();		$this->assertTrue(is_array($workshifts));		$this->assertEquals(3, count($workshifts));		$ids = array(1, 2, 3);		$names = array("New Test Shift", "Workshift 2", "Workshift 3");		$hours = array(5, 8, 6);		foreach ($workshifts as $workshift) {			$id = $workshift->getWorkshiftId();			$index = array_search($id, $ids);			$this->assertTrue($index !== false);			$this->assertEquals($names[$index], $workshift->getName());			$this->assertEquals($hours[$index], $workshift->getHoursPerDay());			unset($ids[$index]);			unset($names[$index]);			unset($hours[$index]);		}    }    /**     * Testcase for testGetWorkshift().     */    public function testGetWorkshift() {		// invalid id		try {			$workshift = Workshift::getWorkshift(null);			$this->fail("Exception not thrown");		} catch (WorkshiftException $e) {			$this->assertEquals(WorkshiftException::INVALID_ID, $e->getCode());		}		// negative id		try {			$workshift = Workshift::getWorkshift(-1);			$this->fail("Negative id!");		} catch (WorkshiftException $e) {			$this->assertEquals(WorkshiftException::INVALID_ID, $e->getCode());		}		// try sql injection in id		// id not found in database		try {			$workshift = Workshift::getWorkshift("'{}");			$this->fail("Invalid ID!");		} catch (WorkshiftException $e) {			$this->assertEquals(WorkshiftException::INVALID_ID, $e->getCode());		}		try {			$workshift = Workshift::getWorkshift(16);		} catch (WorkshiftException $e) {			$this->assertEquals(WorkshiftException::WORKSHIFT_NOT_FOUND, $e->getCode());		}		// valid id		$sql = "INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('3' , 'New Test Shift', '5')";		$result = mysql_query($sql);		$this->assertTrue($result);		// Check whether the returned object is Workshift		$workshift = Workshift::getWorkshift(3);		$this->assertTrue($workshift instanceof Workshift);		$this->assertEquals(3, $workshift->getWorkshiftId());		$this->assertEquals("New Test Shift", $workshift->getName());		$this->assertEquals(5, $workshift->getHoursPerDay());    }    /**     * Test method for deleteWorkshifts().     */    public function testDeleteWorkshifts() {		// Parameter is not an array		try {			Workshift::deleteWorkshifts(null);			$this->fail("null parameter allowed");		} catch (WorkshiftException $e) {			$this->assertEquals(WorkshiftException::INVALID_PARAMETER, $e->getCode());		}		try {			Workshift::deleteWorkshifts(2);			$this->fail("integer parameter allowed");		} catch (WorkshiftException $e) {			$this->assertEquals(WorkshiftException::INVALID_PARAMETER, $e->getCode());		}		// Empty array		$idArray = array();		try {			Workshift::deleteWorkshifts($idArray);			$this->fail("Empty array allowed");		} catch (WorkshiftException $e) {			$this->assertEquals(WorkshiftException::INVALID_PARAMETER, $e->getCode());		}		// array contains invalid ids		$idArray = array(1, 2, -1, 4);		try {			Workshift::deleteWorkshifts($idArray);			$this->fail("Invalid id's allowed");		} catch (WorkshiftException $e) {			$this->assertEquals(WorkshiftException::INVALID_ID, $e->getCode());		}		$this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('1' , 'Work shift 1', '5')"));		$this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('2' , 'Work shift 2', '5')"));		$this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('3' , 'Work shift 3', '5')"));		$this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('4' , 'Work shift 4', '5')"));		// array contains id's not in database		$idArray = array(1, 2, 23);		Workshift::deleteWorkshifts($idArray);		$this->assertEquals(2, $this->_countRows(Workshift::WORKSHIFT_TABLE));		$this->assertEquals(2, $this->_countRows(Workshift::WORKSHIFT_TABLE, "workshift_id IN (3, 4)"));		$this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_workshift`", $this->connection));		$this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('1' , 'Work shift 1', '5')"));		$this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('2' , 'Work shift 2', '5')"));		$this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('3' , 'Work shift 3', '5')"));		$this->assertTrue(mysql_query("INSERT INTO " . Workshift::WORKSHIFT_TABLE . " VALUES ('4' , 'Work shift 4', '5')"));		// array contains valid ids		$idArray = array(1, 2, 3);		Workshift::deleteWorkshifts($idArray);		$this->assertEquals(1, $this->_countRows(Workshift::WORKSHIFT_TABLE));		$row = $this->_getWorkshift(4);		$this->assertNotNull($row);		$this->assertEquals("Work shift 4", $row['name']);		$this->assertEquals(5, $row['hours_per_day']);    }    private function _getWorkshift($id) {    	$sql = "SELECT workshift_id, name, hours_per_day FROM hs_hr_workshift WHERE workshift_id = $id ";    	$result = mysql_query($sql);    	$this->assertTrue($result !== false, mysql_error());		$num = mysql_num_rows($result);		if ($num == 0) {			return null;		} else if ($num == 1) {			return mysql_fetch_array($result);		} else {			$this->fail("Two workshifts with same id");		}    }    private function _countRows($table, $condition = null) {    	$sql = "SELECT COUNT(*) FROM $table";    	if (!empty($condition)) {    		$sql .= " WHERE $condition";    	}    	$result = mysql_query($sql);    	$this->assertTrue($result !== false);    	$this->assertEquals(1, mysql_num_rows($result));    	$row = mysql_fetch_array($result);    	$count = $row[0];    	return $count;    }	/**	 * Checks that the expected employees and only the expected employees	 * are in the given array of employees. Asserts if not	 */    private function _checkEmployeeList($employees, $expected) {		foreach($employees as $employee) {			$empNumber = $employee['emp_number'];			$this->assertTrue(array_key_exists($empNumber, $expected));			$expectedVal = $expected[$empNumber];			$this->assertEquals($expectedVal[1], $employee['employee_id']);			$this->assertEquals($expectedVal[2], $employee['emp_lastname']);			$this->assertEquals($expectedVal[3], $employee['emp_firstname']);			$this->assertEquals($expectedVal[4], $employee['emp_middle_name']);			unset($expected[$empNumber]);		}    }}// Call WorkshiftTest::main() if this source file is executed directly.if (PHPUnit_MAIN_METHOD == "WorkshiftTest::main") {    WorkshiftTest::main();}?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -