Skip to content

Commit

Permalink
[TASK] Detect duplicate table records within importCSVDataSet()
Browse files Browse the repository at this point in the history
The testing-frameworks provides the ability to read csv fixture
files to import database datasets. The internal `DataSet::import()`
implementation is provided by `FunctionalTestCase->importCSVDataSet()`.

Sometimes duplicate rows with the same identifier (`uid` or `hash`)
are included in a csv dataset file which are overriden.

This change now checks during the dataseet import phase for these
duplicates and throw a exception stating the table and id value
to make the usage easier and helps with finding test-setup issues.

Note: `assertCSVDataSet()` counter part will not check for duplicates.

Resolves: #478
Releases: main, 7
  • Loading branch information
sbuerk committed Jul 28, 2023
1 parent ab9a614 commit b18de71
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
28 changes: 25 additions & 3 deletions Classes/Core/Functional/Framework/DataHandling/DataSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class DataSet
* @param bool $applyDefaultValues
* @return DataSet
*/
public static function read(string $fileName, bool $applyDefaultValues = false): DataSet
public static function read(string $fileName, bool $applyDefaultValues = false, bool $checkForDuplicates = false): DataSet
{
$data = self::parseData(self::readData($fileName));
$data = self::parseData(self::readData($fileName), $fileName, $checkForDuplicates);

if ($applyDefaultValues) {
$data = self::applyDefaultValues($data);
Expand Down Expand Up @@ -79,7 +79,7 @@ protected static function readData(string $fileName): array
* @param array $rawData
* @return array
*/
protected static function parseData(array $rawData): array
protected static function parseData(array $rawData, string $fileName, bool $checkForDuplicates): array
{
$data = [];
$tableName = null;
Expand Down Expand Up @@ -137,8 +137,30 @@ protected static function parseData(array $rawData): array
unset($value);
$element = array_combine($data[$tableName]['fields'], $values);
if ($idIndex !== null) {
if ($checkForDuplicates && is_array($data[$tableName]['elements'][$values[$idIndex]] ?? false)) {
throw new \RuntimeException(
sprintf(
'DataSet "%s" containes a duplicate record for idField "%s.uid" => %s',
$fileName,
$tableName,
$values[$idIndex]
),
1690538506
);
}
$data[$tableName]['elements'][$values[$idIndex]] = $element;
} elseif ($hashIndex !== null) {
if ($checkForDuplicates && is_array($data[$tableName]['elements'][$values[$hashIndex]] ?? false)) {
throw new \RuntimeException(
sprintf(
'DataSet "%s" containes a duplicate record for idHash "%s.hash" => %s',
$fileName,
$tableName,
$values[$hashIndex]
),
1690541069
);
}
$data[$tableName]['elements'][$values[$hashIndex]] = $element;
} else {
$data[$tableName]['elements'][] = $element;
Expand Down
2 changes: 1 addition & 1 deletion Classes/Core/Functional/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ protected function importDataSet(string $path): void
*/
public function importCSVDataSet(string $path): void
{
$dataSet = DataSet::read($path, true);
$dataSet = DataSet::read($path, true, true);

foreach ($dataSet->getTableNames() as $tableName) {
$connection = $this->getConnectionPool()->getConnectionForTable($tableName);
Expand Down

0 comments on commit b18de71

Please sign in to comment.