Skip to content

Commit

Permalink
Rely on custom schema options when available
Browse files Browse the repository at this point in the history
They have been removed in doctrine/dbal 4
  • Loading branch information
greg0ire committed Jul 7, 2022
1 parent ed9d444 commit a28bc84
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
8 changes: 6 additions & 2 deletions lib/Doctrine/ORM/Tools/SchemaTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\AbstractAsset;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
Expand Down Expand Up @@ -36,6 +37,7 @@
use function is_array;
use function is_numeric;
use function method_exists;
use function property_exists;
use function strtolower;

/**
Expand Down Expand Up @@ -786,8 +788,10 @@ private function gatherColumnOptions(array $mapping): array
return [];
}

$options = array_intersect_key($mappingOptions, array_flip(self::KNOWN_COLUMN_OPTIONS));
$options['customSchemaOptions'] = array_diff_key($mappingOptions, $options);
$options = array_intersect_key($mappingOptions, array_flip(self::KNOWN_COLUMN_OPTIONS));
if (property_exists(Column::class, '_customSchemaOptions')) {
$options['customSchemaOptions'] = array_diff_key($mappingOptions, $options);
}

return $options;
}
Expand Down
38 changes: 27 additions & 11 deletions tests/Doctrine/Tests/ORM/Tools/SchemaToolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

use function count;
use function current;
use function method_exists;

class SchemaToolTest extends OrmTestCase
{
Expand Down Expand Up @@ -77,7 +78,12 @@ public function testAnnotationOptionsAttribute(): void
);
$table = $schema->getTable('TestEntityWithAnnotationOptionsAttribute');

foreach ([$table->getOptions(), $table->getColumn('test')->getCustomSchemaOptions()] as $options) {
$optionsArrays = [$table->getOptions()];
if (method_exists(Column::class, 'getCustomSchemaOptions')) {
$optionsArrays[] = $table->getColumn('test')->getCustomSchemaOptions();
}

foreach ($optionsArrays as $options) {
self::assertArrayHasKey('foo', $options);
self::assertSame('bar', $options['foo']);
self::assertArrayHasKey('baz', $options);
Expand Down Expand Up @@ -135,16 +141,18 @@ public function testPassColumnOptionsToJoinColumn(): void
'Foreign key/join column should have the same value of option `fixed` as the referenced column'
);

self::assertEquals(
$tableCategory->getColumn('id')->getCustomSchemaOptions(),
$tableBoard->getColumn('category_id')->getCustomSchemaOptions(),
'Foreign key/join column should have the same custom options as the referenced column'
);

self::assertEquals(
['collation' => 'latin1_bin', 'foo' => 'bar'],
$tableBoard->getColumn('category_id')->getCustomSchemaOptions()
);
if (method_exists(Column::class, 'getCustomSchemaOptions')) {
self::assertEquals(
$tableCategory->getColumn('id')->getCustomSchemaOptions(),
$tableBoard->getColumn('category_id')->getCustomSchemaOptions(),
'Foreign key/join column should have the same custom options as the referenced column'
);

self::assertEquals(
['collation' => 'latin1_bin', 'foo' => 'bar'],
$tableBoard->getColumn('category_id')->getCustomSchemaOptions()
);
}
}

/**
Expand Down Expand Up @@ -179,6 +187,10 @@ public function testPostGenerateEvents(): void

public function testNullDefaultNotAddedToCustomSchemaOptions(): void
{
if (! method_exists(Column::class, 'getCustomSchemaOptions')) {
$this->markTestSkipped('This test requires doctrine/dbal 3.x');
}

$em = $this->getTestEntityManager();
$schemaTool = new SchemaTool($em);

Expand All @@ -195,6 +207,10 @@ public function testNullDefaultNotAddedToCustomSchemaOptions(): void
*/
public function testEnumTypeAddedToCustomSchemaOptions(): void
{
if (! method_exists(Column::class, 'getCustomSchemaOptions')) {
$this->markTestSkipped('This test requires doctrine/dbal 3.x');
}

$em = $this->getTestEntityManager();
$schemaTool = new SchemaTool($em);

Expand Down

0 comments on commit a28bc84

Please sign in to comment.