From fba7f0664d129cfebdace3c02d6404704117535d Mon Sep 17 00:00:00 2001 From: Jonathan Massuchetti Date: Thu, 1 Dec 2022 19:27:46 +0100 Subject: [PATCH 1/2] feat: use rows value to delete inserted row if primary key is filled --- src/Codeception/Module/Db.php | 9 +++++++-- tests/data/dumps/mysql.sql | 12 +++++++++++- tests/unit/Codeception/Module/Db/MySqlDbTest.php | 12 ++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/Codeception/Module/Db.php b/src/Codeception/Module/Db.php index 6c116b60..d41d3340 100644 --- a/src/Codeception/Module/Db.php +++ b/src/Codeception/Module/Db.php @@ -801,8 +801,13 @@ private function addInsertedRow(string $table, array $row, $id): void $primaryKey = $this->_getDriver()->getPrimaryKey($table); $primary = []; if ($primaryKey !== []) { - if ($id && count($primaryKey) === 1) { - $primary [$primaryKey[0]] = $id; + $filledKeys = array_intersect($primaryKey, array_keys($row)); + $primaryKeyIsFilled = count($filledKeys) === count($primaryKey); + + if ($primaryKeyIsFilled) { + $primary = array_intersect_key($row, array_flip($primaryKey)); + } elseif ($id && count($primaryKey) === 1) { + $primary[$primaryKey[0]] = $id; } else { foreach ($primaryKey as $column) { if (isset($row[$column])) { diff --git a/tests/data/dumps/mysql.sql b/tests/data/dumps/mysql.sql index 4102f7ef..b1ca296c 100644 --- a/tests/data/dumps/mysql.sql +++ b/tests/data/dumps/mysql.sql @@ -94,8 +94,18 @@ CREATE TABLE `no_pk` ( `status` varchar(255) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +CREATE TABLE `auto_increment_not_on_pk` ( + `id` int(11) NOT NULL, + `counter` int(11) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE INDEX counter ON `auto_increment_not_on_pk` (counter); +ALTER TABLE `auto_increment_not_on_pk` + MODIFY counter int AUTO_INCREMENT; + CREATE TABLE `empty_table` ( `id` int(11) NOT NULL AUTO_INCREMENT, `field` varchar(255), PRIMARY KEY(`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; \ No newline at end of file +) ENGINE=InnoDB DEFAULT CHARSET=utf8; diff --git a/tests/unit/Codeception/Module/Db/MySqlDbTest.php b/tests/unit/Codeception/Module/Db/MySqlDbTest.php index 7fb56397..c0cf32e9 100644 --- a/tests/unit/Codeception/Module/Db/MySqlDbTest.php +++ b/tests/unit/Codeception/Module/Db/MySqlDbTest.php @@ -101,4 +101,16 @@ public function testGrabColumnFromDatabase() ], $emails); } + + public function testHaveInDatabaseAutoIncrementOnANonPrimaryKey() + { + $testData = [ + 'id' => 777, + ]; + $this->module->haveInDatabase('auto_increment_not_on_pk', $testData); + $this->module->seeInDatabase('auto_increment_not_on_pk', $testData); + $this->module->_after(Stub::makeEmpty(TestInterface::class)); + + $this->module->dontSeeInDatabase('auto_increment_not_on_pk', $testData); + } } From 6a50bb3bc78b8968b060758b3d5532e1448b84d5 Mon Sep 17 00:00:00 2001 From: Jonathan Massuchetti Date: Sat, 3 Dec 2022 00:38:52 +0100 Subject: [PATCH 2/2] feat: support auto increment on a composite pk --- src/Codeception/Module/Db.php | 14 ++++++++------ tests/data/dumps/mysql.sql | 7 +++++++ tests/unit/Codeception/Module/Db/MySqlDbTest.php | 16 ++++++++++++++-- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/Codeception/Module/Db.php b/src/Codeception/Module/Db.php index d41d3340..9660ad52 100644 --- a/src/Codeception/Module/Db.php +++ b/src/Codeception/Module/Db.php @@ -759,8 +759,8 @@ protected function loadDumpUsingDriver(string $databaseKey): void } /** - * Inserts an SQL record into a database. This record will be erased after the test, - * unless you've configured "skip_cleanup_if_failed", and the test fails. + * Inserts an SQL record into a database. This record will be erased after the test, + * unless you've configured "skip_cleanup_if_failed", and the test fails. * * ```php * module->_before($testCase1); - + $connection1 = $this->module->dbh->query('SELECT CONNECTION_ID()')->fetch(PDO::FETCH_COLUMN); $this->module->_after($testCase1); @@ -83,7 +83,7 @@ public function testInitialQueriesAreExecuted() ]; $this->module->_reconfigure($config); $this->module->_before(Stub::makeEmpty(TestInterface::class)); - + $usedDatabaseName = $this->module->dbh->query('SELECT DATABASE();')->fetch(PDO::FETCH_COLUMN); $this->assertSame($dbName, $usedDatabaseName); @@ -113,4 +113,16 @@ public function testHaveInDatabaseAutoIncrementOnANonPrimaryKey() $this->module->dontSeeInDatabase('auto_increment_not_on_pk', $testData); } + + public function testHaveInDatabaseAutoIncrementOnCompositePrimaryKey() + { + $testData = [ + 'id' => 777, + ]; + $this->module->haveInDatabase('auto_increment_on_composite_pk', $testData); + $this->module->seeInDatabase('auto_increment_on_composite_pk', $testData); + $this->module->_after(Stub::makeEmpty(TestInterface::class)); + + $this->module->dontSeeInDatabase('auto_increment_on_composite_pk', $testData); + } }