44
55namespace Yiisoft \Db \Mssql \Tests ;
66
7+ use DateTimeImmutable ;
8+ use DateTimeZone ;
79use PDO ;
10+ use PHPUnit \Framework \Attributes \DataProviderExternal ;
811use Yiisoft \Db \Command \Param ;
912use Yiisoft \Db \Expression \Expression ;
1013use Yiisoft \Db \Mssql \Column \BinaryColumn ;
14+ use Yiisoft \Db \Mssql \Column \ColumnBuilder ;
1115use Yiisoft \Db \Mssql \Connection ;
16+ use Yiisoft \Db \Mssql \Tests \Provider \ColumnProvider ;
1217use Yiisoft \Db \Mssql \Tests \Support \TestTrait ;
1318use Yiisoft \Db \Query \Query ;
1419use Yiisoft \Db \Schema \Column \BooleanColumn ;
1520use Yiisoft \Db \Schema \Column \ColumnInterface ;
1621use Yiisoft \Db \Schema \Column \DoubleColumn ;
1722use Yiisoft \Db \Schema \Column \IntegerColumn ;
1823use Yiisoft \Db \Schema \Column \StringColumn ;
19- use Yiisoft \Db \Tests \AbstractColumnTest ;
24+ use Yiisoft \Db \Tests \Common \ CommonColumnTest ;
2025
2126use function str_repeat ;
2227
2328/**
2429 * @group mssql
2530 */
26- final class ColumnTest extends AbstractColumnTest
31+ final class ColumnTest extends CommonColumnTest
2732{
2833 use TestTrait;
2934
35+ protected const COLUMN_BUILDER = ColumnBuilder::class;
36+
3037 private function insertTypeValues (Connection $ db ): void
3138 {
3239 $ db ->createCommand ()->insert (
@@ -44,16 +51,21 @@ private function insertTypeValues(Connection $db): void
4451 )->execute ();
4552 }
4653
47- private function assertResultValues (array $ result ): void
54+ private function assertTypecastedValues (array $ result, bool $ allTypecasted = false ): void
4855 {
4956 $ this ->assertSame (1 , $ result ['int_col ' ]);
5057 $ this ->assertSame (str_repeat ('x ' , 100 ), $ result ['char_col ' ]);
5158 $ this ->assertNull ($ result ['char_col3 ' ]);
5259 $ this ->assertSame (1.234 , $ result ['float_col ' ]);
5360 $ this ->assertSame ("\x10\x11\x12" , $ result ['blob_col ' ]);
54- $ this ->assertSame ( '2023-07-11 14:50:00.123 ' , $ result ['datetime_col ' ]);
61+ $ this ->assertEquals ( new DateTimeImmutable ( '2023-07-11 14:50:00.123 ' , new DateTimeZone ( ' UTC ' )) , $ result ['datetime_col ' ]);
5562 $ this ->assertFalse ($ result ['bool_col ' ]);
56- $ this ->assertSame ('[{"a":1,"b":null,"c":[1,3,5]}] ' , $ result ['json_col ' ]);
63+
64+ if ($ allTypecasted ) {
65+ $ this ->assertSame ([['a ' => 1 , 'b ' => null , 'c ' => [1 , 3 , 5 ]]], $ result ['json_col ' ]);
66+ } else {
67+ $ this ->assertSame ('[{"a":1,"b":null,"c":[1,3,5]}] ' , $ result ['json_col ' ]);
68+ }
5769 }
5870
5971 public function testQueryWithTypecasting (): void
@@ -66,11 +78,11 @@ public function testQueryWithTypecasting(): void
6678
6779 $ result = $ query ->one ();
6880
69- $ this ->assertResultValues ($ result );
81+ $ this ->assertTypecastedValues ($ result );
7082
7183 $ result = $ query ->all ();
7284
73- $ this ->assertResultValues ($ result [0 ]);
85+ $ this ->assertTypecastedValues ($ result [0 ]);
7486
7587 $ db ->close ();
7688 }
@@ -85,11 +97,11 @@ public function testCommandWithPhpTypecasting(): void
8597
8698 $ result = $ command ->queryOne ();
8799
88- $ this ->assertResultValues ($ result );
100+ $ this ->assertTypecastedValues ($ result );
89101
90102 $ result = $ command ->queryAll ();
91103
92- $ this ->assertResultValues ($ result [0 ]);
104+ $ this ->assertTypecastedValues ($ result [0 ]);
93105
94106 $ db ->close ();
95107 }
@@ -138,29 +150,19 @@ public function testPhpTypeCast(): void
138150 {
139151 $ db = $ this ->getConnection (true );
140152 $ schema = $ db ->getSchema ();
141- $ tableSchema = $ schema ->getTableSchema ('type ' );
153+ $ columns = $ schema ->getTableSchema ('type ' )-> getColumns ( );
142154
143155 $ this ->insertTypeValues ($ db );
144156
145157 $ query = (new Query ($ db ))->from ('type ' )->one ();
146158
147- $ intColPhpType = $ tableSchema ->getColumn ('int_col ' )?->phpTypecast($ query ['int_col ' ]);
148- $ charColPhpType = $ tableSchema ->getColumn ('char_col ' )?->phpTypecast($ query ['char_col ' ]);
149- $ charCol3PhpType = $ tableSchema ->getColumn ('char_col3 ' )?->phpTypecast($ query ['char_col3 ' ]);
150- $ floatColPhpType = $ tableSchema ->getColumn ('float_col ' )?->phpTypecast($ query ['float_col ' ]);
151- $ blobColPhpType = $ tableSchema ->getColumn ('blob_col ' )?->phpTypecast($ query ['blob_col ' ]);
152- $ datetimeColPhpType = $ tableSchema ->getColumn ('datetime_col ' )?->phpTypecast($ query ['datetime_col ' ]);
153- $ boolColPhpType = $ tableSchema ->getColumn ('bool_col ' )?->phpTypecast($ query ['bool_col ' ]);
154- $ jsonColPhpType = $ tableSchema ->getColumn ('json_col ' )?->phpTypecast($ query ['json_col ' ]);
155-
156- $ this ->assertSame (1 , $ intColPhpType );
157- $ this ->assertSame (str_repeat ('x ' , 100 ), $ charColPhpType );
158- $ this ->assertNull ($ charCol3PhpType );
159- $ this ->assertSame (1.234 , $ floatColPhpType );
160- $ this ->assertSame ("\x10\x11\x12" , $ blobColPhpType );
161- $ this ->assertSame ('2023-07-11 14:50:00.123 ' , $ datetimeColPhpType );
162- $ this ->assertFalse ($ boolColPhpType );
163- $ this ->assertSame ([['a ' => 1 , 'b ' => null , 'c ' => [1 , 3 , 5 ]]], $ jsonColPhpType );
159+ $ result = [];
160+
161+ foreach ($ columns as $ columnName => $ column ) {
162+ $ result [$ columnName ] = $ column ->phpTypecast ($ query [$ columnName ]);
163+ }
164+
165+ $ this ->assertTypecastedValues ($ result , true );
164166
165167 $ db ->close ();
166168 }
@@ -178,18 +180,24 @@ public function testColumnInstance()
178180 $ this ->assertInstanceOf (BooleanColumn::class, $ tableSchema ->getColumn ('bool_col ' ));
179181 }
180182
181- /** @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\ ColumnProvider::predefinedTypes */
183+ #[DataProviderExternal( ColumnProvider::class, ' predefinedTypes ' )]
182184 public function testPredefinedType (string $ className , string $ type , string $ phpType )
183185 {
184186 parent ::testPredefinedType ($ className , $ type , $ phpType );
185187 }
186188
187- /** @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\ ColumnProvider::dbTypecastColumns */
189+ #[DataProviderExternal( ColumnProvider::class, ' dbTypecastColumns ' )]
188190 public function testDbTypecastColumns (ColumnInterface $ column , array $ values )
189191 {
190192 parent ::testDbTypecastColumns ($ column , $ values );
191193 }
192194
195+ #[DataProviderExternal(ColumnProvider::class, 'phpTypecastColumns ' )]
196+ public function testPhpTypecastColumns (ColumnInterface $ column , array $ values )
197+ {
198+ parent ::testPhpTypecastColumns ($ column , $ values );
199+ }
200+
193201 public function testBinaryColumn ()
194202 {
195203 $ binaryCol = new BinaryColumn ();
0 commit comments