Commit 141761eb authored by Carsten  Rose's avatar Carsten Rose
Browse files

Form: changed detection of load/saved from POST SIP Parameter to POST/GET...

Form: changed detection of load/saved from POST SIP Parameter to POST/GET Method. Introduced Save. Now, loadFormSpecification() differentiate between load and save.
Save: new.
formEditor: fixed some minor bugs in form specification.
parent 17a2fc27
......@@ -22,6 +22,7 @@ use qfq\Store;
require_once(__DIR__ . '/../qfq/store/Store.php');
require_once(__DIR__ . '/../qfq/Constants.php');
require_once(__DIR__ . '/../qfq/Save.php');
require_once(__DIR__ . '/../qfq/helper/KeyValueStringParser.php');
require_once(__DIR__ . '/../qfq/helper/HelperFormElement.php');
require_once(__DIR__ . '/../qfq/exceptions/UserException.php');
......@@ -131,13 +132,15 @@ class Form {
try {
$formName = $this->loadFormSpecification();
// Form action: load or save?
$mode = isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST' ? FORM_SAVE : FORM_LOAD;
$formName = $this->loadFormSpecification($mode);
$sipFound = $this->validateForm();
if (!$sipFound) {
$this->store->createSipAfterFormLoad($formName);
}
$this->store->fillStoreTableDefaultColumnType($this->formSpec['tableName']);
switch ($this->formSpec['render']) {
......@@ -154,13 +157,13 @@ class Form {
throw new CodeException("This statement should never be reached", ERROR_CODE_SHOULD_NOT_HAPPEN);
}
// Form action: load or save?
$mode = isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST' ? FORM_SAVE : FORM_LOAD;
switch ($mode) {
case FORM_LOAD:
$html .= $build->process();
break;
case FORM_SAVE:
$save = new Save($this->formSpec, $this->feSpecAction, $this->feSpecNative);
$save->process();
break;
default:
throw new CodeException("This statement should never be reached", ERROR_CODE_SHOULD_NOT_HAPPEN);
......@@ -188,30 +191,42 @@ class Form {
* Loaded 'action' FormElements are in $this->feSpecAction
* Loaded 'native' FormElements are in $this->feSpecNative
*
* @param string $mode FORM_LOAD|FORM_SAVE
* @return string formName
* @throws DbException
* @throws UserException
*/
private function loadFormSpecification() {
private function loadFormSpecification($mode) {
$formName = $this->getFormName();
$this->store->setVar(SYSTEM_FORM, $formName, STORE_SYSTEM);
$this->formSpec = $this->eval->parseArray($this->db->sql("SELECT * FROM Form AS f WHERE f.name LIKE ? AND f.deleted='no'", ROW_EXACT_1, [$formName], 'Form not found or multiple forms with the same name.'));
$form = $this->db->sql("SELECT * FROM Form AS f WHERE f.name LIKE ? AND f.deleted='no'", ROW_EXACT_1, [$formName], 'Form not found or multiple forms with the same name.');
$this->formSpec = $this->eval->parseArray($form);
HelperFormElement::explodeFieldParameter($this->formSpec);
// Clear
$this->store->setVar(SYSTEM_FORM_ELEMENT, '', STORE_SYSTEM);
// "SELECT *, ? AS 'nestedInFieldSet' FROM FormElement AS fe WHERE fe.formId = ? AND fe.deleted = 'no' AND FIND_IN_SET(fe.class, ? ) AND fe.feIdContainer = ? AND fe.enabled='yes' ORDER BY fe.ord, fe.id";
$sql = SQL_FORM_ELEMENT;
// FE: Action
$this->feSpecAction = $this->eval->parseArray($this->db->sql($sql, ROW_REGULAR, ['no', $this->formSpec["id"], 'action', 0]));
$this->feSpecAction = $this->eval->parseArray($this->db->sql(SQL_FORM_ELEMENT_ALL_CONTAINER, ROW_REGULAR, ['no', $this->formSpec["id"], 'action']));
HelperFormElement::explodeFieldParameter($this->feSpecAction);
// FE: Native & Container
$this->feSpecNative = $this->db->sql($sql, ROW_REGULAR, ['no', $this->formSpec["id"], 'native,container', 0]);
// "SELECT *, ? AS 'nestedInFieldSet' FROM FormElement AS fe WHERE fe.formId = ? AND fe.deleted = 'no' AND FIND_IN_SET(fe.class, ? ) AND fe.feIdContainer = ? AND fe.enabled='yes' ORDER BY fe.ord, fe.id";
switch ($mode) {
case FORM_LOAD:
$this->feSpecNative = $this->db->sql(SQL_FORM_ELEMENT_SPECIFIC_CONTAINER, ROW_REGULAR, ['no', $this->formSpec["id"], 'native,container', 0]);
break;
case FORM_SAVE:
$this->feSpecNative = $this->db->sql(SQL_FORM_ELEMENT_ALL_CONTAINER, ROW_REGULAR, ['no', $this->formSpec["id"], 'native']);
break;
default:
}
HelperFormElement::explodeFieldParameter($this->feSpecNative);
return $formName;
......
<?php
/**
* Created by PhpStorm.
* User: crose
* Date: 1/30/16
* Time: 7:59 PM
*/
namespace qfq;
require_once(__DIR__ . '/../qfq/store/Store.php');
require_once(__DIR__ . '/../qfq/Constants.php');
require_once(__DIR__ . '/../qfq/Evaluate.php');
//require_once(__DIR__ . '/../qfq/exceptions/UserException.php');
//require_once(__DIR__ . '/../qfq/exceptions/CodeException.php');
//require_once(__DIR__ . '/../qfq/exceptions/DbException.php');
//require_once(__DIR__ . '/../qfq/Evaluate.php');
class Save {
private $formSpec = array(); // copy of the loaded form
private $feSpecAction = array(); // copy of all formElement.class='action' of the loaded form
private $feSpecNative = array(); // copy of all formElement.class='native' of the loaded form
/**
* @var null|Store
*/
private $store = null;
private $db = null;
private $evaluate = null;
/**
* @param array $formSpec
* @param array $feSpecAction
* @param array $feSpecNative
*/
public function __construct(array $formSpec, array $feSpecAction, array $feSpecNative) {
$this->formSpec = $formSpec;
$this->feSpecAction = $feSpecAction;
$this->feSpecNative = $feSpecNative;
$this->store = Store::getInstance();
$this->db = new Database();
$this->evaluate = new Evaluate($this->store, $this->db);
}
/**
* @throws CodeException
* @throws DbException
* @throws UserException
*/
public function process() {
if ($this->formSpec['multiMode'] !== 'none') {
$parentRecords = $this->db->sql($this->formSpec['multiSql']);
foreach ($parentRecords as $row) {
$this->store->setVarArray($row, STORE_PARENT_RECORD, true);
$this->elements($row['_id']);
}
} else {
$this->elements($this->store->getVar(SIP_RECORD_ID, STORE_SIP . STORE_0));
}
}
/**
* @param $recordId
* @return string
* @throws CodeException
* @throws DbException
* @throws UserException
*/
public function elements($recordId) {
$html = '';
$newValues = array();
$tableColumns = array_keys($this->store->getStore(STORE_TABLE_COLUMN_TYPES));
$clientValues = $this->store->getStore(STORE_CLIENT);
$sipValues = $this->store->getStore(STORE_SIP);
// Iterate over all table.columns. Built an assoc array $newValues.
foreach ($tableColumns AS $column) {
// Never save a predefined 'id'.
if ($column === 'id')
continue;
// Get related formElement.
$formElement = $this->getFormElementByName($column);
if ($formElement === false)
continue;
// Log / Debug
$this->store->setVar(SYSTEM_FORM_ELEMENT, $formElement['name'] . ' / ' . $formElement['id'], STORE_SYSTEM);
if ($formElement['type'] == 'hidden') {
// Hidden elements will be transferred by SIP
if (!isset($sipValues[$column])) {
throw new CodeException("Missing the hidden field '$column' in SIP.", ERROR_MISSING_HIDDEN_FIELD_IN_SIP);
}
$newValues[$column] = $sipValues[$column];
continue;
}
$clientFieldName = HelperFormElement::buildFormElementId($column, $recordId);
if (isset($clientValues[$clientFieldName])) {
$newValues[$column] = $this->validateValue($formElement, $clientValues[$clientFieldName]);
}
}
return ($recordId == 0) ? $this->insertRecord($this->formSpec['tableName'], $newValues) : $this->updateRecord($this->formSpec['tableName'], $newValues, $recordId);
}
/**
* Get the complete FormElement for $name
*
* @param $name
* @return bool|array if found the FormElement, else false.
*/
private function getFormElementByName($name) {
foreach ($this->feSpecNative as $formElement) {
if ($formElement['name'] === $name)
return $formElement;
}
return false;
}
/**
* Check $value against given checkType/pattern. Throws an UserException if check fails.
*
* @param $formElement
* @param $value
* @return string
* @throws CodeException
* @throws UserException
*/
public function validateValue($formElement, $value) {
$pattern = '';
$minMax = array();
switch ($formElement['checkType']) {
case 'min|max':
$valueCompare = $value;
$minMax = explode('|', $formElement['checkPattern']);
if ($minMax[0] === '' || $minMax[1] === '')
throw new UserException('Missing value for min or max.', ERROR_MISSING_MIN_MAX);
$errorText = "Value '$value' is smaller than min '$minMax[0]' or bigger than max '$minMax[1]'.";
// Date/Time: convert in object to make a comparison.
if (preg_match('date|time', $formElement['type']) === 1) {
$valueCompare = date_create($value);
$minMax[0] = date_create($minMax[0]);
$minMax[1] = date_create($minMax[1]);
}
if ($minMax[0] <= $valueCompare && $valueCompare <= $minMax[1])
return $value;
throw new UserException($errorText, ERROR_MIN_MAX_VIOLATION);
break;
case 'pattern':
$pattern = $formElement['checkPattern'];
break;
case 'number':
case 'email':
$htmlPatternAttribute = $this->inputCheckPattern[$formElement['checkType']];
// remove 'pattern="' and closing ".
$pattern = substr($htmlPatternAttribute, 9, strlen($htmlPatternAttribute) - 9 - 1);
break;
case '': // no checktype specified.
return $value;
default:
throw new CodeException("Unknown checkType: " . $formElement['checkType'], ERROR_UNKNOWN_CHECKTYPE);
}
if (preg_match($pattern, $value) === 1)
return $value;
throw new UserException("Value $value violates checkrule " . $formElement['checkType'] . " with pattern '$pattern'.", ERROR_PATTERN_VIOLATION);
}
/**
* Insert new record in table $this->formSpec['tableName'].
*
* @param array $values
* @return string
* @throws DbException
*/
public function insertRecord($tableName, array $values) {
if (count($values) === 0)
return 0;
$paramList = str_repeat('?, ', count($values));
$paramList = substr($paramList, 0, strlen($paramList) - 2);
$columnList = '`' . implode('`, `', array_keys($values)) . '`';
$sql = 'INSERT INTO ' . $tableName . ' ( ' . $columnList . ' ) VALUES ( ' . $paramList . ' )';
$this->db->sql($sql, ROW_REGULAR, array_values($values));
return $this->db->getLastInsertId();
}
/**
* @param array $values
* @param $recordId
* @return string
* @throws DbException
*/
public function updateRecord($tableName, array $values, $recordId) {
if (count($values) === 0)
return;
if ($recordId === 0)
throw new CodeException('RecordId=0 - this is not possible for update.', ERROR_RECORDID_0_FORBIDDEN);
$paramList = str_repeat('?, ', count($values));
$paramList = substr($paramList, 0, strlen($paramList) - 2);
$sql = 'UPDATE `' . $tableName . '` SET ';
foreach ($values as $column => $value) {
$sql .= '`' . $column . '` = ?, ';
}
$sql = substr($sql, 0, strlen($sql) - 2) . ' WHERE id = ?';
$values[] = $recordId;
$this->db->sql($sql, ROW_REGULAR, array_values($values));
}
}
\ No newline at end of file
......@@ -119,11 +119,11 @@ DELIMITER ;
#
# FormEditor: Form
INSERT INTO Form (name, title, noteInternal, tableName, permitNew, permitEdit, render, multiSql, parameter) VALUES
('form', 'Form Editor: {{SELECT id, " / ", title FROM Form WHERE id = {{recordId:SZ}}}}', 'Please secure the form',
('form', 'Form Editor: {{SELECT id, " / ", title FROM Form WHERE id = {{recordId:S0}}}}', 'Please secure the form',
'Form', 'always', 'always', 'bootstrap', '', 'maxVisiblePill=3');
# FormEditor: FormElements
INSERT INTO FormElement (formId, name, label, mode, class, type, ord, size, maxLength, note, clientJs, value, sql1, sql2, parameter, feIdContainer)
INSERT INTO FormElement (formId, name, label, mode, type, class, ord, size, maxLength, note, clientJs, value, sql1, sql2, parameter, feIdContainer)
VALUES
(1, 'basic', 'Basic', 'show', 'pill', 'container', 10, 0, 0, '', '', '', '', '', '', 0),
(1, 'permission', 'Permission', 'show', 'pill', 'container', 20, 0, 0, '', '', '', '', '', '', 0),
......@@ -164,50 +164,63 @@ VALUES
#
# FormEditor: FormElement
INSERT INTO Form (name, title, noteInternal, tableName, permitNew, permitEdit, render, multiSql, parameter) VALUES
('formElement', 'Form Element Editor : {{SELECT id, " / ", title FROM Form WHERE id = {{recordId:SZ}}}}',
('formElement', 'Form Element Editor : {{SELECT id, " / ", title FROM Form WHERE id = {{recordId:S0}}}}',
'Please secure the form',
'FormElement', 'always', 'always', 'bootstrap', '', 'maxVisiblePill=3');
# FormEditor: FormElements
INSERT INTO FormElement (id, formId, name, label, mode, type, ord, size, maxLength, note, clientJs, value, sql1, sql2, parameter, feIdContainer, debug)
INSERT INTO FormElement (id, formId, name, label, mode, type, class, ord, size, maxLength, note, clientJs, value, sql1, sql2, parameter, feIdContainer, debug)
VALUES
(100, 2, 'basic', 'Basic', 'show', 'pill', 10, 0, 0, '', '', '', '', '', '', 0, 'no'),
(101, 2, 'check_order', 'Check & Order', 'show', 'pill', 20, 0, 0, '', '', '', '', '', '', 0, 'no'),
(102, 2, 'layout', 'Layout', 'show', 'pill', 20, 0, 0, '', '', '', '', '', '', 0, 'no'),
(103, 2, 'value', 'Value', 'show', 'pill', 20, 0, 0, '', '', '', '', '', '', 0, 'no'),
(104, 2, 'info', 'Info', 'show', 'pill', 20, 0, 0, '', '', '', '', '', '', 0, 'no'),
(100, 2, 'basic', 'Basic', 'show', 'pill', 'container', 10, 0, 0, '', '', '', '', '', '', 0, 'no'),
(101, 2, 'check_order', 'Check & Order', 'show', 'pill', 'container', 20, 0, 0, '', '', '', '', '', '', 0, 'no'),
(102, 2, 'layout', 'Layout', 'show', 'pill', 'container', 20, 0, 0, '', '', '', '', '', '', 0, 'no'),
(103, 2, 'value', 'Value', 'show', 'pill', 'container', 20, 0, 0, '', '', '', '', '', '', 0, 'no'),
(104, 2, 'info', 'Info', 'show', 'pill', 'container', 20, 0, 0, '', '', '', '', '', '', 0, 'no'),
(110, 2, 'id', 'id', 'readonly', 'input', 100, 10, 11, '', '', '', '', '', '', 100, 'no'),
(111, 2, 'formId', 'formId', 'readonly', 'input', 120, 40, 255, '', '', '', '', '', '', 100, 'no'),
(112, 2, 'feIdContainer', 'Container', 'show', 'select', 150, 0, 0, '', '', '',
(110, 2, 'id', 'id', 'readonly', 'input', 'native', 100, 10, 11, '', '', '', '', '', '', 100, 'no'),
(111, 2, 'formId', 'formId', 'readonly', 'input', 'native', 120, 40, 255, '', '', '', '', '', '', 100, 'no'),
(112, 2, 'feIdContainer', 'Container', 'show', 'select', 'native', 150, 0, 0, '', '', '',
'{{!SELECT fe.id, CONCAT(fe.class, " / ", fe.label) FROM FormElement As fe WHERE fe.formId={{id}} AND fe.class="container" ORDER BY fe.ord }}',
'', 'emptyItemAtStart', 100, 'no'),
(113, 2, 'enabled', 'Enabled', 'show', 'checkbox', 120, 0, 0, '', '', '', '', '', '', 100, 'no'),
(114, 2, 'name', 'Name', 'show', 'input', 120, 40, 255, '', '', '', '', '', '', 100, 'no'),
(115, 2, 'label', 'Label', 'show', 'input', 130, 40, 255, '', '', '', '', '', '', 100, 'no'),
(116, 2, 'mode', 'Mode', 'show', 'select', 120, 0, 255, '', '', '', '', '', '', 100, 'no'),
(117, 2, 'class', 'Class', 'show', 'select', 120, 0, 255, '', '', '', '', '', '', 100, 'no'),
(118, 2, 'type', 'Type', 'show', 'select', 120, 0, 255, '', '', '', '', '', '', 100, 'no'),
(119, 2, 'checkType', 'Check Type', 'show', 'select', 120, 0, 255, '', '', '', '', '', '', 101, 'no'),
(120, 2, 'checkPattern', 'Check Pattern', 'show', 'input', 130, 40, 255, '', '', '', '', '', '', 101, 'no'),
(121, 2, 'onChange', 'JS onChange', 'show', 'input', 130, 40, 255, '', '', '', '', '', '', 101, 'no'),
(122, 2, 'ord', 'Order', 'show', 'input', 130, 40, 255, '', '', '', '', '', '', 101, 'no'),
(123, 2, 'tabindex', 'tabindex', 'show', 'input', 130, 40, 255, '', '', '', '', '', '', 101, 'no'),
(124, 2, 'size', 'Size', 'show', 'input', 130, 40, 255, '', '', '', '', '', '', 102, 'no'),
(125, 2, 'maxlenght', 'Maxlength', 'show', 'input', 130, 40, 255, '', '', '', '', '', '', 102, 'no'),
(126, 2, 'note', 'note', 'show', 'input', 130, 40, 255, '', '', '', '', '', '', 102, 'no'),
(127, 2, 'tooltip', 'Tooltip', 'show', 'input', 130, 40, 255, '', '', '', '', '', '', 102, 'no'),
(128, 2, 'placeholder', 'Placeholder', 'show', 'input', 130, 40, 255, '', '', '', '', '', '', 102, 'no'),
(129, 2, 'value', 'value', 'show', 'input', 130, 40, 255, '', '', '', '', '', '', 102, 'no'),
(130, 2, 'sql1', 'sql1', 'show', 'input', 130, '40,4', 255, '', '', '', '', '', '', 103, 'no'),
(131, 2, 'parameter', 'Parameter', 'show', 'input', 130, '40,4', 255, '', '', '', '', '', '', 103, 'no'),
(132, 2, 'clientJs', 'ClientJS', 'show', 'input', 130, 40, 255, '', '', '', '', '', '', 103, 'no'),
(133, 2, 'feGroup', 'feGroup', 'show', 'input', 130, 40, 255, '', '', '', '', '', '', 104, 'no'),
(134, 2, 'debug', 'Debug', 'show', 'checkbox', 130, 0, 0, '', '', '', '', '', '', 104, 'no'),
(135, 2, 'deleted', 'Deleted', 'show', 'checkbox', 400, 0, 0, '', '', '', '', '', '', 104, 'no'),
(136, 2, 'modified', 'Modified', 'readonly', 'input', 410, 40, 20, '', '', '', '', '', '', 104, 'no'),
(137, 2, 'created', 'Created', 'readonly', 'input', 420, 40, 20, '', '', '', '', '', '', 104, 'no');
(113, 2, 'enabled', 'Enabled', 'show', 'checkbox', 'native', 120, 0, 0, '', '', '', '', '', '', 100, 'no'),
(114, 2, 'name', 'Name', 'show', 'input', 'native', 120, 40, 255, '', '', '', '', '', '', 100, 'no'),
(115, 2, 'label', 'Label', 'show', 'input', 'native', 130, 40, 255, '', '', '', '', '', '', 100, 'no'),
(116, 2, 'mode', 'Mode', 'show', 'select', 'native', 120, 0, 255, '', '', '', '', '', '', 100, 'no'),
(117, 2, 'class', 'Class', 'show', 'select', 'native', 120, 0, 255, '', '', '', '', '', '', 100, 'no'),
(118, 2, 'type', 'Type', 'show', 'select', 'native', 120, 0, 255, '', '', '', '', '', '', 100, 'no'),
(119, 2, 'checkType', 'Check Type', 'show', 'select', 'native', 120, 0, 255, '', '', '', '', '', '', 101, 'no'),
(120, 2, 'checkPattern', 'Check Pattern', 'show', 'input', 'native', 130, 40, 255, '', '', '', '', '', '', 101, 'no'),
(121, 2, 'onChange', 'JS onChange', 'show', 'input', 'native', 130, 40, 255, '', '', '', '', '', '', 101, 'no'),
(122, 2, 'ord', 'Order', 'show', 'input', 'native', 130, 40, 255, '', '', '', '', '', '', 101, 'no'),
(123, 2, 'tabindex', 'tabindex', 'show', 'input', 'native', 130, 40, 255, '', '', '', '', '', '', 101, 'no'),
(124, 2, 'size', 'Size', 'show', 'input', 'native', 130, 40, 255, '', '', '', '', '', '', 102, 'no'),
(125, 2, 'maxlenght', 'Maxlength', 'show', 'input', 'native', 130, 40, 255, '', '', '', '', '', '', 102, 'no'),
(126, 2, 'note', 'note', 'show', 'input', 'native', 130, 40, 255, '', '', '', '', '', '', 102, 'no'),
(127, 2, 'tooltip', 'Tooltip', 'show', 'input', 'native', 130, 40, 255, '', '', '', '', '', '', 102, 'no'),
(128, 2, 'placeholder', 'Placeholder', 'show', 'input', 'native', 130, 40, 255, '', '', '', '', '', '', 102, 'no'),
(129, 2, 'value', 'value', 'show', 'input', 'native', 130, 40, 255, '', '', '', '', '', '', 102, 'no'),
(130, 2, 'sql1', 'sql1', 'show', 'input', 'native', 130, '40,4', 255, '', '', '', '', '', '', 103, 'no'),
(131, 2, 'parameter', 'Parameter', 'show', 'input', 'native', 130, '40,4', 255, '', '', '', '', '', '', 103, 'no'),
(132, 2, 'clientJs', 'ClientJS', 'show', 'input', 'native', 130, 40, 255, '', '', '', '', '', '', 103, 'no'),
(133, 2, 'feGroup', 'feGroup', 'show', 'input', 'native', 130, 40, 255, '', '', '', '', '', '', 104, 'no'),
(134, 2, 'debug', 'Debug', 'show', 'checkbox', 'native', 130, 0, 0, '', '', '', '', '', '', 104, 'no'),
(135, 2, 'deleted', 'Deleted', 'show', 'checkbox', 'native', 400, 0, 0, '', '', '', '', '', '', 104, 'no'),
(136, 2, 'modified', 'Modified', 'readonly', 'input', 'native', 410, 40, 20, '', '', '', '', '', '', 104, 'no'),
(137, 2, 'created', 'Created', 'readonly', 'input', 'native', 420, 40, 20, '', '', '', '', '', '', 104, 'no');
# FormEditor: Small
INSERT INTO Form (name, title, noteInternal, tableName, permitNew, permitEdit, render, multiSql, parameter) VALUES
('person', 'Person {{SELECT ": ", firstName, " ", name, " (", id, ")" FROM Person WHERE id = {{recordId:S0}}}}',
'Please secure the form',
'Person', 'always', 'always', 'bootstrap', '', '');
# FormEditor: FormElements
INSERT INTO FormElement (id, formId, name, label, mode, type, class, ord, size, maxLength, note, clientJs, value, sql1, sql2, parameter, feIdContainer, debug)
VALUES
(200, 3, 'name', 'Name', 'show', 'input', 'native', 10, 50, 255, '', '', '', '', '', '', 0, 'no'),
(201, 3, 'firstName', 'Firstname', 'show', 'input', 'native', 10, 50, 255, '', '', '', '', '', '', 0, 'no');
<?php
/**
* Created by PhpStorm.
* User: crose
* Date: 2/2/16
* Time: 8:47 AM
*/
//namespace qfq;
//
//use qfq\Save;
//use qfq\Database;
require_once(__DIR__ . '/../../qfq/Save.php');
//require_once(__DIR__ . '/../../qfq/Database.php');
require_once(__DIR__ . '/AbstractDatabaseTest.php');
class SaveTest extends AbstractDatabaseTest {
public function testInsertRecord() {
$save = new qfq\Save(array(), array(), array());
$db = new \qfq\Database();
$values = ['name' => 'Doe', 'firstName' => 'John'];
$id = $save->insertRecord('Person', $values);
$sql = "SELECT name, firstName FROM Person WHERE id = ? ";
$result = $db->sql($sql, ROW_REGULAR, [$id]);
$this->assertEquals($values, $result[0]);
}
public function testUpdateRecord() {
$save = new qfq\Save(array(), array(), array());
$db = new \qfq\Database();
$values = ['name' => 'Doe', 'firstName' => 'John'];
$id = $save->insertRecord('Person', $values);
$values = ['name' => 'Doe', 'firstName' => 'Big John'];
$save->updateRecord('Person', $values, $id);
$sql = "SELECT name, firstName FROM Person WHERE id = ? ";
$result = $db->sql($sql, ROW_REGULAR, [$id]);
$this->assertEquals($values, $result[0]);
}
/**
* @throws Exception
*/
protected function setUp() {
parent::setUp();
$this->executeSQLFile(__DIR__ . '/fixtures/Generic.sql', true);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment