Skip to content

Commit

Permalink
haveUpdatedDb() now returns number of rows affected
Browse files Browse the repository at this point in the history
Also exposed getQuotedName for use from other modules
Plus some minor tweaks and comments update
  • Loading branch information
codemedic committed Nov 8, 2015
1 parent d4f2775 commit c854c43
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"description": "Extension for codeception that makes it possible to switch between and interact with multiple databases. Supports only MySQL for now.",
"type": "library",
"require": {
"php": ">=5.4.0",
"codeception/codeception": "2.0.*"
},
"license": "MIT",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ private function getChosenDriver()
*/
public function createDatabase($database, $options = null, $cleanup_after = self::CLEANUP_AFTER_TEST)
{
$options = $options?:[];
$options = $options ?: [];
if (!is_array($options)) {
throw new TestRuntimeException('Invalid options given for '.__METHOD__);
}
Expand Down Expand Up @@ -285,7 +285,7 @@ public function amConnectedToDb($connector)
*
* @return mixed what ever the callable returns
*/
public function connectToDbAndExecute($connector, $callable)
public function connectToDbAndExecute($connector, callable $callable)
{
$old_connector = $this->amConnectedToDb($connector);
$result = call_user_func($callable);
Expand Down Expand Up @@ -376,18 +376,18 @@ public function haveInDb(
// mapper function that maps each field from $pk_field to one or zero
// 1 if the field is set in the given $field_values and is not NULL; 0 otherwise
$fnIssetFieldAndNotNull = function ($a_pk_field) use ($field_values) {
return (isset($field_values[$a_pk_field]) && $field_values[$a_pk_field] !== null)? 1: 0;
return isset($field_values[$a_pk_field]) ? 1 : 0;
};

if (!$pk_value_for_cleanup // is empty
// or $pk_value_for_cleanup is an array with all null values
// is empty or $pk_value_for_cleanup is an array with all null values
if (empty($pk_value_for_cleanup)
|| (is_array($pk_value_for_cleanup) && count(array_diff($pk_value_for_cleanup, [null])) == 0)
) {
if (count($pk_field) == 1 && $last_insert_id) {
$pk_value_for_cleanup = [reset($pk_field) => $last_insert_id];

// if the fields in pk_field are present in $field_values and none of them are NULL,
// then we can take values from there
// if the fields in pk_field are present in $field_values and none of them are NULL,
// then we can take values from there
} elseif (0 !== array_product(array_map($fnIssetFieldAndNotNull, $pk_field))) {
// filter the $field_values using $pk_field as keys
$pk_value_for_cleanup = array_intersect_key($field_values, array_flip($pk_field));
Expand All @@ -398,7 +398,7 @@ public function haveInDb(
} else {
$pk_value_for_cleanup = array_combine(
$pk_field,
is_array($pk_value_for_cleanup)? $pk_value_for_cleanup: [$pk_value_for_cleanup]
is_array($pk_value_for_cleanup) ? $pk_value_for_cleanup : [$pk_value_for_cleanup]
);
}

Expand All @@ -407,8 +407,7 @@ public function haveInDb(
}

if ($last_insert_id !== null) {
$ret = $pk_field_was_array? array_combine($pk_field, [$last_insert_id]) : $last_insert_id;
return $ret;
return $pk_field_was_array ? array_combine($pk_field, [$last_insert_id]) : $last_insert_id;
}

if ($multi_field_pk_values !== null) {
Expand Down Expand Up @@ -474,7 +473,7 @@ public function haveInDbMultipleRows(
* @param array $field_updates and array of field values of the form ['Field1'=>Value, 'Field2'=>Value] which will
* describe the new values for the given fields
*
* @return void
* @return int number of rows updated
*/
public function haveUpdatedDb($table, $field_updates, $criteria)
{
Expand All @@ -500,6 +499,8 @@ public function haveUpdatedDb($table, $field_updates, $criteria)
)
);
}

return $statement->rowCount();
}

/**
Expand Down Expand Up @@ -737,10 +738,11 @@ public function rollbackTransaction()
*
* @internal Use this only from other modules, as it does not generate a readable step output
*/
public function transaction($callable)
public function transaction(callable $callable)
{
try {
$this->debug('Current Connector is '.$this->getCurrentConnector());

$this->startTransaction();
$result = call_user_func($callable);
$this->commitTransaction();
Expand Down Expand Up @@ -874,7 +876,7 @@ private static function normaliseAsIs(&$value)
protected static function normaliseParameterList($params)
{
$toScalar = function ($value) {
return (null === $value)? $value: (string)$value;
return (null === $value) ? $value : (string)$value;
};

array_walk(
Expand All @@ -884,7 +886,7 @@ function (&$value, $field) use ($toScalar) {

// Check if no field was specified (so the array index will be an integer).
if (is_numeric($field)) {
$value = ($value instanceof AsIs)?
$value = ($value instanceof AsIs) ?
array(null, null, $toScalar($value)) : array(null, '?', $toScalar($value));
} else {
$value = ($value instanceof AsIs)?
Expand Down Expand Up @@ -918,7 +920,7 @@ private function formSqlInsert($table, array $data_rows, $ignore_duplicate_key)
$ignore_duplicate_key_sql = null;
// either true or an array containing field names
if ($ignore_duplicate_key && (!is_array($ignore_duplicate_key) || count($ignore_duplicate_key))) {
$update_fields = is_array($ignore_duplicate_key)? $ignore_duplicate_key: array_keys($data_rows[0]);
$update_fields = is_array($ignore_duplicate_key) ? $ignore_duplicate_key : array_keys($data_rows[0]);
$ignore_duplicate_key_sql = ' ON DUPLICATE KEY UPDATE '.
implode(
', ',
Expand Down Expand Up @@ -985,7 +987,7 @@ private function formSqlInsertSingle($table, array $data, array $pk_field, $igno
// either true or an array containing field names
if ($ignore_duplicate_key && (!is_array($ignore_duplicate_key) || count($ignore_duplicate_key))) {
$update_fields = array_filter(
is_array($ignore_duplicate_key)? $ignore_duplicate_key: array_keys($data),
is_array($ignore_duplicate_key) ? $ignore_duplicate_key : array_keys($data),
function ($field) use ($pk_field) {
return !in_array($field, $pk_field);
}
Expand Down Expand Up @@ -1054,14 +1056,14 @@ function ($value) use (&$param_list) {
*/
private static function prepareClause($field, $placeholder, $value, Driver $driver, $is_for_nulls = true)
{
$rhs = ($placeholder === null)? $value : $placeholder;
$rhs = ($placeholder === null) ? $value : $placeholder;

if ($field === null) {
return $rhs;
}

// If the value is NULL, we need to use IS NULL, rather than =.
$operator = (($is_for_nulls && ($value === null))? 'IS': '=');
$operator = (($is_for_nulls && ($value === null)) ? 'IS' : '=');
return "{$driver->getQuotedName($field)} {$operator} {$rhs}";
}

Expand Down Expand Up @@ -1228,4 +1230,19 @@ function ($value) use (&$param_list, $driver) {

return [$sql, $param_list];
}

/**
* Get driver specific encoded table and field names; eg. back-tick for database, table and field names in MySQL
*
* @param string $name
*
* @return string
*
* @internal for use from other modules due to incompatibility with readable steps output
*
*/
public function getQuotedName($name)
{
return $this->getChosenDriver()->getQuotedName($name);
}
}

0 comments on commit c854c43

Please sign in to comment.