📄 projectactivitytest.php
字号:
// Expected } // Verify that invalid project ids throw exceptions try { ProjectActivity::getActivity("xfe"); $this->fail("Exception not thrown"); } catch (ProjectActivityException $e) { // Expected } // Verify that invalid project ids throw exceptions try { ProjectActivity::getActivity(null); $this->fail("Exception not thrown"); } catch (ProjectActivityException $e) { // Expected } // non existant activity id. $obj = ProjectActivity::getActivity(1); $this->assertNull($obj); // create some activities $actList = $this->_getTestActivities(); $this->_createActivites($actList); $obj = ProjectActivity::getActivity(2); $this->assertNotNull($obj); $this->assertTrue($obj instanceof ProjectActivity); $this->assertEquals($actList[$obj->getId()], $obj); // verify that deleted activites are returned as well $obj = ProjectActivity::getActivity(3); $this->assertNotNull($obj); $this->assertTrue($obj instanceof ProjectActivity); $this->assertTrue($obj->isDeleted()); $this->assertEquals($actList[$obj->getId()], $obj); // non existant activity id (with entries in table) $obj = ProjectActivity::getActivity(5); $this->assertNull($obj); } /** * test testgetActivitiesWithName() method. */ public function testGetActivitiesWithName() { // Verify that invalid project ids throw exceptions try { ProjectActivity::getActivitiesWithName("", "Test"); $this->fail("Exception not thrown"); } catch (ProjectActivityException $e) { // Expected } // Verify that invalid project ids throw exceptions try { ProjectActivity::getActivitiesWithName("xafd", "Test"); $this->fail("Exception not thrown"); } catch (ProjectActivityException $e) { // Expected } // Verify that invalid project ids throw exceptions try { ProjectActivity::getActivitiesWithName(null, "Test"); $this->fail("Exception not thrown"); } catch (ProjectActivityException $e) { // Expected } // Test that activity name is escaped to avoid sql injection. // If not, following will throw an error. ProjectActivity::getActivitiesWithName(1, "' WHERE xkaf in (SELECT * from xaf)"); // non existent name (with empty table) $list = ProjectActivity::getActivitiesWithName(1, "Test activity"); $this->assertEquals(0, count($list)); // create some activities $actList = $this->_getTestActivities(); $this->_createActivites($actList); // non existent name $list = ProjectActivity::getActivitiesWithName(1, "Test activity 2"); $this->assertEquals(0, count($list)); // valid name $list = ProjectActivity::getActivitiesWithName(1, "test 1"); $this->assertEquals(1, count($list)); $obj = $list[0]; $this->assertEquals($actList[$obj->getId()], $obj); // verify that deleted activities are not included by default $list = ProjectActivity::getActivitiesWithName(1, "test 3"); $this->assertEquals(0, count($list)); // include deleted activities $list = ProjectActivity::getActivitiesWithName(1, "test 3", true); $this->assertEquals(1, count($list)); $obj = $list[0]; $this->assertEquals($actList[$obj->getId()], $obj); // multiple matches mysql_query("UPDATE hs_hr_project_activity SET name = 'test name' where project_id = 1"); $list = ProjectActivity::getActivitiesWithName(1, "test name"); $this->assertEquals(2, count($list)); $list = ProjectActivity::getActivitiesWithName(1, "test name", true); $this->assertEquals(3, count($list)); } /** * Tests the deleteActivities() method. */ public function testDeleteActivities() { $projId = 1; $ids = array(1, 2, 3, 4); // Verify that invalid project ids throw exceptions try { ProjectActivity::deleteActivities($ids, "Test"); $this->fail("Exception not thrown"); } catch (ProjectActivityException $e) { // Expected } // Verify that invalid project ids throw exceptions try { ProjectActivity::deleteActivities($ids, ""); $this->fail("Exception not thrown"); } catch (ProjectActivityException $e) { // Expected } // Verify that invalid activity ids throw exceptions try { ProjectActivity::deleteActivities(null, 1); $this->fail("Exception not thrown"); } catch (ProjectActivityException $e) { // Expected } // Verify that invalid activity ids throw exceptions try { ProjectActivity::deleteActivities(array(1, ""), 1); $this->fail("Exception not thrown"); } catch (ProjectActivityException $e) { // Expected } // Verify that invalid activity ids throw exceptions try { ProjectActivity::deleteActivities(array(1, "ew"), 1); $this->fail("Exception not thrown"); } catch (ProjectActivityException $e) { // Expected } // try deleting unavailable ids. $numDeleted = ProjectActivity::deleteActivities($ids, $projId); $this->assertEquals(0, $numDeleted); $numDeleted = ProjectActivity::deleteActivities($ids); $this->assertEquals(0, $numDeleted); // create some activites $actList = $this->_getTestActivities(); $this->_createActivites($actList); mysql_query("UPDATE hs_hr_project_activity SET deleted = 0"); // delete one and check $ids = array(1); $numDeleted = ProjectActivity::deleteActivities($ids); $this->assertEquals(1, $numDeleted); $num = $this->_getNumActivities("activity_id = 1 AND deleted = 1"); $this->assertEquals(1, $num); $num = $this->_getNumActivities("deleted = 1"); $this->assertEquals(1, $num); $num = $this->_getNumActivities("deleted = 0"); $this->assertEquals(3, $num); // delete already deleted activity, verify no change $numDeleted = ProjectActivity::deleteActivities($ids); $this->assertEquals(0, $numDeleted); $num = $this->_getNumActivities("activity_id = 1 AND deleted = 1"); $this->assertEquals(1, $num); $num = $this->_getNumActivities("deleted = 1"); $this->assertEquals(1, $num); mysql_query("UPDATE hs_hr_project_activity SET deleted = 0"); // verify that only activies in given project are deleted. // NOTE: 1,2,3 belong to projId 1, 4 to projId 2 $projId = 2; $ids = array(1, 2, 3); $numDeleted = ProjectActivity::deleteActivities($ids, $projId); $this->assertEquals(0, $numDeleted); $num = $this->_getNumActivities("deleted = 1"); $this->assertEquals(0, $num); $ids = array(1, 2, 3, 4); $numDeleted = ProjectActivity::deleteActivities($ids, $projId); $this->assertEquals(1, $numDeleted); $num = $this->_getNumActivities("deleted = 1"); $this->assertEquals(1, $num); $num = $this->_getNumActivities("activity_id = 4 AND deleted = 1"); $this->assertEquals(1, $num); // delete multiple activities $ids = array(1, 2, 3); $numDeleted = ProjectActivity::deleteActivities($ids); $this->assertEquals(3, $numDeleted); $num = $this->_getNumActivities("deleted = 1"); $this->assertEquals(4, $num); } /** * Returns the number of rows in the project_activity table * * @param string $where where clause * @return int number of rows */ private function _getNumActivities($where = null) { $sql = "SELECT COUNT(*) FROM hs_hr_project_activity"; if (!empty($where)) { $sql .= " WHERE " . $where; } $result = mysql_query($sql); $row = mysql_fetch_array($result, MYSQL_NUM); $count = $row[0]; return $count; } /** * Checks that the attributes of the activity object and the database row match. * * @param ProjectActivity $activity * @param array $row */ private function _checkRow($activity, $row) { $this->assertEquals($activity->getName(), $row['name'], "Activity name not correct"); $this->assertEquals($activity->getProjectId(), $row['project_id'], "Project id wrong"); $this->assertEquals($activity->getId(), $row['activity_id'], "Activity id wrong"); $this->assertEquals($activity->isDeleted(), (bool)$row['deleted'], "Deleted value wrong"); } /** * Creates some ProjectActivity objects for use in the tests * @return array Array of ProjectActivity objects */ private function _getTestActivities() { $activities['1'] = $this->_getActivityObject(1, 1, "test 1", false); $activities['2'] = $this->_getActivityObject(2, 1, "test 2", false); $activities['3'] = $this->_getActivityObject(3, 1, "test 3", true); $activities['4'] = $this->_getActivityObject(4, 2, "test 4", false); return $activities; } /** * Create a ProjectActivity object with the passed parameters */ private function _getActivityObject($activity_id, $project_id, $name, $deleted) { $activity = new ProjectActivity($activity_id); $activity->setProjectId($project_id); $activity->setName($name); $activity->setDeleted($deleted); return $activity; } /** * Saves the given Project Activity objects in the databas * * @param ProjectActivity $activities ProjectActivity objects to save. */ private function _createActivites($activities) { foreach ($activities as $activity) { $sql = sprintf("INSERT INTO hs_hr_project_activity(activity_id, project_id, name, deleted) " . "VALUES(%d, %d, '%s', %d)", $activity->getId(), $activity->getProjectId(), $activity->getName(), ($activity->isDeleted() ? 1 : 0)); mysql_query($sql); UniqueIDGenerator::getInstance()->initTable(); } }}// Call ProjectActivityTest::main() if this source file is executed directly.if (PHPUnit_MAIN_METHOD == "ProjectActivityTest::main") { ProjectActivityTest::main();}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -