/** * @todo Implement testAssignEmployees(). */ public function testAssignEmployees() { $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')")); // Try to assign without valid workshift id, should throw an error $employees = array(1, 2, 3); $workshift = new Workshift(); try { $workshift->assignEmployees($employees); $this->fail("Trying to assign employees without setting workshift id should throw exception"); } catch (WorkshiftException $e) { $this->assertEquals(WorkshiftException::INVALID_ID, $e->getCode()); } // Assigning to non existing workshift, should not insert any rows $workshift->setWorkshiftId(4); $count = $workshift->assignEmployees($employees); $this->assertEquals(0, $count); $this->assertEquals(0, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE)); // Assign empty list of employees should be allowed $workshift->setWorkshiftId(1); $count = $workshift->assignEmployees(array()); $this->assertEquals(0, $count); $this->assertEquals(0, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE)); // Assign valid employee list $employees = array(1, 3); $count = $workshift->assignEmployees($employees); $this->assertEquals(2, $count); $this->assertEquals(2, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE)); $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "(workshift_id = 1 AND emp_number = 1)")); $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "(workshift_id = 1 AND emp_number = 3)")); // reassigning already assigned employees shouldn't assign them again $employees = array(1, 2, 3); $count = $workshift->assignEmployees($employees); $this->assertEquals(1, $count); $this->assertEquals(3, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE)); $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "(workshift_id = 1 AND emp_number = 1)")); $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "(workshift_id = 1 AND emp_number = 2)")); $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "(workshift_id = 1 AND emp_number = 3)")); // Passing same employee several times should not add duplicate entries $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_employee_workshift`")); $employees = array(1, 3, 1, 1, 3); $count = $workshift->assignEmployees($employees); $this->assertEquals(2, $count); $this->assertEquals(2, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE)); $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "(workshift_id = 1 AND emp_number = 1)")); $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "(workshift_id = 1 AND emp_number = 3)")); // Invalid employee ID's should not be assigned. $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_employee_workshift`")); $employees = array(1, -1, "')", 3); $count = $workshift->assignEmployees($employees); $this->assertEquals(2, $count); $this->assertEquals(2, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE)); $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "(workshift_id = 1 AND emp_number = 1)")); $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "(workshift_id = 1 AND emp_number = 3)")); // Non existing employee should not be added $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_employee_workshift`")); $employees = array(1, 2, 4, 3, 5); $count = $workshift->assignEmployees($employees); $this->assertEquals(3, $count); $this->assertEquals(3, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE)); $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "(workshift_id = 1 AND emp_number = 1)")); $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "(workshift_id = 1 AND emp_number = 2)")); $this->assertEquals(1, $this->_countRows(Workshift::EMPLOYEE_WORKSHIFT_TABLE, "(workshift_id = 1 AND emp_number = 3)")); }