From 09cda2854561c8f48a7d827bc8a574c633137604 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Thu, 20 Jul 2023 18:49:18 +0700 Subject: [PATCH] Add ColumnSchemaTest --- tests/ColumnSchemaTest.php | 63 +++++++++++++++++++++++++++++++ tests/Provider/SchemaProvider.php | 2 +- tests/Support/Fixture/sqlite.sql | 2 +- 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 tests/ColumnSchemaTest.php diff --git a/tests/ColumnSchemaTest.php b/tests/ColumnSchemaTest.php new file mode 100644 index 00000000..6bd676bf --- /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", + 'timestamp_col' => '2023-07-11 14:50:23', + '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']); + $timestampColPhpType = $tableSchema->getColumn('timestamp_col')?->phpTypecast($query['timestamp_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:23', $timestampColPhpType); + $this->assertFalse($boolColPhpType); + + $db->close(); + } +} diff --git a/tests/Provider/SchemaProvider.php b/tests/Provider/SchemaProvider.php index 17b32149..4c517e4a 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' => [ + 'timestamp_col' => [ 'type' => 'timestamp', 'dbType' => 'timestamp', 'phpType' => 'string', diff --git a/tests/Support/Fixture/sqlite.sql b/tests/Support/Fixture/sqlite.sql index 45de6756..74b3b0d9 100644 --- a/tests/Support/Fixture/sqlite.sql +++ b/tests/Support/Fixture/sqlite.sql @@ -128,7 +128,7 @@ CREATE TABLE "type" ( float_col2 double DEFAULT '1.23', blob_col blob, numeric_col decimal(5,2) DEFAULT '33.22', - time timestamp NOT NULL DEFAULT '2002-01-01 00:00:00', + timestamp_col timestamp NOT NULL DEFAULT '2002-01-01 00:00:00', bool_col tinyint(1) NOT NULL, bool_col2 tinyint(1) DEFAULT '1', ts_default TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP