4
4
5
5
namespace Yiisoft \Db \Pgsql \Tests ;
6
6
7
+ use DateTimeImmutable ;
8
+ use DateTimeZone ;
9
+ use PHPUnit \Framework \Attributes \DataProviderExternal ;
7
10
use Throwable ;
8
11
use Yiisoft \Db \Constant \ColumnType ;
9
12
use Yiisoft \Db \Exception \Exception ;
20
23
use Yiisoft \Db \Pgsql \Column \IntegerColumn ;
21
24
use Yiisoft \Db \Pgsql \Column \StructuredColumn ;
22
25
use Yiisoft \Db \Pgsql \Connection ;
26
+ use Yiisoft \Db \Pgsql \Tests \Provider \ColumnProvider ;
23
27
use Yiisoft \Db \Pgsql \Tests \Support \TestTrait ;
24
28
use Yiisoft \Db \Query \Query ;
25
29
use Yiisoft \Db \Schema \Column \ColumnInterface ;
26
30
use Yiisoft \Db \Schema \Column \DoubleColumn ;
27
31
use Yiisoft \Db \Schema \Column \JsonColumn ;
28
32
use Yiisoft \Db \Schema \Column \StringColumn ;
29
- use Yiisoft \Db \Tests \AbstractColumnTest ;
33
+ use Yiisoft \Db \Tests \Common \CommonColumnTest ;
34
+ use Yiisoft \Db \Tests \Support \Assert ;
30
35
36
+ use function str_repeat ;
31
37
use function stream_get_contents ;
32
38
33
39
/**
34
40
* @group pgsql
35
41
*
36
42
* @psalm-suppress PropertyNotSetInConstructor
37
43
*/
38
- final class ColumnTest extends AbstractColumnTest
44
+ final class ColumnTest extends CommonColumnTest
39
45
{
40
46
use TestTrait;
41
47
48
+ protected const COLUMN_BUILDER = ColumnBuilder::class;
49
+
42
50
private function insertTypeValues (Connection $ db ): void
43
51
{
44
52
$ db ->createCommand ()->insert (
@@ -49,6 +57,8 @@ private function insertTypeValues(Connection $db): void
49
57
'char_col3 ' => null ,
50
58
'float_col ' => 1.234 ,
51
59
'blob_col ' => "\x10\x11\x12" ,
60
+ 'timestamp_col ' => '2023-07-11 14:50:23 ' ,
61
+ 'timestamp_default ' => new DateTimeImmutable ('2023-07-11 14:50:23 ' ),
52
62
'bool_col ' => false ,
53
63
'bit_col ' => 0b0110_0100 , // 100
54
64
'varbit_col ' => 0b1_1100_1000 , // 456
@@ -64,12 +74,14 @@ private function insertTypeValues(Connection $db): void
64
74
)->execute ();
65
75
}
66
76
67
- private function assertResultValues (array $ result ): void
77
+ private function assertTypecastedValues (array $ result ): void
68
78
{
69
79
$ this ->assertSame (1 , $ result ['int_col ' ]);
70
80
$ this ->assertSame (str_repeat ('x ' , 100 ), $ result ['char_col ' ]);
71
81
$ this ->assertSame (1.234 , $ result ['float_col ' ]);
72
82
$ this ->assertSame ("\x10\x11\x12" , stream_get_contents ($ result ['blob_col ' ]));
83
+ $ this ->assertEquals (new DateTimeImmutable ('2023-07-11 14:50:23 ' , new DateTimeZone ('UTC ' )), $ result ['timestamp_col ' ]);
84
+ $ this ->assertEquals (new DateTimeImmutable ('2023-07-11 14:50:23 ' ), $ result ['timestamp_default ' ]);
73
85
$ this ->assertFalse ($ result ['bool_col ' ]);
74
86
$ this ->assertSame (0b0110_0100 , $ result ['bit_col ' ]);
75
87
$ this ->assertSame (0b1_1100_1000 , $ result ['varbit_col ' ]);
@@ -93,11 +105,11 @@ public function testQueryWithTypecasting(): void
93
105
94
106
$ result = $ query ->one ();
95
107
96
- $ this ->assertResultValues ($ result );
108
+ $ this ->assertTypecastedValues ($ result );
97
109
98
110
$ result = $ query ->all ();
99
111
100
- $ this ->assertResultValues ($ result [0 ]);
112
+ $ this ->assertTypecastedValues ($ result [0 ]);
101
113
102
114
$ db ->close ();
103
115
}
@@ -112,11 +124,11 @@ public function testCommandWithPhpTypecasting(): void
112
124
113
125
$ result = $ command ->queryOne ();
114
126
115
- $ this ->assertResultValues ($ result );
127
+ $ this ->assertTypecastedValues ($ result );
116
128
117
129
$ result = $ command ->queryAll ();
118
130
119
- $ this ->assertResultValues ($ result [0 ]);
131
+ $ this ->assertTypecastedValues ($ result [0 ]);
120
132
121
133
$ db ->close ();
122
134
}
@@ -190,43 +202,19 @@ public function testPhpTypeCast(): void
190
202
{
191
203
$ db = $ this ->getConnection (true );
192
204
$ schema = $ db ->getSchema ();
193
- $ tableSchema = $ schema ->getTableSchema ('type ' );
205
+ $ columns = $ schema ->getTableSchema ('type ' )-> getColumns ( );
194
206
195
207
$ this ->insertTypeValues ($ db );
196
208
197
209
$ query = (new Query ($ db ))->from ('type ' )->one ();
198
210
199
- $ intColPhpTypeCast = $ tableSchema ->getColumn ('int_col ' )?->phpTypecast($ query ['int_col ' ]);
200
- $ charColPhpTypeCast = $ tableSchema ->getColumn ('char_col ' )?->phpTypecast($ query ['char_col ' ]);
201
- $ floatColPhpTypeCast = $ tableSchema ->getColumn ('float_col ' )?->phpTypecast($ query ['float_col ' ]);
202
- $ blobColPhpTypeCast = $ tableSchema ->getColumn ('blob_col ' )?->phpTypecast($ query ['blob_col ' ]);
203
- $ boolColPhpTypeCast = $ tableSchema ->getColumn ('bool_col ' )?->phpTypecast($ query ['bool_col ' ]);
204
- $ bitColPhpTypeCast = $ tableSchema ->getColumn ('bit_col ' )?->phpTypecast($ query ['bit_col ' ]);
205
- $ varbitColPhpTypeCast = $ tableSchema ->getColumn ('varbit_col ' )?->phpTypecast($ query ['varbit_col ' ]);
206
- $ numericColPhpTypeCast = $ tableSchema ->getColumn ('numeric_col ' )?->phpTypecast($ query ['numeric_col ' ]);
207
- $ intArrayColPhpType = $ tableSchema ->getColumn ('intarray_col ' )?->phpTypecast($ query ['intarray_col ' ]);
208
- $ numericArrayColPhpTypeCast = $ tableSchema ->getColumn ('numericarray_col ' )?->phpTypecast($ query ['numericarray_col ' ]);
209
- $ varcharArrayColPhpTypeCast = $ tableSchema ->getColumn ('varchararray_col ' )?->phpTypecast($ query ['varchararray_col ' ]);
210
- $ textArray2ColPhpType = $ tableSchema ->getColumn ('textarray2_col ' )?->phpTypecast($ query ['textarray2_col ' ]);
211
- $ jsonColPhpType = $ tableSchema ->getColumn ('json_col ' )?->phpTypecast($ query ['json_col ' ]);
212
- $ jsonBColPhpType = $ tableSchema ->getColumn ('jsonb_col ' )?->phpTypecast($ query ['jsonb_col ' ]);
213
- $ jsonArrayColPhpType = $ tableSchema ->getColumn ('jsonarray_col ' )?->phpTypecast($ query ['jsonarray_col ' ]);
214
-
215
- $ this ->assertSame (1 , $ intColPhpTypeCast );
216
- $ this ->assertSame (str_repeat ('x ' , 100 ), $ charColPhpTypeCast );
217
- $ this ->assertSame (1.234 , $ floatColPhpTypeCast );
218
- $ this ->assertSame ("\x10\x11\x12" , stream_get_contents ($ blobColPhpTypeCast ));
219
- $ this ->assertFalse ($ boolColPhpTypeCast );
220
- $ this ->assertSame (0b0110_0100 , $ bitColPhpTypeCast );
221
- $ this ->assertSame (0b1_1100_1000 , $ varbitColPhpTypeCast );
222
- $ this ->assertSame (33.22 , $ numericColPhpTypeCast );
223
- $ this ->assertSame ([1 , -2 , null , 42 ], $ intArrayColPhpType );
224
- $ this ->assertSame ([null , 1.2 , -2.2 , null , null ], $ numericArrayColPhpTypeCast );
225
- $ this ->assertSame (['' , 'some text ' , '"" ' , '\\\\' , '[",","null",true,"false","f"] ' , null ], $ varcharArrayColPhpTypeCast );
226
- $ this ->assertNull ($ textArray2ColPhpType );
227
- $ this ->assertSame ([['a ' => 1 , 'b ' => null , 'c ' => [1 , 3 , 5 ]]], $ jsonColPhpType );
228
- $ this ->assertSame (['1 ' , '2 ' , '3 ' ], $ jsonBColPhpType );
229
- $ this ->assertSame ([[[', ' , 'null ' , true , 'false ' , 'f ' ]]], $ jsonArrayColPhpType );
211
+ $ result = [];
212
+
213
+ foreach ($ columns as $ columnName => $ column ) {
214
+ $ result [$ columnName ] = $ column ->phpTypecast ($ query [$ columnName ]);
215
+ }
216
+
217
+ $ this ->assertTypecastedValues ($ result );
230
218
231
219
$ db ->close ();
232
220
}
@@ -430,32 +418,48 @@ public function testColumnInstance()
430
418
$ db ->close ();
431
419
}
432
420
433
- /** @dataProvider \Yiisoft\Db\Pgsql\Tests\Provider\ ColumnProvider::predefinedTypes */
421
+ #[DataProviderExternal( ColumnProvider::class, ' predefinedTypes ' )]
434
422
public function testPredefinedType (string $ className , string $ type , string $ phpType )
435
423
{
436
424
parent ::testPredefinedType ($ className , $ type , $ phpType );
437
425
}
438
426
439
- /** @dataProvider \Yiisoft\Db\Pgsql\Tests\Provider\ ColumnProvider::dbTypecastColumns */
427
+ #[DataProviderExternal( ColumnProvider::class, ' dbTypecastColumns ' )]
440
428
public function testDbTypecastColumns (ColumnInterface $ column , array $ values )
441
429
{
442
430
parent ::testDbTypecastColumns ($ column , $ values );
443
431
}
444
432
445
- /** @dataProvider \Yiisoft\Db\Pgsql\Tests\Provider\ ColumnProvider::phpTypecastColumns */
433
+ #[DataProviderExternal( ColumnProvider::class, ' phpTypecastColumns ' )]
446
434
public function testPhpTypecastColumns (ColumnInterface $ column , array $ values )
447
435
{
448
436
parent ::testPhpTypecastColumns ($ column , $ values );
449
437
}
450
438
451
- /** @dataProvider \Yiisoft\Db\Pgsql\Tests\Provider\ColumnProvider::phpTypecastArrayColumns */
439
+ #[DataProviderExternal(ColumnProvider::class, 'dbTypecastArrayColumns ' )]
440
+ public function testArrayColumnDbTypecast (ColumnInterface $ column , array $ values ): void
441
+ {
442
+ $ arrayCol = (new ArrayColumn ())->column ($ column );
443
+
444
+ foreach ($ values as [$ dimension , $ expected , $ value ]) {
445
+ $ arrayCol ->dimension ($ dimension );
446
+ $ dbValue = $ arrayCol ->dbTypecast ($ value );
447
+
448
+ $ this ->assertInstanceOf (ArrayExpression::class, $ dbValue );
449
+ $ this ->assertSame ($ arrayCol , $ dbValue ->getType ());
450
+ $ this ->assertEquals ($ value , $ dbValue ->getValue ());
451
+ }
452
+ }
453
+
454
+ #[DataProviderExternal(ColumnProvider::class, 'phpTypecastArrayColumns ' )]
452
455
public function testPhpTypecastArrayColumn (ColumnInterface $ column , array $ values ): void
453
456
{
454
457
$ arrayCol = ColumnBuilder::array ($ column );
455
458
456
459
foreach ($ values as [$ dimension , $ expected , $ value ]) {
457
460
$ arrayCol ->dimension ($ dimension );
458
- $ this ->assertSame ($ expected , $ arrayCol ->phpTypecast ($ value ));
461
+
462
+ Assert::arraysEquals ($ expected , $ arrayCol ->phpTypecast ($ value ));
459
463
}
460
464
}
461
465
0 commit comments