/** * Assign given location to given employee * * @param int $empNumber Employee number * @param string $locationCode Location code to assign * * @return boolean true if successfully assigned, false otherwise */ public function assignLocation($empNumber, $locationCode) { $result = false; $auth = new authorize($_SESSION['empID'], $_SESSION['isAdmin']); /* Only allow admins and supervisors of the given employee to assign locations */ if ($auth->isAdmin() || $auth->isSupervisor() && $auth->isTheSupervisor($empNumber)) { $empLocation = new EmpLocation($empNumber, $locationCode); try { $empLocation->save(); $result = true; $history = new LocationHistory(); $history->updateHistory($empNumber, $locationCode); } catch (EmpLocationException $e) { } } return $result; }
/** * Test case for save function */ public function testSave() { // Invalid emp number $empLoc = new EmpLocation('a1', 'LOC001'); try { $empLoc->save(); $this->fail("Exception expected"); } catch (EmpLocationException $e) { $this->assertEquals(EmpLocationException::INVALID_PARAMETER, $e->getCode()); } // Invalid location code $empLoc = new EmpLocation(11, 'LOCX001'); try { $empLoc->save(); $this->fail("Exception expected"); } catch (EmpLocationException $e) { $this->assertEquals(EmpLocationException::INVALID_PARAMETER, $e->getCode()); } // Emp Number not belonging to any employee $this->_clearError(); set_error_handler(array($this, 'errorHandler')); $empLoc = new EmpLocation(112, 'LOC011'); try { $empLoc->save(); $this->fail("Exception expected"); } catch (EmpLocationException $e) { $this->assertEquals(EmpLocationException::DB_ERROR, $e->getCode()); } restore_error_handler(); $this->assertNotNull($this->errorLevel); // Location code not belonging to any location $this->_clearError(); set_error_handler(array($this, 'errorHandler')); $empLoc = new EmpLocation(11, 'LOC011'); try { $empLoc->save(); $this->fail("Exception expected"); } catch (EmpLocationException $e) { $this->assertEquals(EmpLocationException::DB_ERROR, $e->getCode()); } restore_error_handler(); $this->assertNotNull($this->errorLevel); // Emp Number and location ok $this->assertEquals(0, $this->_getNumRows('emp_number = 13')); $empLoc = new EmpLocation(13, 'LOC002'); $empLoc->save(); $this->assertEquals(1, $this->_getNumRows('emp_number = 13')); // Reassign same location $empLoc = new EmpLocation(13, 'LOC002'); $empLoc->save(); $this->assertEquals(1, $this->_getNumRows('emp_number = 13')); // Assign different location $empLoc = new EmpLocation(13, 'LOC004'); $empLoc->save(); $this->assertEquals(2, $this->_getNumRows('emp_number = 13')); }