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