function parseEditData($postArr) { $export = new CustomExport(); if (isset($postArr['txtId']) && !empty($postArr['txtId'])) { $export->setId(trim($postArr['txtId'])); } if (isset($postArr['txtFieldName']) && !empty($postArr['txtFieldName'])) { $export->setName(trim($postArr['txtFieldName'])); } if (isset($postArr['cmbAssignedFields']) && is_array($postArr['cmbAssignedFields'])) { $export->setAssignedFields($postArr['cmbAssignedFields']); } if (isset($postArr['headerValues']) && is_array($postArr['headerValues'])) { $export->setHeadings($postArr['headerValues']); } return $export; }
/** * Creates a CustomExport object from a resultset row * * @param array $row Resultset row from the database. * @return CustomExport Custom Export object. */ private static function _createFromRow($row) { $tmp = new CustomExport(); $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()); } $headers = $row[self::DB_FIELDS_HEADINGS]; if (!empty($headers)) { $tmp->setHeadings(explode(",", $headers)); } else { $tmp->setHeadings(array()); } return $tmp; }
/** * Test case for save() method for existing custom export definition */ public function testSaveUpdate() { $countBefore = $this->_count(); // save with duplicate name should throw exception $export = new CustomExport(); // we set id = 2, so save should update the record with id=2 $export->setId(2); $export->setName("Export 1"); $export->setAssignedFields(array("empId", "street1", "gender")); try { $export->save(); $this->fail("Exception should be thrown on duplicate name"); } catch (CustomExportException $e) { $this->assertEquals(CustomExportException::DUPLICATE_EXPORT_NAME, $e->getCode()); } // save with empty fields should throw exception $export->setName("New Export 1"); $export->setAssignedFields(array()); try { $export->save(); $this->fail("Exception should be thrown on empty assigned fields"); } catch (CustomExportException $e) { $this->assertEquals(CustomExportException::NO_ASSIGNED_FIELDS, $e->getCode()); } $export->setName("New Export 1"); $export->setAssignedFields(null); try { $export->save(); $this->fail("Exception should be thrown on empty assigned fields"); } catch (CustomExportException $e) { $this->assertEquals(CustomExportException::NO_ASSIGNED_FIELDS, $e->getCode()); } // save with field not in field list should throw exception $export->setName("New Export 1"); $export->setAssignedFields(array("firstName", "lastName", "EmployeeId")); try { $export->save(); $this->fail("Exception should be thrown on invalid field"); } catch (CustomExportException $e) { $this->assertEquals(CustomExportException::INVALID_FIELD_NAME, $e->getCode()); } // save with field count != header count should throw exception $export->setName("New Export 1"); $export->setAssignedFields(array("empId", "street1", "gender")); $export->setHeadings(array("Employee Id", "Street 1", "Street 2", "Gender")); try { $export->save(); $this->fail("Exception should be thrown on header count mismatch"); } catch (CustomExportException $e) { $this->assertEquals(CustomExportException::HEADER_COUNT_DOESNT_MATCH_FIELD_COUNT, $e->getCode()); } // save with header containing comma should throw exception $export->setName("New Export 1"); $export->setAssignedFields(array("empId", "street1", "gender")); $export->setHeadings(array("Employee Id", "Street 1", "Street, 2")); try { $export->save(); $this->fail("Exception should be thrown on invalid header name"); } catch (CustomExportException $e) { $this->assertEquals(CustomExportException::INVALID_HEADER_NAME, $e->getCode()); } // valid save, verify data saved $export->setName("New Export 1"); $export->setAssignedFields(array("empId", "street1", "gender")); $export->setHeadings(array("Employee Id", "Street 1", "Gender")); $export->save(); $id = $export->getId(); // verify id not changed $this->assertTrue(!empty($id)); $this->assertEquals(2, $id); // verify saved $name = $export->getName(); $fields = implode(",", $export->getAssignedFields()); $header = implode(",", $export->getHeadings()); $countAfter = $this->_count(); $this->assertEquals($countAfter, $countBefore); $count = $this->_count("export_id={$id} AND name='{$name}' AND fields='{$fields}' AND headings='{$header}'"); $this->assertEquals(1, $count, "Not Updated"); // Save without changing anything $export->save(); $id = $export->getId(); // verify id not changed $this->assertTrue(!empty($id)); $this->assertEquals(2, $id); $countAfter = $this->_count(); $this->assertEquals($countAfter, $countBefore); $count = $this->_count("export_id={$id} AND name='{$name}' AND fields='{$fields}' AND headings='{$header}'"); $this->assertEquals(1, $count, "Not Updated"); }