Ejemplo n.º 1
0
 private function _parseCommonData($postArr)
 {
     $import = new CustomImport();
     if (isset($postArr['txtFieldName']) && !empty($postArr['txtFieldName'])) {
         $import->setName(trim($postArr['txtFieldName']));
     }
     if (isset($postArr['cmbAssignedFields']) && is_array($postArr['cmbAssignedFields'])) {
         $import->setAssignedFields($postArr['cmbAssignedFields']);
     }
     if (isset($postArr['containsHeader']) && !empty($postArr['containsHeader'])) {
         $containsHeader = true;
     } else {
         $containsHeader = false;
     }
     $import->setContainsHeader($containsHeader);
     return $import;
 }
 /**
  * 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");
 }
 /**
  * Creates a CustomImport object from a resultset row
  *
  * @param array $row Resultset row from the database.
  * @return CustomImport Custom Import object.
  */
 private static function _createFromRow($row)
 {
     $tmp = new CustomImport();
     $tmp->setId($row[self::DB_FIELDS_ID]);
     $tmp->setName($row[self::DB_FIELDS_NAME]);
     $assignedFields = $row[self::DB_FIELDS_FIELDS];
     if (!empty($assignedFields)) {
         $tmp->setAssignedFields(explode(",", $assignedFields));
     } else {
         $tmp->setAssignedFields(array());
     }
     $hasHeader = $row[self::DB_FIELDS_HAS_HEADING] == self::HAS_HEADING ? true : false;
     $tmp->setContainsHeader($hasHeader);
     return $tmp;
 }