/** * 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"); }