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

Add ColumnSchemaTest #272

Merged
merged 1 commit into from
Jul 24, 2023
Merged
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
63 changes: 63 additions & 0 deletions tests/ColumnSchemaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Mssql\Tests;

use PHPUnit\Framework\TestCase;
use Yiisoft\Db\Mssql\Tests\Support\TestTrait;
use Yiisoft\Db\Query\Query;

use function str_repeat;

/**
* @group mssql
*/
final class ColumnSchemaTest extends TestCase
{
use TestTrait;

public function testPhpTypeCast(): void
{
$db = $this->getConnection(true);

$command = $db->createCommand();
$schema = $db->getSchema();
$tableSchema = $schema->getTableSchema('type');

$command->insert(
'type',
[
'int_col' => 1,
'char_col' => str_repeat('x', 100),
'char_col3' => null,
'float_col' => 1.234,
'blob_col' => "\x10\x11\x12",
'datetime_col' => '2023-07-11 14:50:00.123',
'bool_col' => false,
]
);
$command->execute();
$query = (new Query($db))->from('type')->one();

$this->assertNotNull($tableSchema);

$intColPhpType = $tableSchema->getColumn('int_col')?->phpTypecast($query['int_col']);
$charColPhpType = $tableSchema->getColumn('char_col')?->phpTypecast($query['char_col']);
$charCol3PhpType = $tableSchema->getColumn('char_col3')?->phpTypecast($query['char_col3']);
$floatColPhpType = $tableSchema->getColumn('float_col')?->phpTypecast($query['float_col']);
$blobColPhpType = $tableSchema->getColumn('blob_col')?->phpTypecast($query['blob_col']);
$datetimeColPhpType = $tableSchema->getColumn('datetime_col')?->phpTypecast($query['datetime_col']);
$boolColPhpType = $tableSchema->getColumn('bool_col')?->phpTypecast($query['bool_col']);

$this->assertSame(1, $intColPhpType);
$this->assertSame(str_repeat('x', 100), $charColPhpType);
$this->assertNull($charCol3PhpType);
$this->assertSame(1.234, $floatColPhpType);
$this->assertSame("\x10\x11\x12", $blobColPhpType);
$this->assertSame('2023-07-11 14:50:00.123', $datetimeColPhpType);
$this->assertEquals(false, $boolColPhpType);

$db->close();
}
}
2 changes: 1 addition & 1 deletion tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static function insertWithReturningPks(): array
['{{%type}}.[[related_id]]' => null, '[[time]]' => new Expression('now()')],
[],
<<<SQL
SET NOCOUNT ON;DECLARE @temporary_inserted TABLE ([int_col] int , [int_col2] int NULL, [tinyint_col] tinyint NULL, [smallint_col] smallint NULL, [char_col] char(100) , [char_col2] varchar(100) NULL, [char_col3] text NULL, [float_col] decimal(4,3) , [float_col2] float NULL, [blob_col] varbinary(MAX) NULL, [numeric_col] decimal(5,2) NULL, [time] datetime , [bool_col] tinyint , [bool_col2] tinyint NULL);INSERT INTO {{%type}} ([related_id], [time]) OUTPUT INSERTED.[int_col],INSERTED.[int_col2],INSERTED.[tinyint_col],INSERTED.[smallint_col],INSERTED.[char_col],INSERTED.[char_col2],INSERTED.[char_col3],INSERTED.[float_col],INSERTED.[float_col2],INSERTED.[blob_col],INSERTED.[numeric_col],INSERTED.[time],INSERTED.[bool_col],INSERTED.[bool_col2] INTO @temporary_inserted VALUES (:qp0, now());SELECT * FROM @temporary_inserted;
SET NOCOUNT ON;DECLARE @temporary_inserted TABLE ([int_col] int , [int_col2] int NULL, [tinyint_col] tinyint NULL, [smallint_col] smallint NULL, [char_col] char(100) , [char_col2] varchar(100) NULL, [char_col3] text NULL, [float_col] decimal(4,3) , [float_col2] float NULL, [blob_col] varbinary(MAX) NULL, [numeric_col] decimal(5,2) NULL, [datetime_col] datetime , [bool_col] tinyint , [bool_col2] tinyint NULL);INSERT INTO {{%type}} ([related_id], [time]) OUTPUT INSERTED.[int_col],INSERTED.[int_col2],INSERTED.[tinyint_col],INSERTED.[smallint_col],INSERTED.[char_col],INSERTED.[char_col2],INSERTED.[char_col3],INSERTED.[float_col],INSERTED.[float_col2],INSERTED.[blob_col],INSERTED.[numeric_col],INSERTED.[datetime_col],INSERTED.[bool_col],INSERTED.[bool_col2] INTO @temporary_inserted VALUES (:qp0, now());SELECT * FROM @temporary_inserted;
SQL,
[':qp0' => null],
],
Expand Down
2 changes: 1 addition & 1 deletion tests/Provider/SchemaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public static function columns(): array
'scale' => 2,
'defaultValue' => 33.22,
],
'time' => [
'datetime_col' => [
'type' => 'datetime',
'dbType' => 'datetime',
'phpType' => 'string',
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/Fixture/mssql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ CREATE TABLE [dbo].[type] (
[float_col2] [float] DEFAULT '1.23',
[blob_col] [varbinary](MAX),
[numeric_col] [decimal](5,2) DEFAULT '33.22',
[time] [datetime] NOT NULL DEFAULT '2002-01-01 00:00:00',
[datetime_col] [datetime] NOT NULL DEFAULT '2002-01-01 00:00:00',
[bool_col] [tinyint] NOT NULL,
[bool_col2] [tinyint] DEFAULT '1'
);
Expand Down
Loading