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
2 changes: 1 addition & 1 deletion .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
'single_item_single_line' => true,
'inline_constructor_arguments' => false,
],
'php_unit_test_case_static_method_calls' => ['call_type' => 'self'],
'php_unit_test_case_static_method_calls' => ['call_type' => 'self', 'methods' => ['createStub' => 'this']],

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Note

Well... That fixer is of... questionable quality. It hard-codes list of method it thinks should be static rather than inferring from the installed source (maybe can't?). PHPUnit methods' signatures change across different PHPUnit version. Right now createStub is not static (PHPUnit 9).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe that should be delegated to phpstan? tbh, fixer saying that something is static or not seems kinda too much.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

maybe that should be delegated to phpstan? tbh, fixer saying that something is static or not seems kinda too much.

Yeah, not sure why it was enabled here. It belongs to the @PhpCsFixer:risky rule set, not enabled by default.
I can drop it completely.

POV ping @Steveb-p

])
->buildConfig()
->setFinder(
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"require-dev": {
"phpunit/phpunit": "^9",
"ibexa/code-style": "^2.0",
"ibexa/code-style": "~2.3.0",

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Bumped while at it, causes no CS drifts.

"ibexa/core": "~6.0.x-dev",
"ibexa/doctrine-schema": "~6.0.x-dev",
"ibexa/rector": "~6.0.x-dev",
Expand Down
28 changes: 26 additions & 2 deletions src/contracts/Bootstrapper/DatabaseSchemaHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
namespace Ibexa\Contracts\Test\Core\Bootstrapper;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Ibexa\Bundle\Test\Core\DependencyInjection\CompilerPass\RemoveUnsatisfiableHooksPass;
use Ibexa\Contracts\DoctrineSchema\Builder\SchemaBuilderInterface;
use Ibexa\Contracts\DoctrineSchema\DbPlatformFactoryInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
Expand All @@ -36,12 +38,16 @@ final class DatabaseSchemaHook implements HookInterface

private Connection $connection;

private DbPlatformFactoryInterface $dbPlatformFactory;

public function __construct(
SchemaBuilderInterface $schemaBuilder,
Connection $connection
Connection $connection,
DbPlatformFactoryInterface $dbPlatformFactory
) {
$this->schemaBuilder = $schemaBuilder;
$this->connection = $connection;
$this->dbPlatformFactory = $dbPlatformFactory;
}

public function configureOptions(OptionsResolver $resolver): void
Expand All @@ -58,10 +64,28 @@ public function __invoke(array $options): void
}

$schema = $this->schemaBuilder->buildSchema();
$platform = $this->connection->getDatabasePlatform();
$platform = $this->getIbexaDatabasePlatform();

foreach ($schema->toSql($platform) as $sql) {
$this->connection->executeStatement($sql);
}
}

/**
* Ibexa ships its own platform subclasses which adjust the generated DDL. The SQLite one is
* load-bearing here: vanilla SQLitePlatform drops the table-level PRIMARY KEY clause as soon as
* a table has an autoincrement column, so composite primary keys such as
* ibexa_content_field(id, version) silently degrade to a single-column one.
*
* DBAL 4 removed doctrine-bundle's `platform_service`, so the connection no longer carries those
* subclasses and they have to be resolved explicitly, the same way ibexa/core's CoreInstaller
* and LegacySchemaImporter already do.
*/
private function getIbexaDatabasePlatform(): AbstractPlatform
{
$driverName = $this->connection->getParams()['driver'] ?? '';

return $this->dbPlatformFactory->createDatabasePlatformFromDriverName($driverName)
?? $this->connection->getDatabasePlatform();
}
}
208 changes: 208 additions & 0 deletions tests/contracts/Bootstrapper/DatabaseSchemaHookTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Contracts\Test\Core\Bootstrapper;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Exception\TypesException;
use Ibexa\Contracts\DoctrineSchema\Builder\SchemaBuilderInterface;
use Ibexa\Contracts\DoctrineSchema\DbPlatformFactoryInterface;
use Ibexa\Contracts\Test\Core\Bootstrapper\DatabaseSchemaHook;
use Ibexa\DoctrineSchema\Database\DbPlatform\SqliteDbPlatform;
use PHPUnit\Framework\TestCase;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @covers \Ibexa\Contracts\Test\Core\Bootstrapper\DatabaseSchemaHook
*/
final class DatabaseSchemaHookTest extends TestCase
{
/** @var list<string> */
private array $executedStatements = [];

protected function setUp(): void
{
$this->executedStatements = [];
}

/**
* Vanilla SQLitePlatform drops the table-level PRIMARY KEY clause once a table has an
* autoincrement column, which silently turns a composite key into a single-column one. The
* schema has to be generated with Ibexa's platform, not with whatever the connection carries.
*
* @throws \Exception
* @throws TypesException
*/
public function testGeneratesSchemaWithIbexaDatabasePlatform(): void
{
$hook = $this->createHook(new SqliteDbPlatform());

$hook($this->resolve($hook, []));

self::assertStringContainsString('PRIMARY KEY (id, version)', $this->getExecutedDdl());
}

/**
* @throws \Exception
* @throws TypesException
*/
public function testFallsBackToConnectionPlatformWhenFactoryProvidesNone(): void
{
$hook = $this->createHook(null);

$hook($this->resolve($hook, []));

// the connection's own vanilla platform is used, PK degradation and all
$ddl = $this->getExecutedDdl();
self::assertStringContainsString('CREATE TABLE ibexa_content_field', $ddl);
self::assertStringContainsString('id INTEGER PRIMARY KEY AUTOINCREMENT', $ddl);
self::assertStringNotContainsString('PRIMARY KEY (id, version)', $ddl);
}

/**
* A connection configured with driverClass rather than driver has no 'driver' key at all. That
* must degrade to the connection's own platform, not blow up on a missing array key.
*
* @throws \Exception
* @throws TypesException
*/
public function testDegradesToConnectionPlatformWhenDriverParamIsAbsent(): void
{
$hook = $this->createHook(new SqliteDbPlatform(), []);

$hook($this->resolve($hook, []));

$ddl = $this->getExecutedDdl();
self::assertStringContainsString('id INTEGER PRIMARY KEY AUTOINCREMENT', $ddl);
self::assertStringNotContainsString('PRIMARY KEY (id, version)', $ddl);
}

/**
* @throws TypesException
*/
public function testLoadSchemaOptionDefaultsToTrue(): void
{
$hook = $this->createHook(new SqliteDbPlatform());

$options = $this->resolve($hook, []);

self::assertTrue($options[DatabaseSchemaHook::OPTION_LOAD_SCHEMA]);
}

/**
* @throws \Exception
*/
public function testDoesNotBuildSchemaWhenDisabled(): void
{
$schemaBuilder = $this->createMock(SchemaBuilderInterface::class);
$schemaBuilder->expects(self::never())->method('buildSchema');

$hook = new DatabaseSchemaHook(
$schemaBuilder,
$this->createConnection(),
$this->createMock(DbPlatformFactoryInterface::class)
);

$hook($this->resolve($hook, [DatabaseSchemaHook::OPTION_LOAD_SCHEMA => false]));

self::assertSame([], $this->executedStatements);
}

private function getExecutedDdl(): string
{
self::assertNotEmpty($this->executedStatements, 'the hook executed no statements at all');

return implode("\n", $this->executedStatements);
}

/**
* @param array<string, mixed> $connectionParams
*
* @throws TypesException
*/
private function createHook(
?AbstractPlatform $ibexaPlatform,
array $connectionParams = ['driver' => 'pdo_sqlite']
): DatabaseSchemaHook {
$schemaBuilder = $this->createStub(SchemaBuilderInterface::class);
$schemaBuilder->method('buildSchema')->willReturn($this->createSchema());

// keyed like the real factory, so a driver it does not know about resolves to null
$dbPlatformFactory = $this->createStub(DbPlatformFactoryInterface::class);
$dbPlatformFactory->method('createDatabasePlatformFromDriverName')->willReturnCallback(
static fn (string $driverName): ?AbstractPlatform => $driverName === 'pdo_sqlite'
? $ibexaPlatform
: null
);

return new DatabaseSchemaHook(
$schemaBuilder,
$this->createConnection($connectionParams),
$dbPlatformFactory
);
}

/**
* A connection reporting the vanilla SQLite platform, recording every statement it is asked
* to run.
*
* @param array<string, mixed> $params
*/
private function createConnection(array $params = ['driver' => 'pdo_sqlite']): Connection
{
$connection = $this->createStub(Connection::class);
$connection->method('getParams')->willReturn($params);
$connection->method('getDatabasePlatform')->willReturn(new SQLitePlatform());
$connection->method('executeStatement')->willReturnCallback(
function (string $sql): int {
$this->executedStatements[] = $sql;

return 0;
}
);

return $connection;
}

/**
* Mirrors ibexa_content_field: a composite primary key over an autoincrement column.
*
* @throws TypesException
*/
private function createSchema(): Schema
{
$schema = new Schema();
$table = $schema->createTable('ibexa_content_field');
$table->addColumn('id', 'integer', ['autoincrement' => true]);
$table->addColumn('version', 'integer', ['default' => 0]);
$table->addPrimaryKeyConstraint(
PrimaryKeyConstraint::editor()->setUnquotedColumnNames('id', 'version')->create()
);

return $schema;
}

/**
* @param array<string, mixed> $options
*
* @return array<string, mixed>
*/
private function resolve(
DatabaseSchemaHook $hook,
array $options
): array {
$resolver = new OptionsResolver();
$hook->configureOptions($resolver);

return $resolver->resolve($options);
}
}