Skip to content

Commit b0cc669

Browse files
guilhermeblancomorozov
authored andcommitted
Fixed tests
1 parent fe067f2 commit b0cc669

File tree

7 files changed

+57
-55
lines changed

7 files changed

+57
-55
lines changed

lib/Doctrine/DBAL/Platforms/AbstractPlatform.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2387,15 +2387,17 @@ public function getUniqueConstraintDeclarationSQL($name, UniqueConstraint $const
23872387
throw new InvalidArgumentException("Incomplete definition. 'columns' required.");
23882388
}
23892389

2390-
$flags = '';
2390+
$flags = ['UNIQUE'];
23912391

23922392
if ($constraint->hasFlag('clustered')) {
2393-
$flags = 'CLUSTERED ';
2393+
$flags[] = 'CLUSTERED';
23942394
}
23952395

2396-
return 'CONSTRAINT ' . $name->getQuotedName($this) . ' UNIQUE ' . $flags . ' ('
2397-
. $this->getIndexFieldDeclarationListSQL($index)
2398-
. ')';
2396+
$constraintName = $name->getQuotedName($this);
2397+
$constraintName = ! empty($constraintName) ? $constraintName . ' ' : '';
2398+
$columnListNames = $this->getIndexFieldDeclarationListSQL($columns);
2399+
2400+
return sprintf('CONSTRAINT %s%s (%s)', $constraintName, implode(' ', $flags), $columnListNames);
23992401
}
24002402

24012403
/**

lib/Doctrine/DBAL/Platforms/SqlitePlatform.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ public function getForeignKeyDeclarationSQL(ForeignKeyConstraint $foreignKey)
316316
*/
317317
protected function _getCreateTableSQL($tableName, array $columns, array $options = [])
318318
{
319-
$tableName = str_replace('.', '__', $tableName);
319+
$tableName = str_replace('.', '__', $tableName);
320320
$queryFields = $this->getColumnDeclarationListSQL($columns);
321321

322322
if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
@@ -391,8 +391,7 @@ protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
391391
{
392392
return $fixed
393393
? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)')
394-
: ($length ? 'VARCHAR(' . $length . ')' : 'TEXT')
395-
;
394+
: ($length ? 'VARCHAR(' . $length . ')' : 'TEXT');
396395
}
397396

398397
/**

lib/Doctrine/DBAL/Schema/Table.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ public function setPrimaryKey(array $columnNames, $indexName = false)
123123
/**
124124
* @param mixed[] $columnNames
125125
* @param string|null $indexName
126-
* @param array $flags
126+
* @param string[] $flags
127127
* @param mixed[] $options
128128
*
129129
* @return self
130130
*/
131-
public function addUniqueConstraint(array $columnNames, $indexName = null, array $flags = array(), array $options = [])
131+
public function addUniqueConstraint(array $columnNames, $indexName = null, array $flags = [], array $options = [])
132132
{
133133
if ($indexName === null) {
134134
$indexName = $this->_generateIdentifierName(
@@ -981,13 +981,13 @@ public function getComment() : ?string
981981
* @param mixed[] $columns
982982
* @param string $indexName
983983
* @param mixed[] $flags
984-
* @param array $options
984+
* @param mixed[] $options
985985
*
986986
* @return UniqueConstraint
987987
*
988988
* @throws SchemaException
989989
*/
990-
private function _createUniqueConstraint(array $columns, $indexName, array $flags = array(), array $options = [])
990+
private function _createUniqueConstraint(array $columns, $indexName, array $flags = [], array $options = [])
991991
{
992992
if (preg_match('(([^a-zA-Z0-9_]+))', $this->normalizeIdentifier($indexName))) {
993993
throw SchemaException::indexNameInvalid($indexName);

lib/Doctrine/DBAL/Schema/UniqueConstraint.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ class UniqueConstraint extends AbstractAsset implements Constraint
2626
* Platform specific flags
2727
* array($flagName => true)
2828
*
29-
* @var array
29+
* @var true[]
3030
*/
31-
protected $flags = array();
31+
protected $flags = [];
3232

3333
/**
3434
* Platform specific options
@@ -40,10 +40,10 @@ class UniqueConstraint extends AbstractAsset implements Constraint
4040
/**
4141
* @param string $indexName
4242
* @param string[] $columns
43-
* @param array $flags
43+
* @param string[] $flags
4444
* @param mixed[] $options
4545
*/
46-
public function __construct($indexName, array $columns, array $flags = array(), array $options = [])
46+
public function __construct($indexName, array $columns, array $flags = [], array $options = [])
4747
{
4848
$this->_setName($indexName);
4949

@@ -58,28 +58,12 @@ public function __construct($indexName, array $columns, array $flags = array(),
5858
}
5959
}
6060

61-
/**
62-
* @param string $column
63-
*
64-
* @return void
65-
*
66-
* @throws InvalidArgumentException
67-
*/
68-
protected function _addColumn($column)
69-
{
70-
if (! is_string($column)) {
71-
throw new InvalidArgumentException('Expecting a string as Index Column');
72-
}
73-
74-
$this->_columns[$column] = new Identifier($column);
75-
}
76-
7761
/**
7862
* {@inheritdoc}
7963
*/
8064
public function getColumns()
8165
{
82-
return array_keys($this->_columns);
66+
return array_keys($this->columns);
8367
}
8468

8569
/**
@@ -89,7 +73,7 @@ public function getQuotedColumns(AbstractPlatform $platform)
8973
{
9074
$columns = [];
9175

92-
foreach ($this->_columns as $column) {
76+
foreach ($this->columns as $column) {
9377
$columns[] = $column->getQuotedName($platform);
9478
}
9579

@@ -117,11 +101,11 @@ public function getFlags()
117101
/**
118102
* Adds flag for a unique constraint that translates to platform specific handling.
119103
*
120-
* @example $uniqueConstraint->addFlag('CLUSTERED')
121-
*
122104
* @param string $flag
123105
*
124106
* @return self
107+
*
108+
* @example $uniqueConstraint->addFlag('CLUSTERED')
125109
*/
126110
public function addFlag($flag)
127111
{
@@ -135,7 +119,7 @@ public function addFlag($flag)
135119
*
136120
* @param string $flag
137121
*
138-
* @return boolean
122+
* @return bool
139123
*/
140124
public function hasFlag($flag)
141125
{
@@ -181,4 +165,20 @@ public function getOptions()
181165
{
182166
return $this->options;
183167
}
168+
169+
/**
170+
* @param string $column
171+
*
172+
* @return void
173+
*
174+
* @throws InvalidArgumentException
175+
*/
176+
protected function _addColumn($column)
177+
{
178+
if (! is_string($column)) {
179+
throw new InvalidArgumentException('Expecting a string as Index Column');
180+
}
181+
182+
$this->columns[$column] = new Identifier($column);
183+
}
184184
}

tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\DBAL\Schema\Index;
1515
use Doctrine\DBAL\Schema\Table;
1616
use Doctrine\DBAL\Schema\TableDiff;
17+
use Doctrine\DBAL\Schema\UniqueConstraint;
1718
use Doctrine\DBAL\Types\Type;
1819
use Doctrine\Tests\DbalTestCase;
1920
use Doctrine\Tests\Types\CommentedType;
@@ -218,9 +219,9 @@ abstract public function getGenerateUniqueIndexSql() : string;
218219

219220
public function testGeneratesPartialIndexesSqlOnlyWhenSupportingPartialIndexes() : void
220221
{
221-
$where = 'test IS NULL AND test2 IS NOT NULL';
222-
$indexDef = new Index('name', ['test', 'test2'], false, false, [], ['where' => $where]);
223-
$uniqueIndex = new Index('name', ['test', 'test2'], true, false, [], ['where' => $where]);
222+
$where = 'test IS NULL AND test2 IS NOT NULL';
223+
$indexDef = new Index('name', ['test', 'test2'], false, false, [], ['where' => $where]);
224+
$uniqueConstraint = new UniqueConstraint('name', ['test', 'test2'], [], []);
224225

225226
$expected = ' WHERE ' . $where;
226227

@@ -230,15 +231,14 @@ public function testGeneratesPartialIndexesSqlOnlyWhenSupportingPartialIndexes()
230231
$actuals[] = $this->platform->getIndexDeclarationSQL('name', $indexDef);
231232
}
232233

233-
$actuals[] = $this->platform->getUniqueConstraintDeclarationSQL('name', $uniqueIndex);
234-
$actuals[] = $this->platform->getCreateIndexSQL($indexDef, 'table');
234+
$uniqueConstraintSQL = $this->platform->getUniqueConstraintDeclarationSQL('name', $uniqueConstraint);
235+
$indexSQL = $this->platform->getCreateIndexSQL($indexDef, 'table');
235236

236-
foreach ($actuals as $actual) {
237-
if ($this->platform->supportsPartialIndexes()) {
238-
self::assertStringEndsWith($expected, $actual, 'WHERE clause should be present');
239-
} else {
240-
self::assertStringEndsNotWith($expected, $actual, 'WHERE clause should NOT be present');
241-
}
237+
$this->assertStringEndsNotWith($expected, $uniqueConstraintSQL, 'WHERE clause should NOT be present');
238+
if ($this->platform->supportsPartialIndexes()) {
239+
self::assertStringEndsWith($expected, $indexSQL, 'WHERE clause should be present');
240+
} else {
241+
self::assertStringEndsNotWith($expected, $indexSQL, 'WHERE clause should NOT be present');
242242
}
243243
}
244244

@@ -731,11 +731,11 @@ public function testQuotedColumnInForeignKeyPropagation() : void
731731
*/
732732
public function testQuotesReservedKeywordInUniqueConstraintDeclarationSQL() : void
733733
{
734-
$index = new Index('select', ['foo'], true);
734+
$constraint = new UniqueConstraint('select', ['foo'], [], []);
735735

736736
self::assertSame(
737737
$this->getQuotesReservedKeywordInUniqueConstraintDeclarationSQL(),
738-
$this->platform->getUniqueConstraintDeclarationSQL('select', $index)
738+
$this->platform->getUniqueConstraintDeclarationSQL('select', $constraint)
739739
);
740740
}
741741

tests/Doctrine/Tests/DBAL/Platforms/AbstractSQLServerPlatformTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1416,7 +1416,7 @@ public static function getReturnsForeignKeyReferentialActionSQL() : iterable
14161416
*/
14171417
protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL() : string
14181418
{
1419-
return 'CONSTRAINT [select] UNIQUE (foo) WHERE foo IS NOT NULL';
1419+
return 'CONSTRAINT [select] UNIQUE (foo)';
14201420
}
14211421

14221422
/**

tests/Doctrine/Tests/DBAL/Platforms/SQLAnywherePlatformTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Doctrine\DBAL\Schema\Index;
1616
use Doctrine\DBAL\Schema\Table;
1717
use Doctrine\DBAL\Schema\TableDiff;
18+
use Doctrine\DBAL\Schema\UniqueConstraint;
1819
use Doctrine\DBAL\TransactionIsolationLevel;
1920
use Doctrine\DBAL\Types\Type;
2021
use InvalidArgumentException;
@@ -391,20 +392,20 @@ public function testGeneratesUniqueConstraintDeclarationSQL() : void
391392
'CONSTRAINT unique_constraint UNIQUE CLUSTERED (a, b)',
392393
$this->platform->getUniqueConstraintDeclarationSQL(
393394
'unique_constraint',
394-
new Index(null, ['a', 'b'], true, false, ['clustered'])
395+
new UniqueConstraint(null, ['a', 'b'], ['clustered'])
395396
)
396397
);
397398
self::assertEquals(
398-
'UNIQUE (a, b)',
399-
$this->platform->getUniqueConstraintDeclarationSQL(null, new Index(null, ['a', 'b'], true, false))
399+
'CONSTRAINT UNIQUE (a, b)',
400+
$this->platform->getUniqueConstraintDeclarationSQL(null, new UniqueConstraint(null, ['a', 'b']))
400401
);
401402
}
402403

403404
public function testCannotGenerateUniqueConstraintDeclarationSQLWithEmptyColumns() : void
404405
{
405406
$this->expectException(InvalidArgumentException::class);
406407

407-
$this->platform->getUniqueConstraintDeclarationSQL('constr', new Index('constr', [], true));
408+
$this->platform->getUniqueConstraintDeclarationSQL('constr', new UniqueConstraint('constr', []));
408409
}
409410

410411
public function testGeneratesForeignKeyConstraintsWithAdvancedPlatformOptionsSQL() : void

0 commit comments

Comments
 (0)