Skip to content
Open
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
21 changes: 20 additions & 1 deletion CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,26 @@ To provide a change to the testing framework, you have to

./Build/Scripts/runTests.sh -s functional

and confirming that the test runs have the same result as without the
* Testing framework functional tests. Composer's distribution archive
omits the ``Tests/`` directory. From the Core checkout, replace the
distribution copy with a clone of your branch if you have not already
done so:

.. code-block:: bash

mv vendor/typo3/testing-framework "$(mktemp -d)" && \
git clone --branch "[BRANCH_OF_CHANGE]" \
git@github.com:[YOUR_USERNAME]/testing-framework.git \
vendor/typo3/testing-framework

Then run the functional tests from the Core checkout:

.. code-block:: bash

./Build/Scripts/runTests.sh -s functional -d sqlite -- \
vendor/typo3/testing-framework/Tests/Functional/

and confirming that the Core test runs have the same result as without the
changes,

4. push the change to your fork by
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,12 @@ private function prepareColumns(array $columnNames, Table $table): array
{
$columnTypes = array_map(
function (string $columnName) use ($table) {
// Doctrine DBAL v4 converted the `*ParameterType` to an enum, and therefore returning this enum instead
// of the string value like before. As this is a non-baked enum, it cannot be serialized or json_encoded,
// and breaking the snapshot export badly. Due to the requirement to support Doctrine DBAL v3 and v4 it
// is necessary to detect the enum and return the doctrine type name instead. Connection transforms
// the provided types during import to the correct ParameterType again.
// @see https://github.com/doctrine/dbal/blob/4.0.x/UPGRADE.md#bc-break-converted-enum-like-classes-to-enums
// @todo Simplify this after Doctine DBAL v3 support can be dropped.
// Bind raw strings without reconverting them, which would double-encode JSON.
// Use type names because ParameterType enum cases cannot be JSON-encoded.
$type = $table->getColumn($columnName)->getType();
$bindingType = $type->getBindingType();
return (enum_exists(ParameterType::class))
? Type::lookupName($type)
: $type->getBindingType();
return $type->getBindingType() === ParameterType::STRING
? Types::STRING
: Type::lookupName($type);
},
$columnNames
);
Expand Down
83 changes: 83 additions & 0 deletions Tests/Functional/DatabaseAccessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace TYPO3\TestingFramework\Tests\Functional;

use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Types;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\TestingFramework\Core\Functional\Framework\DataHandling\Snapshot\DatabaseAccessor;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;

final class DatabaseAccessorTest extends FunctionalTestCase

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 is the first functional test in this repository, and nothing here runs it: Build/phpunit/ only has the unit test config, runTests.sh has no functional suite and ci.yml only runs -s unit. Only cgl and phpstan see this file. Fine for now as a manual test (see the review summary), I'll create a follow-up issue for a CI job against a Core checkout.

{
#[Test]
public function snapshotPreservesDatabaseValues(): void
{
$connection = $this->get(ConnectionPool::class)->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME);
$schemaManager = $connection->createSchemaManager();
$table = new Table('tx_testingframework_snapshot');
$table->addColumn('uid', Types::INTEGER, ['autoincrement' => true]);
$table->addColumn('payload', Types::JSON, ['notnull' => false]);
$table->addColumn('created_at', Types::DATETIME_MUTABLE);
$table->addColumn('enabled', Types::BOOLEAN);
$table->setPrimaryKey(['uid']);
$schemaManager->createTable($table);

try {
$jsonValues = [
'{"name":"Alice","nested":{"items":[1,true,null]}}',
'[{"name":"Alice"},{"name":"Bob"}]',
'"quoted \\"text\\""',
'42',
'1.5',
'true',
'false',
'null',
'{}',
'[]',
null,
];
foreach ($jsonValues as $index => $json) {
$connection->insert($table->getName(), [
'uid' => $index + 1,
'payload' => $json,
'created_at' => '2026-11-14 12:34:56',
'enabled' => $index % 2 === 0,
], [
'uid' => ParameterType::INTEGER,
'payload' => $json === null ? ParameterType::NULL : ParameterType::STRING,
'created_at' => ParameterType::STRING,
'enabled' => ParameterType::BOOLEAN,
]);
}
$expected = $connection->select(['*'], $table->getName(), [], [], ['uid' => 'ASC'])->fetchAllAssociative();
self::assertCount(count($jsonValues), $expected);
$subject = new DatabaseAccessor($connection);
$snapshot = json_decode(json_encode($subject->export(), JSON_THROW_ON_ERROR), true, 512, JSON_THROW_ON_ERROR);
$connection->executeStatement('DELETE FROM ' . $connection->quoteIdentifier($table->getName()));

$subject->import($snapshot);

self::assertSame($expected, $connection->select(['*'], $table->getName(), [], [], ['uid' => 'ASC'])->fetchAllAssociative());
} finally {
$schemaManager->dropTable($table->getName());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace TYPO3\TestingFramework\Tests\Unit\Core\Functional\Framework\DataHandling\Snapshot;

use Doctrine\DBAL\Result;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Types;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Database\Query\Restriction\QueryRestrictionContainerInterface;
use TYPO3\TestingFramework\Core\Functional\Framework\DataHandling\Snapshot\DatabaseAccessor;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

final class DatabaseAccessorTest extends UnitTestCase
{
public static function databaseValuesDataProvider(): iterable
{
yield 'encoded JSON' => [Types::JSON, '{"value":[1,true,null]}', Types::STRING];
yield 'database datetime' => [Types::DATETIME_MUTABLE, '2026-11-14 12:34:56', Types::STRING];
yield 'plain text' => [Types::TEXT, 'unchanged', Types::STRING];
yield 'integer keeps its type' => [Types::INTEGER, '42', Types::INTEGER];
}

#[Test]
#[DataProvider('databaseValuesDataProvider')]
public function exportPreservesValuesAndNormalizesStringBoundTypes(string $type, string $value, string $expectedType): void
{
$table = new Table('tx_testingframework_snapshot');
$table->addColumn('payload', $type);
$schemaManager = self::createStub(AbstractSchemaManager::class);
$schemaManager->method('listTables')->willReturn([$table]);
$result = self::createStub(Result::class);
$result->method('fetchAllAssociative')->willReturn([['payload' => $value]]);
$queryBuilder = self::createStub(QueryBuilder::class);
$queryBuilder->method('getRestrictions')->willReturn(self::createStub(QueryRestrictionContainerInterface::class));
$queryBuilder->method('select')->willReturnSelf();
$queryBuilder->method('from')->willReturnSelf();
$queryBuilder->method('executeQuery')->willReturn($result);
$connection = self::createStub(Connection::class);
$connection->method('createSchemaManager')->willReturn($schemaManager);
$connection->method('createQueryBuilder')->willReturn($queryBuilder);

$export = (new DatabaseAccessor($connection))->export();
self::assertSame(['payload' => $expectedType], $export[0]['columns']);
self::assertSame([[$value]], $export[0]['items']);
}
}