📄 customimporttest.php
字号:
$count = $this->_count(); $this->assertEquals($countBefore - 1, $count); // two id's $ids = array(2, 3); $deleted = CustomImport::deleteImports($ids); $this->assertEquals(2, $deleted); $count = $this->_count(); $this->assertEquals($countBefore - 3, $count); } /** * Test case for save() method for new custom import definition */ public function testSaveNew() { $countBefore = $this->_count(); // save with duplicate name should throw exception $import = new CustomImport(); $import->setName("Import 1"); $import->setAssignedFields(array("empId", "lastName", "firstName", "street1", "gender")); try { $import->save(); $this->fail("Exception should be thrown on duplicate name"); } catch (CustomImportException $e) { $this->assertEquals(CustomImportException::DUPLICATE_IMPORT_NAME, $e->getCode(), $e->getMessage()); } // Exception should be thrown on empty name $import = new CustomImport(); $import->setName(""); $import->setAssignedFields(array("empId", "street1", "gender", "lastName", "firstName")); try { $import->save(); $this->fail("Exception should be thrown on empty name"); } catch (CustomImportException $e) { $this->assertEquals(CustomImportException::EMPTY_IMPORT_NAME, $e->getCode()); } // save with empty fields should throw exception $import->setName("New Import 1"); $import->setAssignedFields(array()); try { $import->save(); $this->fail("Exception should be thrown on empty assigned fields"); } catch (CustomImportException $e) { $this->assertEquals(CustomImportException::NO_ASSIGNED_FIELDS, $e->getCode()); } $import->setName("New Import 1"); $import->setAssignedFields(null); try { $import->save(); $this->fail("Exception should be thrown on empty assigned fields"); } catch (CustomImportException $e) { $this->assertEquals(CustomImportException::NO_ASSIGNED_FIELDS, $e->getCode()); } // save with field not in field list should throw exception $import->setName("New Import 1"); $import->setAssignedFields(array("firstName", "lastName", "EmployeeId")); try { $import->save(); $this->fail("Exception should be thrown on invalid field"); } catch (CustomImportException $e) { $this->assertEquals(CustomImportException::INVALID_FIELD_NAME, $e->getCode()); } // save with compulsary field missing should throw exception. $import->setAssignedFields(array("firstName", "empId")); try { $import->save(); $this->fail("Exception should be thrown when compulsary field is missing"); } catch (CustomImportException $e) { $this->assertEquals(CustomImportException::COMPULSARY_FIELDS_NOT_ASSIGNED, $e->getCode()); } // valid save, verify data saved $import->setName("New Import 1"); $import->setAssignedFields(array("empId", "street1", "gender", "lastName", "firstName")); $import->setContainsHeader(true); $import->save(); $id = $import->getId(); // verify id set $this->assertTrue(!empty($id)); $this->assertEquals(4, $id); // verify saved $name = $import->getName(); $fields = implode(",", $import->getAssignedFields()); $hasHeader = CustomImport::HAS_HEADING; $countAfter = $this->_count(); $this->assertEquals(1, $countAfter - $countBefore); $count = $this->_count("import_id={$id} AND name='{$name}' AND fields='{$fields}' AND has_heading={$hasHeader}"); $this->assertEquals(1, $count, "Not inserted"); // should be able to save without setting containsHeader $import2 = new CustomImport(); $import2->setName("New Import 2"); $import2->setAssignedFields(array("empId", "street1", "gender", "lastName", "firstName")); $import2->save(); $id = $import2->getId(); // verify id set $this->assertTrue(!empty($id)); $this->assertEquals(5, $id); // verify saved $name = $import2->getName(); $fields = implode(",", $import2->getAssignedFields()); $hasHeader = CustomImport::NO_HEADING;; $countAfter = $this->_count(); $this->assertEquals(2, $countAfter - $countBefore); $count = $this->_count("import_id={$id} AND name='{$name}' AND fields='{$fields}' AND has_heading={$hasHeader}"); $this->assertEquals(1, $count, "Not inserted"); } /** * Test case for save() method for existing custom import definition */ public function testSaveUpdate() { $countBefore = $this->_count(); // save with duplicate name should throw exception $import = new CustomImport(); // we set id = 2, so save should update the record with id=2 $import->setId(2); $import->setName("Import 1"); $import->setAssignedFields(array("empId", "street1", "gender", "firstName", "lastName")); try { $import->save(); $this->fail("Exception should be thrown on duplicate name"); } catch (CustomImportException $e) { $this->assertEquals(CustomImportException::DUPLICATE_IMPORT_NAME, $e->getCode()); } // save with empty fields should throw exception $import->setName("New Import 1"); $import->setAssignedFields(array()); try { $import->save(); $this->fail("Exception should be thrown on empty assigned fields"); } catch (CustomImportException $e) { $this->assertEquals(CustomImportException::NO_ASSIGNED_FIELDS, $e->getCode()); } $import->setName("New Import 1"); $import->setAssignedFields(null); try { $import->save(); $this->fail("Exception should be thrown on empty assigned fields"); } catch (CustomImportException $e) { $this->assertEquals(CustomImportException::NO_ASSIGNED_FIELDS, $e->getCode()); } // save with field not in field list should throw exception $import->setName("New Import 1"); $import->setAssignedFields(array("firstName", "lastName", "EmployeeId")); try { $import->save(); $this->fail("Exception should be thrown on invalid field"); } catch (CustomImportException $e) { $this->assertEquals(CustomImportException::INVALID_FIELD_NAME, $e->getCode()); } // save with compulsary field missing should throw exception. $import->setName("New Import 1"); $import->setAssignedFields(array("firstName", "empId")); try { $import->save(); $this->fail("Exception should be thrown when compulsary field is missing"); } catch (CustomImportException $e) { $this->assertEquals(CustomImportException::COMPULSARY_FIELDS_NOT_ASSIGNED, $e->getCode()); } // valid save, verify data saved $import->setName("New Import 1"); $import->setAssignedFields(array("empId", "street1", "firstName", "lastName", "gender")); $import->setContainsHeader(true); $import->save(); $id = $import->getId(); // verify id not changed $this->assertTrue(!empty($id)); $this->assertEquals(2, $id); // verify saved $name = $import->getName(); $fields = implode(",", $import->getAssignedFields()); $hasHeader = CustomImport::HAS_HEADING; $countAfter = $this->_count(); $this->assertEquals($countAfter, $countBefore); $count = $this->_count("import_id={$id} AND name='{$name}' AND fields='{$fields}' AND has_heading='{$hasHeader}'"); $this->assertEquals(1, $count, "Not Updated"); // Save without changing anything $import->save(); $id = $import->getId(); // verify id not changed $this->assertTrue(!empty($id)); $this->assertEquals(2, $id); $countAfter = $this->_count(); $this->assertEquals($countAfter, $countBefore); $count = $this->_count("import_id={$id} AND name='{$name}' AND fields='{$fields}' AND has_heading='{$hasHeader}'"); $this->assertEquals(1, $count, "Not Updated"); } private function _runQuery($sql) { $this->assertTrue(mysql_query($sql), mysql_error()); } /** * Count the number of rows in the database with the give condition * * @param string $where Optional where condition * @return int Number of matching rows in database */ private function _count($where = null) { $sql = "SELECT COUNT(*) FROM hs_hr_custom_import"; if (!empty($where)) { $sql .= " WHERE " . $where; } $result = mysql_query($sql); $row = mysql_fetch_array($result, MYSQL_NUM); $count = $row[0]; return $count; }}// Call CustomImportTest::main() if this source file is executed directly.if (PHPUnit_MAIN_METHOD == "CustomImportTest::main") { CustomImportTest::main();}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -