📄 workshifttest.php
字号:
<?php// Call WorkshiftTest::main() if this source file is executed directly.if (!defined("PHPUnit_MAIN_METHOD")) { define("PHPUnit_MAIN_METHOD", "WorkshiftTest::main");}require_once "PHPUnit/Framework/TestCase.php";require_once "PHPUnit/Framework/TestSuite.php";require_once ROOT_PATH."/lib/confs/Conf.php";require_once ROOT_PATH."/lib/dao/DMLFunctions.php";require_once ROOT_PATH."/lib/dao/SQLQBuilder.php";require_once ROOT_PATH."/lib/common/UniqueIDGenerator.php";require_once ROOT_PATH."/lib/models/time/Workshift.php";/** * Test class for Workshift. * Generated by PHPUnit_Util_Skeleton on 2007-09-03 at 14:23:03. */class WorkshiftTest 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("WorkshiftTest"); $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->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_employee_workshift`", $this->connection)); $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_workshift`", $this->connection)); $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_employee`", $this->connection)); // Insert a project and customer and employees for use in the test $this->assertTrue(mysql_query("INSERT INTO hs_hr_employee(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name) " . "VALUES(1, '0011', 'Rajasinghe', 'Saman', 'Marlon')")); $this->assertTrue(mysql_query("INSERT INTO hs_hr_employee(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name) " . "VALUES(2, '0022', 'Jayasinghe', 'Aruna', 'Shantha')")); $this->assertTrue(mysql_query("INSERT INTO hs_hr_employee(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name) " . "VALUES(3, '0034', 'Ranasinghe', 'Nimal', 'Bandara')")); 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->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_employee_workshift`", $this->connection)); $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_workshift`", $this->connection)); $this->assertTrue(mysql_query("TRUNCATE TABLE `hs_hr_employee`", $this->connection)); UniqueIDGenerator::getInstance()->resetIDs(); } /** * Test case for testSave(). */ public function testSave() { $workshift = new Workshift(); try { $workshift->save(); $this->fail("No exception thrown"); } catch (WorkshiftException $e) { $this->assertEquals(WorkshiftException::VALUES_EMPTY_OR_NOT_SET, $e->getCode()); } $workshift = new Workshift(); $workshift->setName("Test shift"); try { $workshift->save(); $this->fail("No exception thrown"); } catch (WorkshiftException $e) { $this->assertEquals(WorkshiftException::VALUES_EMPTY_OR_NOT_SET, $e->getCode()); } $workshift = new Workshift(); $workshift->setHoursPerDay(12); try { $workshift->save(); $this->fail("No exception thrown"); } catch (WorkshiftException $e) { $this->assertEquals(WorkshiftException::VALUES_EMPTY_OR_NOT_SET, $e->getCode()); } $workshift = new Workshift(); $workshift->setName("Test shift"); $workshift->setHoursPerDay(-1); try { $workshift->save(); $this->fail("No exception thrown"); } catch (WorkshiftException $e) { $this->assertEquals(WorkshiftException::VALUES_EMPTY_OR_NOT_SET, $e->getCode()); } $workshift = new Workshift(); $workshift->setName("Test shift"); $workshift->setHoursPerDay(12); $workshift->save(); $id = $workshift->getWorkshiftId(); $this->assertNotNull($id); $this->assertEquals(1, $id); $workshift = $this->_getWorkshift($id); $this->assertTrue($workshift !== false); $this->assertEquals("Test shift", $workshift['name']); $this->assertEquals(12, $workshift['hours_per_day']); $this->assertEquals(1, $workshift['workshift_id']); // Update $workshift = new Workshift(); $workshift->setName("Normal Shift"); $workshift->setHoursPerDay(8); $workshift->setWorkshiftId(3); // Set invalid id exception should be thrown $affected = $workshift->save(); $this->assertEquals(0, $affected); // valid id, verify that values have changed $workshift->setWorkshiftId(1); $affected = $workshift->save(); $this->assertEquals(1, $affected); $updatedRow = $this->_getWorkshift(1); $this->assertEquals("Normal Shift", $updatedRow['name']); $this->assertEquals(8, $updatedRow['hours_per_day']); $workshift = new Workshift(); $workshift->setWorkshiftId(1); try { $workshift->save(); $this->fail("Workshift without name and hours per day saved"); } catch (WorkshiftException $e) { $this->assertEquals(WorkshiftException::VALUES_EMPTY_OR_NOT_SET, $e->getCode()); } // Invalid hours per day $workshift = new Workshift(); $workshift->setName("Invalid workshift"); $workshift->setHoursPerDay("' {}' '"); try { $workshift->save(); $this->fail("Invalid hours per day allowed"); } catch (WorkshiftException $e) { $this->assertEquals(WorkshiftException::VALUES_EMPTY_OR_NOT_SET, $e->getCode()); } $workshift = new Workshift(); $workshift->setWorkshiftId(1); $workshift->setName("Invalid workshift"); $workshift->setHoursPerDay("' {}' '"); try { $workshift->save(); $this->fail("Invalid hours per day allowed"); } catch (WorkshiftException $e) { $this->assertEquals(WorkshiftException::VALUES_EMPTY_OR_NOT_SET, $e->getCode()); } // Test if sql injection works - update $workshift = new Workshift(); $workshift->setWorkshiftId(1); $workshift->setName("sfdk'"); $workshift->setHoursPerDay("22"); $workshift->save(); // check that the value is saved $updatedRow = $this->_getWorkshift(1); $this->assertEquals("sfdk'", $updatedRow['name']); $this->assertEquals(22, $updatedRow['hours_per_day']); // Test if sql injection works - save $workshift = new Workshift(); $workshift->setName("eeee'"); $workshift->setHoursPerDay("22"); $workshift->save(); $id = $workshift->getWorkshiftId(); // check that the value is saved $updatedRow = $this->_getWorkshift($id); $this->assertEquals("eeee'", $updatedRow['name']); $this->assertEquals(22, $updatedRow['hours_per_day']); } /** * Test method for delete(). */ public function testDelete() { // Test for id not available $workshift = new Workshift(); $workshift->setWorkshiftId(15); try { $workshift->delete(); $this->fail("Non existing ID was not checked!"); } catch (WorkshiftException $e) { $this->assertEquals(WorkshiftException::INVALID_ROW_COUNT, $e->getCode()); } // Test for valid id $workshift = new Workshift(); $workshift->setName("Delete Shift"); $workshift->setHoursPerDay(7); $workshift->save(); // Check the saving $id = $workshift->getWorkshiftId(); $updatedRow = $this->_getWorkshift($id); $this->assertNotNull($updatedRow); // check whether the ID exists $workshift->delete(); $id = $workshift->getWorkshiftId(); $updatedRow = $this->_getWorkshift($id); $this->assertNull($updatedRow); // Empty id $workshift = new Workshift(); try { $workshift->delete(); $this->fail("Empty ID was not checked!"); } catch (WorkshiftException $e) { $this->assertEquals(WorkshiftException::INVALID_ID, $e->getCode()); } // Invalid id $workshift = new Workshift(); $workshift->setWorkshiftId("'fgW"); try { $workshift->delete(); $this->fail("Invalid ID was not checked!"); } catch (WorkshiftException $e) { $this->assertEquals(WorkshiftException::INVALID_ID, $e->getCode()); } } /** * @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)")); } /** * Test method for getWorkshiftForEmployee */ public function testGetWorkshiftForEmployee() { $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)")); // Invalid employee id try { Workshift::getWorkshiftForEmployee('sdf'); $this->fail("Invalid employee number should throw exception"); } catch (WorkshiftException $e) { $this->assertEquals(WorkshiftException::INVALID_ID, $e->getCode()); } // Get workshift for non-existant employee $this->assertNull(Workshift::getWorkshiftForEmployee(4)); // Get workshift for employee without assigned workshift $this->assertNull(Workshift::getWorkshiftForEmployee(3)); // Get workshift for employee with workshift assigned $shift = Workshift::getWorkshiftForEmployee(1); $this->assertNotNull($shift); $this->assertEquals("New Test Shift", $shift->getName()); $this->assertEquals(5, $shift->getHoursPerDay()); $this->assertEquals(1, $shift->getWorkshiftId());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -