From 767864dcf9b9aef99ad279c2f0c03308e2369175 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Thu, 26 Oct 2023 21:57:53 +0700 Subject: [PATCH] Fix tests according to main PR --- tests/CommandTest.php | 20 ++++++++----- tests/Provider/CommandProvider.php | 10 +++++++ tests/QueryBuilderTest.php | 48 ++++++++---------------------- 3 files changed, 35 insertions(+), 43 deletions(-) diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 2c1896c4e..261afc4ae 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -17,6 +17,7 @@ use Yiisoft\Db\Mssql\Driver; use Yiisoft\Db\Mssql\Tests\Support\TestTrait; use Yiisoft\Db\Query\Query; +use Yiisoft\Db\Schema\Builder\ColumnInterface; use Yiisoft\Db\Tests\Common\CommonCommandTest; use Yiisoft\Db\Tests\Support\DbHelper; @@ -33,22 +34,25 @@ final class CommandTest extends CommonCommandTest protected string $upsertTestCharCast = 'CAST([[address]] AS VARCHAR(255))'; - /** - * @throws Exception - * @throws InvalidConfigException - * @throws Throwable - */ - public function testAlterColumn(): void + /** @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\CommandProvider::columnTypes */ + public function testAlterColumn(ColumnInterface|string $type): void { $db = $this->getConnection(true); $command = $db->createCommand(); - $command->alterColumn('{{customer}}', 'email', 'ntext')->execute(); + $command->alterColumn('{{customer}}', 'email', $type)->execute(); $schema = $db->getSchema(); $columns = $schema->getTableSchema('{{customer}}')?->getColumns(); + $dbType = $db->getQueryBuilder()->getColumnType($type); + $dbType = explode(' ', $dbType, 2)[0]; + $this->assertArrayHasKey('email', $columns); - $this->assertSame('ntext', $columns['email']->getDbType()); + $this->assertSame($dbType, $columns['email']->getDbType()); + + if ($type instanceof ColumnInterface) { + $this->assertSame($type->isNotNull(), !$columns['email']->isAllowNull()); + } } /** diff --git a/tests/Provider/CommandProvider.php b/tests/Provider/CommandProvider.php index 5f429c403..1168d9bdc 100644 --- a/tests/Provider/CommandProvider.php +++ b/tests/Provider/CommandProvider.php @@ -7,7 +7,9 @@ use JsonException; use PDO; use Yiisoft\Db\Command\Param; +use Yiisoft\Db\Mssql\Column; use Yiisoft\Db\Mssql\Tests\Support\TestTrait; +use Yiisoft\Db\Schema\SchemaInterface; use function json_encode; use function serialize; @@ -38,4 +40,12 @@ public static function dataInsertVarbinary(): array ], ]; } + + public static function columnTypes(): array + { + return [ + ['ntext'], + [(new Column(SchemaInterface::TYPE_STRING, 255))->notNull()], + ]; + } } diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 1591f9c42..5813c834a 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -17,6 +17,7 @@ use Yiisoft\Db\Mssql\Tests\Support\TestTrait; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Query\QueryInterface; +use Yiisoft\Db\Schema\Builder\ColumnInterface; use Yiisoft\Db\Schema\SchemaInterface; use Yiisoft\Db\Tests\Common\CommonQueryBuilderTest; @@ -592,42 +593,16 @@ public function testUpsertVarbinary(): void $this->assertSame($testData, $resultData['blob_col']); } - /** - * @throws Exception - * @throws InvalidConfigException - */ - public function testAlterColumn(): void + /** @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::columnTypes */ + public function testAlterColumn(ColumnInterface|string $type): void { $db = $this->getConnection(true); $qb = $db->getQueryBuilder(); - $expected = "ALTER TABLE [foo1] ALTER COLUMN [bar] varchar(255) -DECLARE @tableName VARCHAR(MAX) = '[foo1]' -DECLARE @columnName VARCHAR(MAX) = 'bar' -WHILE 1=1 BEGIN - DECLARE @constraintName NVARCHAR(128) - SET @constraintName = (SELECT TOP 1 OBJECT_NAME(cons.[object_id]) - FROM ( - SELECT sc.[constid] object_id - FROM [sys].[sysconstraints] sc - JOIN [sys].[columns] c ON c.[object_id]=sc.[id] AND c.[column_id]=sc.[colid] AND c.[name]=@columnName - WHERE sc.[id] = OBJECT_ID(@tableName) - UNION - SELECT object_id(i.[name]) FROM [sys].[indexes] i - JOIN [sys].[columns] c ON c.[object_id]=i.[object_id] AND c.[name]=@columnName - JOIN [sys].[index_columns] ic ON ic.[object_id]=i.[object_id] AND i.[index_id]=ic.[index_id] AND c.[column_id]=ic.[column_id] - WHERE i.[is_unique_constraint]=1 and i.[object_id]=OBJECT_ID(@tableName) - ) cons - JOIN [sys].[objects] so ON so.[object_id]=cons.[object_id] - WHERE so.[type]='D') - IF @constraintName IS NULL BREAK - EXEC (N'ALTER TABLE ' + @tableName + ' DROP CONSTRAINT [' + @constraintName + ']') -END"; - $sql = $qb->alterColumn('foo1', 'bar', 'varchar(255)'); - $this->assertEquals($expected, $sql); + $type = $qb->getColumnType($type); - $expected = "ALTER TABLE [foo1] ALTER COLUMN [bar] nvarchar(255) NOT NULL + $expected = "ALTER TABLE [foo1] ALTER COLUMN [bar] {$type} DECLARE @tableName VARCHAR(MAX) = '[foo1]' DECLARE @columnName VARCHAR(MAX) = 'bar' WHILE 1=1 BEGIN @@ -649,12 +624,15 @@ public function testAlterColumn(): void IF @constraintName IS NULL BREAK EXEC (N'ALTER TABLE ' + @tableName + ' DROP CONSTRAINT [' + @constraintName + ']') END"; - $sql = $qb->alterColumn( - 'foo1', - 'bar', - (new Column(SchemaInterface::TYPE_STRING, 255))->notNull() - ); + $sql = $qb->alterColumn('foo1', 'bar', $type); $this->assertEquals($expected, $sql); + } + + public function testAlterColumnWithConstraints(): void + { + $db = $this->getConnection(true); + + $qb = $db->getQueryBuilder(); $expected = "ALTER TABLE [foo1] ALTER COLUMN [bar] nvarchar(255) DECLARE @tableName VARCHAR(MAX) = '[foo1]'