Unverified Commit b0cc6692 authored by Guilherme Blanco's avatar Guilherme Blanco Committed by Sergei Morozov

Fixed tests

parent fe067f20
......@@ -2387,15 +2387,17 @@ abstract class AbstractPlatform
throw new InvalidArgumentException("Incomplete definition. 'columns' required.");
}
$flags = '';
$flags = ['UNIQUE'];
if ($constraint->hasFlag('clustered')) {
$flags = 'CLUSTERED ';
$flags[] = 'CLUSTERED';
}
return 'CONSTRAINT ' . $name->getQuotedName($this) . ' UNIQUE ' . $flags . ' ('
. $this->getIndexFieldDeclarationListSQL($index)
. ')';
$constraintName = $name->getQuotedName($this);
$constraintName = ! empty($constraintName) ? $constraintName . ' ' : '';
$columnListNames = $this->getIndexFieldDeclarationListSQL($columns);
return sprintf('CONSTRAINT %s%s (%s)', $constraintName, implode(' ', $flags), $columnListNames);
}
/**
......
......@@ -316,7 +316,7 @@ class SqlitePlatform extends AbstractPlatform
*/
protected function _getCreateTableSQL($tableName, array $columns, array $options = [])
{
$tableName = str_replace('.', '__', $tableName);
$tableName = str_replace('.', '__', $tableName);
$queryFields = $this->getColumnDeclarationListSQL($columns);
if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
......@@ -391,8 +391,7 @@ class SqlitePlatform extends AbstractPlatform
{
return $fixed
? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)')
: ($length ? 'VARCHAR(' . $length . ')' : 'TEXT')
;
: ($length ? 'VARCHAR(' . $length . ')' : 'TEXT');
}
/**
......
......@@ -123,12 +123,12 @@ class Table extends AbstractAsset
/**
* @param mixed[] $columnNames
* @param string|null $indexName
* @param array $flags
* @param string[] $flags
* @param mixed[] $options
*
* @return self
*/
public function addUniqueConstraint(array $columnNames, $indexName = null, array $flags = array(), array $options = [])
public function addUniqueConstraint(array $columnNames, $indexName = null, array $flags = [], array $options = [])
{
if ($indexName === null) {
$indexName = $this->_generateIdentifierName(
......@@ -981,13 +981,13 @@ class Table extends AbstractAsset
* @param mixed[] $columns
* @param string $indexName
* @param mixed[] $flags
* @param array $options
* @param mixed[] $options
*
* @return UniqueConstraint
*
* @throws SchemaException
*/
private function _createUniqueConstraint(array $columns, $indexName, array $flags = array(), array $options = [])
private function _createUniqueConstraint(array $columns, $indexName, array $flags = [], array $options = [])
{
if (preg_match('(([^a-zA-Z0-9_]+))', $this->normalizeIdentifier($indexName))) {
throw SchemaException::indexNameInvalid($indexName);
......
......@@ -26,9 +26,9 @@ class UniqueConstraint extends AbstractAsset implements Constraint
* Platform specific flags
* array($flagName => true)
*
* @var array
* @var true[]
*/
protected $flags = array();
protected $flags = [];
/**
* Platform specific options
......@@ -40,10 +40,10 @@ class UniqueConstraint extends AbstractAsset implements Constraint
/**
* @param string $indexName
* @param string[] $columns
* @param array $flags
* @param string[] $flags
* @param mixed[] $options
*/
public function __construct($indexName, array $columns, array $flags = array(), array $options = [])
public function __construct($indexName, array $columns, array $flags = [], array $options = [])
{
$this->_setName($indexName);
......@@ -58,28 +58,12 @@ class UniqueConstraint extends AbstractAsset implements Constraint
}
}
/**
* @param string $column
*
* @return void
*
* @throws InvalidArgumentException
*/
protected function _addColumn($column)
{
if (! is_string($column)) {
throw new InvalidArgumentException('Expecting a string as Index Column');
}
$this->_columns[$column] = new Identifier($column);
}
/**
* {@inheritdoc}
*/
public function getColumns()
{
return array_keys($this->_columns);
return array_keys($this->columns);
}
/**
......@@ -89,7 +73,7 @@ class UniqueConstraint extends AbstractAsset implements Constraint
{
$columns = [];
foreach ($this->_columns as $column) {
foreach ($this->columns as $column) {
$columns[] = $column->getQuotedName($platform);
}
......@@ -117,11 +101,11 @@ class UniqueConstraint extends AbstractAsset implements Constraint
/**
* Adds flag for a unique constraint that translates to platform specific handling.
*
* @example $uniqueConstraint->addFlag('CLUSTERED')
*
* @param string $flag
*
* @return self
*
* @example $uniqueConstraint->addFlag('CLUSTERED')
*/
public function addFlag($flag)
{
......@@ -135,7 +119,7 @@ class UniqueConstraint extends AbstractAsset implements Constraint
*
* @param string $flag
*
* @return boolean
* @return bool
*/
public function hasFlag($flag)
{
......@@ -181,4 +165,20 @@ class UniqueConstraint extends AbstractAsset implements Constraint
{
return $this->options;
}
/**
* @param string $column
*
* @return void
*
* @throws InvalidArgumentException
*/
protected function _addColumn($column)
{
if (! is_string($column)) {
throw new InvalidArgumentException('Expecting a string as Index Column');
}
$this->columns[$column] = new Identifier($column);
}
}
......@@ -14,6 +14,7 @@ use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Schema\UniqueConstraint;
use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DbalTestCase;
use Doctrine\Tests\Types\CommentedType;
......@@ -218,9 +219,9 @@ abstract class AbstractPlatformTestCase extends DbalTestCase
public function testGeneratesPartialIndexesSqlOnlyWhenSupportingPartialIndexes() : void
{
$where = 'test IS NULL AND test2 IS NOT NULL';
$indexDef = new Index('name', ['test', 'test2'], false, false, [], ['where' => $where]);
$uniqueIndex = new Index('name', ['test', 'test2'], true, false, [], ['where' => $where]);
$where = 'test IS NULL AND test2 IS NOT NULL';
$indexDef = new Index('name', ['test', 'test2'], false, false, [], ['where' => $where]);
$uniqueConstraint = new UniqueConstraint('name', ['test', 'test2'], [], []);
$expected = ' WHERE ' . $where;
......@@ -230,15 +231,14 @@ abstract class AbstractPlatformTestCase extends DbalTestCase
$actuals[] = $this->platform->getIndexDeclarationSQL('name', $indexDef);
}
$actuals[] = $this->platform->getUniqueConstraintDeclarationSQL('name', $uniqueIndex);
$actuals[] = $this->platform->getCreateIndexSQL($indexDef, 'table');
$uniqueConstraintSQL = $this->platform->getUniqueConstraintDeclarationSQL('name', $uniqueConstraint);
$indexSQL = $this->platform->getCreateIndexSQL($indexDef, 'table');
foreach ($actuals as $actual) {
if ($this->platform->supportsPartialIndexes()) {
self::assertStringEndsWith($expected, $actual, 'WHERE clause should be present');
} else {
self::assertStringEndsNotWith($expected, $actual, 'WHERE clause should NOT be present');
}
$this->assertStringEndsNotWith($expected, $uniqueConstraintSQL, 'WHERE clause should NOT be present');
if ($this->platform->supportsPartialIndexes()) {
self::assertStringEndsWith($expected, $indexSQL, 'WHERE clause should be present');
} else {
self::assertStringEndsNotWith($expected, $indexSQL, 'WHERE clause should NOT be present');
}
}
......@@ -731,11 +731,11 @@ abstract class AbstractPlatformTestCase extends DbalTestCase
*/
public function testQuotesReservedKeywordInUniqueConstraintDeclarationSQL() : void
{
$index = new Index('select', ['foo'], true);
$constraint = new UniqueConstraint('select', ['foo'], [], []);
self::assertSame(
$this->getQuotesReservedKeywordInUniqueConstraintDeclarationSQL(),
$this->platform->getUniqueConstraintDeclarationSQL('select', $index)
$this->platform->getUniqueConstraintDeclarationSQL('select', $constraint)
);
}
......
......@@ -1416,7 +1416,7 @@ abstract class AbstractSQLServerPlatformTestCase extends AbstractPlatformTestCas
*/
protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL() : string
{
return 'CONSTRAINT [select] UNIQUE (foo) WHERE foo IS NOT NULL';
return 'CONSTRAINT [select] UNIQUE (foo)';
}
/**
......
......@@ -15,6 +15,7 @@ use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Schema\UniqueConstraint;
use Doctrine\DBAL\TransactionIsolationLevel;
use Doctrine\DBAL\Types\Type;
use InvalidArgumentException;
......@@ -391,12 +392,12 @@ class SQLAnywherePlatformTest extends AbstractPlatformTestCase
'CONSTRAINT unique_constraint UNIQUE CLUSTERED (a, b)',
$this->platform->getUniqueConstraintDeclarationSQL(
'unique_constraint',
new Index(null, ['a', 'b'], true, false, ['clustered'])
new UniqueConstraint(null, ['a', 'b'], ['clustered'])
)
);
self::assertEquals(
'UNIQUE (a, b)',
$this->platform->getUniqueConstraintDeclarationSQL(null, new Index(null, ['a', 'b'], true, false))
'CONSTRAINT UNIQUE (a, b)',
$this->platform->getUniqueConstraintDeclarationSQL(null, new UniqueConstraint(null, ['a', 'b']))
);
}
......@@ -404,7 +405,7 @@ class SQLAnywherePlatformTest extends AbstractPlatformTestCase
{
$this->expectException(InvalidArgumentException::class);
$this->platform->getUniqueConstraintDeclarationSQL('constr', new Index('constr', [], true));
$this->platform->getUniqueConstraintDeclarationSQL('constr', new UniqueConstraint('constr', []));
}
public function testGeneratesForeignKeyConstraintsWithAdvancedPlatformOptionsSQL() : void
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment