4
4
5
5
namespace Yiisoft \Db \Sqlite \Tests ;
6
6
7
+ use DateTimeImmutable ;
8
+ use DateTimeZone ;
7
9
use PDO ;
8
10
use Yiisoft \Db \Command \Param ;
9
11
use Yiisoft \Db \Schema \Column \BinaryColumn ;
12
14
use Yiisoft \Db \Schema \Column \IntegerColumn ;
13
15
use Yiisoft \Db \Schema \Column \JsonColumn ;
14
16
use Yiisoft \Db \Schema \Column \StringColumn ;
17
+ use Yiisoft \Db \Sqlite \Column \ColumnBuilder ;
15
18
use Yiisoft \Db \Sqlite \Connection ;
16
19
use Yiisoft \Db \Sqlite \Tests \Support \TestTrait ;
17
20
use Yiisoft \Db \Query \Query ;
18
- use Yiisoft \Db \Tests \AbstractColumnTest ;
21
+ use Yiisoft \Db \Tests \Common \ CommonColumnTest ;
19
22
20
23
use function str_repeat ;
21
24
22
25
/**
23
26
* @group sqlite
24
27
*/
25
- final class ColumnTest extends AbstractColumnTest
28
+ final class ColumnTest extends CommonColumnTest
26
29
{
27
30
use TestTrait;
28
31
32
+ protected const COLUMN_BUILDER = ColumnBuilder::class;
33
+
29
34
private function insertTypeValues (Connection $ db ): void
30
35
{
31
36
$ command = $ db ->createCommand ();
@@ -39,6 +44,7 @@ private function insertTypeValues(Connection $db): void
39
44
'float_col ' => 1.234 ,
40
45
'blob_col ' => "\x10\x11\x12" ,
41
46
'timestamp_col ' => '2023-07-11 14:50:23 ' ,
47
+ 'timestamp_default ' => new DateTimeImmutable ('2023-07-11 14:50:23 ' ),
42
48
'bool_col ' => false ,
43
49
'bit_col ' => 0b0110_0110 , // 102
44
50
'json_col ' => [['a ' => 1 , 'b ' => null , 'c ' => [1 , 3 , 5 ]]],
@@ -48,18 +54,24 @@ private function insertTypeValues(Connection $db): void
48
54
$ command ->execute ();
49
55
}
50
56
51
- private function assertResultValues (array $ result ): void
57
+ private function assertTypecastedValues (array $ result, bool $ allTypecasted = false ): void
52
58
{
53
59
$ this ->assertSame (1 , $ result ['int_col ' ]);
54
60
$ this ->assertSame (str_repeat ('x ' , 100 ), $ result ['char_col ' ]);
55
61
$ this ->assertNull ($ result ['char_col3 ' ]);
56
62
$ this ->assertSame (1.234 , $ result ['float_col ' ]);
57
63
$ this ->assertSame ("\x10\x11\x12" , $ result ['blob_col ' ]);
58
- $ this ->assertSame ('2023-07-11 14:50:23 ' , $ result ['timestamp_col ' ]);
64
+ $ this ->assertEquals (new DateTimeImmutable ('2023-07-11 14:50:23 ' , new DateTimeZone ('UTC ' )), $ result ['timestamp_col ' ]);
65
+ $ this ->assertEquals (new DateTimeImmutable ('2023-07-11 14:50:23 ' ), $ result ['timestamp_default ' ]);
59
66
$ this ->assertFalse ($ result ['bool_col ' ]);
60
67
$ this ->assertSame (0b0110_0110 , $ result ['bit_col ' ]);
61
68
$ this ->assertSame ([['a ' => 1 , 'b ' => null , 'c ' => [1 , 3 , 5 ]]], $ result ['json_col ' ]);
62
- $ this ->assertSame ('[1,2,3,"string",null] ' , $ result ['json_text_col ' ]);
69
+
70
+ if ($ allTypecasted ) {
71
+ $ this ->assertSame ([1 , 2 , 3 , 'string ' , null ], $ result ['json_text_col ' ]);
72
+ } else {
73
+ $ this ->assertSame ('[1,2,3,"string",null] ' , $ result ['json_text_col ' ]);
74
+ }
63
75
}
64
76
65
77
public function testQueryWithTypecasting (): void
@@ -72,11 +84,11 @@ public function testQueryWithTypecasting(): void
72
84
73
85
$ result = $ query ->one ();
74
86
75
- $ this ->assertResultValues ($ result );
87
+ $ this ->assertTypecastedValues ($ result );
76
88
77
89
$ result = $ query ->all ();
78
90
79
- $ this ->assertResultValues ($ result [0 ]);
91
+ $ this ->assertTypecastedValues ($ result [0 ]);
80
92
81
93
$ db ->close ();
82
94
}
@@ -91,11 +103,11 @@ public function testCommandWithPhpTypecasting(): void
91
103
92
104
$ result = $ command ->queryOne ();
93
105
94
- $ this ->assertResultValues ($ result );
106
+ $ this ->assertTypecastedValues ($ result );
95
107
96
108
$ result = $ command ->queryAll ();
97
109
98
- $ this ->assertResultValues ($ result [0 ]);
110
+ $ this ->assertTypecastedValues ($ result [0 ]);
99
111
100
112
$ db ->close ();
101
113
}
@@ -130,33 +142,19 @@ public function testPhpTypeCast(): void
130
142
{
131
143
$ db = $ this ->getConnection (true );
132
144
$ schema = $ db ->getSchema ();
133
- $ tableSchema = $ schema ->getTableSchema ('type ' );
145
+ $ columns = $ schema ->getTableSchema ('type ' )-> getColumns ( );
134
146
135
147
$ this ->insertTypeValues ($ db );
136
148
137
149
$ query = (new Query ($ db ))->from ('type ' )->one ();
138
150
139
- $ intColPhpType = $ tableSchema ->getColumn ('int_col ' )?->phpTypecast($ query ['int_col ' ]);
140
- $ charColPhpType = $ tableSchema ->getColumn ('char_col ' )?->phpTypecast($ query ['char_col ' ]);
141
- $ charCol3PhpType = $ tableSchema ->getColumn ('char_col3 ' )?->phpTypecast($ query ['char_col3 ' ]);
142
- $ floatColPhpType = $ tableSchema ->getColumn ('float_col ' )?->phpTypecast($ query ['float_col ' ]);
143
- $ blobColPhpType = $ tableSchema ->getColumn ('blob_col ' )?->phpTypecast($ query ['blob_col ' ]);
144
- $ timestampColPhpType = $ tableSchema ->getColumn ('timestamp_col ' )?->phpTypecast($ query ['timestamp_col ' ]);
145
- $ boolColPhpType = $ tableSchema ->getColumn ('bool_col ' )?->phpTypecast($ query ['bool_col ' ]);
146
- $ bitColPhpType = $ tableSchema ->getColumn ('bit_col ' )?->phpTypecast($ query ['bit_col ' ]);
147
- $ jsonColPhpType = $ tableSchema ->getColumn ('json_col ' )?->phpTypecast($ query ['json_col ' ]);
148
- $ jsonTextColPhpType = $ tableSchema ->getColumn ('json_text_col ' )?->phpTypecast($ query ['json_text_col ' ]);
149
-
150
- $ this ->assertSame (1 , $ intColPhpType );
151
- $ this ->assertSame (str_repeat ('x ' , 100 ), $ charColPhpType );
152
- $ this ->assertNull ($ charCol3PhpType );
153
- $ this ->assertSame (1.234 , $ floatColPhpType );
154
- $ this ->assertSame ("\x10\x11\x12" , $ blobColPhpType );
155
- $ this ->assertSame ('2023-07-11 14:50:23 ' , $ timestampColPhpType );
156
- $ this ->assertFalse ($ boolColPhpType );
157
- $ this ->assertSame (0b0110_0110 , $ bitColPhpType );
158
- $ this ->assertSame ([['a ' => 1 , 'b ' => null , 'c ' => [1 , 3 , 5 ]]], $ jsonColPhpType );
159
- $ this ->assertSame ([1 , 2 , 3 , 'string ' , null ], $ jsonTextColPhpType );
151
+ $ result = [];
152
+
153
+ foreach ($ columns as $ columnName => $ column ) {
154
+ $ result [$ columnName ] = $ column ->phpTypecast ($ query [$ columnName ]);
155
+ }
156
+
157
+ $ this ->assertTypecastedValues ($ result , true );
160
158
161
159
$ db ->close ();
162
160
}
0 commit comments