Skip to content

[FEATURE] Add CSV dataset export for functional tests - #698

Open
bmack wants to merge 1 commit into
TYPO3:mainfrom
bmack:feature/csvs
Open

bmack wants to merge 1 commit into
TYPO3:mainfrom
bmack:feature/csvs

Conversation

@bmack

@bmack bmack commented Feb 5, 2026

Copy link
Copy Markdown
Member

Introduce exportCSVDataSet() to write current database state to CSV files matching the existing fixture format. This completes the import → assert → export workflow for functional tests.

Usage example:

$this->exportCSVDataSet(
    $this->instancePath . '/export.csv',
    ['pages' => ['uid', 'pid', 'title'], 'tt_content']
);

Tables can specify explicit fields or export all columns.

Additionally, assertCSVDataSet() now auto-exports the actual database state to a _actual.csv file when assertions fail and the TYPO3_TESTING_EXPORT_DATASETS environment variable is set.

}

if (!empty($failMessages)) {
if (getenv('TYPO3_TESTING_EXPORT_DATASETS')) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about that ... at least we should use a naming reflecting the case DATASETS are not always exported:

Suggested change
if (getenv('TYPO3_TESTING_EXPORT_DATASETS')) {
if ((bool)getenv('TYPO3_TESTING_EXPORT_DATASETS_ON_FAILED_ASSERTION')) {

if ($value === null) {
return '\\NULL';
}
$stringValue = (string)$value;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the implementation here, all values are converted to strings and some generic formating.

Not sure if we should bind that to the real database schema inforamation for table/column ... , having text/string/varchar field values always enquoted and other data (int, float/double, ) in the respected form.

Also thinking about native json fields for supported dbms (not using fallback TEXT/CBOLB for the others etc ) ....

Needs some thinking about this ...

return '""';
}
if (preg_match('/[,"\n\r\\\\]/', $stringValue)) {
return '"' . str_replace('"', '""', $stringValue) . '"';

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only quotes text values if they contain , " or some line breaks. If not, they are not quoted ...

GIVEN...: Some Text
EXPECTED: "Some Text"
ACTUAL..: Some Text

@sbuerk
sbuerk force-pushed the feature/csvs branch 7 times, most recently from ce2c366 to 9baf42c Compare February 10, 2026 01:56
Introduce exportCSVDataSet() to write current database state
to CSV files matching the existing fixture format. This completes
the import → assert → export workflow for functional tests.

Usage example:

    $this->exportCSVDataSet(
        $this->instancePath . '/export.csv',
        ['pages' => ['uid', 'pid', 'title'], 'tt_content']
    );

Tables can specify explicit fields or export all columns.

Additionally, assertCSVDataSet() now auto-exports the actual
database state to a _actual.csv file when assertions fail and
the TYPO3_TESTING_EXPORT_DATASETS environment variable is set.
@ohader

ohader commented Feb 10, 2026

Copy link
Copy Markdown
Member

DataSet::persist was part of the Testing Framework as was removed - for whatever reason - in b3d4daa

@CybotTM CybotTM left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes: exported files do not always read back correctly through DataSet::read(), and the unit tests would not notice a broken exportCSVDataSet().

I applied the PR to current main (480a74d) and ran a functional probe in TYPO3 core main (8ef5361) on SQLite, MariaDB and PostgreSQL, with identical results:

  • Works: exporting the tables of DataScenarios/Regular/DataSet/ImportDefault.csv and Fixtures/be_users_admin.csv, then assertCSVDataSet() on the export; truncating and re-importing the export, then asserting the original fixtures; native date/datetime/time columns including NULL; the _actual.csv written on a failed assertion.
  • Fails: blob columns and values with a backslash before a quote (inline comments below).

Requested changes:

  1. Quote blob/binary values. They fall into the default branch and are written unquoted; a serialized be_users.uc is cut at its first comma on read back and the following columns shift.
  2. Make writer and reader agree on escaping. formatCsvValue() doubles ", but DataSet::read() calls fgetcsv(..., '"', '\\'). A JSON value {"a":"b,c","q":"x\"y"} comes back as {"a":"b,c","q":"x\"y""}", and a value ending in a backslash (C:\path\) swallows the next row.
  3. Test exportCSVDataSet() itself. exportedCsvCanBeReadBackByDataSetRead rebuilds the export loop instead of calling the method: with the header line in exportCSVDataSet() broken, all 14 new tests stay green. A functional test that imports a fixture, exports it and reads or asserts the export would cover it, including blob, json and backslash values.
  4. Never overwrite the asserted fixture, and do not report an export that did not happen (inline comments).
  5. Update the PR description: it names TYPO3_TESTING_EXPORT_DATASETS, the code reads TYPO3_TESTING_EXPORT_DATASETS_ON_FAILED_ASSERTION.
Smaller points
  • Only 1 enables the export; true, yes and on are read as off because of (bool)((int)getenv(...)).
  • On PHP 8.5 (PHPUnit 13) each of the 14 new tests raises the notice "No expectations were configured for the mock object" from getAccessibleMock(); main has none.
  • The Build/phpunit/UnitTests.xml change is unrelated to the feature.
  • The comment in Resources/Core/Build/FunctionalTests.xml says "Comment following line out" where uncommenting is meant, and contains "Do not use provide it here".
  • The CI runs on this PR are from 2026-02-10, 34 commits behind main. On current main, PHP 8.2 gates are green; on PHP 8.5 the PHPStan error in Testbase.php and the cacheResult deprecation also occur on main without this PR.

$columnDoctrineType instanceof TimeType,
$columnDoctrineType instanceof TimeImmutableType => '"' . str_replace('"', '""', (string)$value) . '"',
$columnDoctrineType instanceof BooleanType => (string)(int)($value),
default => (string)$value,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blob and binary columns land here and are written unquoted. Exporting be_users.uc with a serialized array containing , and " and reading it back with DataSet::read() returns the value cut at the first comma, and the remaining columns shift (SQLite, MariaDB, PostgreSQL). Other core blob columns: be_users.mfa, fe_users.uc, sys_registry.entry_value. Quoting everything except the numeric and boolean types would avoid listing each string-like type.

$columnDoctrineType instanceof DateTimeImmutableType,
$columnDoctrineType instanceof DateType,
$columnDoctrineType instanceof TimeType,
$columnDoctrineType instanceof TimeImmutableType => '"' . str_replace('"', '""', (string)$value) . '"',

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doubling " does not match the reader: DataSet::read() uses \ as escape character (fgetcsv($fileHandle, 0, ',', '"', '\\')). A json value {"a":"b,c","q":"x\"y"} is read back as {"a":"b,c","q":"x\"y""}", and a string ending in a backslash merges the next row into it. Either the reader needs escape '' or the writer has to escape for the reader.

foreach ($dataSet->getTableNames() as $tableName) {
$exportTables[$tableName] = $dataSet->getFields($tableName) ?? [];
}
$exportPath = preg_replace('/\.csv$/', '_actual.csv', $fileName);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the fixture name does not end in lowercase .csv (e.g. Result.CSV), the pattern does not match, $exportPath equals $fileName and the export overwrites the fixture being asserted. Reproduced with wrong.CSV. Appending the suffix unconditionally or refusing when both paths are equal would prevent it.

}
}

file_put_contents($path, $output);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return value is not checked. In a read-only directory this raises a PHP warning and the failure message still says "Actual database state exported to: .../wrong_actual.csv" although no file exists.

],
];

// Build CSV output the same way exportCSVDataSet does

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rebuilds the export instead of calling exportCSVDataSet(), so the method body is not covered: changing its field header line to implode(';', $fields) leaves all 14 tests green. The copy has also drifted already (str_repeat(',', ...) in the table line, which the implementation dropped).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants