4
4
5
5
namespace Yiisoft\Db\Mssql\Tests;
6
6
7
+ use DateTimeImmutable;
8
+ use DateTimeZone;
7
9
use PDO;
10
+ use PHPUnit\Framework\Attributes\DataProviderExternal;
8
11
use Yiisoft\Db\Command\Param;
9
12
use Yiisoft\Db\Expression\Expression;
10
13
use Yiisoft\Db\Mssql\Column\BinaryColumn;
14
+ use Yiisoft\Db\Mssql\Column\ColumnBuilder;
11
15
use Yiisoft\Db\Mssql\Connection;
16
+ use Yiisoft\Db\Mssql\Tests\Provider\ColumnProvider;
12
17
use Yiisoft\Db\Mssql\Tests\Support\TestTrait;
13
18
use Yiisoft\Db\Query\Query;
14
19
use Yiisoft\Db\Schema\Column\BooleanColumn;
15
20
use Yiisoft\Db\Schema\Column\ColumnInterface;
16
21
use Yiisoft\Db\Schema\Column\DoubleColumn;
17
22
use Yiisoft\Db\Schema\Column\IntegerColumn;
18
23
use Yiisoft\Db\Schema\Column\StringColumn;
19
- use Yiisoft\Db\Tests\AbstractColumnTest ;
24
+ use Yiisoft\Db\Tests\Common\CommonColumnTest ;
20
25
21
26
use function str_repeat;
22
27
23
28
/**
24
29
* @group mssql
25
30
*/
26
- final class ColumnTest extends AbstractColumnTest
31
+ final class ColumnTest extends CommonColumnTest
27
32
{
28
33
use TestTrait;
29
34
35
+ protected const COLUMN_BUILDER = ColumnBuilder::class;
36
+
30
37
private function insertTypeValues(Connection $db): void
31
38
{
32
39
$db->createCommand()->insert(
@@ -44,16 +51,21 @@ private function insertTypeValues(Connection $db): void
44
51
)->execute();
45
52
}
46
53
47
- private function assertResultValues (array $result): void
54
+ private function assertTypecastedValues (array $result, bool $allTypecasted = false ): void
48
55
{
49
56
$this->assertSame(1, $result['int_col']);
50
57
$this->assertSame(str_repeat('x', 100), $result['char_col']);
51
58
$this->assertNull($result['char_col3']);
52
59
$this->assertSame(1.234, $result['float_col']);
53
60
$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']);
55
62
$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
+ }
57
69
}
58
70
59
71
public function testQueryWithTypecasting(): void
@@ -66,11 +78,11 @@ public function testQueryWithTypecasting(): void
66
78
67
79
$result = $query->one();
68
80
69
- $this->assertResultValues ($result);
81
+ $this->assertTypecastedValues ($result);
70
82
71
83
$result = $query->all();
72
84
73
- $this->assertResultValues ($result[0]);
85
+ $this->assertTypecastedValues ($result[0]);
74
86
75
87
$db->close();
76
88
}
@@ -85,11 +97,11 @@ public function testCommandWithPhpTypecasting(): void
85
97
86
98
$result = $command->queryOne();
87
99
88
- $this->assertResultValues ($result);
100
+ $this->assertTypecastedValues ($result);
89
101
90
102
$result = $command->queryAll();
91
103
92
- $this->assertResultValues ($result[0]);
104
+ $this->assertTypecastedValues ($result[0]);
93
105
94
106
$db->close();
95
107
}
@@ -138,29 +150,19 @@ public function testPhpTypeCast(): void
138
150
{
139
151
$db = $this->getConnection(true);
140
152
$schema = $db->getSchema();
141
- $tableSchema = $schema->getTableSchema('type');
153
+ $columns = $schema->getTableSchema('type')->getColumns( );
142
154
143
155
$this->insertTypeValues($db);
144
156
145
157
$query = (new Query($db))->from('type')->one();
146
158
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);
164
166
165
167
$db->close();
166
168
}
@@ -178,18 +180,24 @@ public function testColumnInstance()
178
180
$this->assertInstanceOf(BooleanColumn::class, $tableSchema->getColumn('bool_col'));
179
181
}
180
182
181
- /** @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\ ColumnProvider::predefinedTypes */
183
+ #[DataProviderExternal( ColumnProvider::class, 'predefinedTypes')]
182
184
public function testPredefinedType(string $className, string $type, string $phpType)
183
185
{
184
186
parent::testPredefinedType($className, $type, $phpType);
185
187
}
186
188
187
- /** @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\ ColumnProvider::dbTypecastColumns */
189
+ #[DataProviderExternal( ColumnProvider::class, 'dbTypecastColumns')]
188
190
public function testDbTypecastColumns(ColumnInterface $column, array $values)
189
191
{
190
192
parent::testDbTypecastColumns($column, $values);
191
193
}
192
194
195
+ #[DataProviderExternal(ColumnProvider::class, 'phpTypecastColumns')]
196
+ public function testPhpTypecastColumns(ColumnInterface $column, array $values)
197
+ {
198
+ parent::testPhpTypecastColumns($column, $values);
199
+ }
200
+
193
201
public function testBinaryColumn()
194
202
{
195
203
$binaryCol = new BinaryColumn();
0 commit comments