Skip to content

Commit

Permalink
Merge pull request doctrine#3519 from doctrine/fix-unique-constraint-…
Browse files Browse the repository at this point in the history
…with-empty-name

Fix UniqueConstraint with empty name.
  • Loading branch information
jwage authored and morozov committed May 31, 2019
2 parents 7d9d4fc + ce48efe commit addae80
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Schema/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ protected function _addUniqueConstraint(UniqueConstraint $constraint)
$name = strlen($constraint->getName())
? $constraint->getName()
: $this->_generateIdentifierName(
array_merge((array) $this->getName(), $constraint->getLocalColumns()),
array_merge((array) $this->getName(), $constraint->getColumns()),
'fk',
$this->_getMaxIdentifierLength()
);
Expand Down
3 changes: 0 additions & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ parameters:
# weird class name, doesn't exist in stubs either
- '~unknown class OCI-(Lob|Collection)~'

# https://github.com/doctrine/dbal/issues/3236
- '~^Call to an undefined method Doctrine\\DBAL\\Schema\\UniqueConstraint::getLocalColumns\(\)~'

# https://github.com/doctrine/dbal/issues/3237
- '~^Call to an undefined method Doctrine\\DBAL\\Driver\\PDOStatement::nextRowset\(\)~'

Expand Down
38 changes: 36 additions & 2 deletions tests/Doctrine/Tests/DBAL/Schema/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\SchemaException;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\UniqueConstraint;
use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DbalTestCase;
use function array_shift;
use function array_keys;
use function current;

class TableTest extends DbalTestCase
Expand Down Expand Up @@ -206,7 +207,11 @@ public function testConstraints() : void
$constraints = $tableA->getForeignKeys();

self::assertCount(1, $constraints);
self::assertSame($constraint, array_shift($constraints));

$constraintNames = array_keys($constraints);

self::assertSame('fk_8c736521', $constraintNames[0]);
self::assertSame($constraint, $constraints['fk_8c736521']);
}

public function testOptions() : void
Expand Down Expand Up @@ -887,4 +892,33 @@ public static function getNormalizesAssetNames() : iterable
['"FOO"'],
];
}

public function testUniqueConstraintWithEmptyName() : void
{
$columns = [
new Column('column1', Type::getType(Type::STRING)),
new Column('column2', Type::getType(Type::STRING)),
new Column('column3', Type::getType(Type::STRING)),
new Column('column4', Type::getType(Type::STRING)),
];

$uniqueConstraints = [
new UniqueConstraint('', ['column1', 'column2']),
new UniqueConstraint('', ['column3', 'column4']),
];

$table = new Table('test', $columns, [], $uniqueConstraints);

$constraints = $table->getUniqueConstraints();

self::assertCount(2, $constraints);

$constraintNames = array_keys($constraints);

self::assertSame('fk_d87f7e0c341ce00bad15b1b1', $constraintNames[0]);
self::assertSame('fk_d87f7e0cda12812744761484', $constraintNames[1]);

self::assertSame($uniqueConstraints[0], $constraints['fk_d87f7e0c341ce00bad15b1b1']);
self::assertSame($uniqueConstraints[1], $constraints['fk_d87f7e0cda12812744761484']);
}
}

0 comments on commit addae80

Please sign in to comment.