-
Notifications
You must be signed in to change notification settings - Fork 1
IBX-12603: Resolved Ibexa DB platform when generating test database schema #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,7 @@ | |
| }, | ||
| "require-dev": { | ||
| "phpunit/phpunit": "^9", | ||
| "ibexa/code-style": "^2.0", | ||
| "ibexa/code-style": "~2.3.0", | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
208 changes: 208 additions & 0 deletions
208
tests/contracts/Bootstrapper/DatabaseSchemaHookTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
createStubis not static (PHPUnit 9).There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, not sure why it was enabled here. It belongs to the
@PhpCsFixer:riskyrule set, not enabled by default.I can drop it completely.
POV ping @Steveb-p