Skip to content

Commit d0554b7

Browse files
authored
Merge pull request #44 from JesusTheHun/feat/delete-row-use-row-values
Feat: `haveInDatabase` tear down use row values if primary key values are available
2 parents b65b3ee + 6a50bb3 commit d0554b7

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

src/Codeception/Module/Db.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -759,8 +759,8 @@ protected function loadDumpUsingDriver(string $databaseKey): void
759759
}
760760

761761
/**
762-
* Inserts an SQL record into a database. This record will be erased after the test,
763-
* unless you've configured "skip_cleanup_if_failed", and the test fails.
762+
* Inserts an SQL record into a database. This record will be erased after the test,
763+
* unless you've configured "skip_cleanup_if_failed", and the test fails.
764764
*
765765
* ```php
766766
* <?php
@@ -801,8 +801,15 @@ private function addInsertedRow(string $table, array $row, $id): void
801801
$primaryKey = $this->_getDriver()->getPrimaryKey($table);
802802
$primary = [];
803803
if ($primaryKey !== []) {
804-
if ($id && count($primaryKey) === 1) {
805-
$primary [$primaryKey[0]] = $id;
804+
$filledKeys = array_intersect($primaryKey, array_keys($row));
805+
$missingPrimaryKeyColumns = array_diff_key($primaryKey, $filledKeys);
806+
807+
if (count($missingPrimaryKeyColumns) === 0) {
808+
$primary = array_intersect_key($row, array_flip($primaryKey));
809+
} elseif (count($missingPrimaryKeyColumns) === 1) {
810+
$primary = array_intersect_key($row, array_flip($primaryKey));
811+
$missingColumn = reset($missingPrimaryKeyColumns);
812+
$primary[$missingColumn] = $id;
806813
} else {
807814
foreach ($primaryKey as $column) {
808815
if (isset($row[$column])) {

tests/data/dumps/mysql.sql

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,25 @@ CREATE TABLE `no_pk` (
9494
`status` varchar(255) NOT NULL
9595
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
9696

97+
CREATE TABLE `auto_increment_not_on_pk` (
98+
`id` int(11) NOT NULL,
99+
`counter` int(11) NOT NULL,
100+
PRIMARY KEY (`id`)
101+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
102+
103+
CREATE INDEX counter ON `auto_increment_not_on_pk` (counter);
104+
ALTER TABLE `auto_increment_not_on_pk`
105+
MODIFY counter int AUTO_INCREMENT;
106+
107+
108+
CREATE TABLE `auto_increment_on_composite_pk` (
109+
`id` int(11) NOT NULL,
110+
`counter` int(11) AUTO_INCREMENT NOT NULL,
111+
PRIMARY KEY (`id`, `counter`)
112+
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
113+
97114
CREATE TABLE `empty_table` (
98115
`id` int(11) NOT NULL AUTO_INCREMENT,
99116
`field` varchar(255),
100117
PRIMARY KEY(`id`)
101-
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
118+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

tests/unit/Codeception/Module/Db/MySqlDbTest.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testConnectionIsResetOnEveryTestWhenReconnectIsTrue()
5151

5252
// Simulate a test that runs
5353
$this->module->_before($testCase1);
54-
54+
5555
$connection1 = $this->module->dbh->query('SELECT CONNECTION_ID()')->fetch(PDO::FETCH_COLUMN);
5656
$this->module->_after($testCase1);
5757

@@ -83,7 +83,7 @@ public function testInitialQueriesAreExecuted()
8383
];
8484
$this->module->_reconfigure($config);
8585
$this->module->_before(Stub::makeEmpty(TestInterface::class));
86-
86+
8787
$usedDatabaseName = $this->module->dbh->query('SELECT DATABASE();')->fetch(PDO::FETCH_COLUMN);
8888

8989
$this->assertSame($dbName, $usedDatabaseName);
@@ -101,4 +101,28 @@ public function testGrabColumnFromDatabase()
101101
],
102102
$emails);
103103
}
104+
105+
public function testHaveInDatabaseAutoIncrementOnANonPrimaryKey()
106+
{
107+
$testData = [
108+
'id' => 777,
109+
];
110+
$this->module->haveInDatabase('auto_increment_not_on_pk', $testData);
111+
$this->module->seeInDatabase('auto_increment_not_on_pk', $testData);
112+
$this->module->_after(Stub::makeEmpty(TestInterface::class));
113+
114+
$this->module->dontSeeInDatabase('auto_increment_not_on_pk', $testData);
115+
}
116+
117+
public function testHaveInDatabaseAutoIncrementOnCompositePrimaryKey()
118+
{
119+
$testData = [
120+
'id' => 777,
121+
];
122+
$this->module->haveInDatabase('auto_increment_on_composite_pk', $testData);
123+
$this->module->seeInDatabase('auto_increment_on_composite_pk', $testData);
124+
$this->module->_after(Stub::makeEmpty(TestInterface::class));
125+
126+
$this->module->dontSeeInDatabase('auto_increment_on_composite_pk', $testData);
127+
}
104128
}

0 commit comments

Comments
 (0)