Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests according to main PR #281

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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());
}
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/Provider/CommandProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,4 +40,12 @@ public static function dataInsertVarbinary(): array
],
];
}

public static function columnTypes(): array
{
return [
['ntext'],
[(new Column(SchemaInterface::TYPE_STRING, 255))->notNull()],
];
}
}
48 changes: 13 additions & 35 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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]'
Expand Down
Loading