Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/Codeception/Module/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <?php
Expand Down Expand Up @@ -801,8 +801,15 @@ 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));
$missingPrimaryKeyColumns = array_diff_key($primaryKey, $filledKeys);

if (count($missingPrimaryKeyColumns) === 0) {
$primary = array_intersect_key($row, array_flip($primaryKey));
} elseif (count($missingPrimaryKeyColumns) === 1) {
$primary = array_intersect_key($row, array_flip($primaryKey));
$missingColumn = reset($missingPrimaryKeyColumns);
$primary[$missingColumn] = $id;
} else {
foreach ($primaryKey as $column) {
if (isset($row[$column])) {
Expand Down
19 changes: 18 additions & 1 deletion tests/data/dumps/mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,25 @@ 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 `auto_increment_on_composite_pk` (
`id` int(11) NOT NULL,
`counter` int(11) AUTO_INCREMENT NOT NULL,
PRIMARY KEY (`id`, `counter`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE `empty_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`field` varchar(255),
PRIMARY KEY(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
28 changes: 26 additions & 2 deletions tests/unit/Codeception/Module/Db/MySqlDbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function testConnectionIsResetOnEveryTestWhenReconnectIsTrue()

// Simulate a test that runs
$this->module->_before($testCase1);

$connection1 = $this->module->dbh->query('SELECT CONNECTION_ID()')->fetch(PDO::FETCH_COLUMN);
$this->module->_after($testCase1);

Expand Down Expand Up @@ -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);
Expand All @@ -101,4 +101,28 @@ 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);
}

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);
}
}