From 27185eb13493e54a148b40054af4d524af4421f7 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Thu, 20 Jul 2023 20:07:25 +0700 Subject: [PATCH] Add ColumnSchemaTest --- tests/ColumnSchemaTest.php | 63 +++++++++++++++++++++++++ tests/Provider/QueryBuilderProvider.php | 2 +- tests/Provider/SchemaProvider.php | 2 +- tests/Support/Fixture/mssql.sql | 2 +- 4 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 tests/ColumnSchemaTest.php diff --git a/tests/ColumnSchemaTest.php b/tests/ColumnSchemaTest.php new file mode 100644 index 000000000..f7ef59a68 --- /dev/null +++ b/tests/ColumnSchemaTest.php @@ -0,0 +1,63 @@ +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(); + } +} diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index 458682cda..aea94267a 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -128,7 +128,7 @@ public static function insertWithReturningPks(): array ['{{%type}}.[[related_id]]' => null, '[[time]]' => new Expression('now()')], [], << null], ], diff --git a/tests/Provider/SchemaProvider.php b/tests/Provider/SchemaProvider.php index 554405646..039aa5c17 100644 --- a/tests/Provider/SchemaProvider.php +++ b/tests/Provider/SchemaProvider.php @@ -157,7 +157,7 @@ public static function columns(): array 'scale' => 2, 'defaultValue' => 33.22, ], - 'time' => [ + 'datetime_col' => [ 'type' => 'datetime', 'dbType' => 'datetime', 'phpType' => 'string', diff --git a/tests/Support/Fixture/mssql.sql b/tests/Support/Fixture/mssql.sql index 652fd3664..b50283531 100644 --- a/tests/Support/Fixture/mssql.sql +++ b/tests/Support/Fixture/mssql.sql @@ -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' );