1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Tests\Schema;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Schema\Exception\InvalidState;
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
use Doctrine\DBAL\Schema\UniqueConstraint;
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
class UniqueConstraintTest extends TestCase
{
use VerifyDeprecations;
/** @throws Exception */
public function testGetNonNullObjectName(): void
{
$name = UnqualifiedName::unquoted('uq_user_id');
$uniqueConstraint = UniqueConstraint::editor()
->setName($name)
->setColumnNames(
UnqualifiedName::unquoted('user_id'),
)
->create();
self::assertEquals($name, $uniqueConstraint->getObjectName());
}
/** @throws Exception */
public function testGetNullObjectName(): void
{
$uniqueConstraint = UniqueConstraint::editor()
->setUnquotedColumnNames('user_id')
->create();
self::assertNull($uniqueConstraint->getObjectName());
}
public function testInstantiateWithOptions(): void
{
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/6685');
new UniqueConstraint('', ['user_id'], [], ['option' => 'value']);
}
public function testGetColumnNames(): void
{
$uniqueConstraint = new UniqueConstraint('', ['user_id']);
self::assertEquals([
UnqualifiedName::unquoted('user_id'),
], $uniqueConstraint->getColumnNames());
}
public function testInvalidColumnNames(): void
{
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/6685');
$uniqueConstraint = new UniqueConstraint('', ['']);
$this->expectException(InvalidState::class);
$uniqueConstraint->getColumnNames();
}
public function testEmptyColumnNames(): void
{
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/6685');
/** @phpstan-ignore argument.type */
$uniqueConstraint = new UniqueConstraint('', []);
$this->expectException(InvalidState::class);
$uniqueConstraint->getColumnNames();
}
/** @param array<string> $flags */
#[DataProvider('clusteredFlagsProvider')]
public function testIsClustered(array $flags, bool $expected): void
{
$uniqueConstraint = new UniqueConstraint('', ['user_id'], $flags);
self::assertSame($expected, $uniqueConstraint->isClustered());
}
/** @return iterable<array{array<string>, bool}> $flags */
public static function clusteredFlagsProvider(): iterable
{
yield 'clustered' => [['clustered'], true];
yield 'not clustered' => [[], false];
}
}
|