From 3ce8b44f29aa89649ab000a10a09508019901f6b Mon Sep 17 00:00:00 2001 From: Anton Fedurtsya Date: Wed, 24 Mar 2021 12:59:58 +0200 Subject: [PATCH 1/3] Fix not primary auto_increment case The assumption that auto_increment is always the value for primary key is wrong. As example: * there is a table user, where primary id is - userId which is a md5 random string. * Autoincrement is on the userNumber field, which is not primary, but by some reason it is there. Now we add a user with haveInDatabase, and with specific id:['userId'=>'special'], what will endup in insertedRows? usesrId => 10 for example, as insertId for user was 10, and sure, it will not be cleaned up. --- src/Codeception/Module/Db.php | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/Codeception/Module/Db.php b/src/Codeception/Module/Db.php index 9414278b..78c34903 100644 --- a/src/Codeception/Module/Db.php +++ b/src/Codeception/Module/Db.php @@ -769,17 +769,13 @@ private function addInsertedRow($table, array $row, $id) $primaryKey = $this->_getDriver()->getPrimaryKey($table); $primary = []; if ($primaryKey) { - if ($id && count($primaryKey) === 1) { - $primary [$primaryKey[0]] = $id; - } else { - foreach ($primaryKey as $column) { - if (isset($row[$column])) { - $primary[$column] = $row[$column]; - } else { - throw new \InvalidArgumentException( - 'Primary key field ' . $column . ' is not set for table ' . $table - ); - } + foreach ($primaryKey as $column) { + if (isset($row[$column])) { + $primary[$column] = $row[$column] ?? $id; + } else { + throw new \InvalidArgumentException( + 'Primary key field ' . $column . ' is not set for table ' . $table + ); } } } else { From c76aa447a45ca3904cc8afd28024bfd1bc01db01 Mon Sep 17 00:00:00 2001 From: Anton Fedurtsya Date: Wed, 24 Mar 2021 13:24:20 +0200 Subject: [PATCH 2/3] Fix compatibility with older php versions --- src/Codeception/Module/Db.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Codeception/Module/Db.php b/src/Codeception/Module/Db.php index 78c34903..7ab8b324 100644 --- a/src/Codeception/Module/Db.php +++ b/src/Codeception/Module/Db.php @@ -771,7 +771,7 @@ private function addInsertedRow($table, array $row, $id) if ($primaryKey) { foreach ($primaryKey as $column) { if (isset($row[$column])) { - $primary[$column] = $row[$column] ?? $id; + $primary[$column] = isset($row[$column]) ? $row[$column] : $id; } else { throw new \InvalidArgumentException( 'Primary key field ' . $column . ' is not set for table ' . $table From ce8c5d8eb9d5dd59c276f82c99ee6e3688ed133b Mon Sep 17 00:00:00 2001 From: Anton Fedurtsya Date: Wed, 24 Mar 2021 13:51:35 +0200 Subject: [PATCH 3/3] Fix condition to not ignore auto_increment value --- src/Codeception/Module/Db.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Codeception/Module/Db.php b/src/Codeception/Module/Db.php index 7ab8b324..246ddc44 100644 --- a/src/Codeception/Module/Db.php +++ b/src/Codeception/Module/Db.php @@ -770,7 +770,7 @@ private function addInsertedRow($table, array $row, $id) $primary = []; if ($primaryKey) { foreach ($primaryKey as $column) { - if (isset($row[$column])) { + if (isset($row[$column]) || $id) { $primary[$column] = isset($row[$column]) ? $row[$column] : $id; } else { throw new \InvalidArgumentException(