Skip to content

Commit af9a6ce

Browse files
authored
Add ColumnSchemaTest (#261)
1 parent b35f40e commit af9a6ce

3 files changed

Lines changed: 65 additions & 2 deletions

File tree

tests/ColumnSchemaTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Db\Sqlite\Tests;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Yiisoft\Db\Sqlite\Tests\Support\TestTrait;
9+
use Yiisoft\Db\Query\Query;
10+
11+
use function str_repeat;
12+
13+
/**
14+
* @group sqlite
15+
*/
16+
final class ColumnSchemaTest extends TestCase
17+
{
18+
use TestTrait;
19+
20+
public function testPhpTypeCast(): void
21+
{
22+
$db = $this->getConnection(true);
23+
24+
$command = $db->createCommand();
25+
$schema = $db->getSchema();
26+
$tableSchema = $schema->getTableSchema('type');
27+
28+
$command->insert(
29+
'type',
30+
[
31+
'int_col' => 1,
32+
'char_col' => str_repeat('x', 100),
33+
'char_col3' => null,
34+
'float_col' => 1.234,
35+
'blob_col' => "\x10\x11\x12",
36+
'timestamp_col' => '2023-07-11 14:50:23',
37+
'bool_col' => false,
38+
]
39+
);
40+
$command->execute();
41+
$query = (new Query($db))->from('type')->one();
42+
43+
$this->assertNotNull($tableSchema);
44+
45+
$intColPhpType = $tableSchema->getColumn('int_col')?->phpTypecast($query['int_col']);
46+
$charColPhpType = $tableSchema->getColumn('char_col')?->phpTypecast($query['char_col']);
47+
$charCol3PhpType = $tableSchema->getColumn('char_col3')?->phpTypecast($query['char_col3']);
48+
$floatColPhpType = $tableSchema->getColumn('float_col')?->phpTypecast($query['float_col']);
49+
$blobColPhpType = $tableSchema->getColumn('blob_col')?->phpTypecast($query['blob_col']);
50+
$timestampColPhpType = $tableSchema->getColumn('timestamp_col')?->phpTypecast($query['timestamp_col']);
51+
$boolColPhpType = $tableSchema->getColumn('bool_col')?->phpTypecast($query['bool_col']);
52+
53+
$this->assertSame(1, $intColPhpType);
54+
$this->assertSame(str_repeat('x', 100), $charColPhpType);
55+
$this->assertNull($charCol3PhpType);
56+
$this->assertSame(1.234, $floatColPhpType);
57+
$this->assertSame("\x10\x11\x12", $blobColPhpType);
58+
$this->assertSame('2023-07-11 14:50:23', $timestampColPhpType);
59+
$this->assertFalse($boolColPhpType);
60+
61+
$db->close();
62+
}
63+
}

tests/Provider/SchemaProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public static function columns(): array
157157
'scale' => 2,
158158
'defaultValue' => 33.22,
159159
],
160-
'time' => [
160+
'timestamp_col' => [
161161
'type' => 'timestamp',
162162
'dbType' => 'timestamp',
163163
'phpType' => 'string',

tests/Support/Fixture/sqlite.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ CREATE TABLE "type" (
128128
float_col2 double DEFAULT '1.23',
129129
blob_col blob,
130130
numeric_col decimal(5,2) DEFAULT '33.22',
131-
time timestamp NOT NULL DEFAULT '2002-01-01 00:00:00',
131+
timestamp_col timestamp NOT NULL DEFAULT '2002-01-01 00:00:00',
132132
bool_col tinyint(1) NOT NULL,
133133
bool_col2 tinyint(1) DEFAULT '1',
134134
ts_default TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP

0 commit comments

Comments
 (0)