-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathColumnTest.php
115 lines (97 loc) · 4.29 KB
/
ColumnTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types=1);
namespace Yiisoft\Db\Mssql\Tests;
use PDO;
use Yiisoft\Db\Command\Param;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Mssql\Column\BinaryColumn;
use Yiisoft\Db\Mssql\Tests\Support\TestTrait;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Schema\Column\BooleanColumn;
use Yiisoft\Db\Schema\Column\ColumnInterface;
use Yiisoft\Db\Schema\Column\DoubleColumn;
use Yiisoft\Db\Schema\Column\IntegerColumn;
use Yiisoft\Db\Schema\Column\StringColumn;
use Yiisoft\Db\Tests\AbstractColumnTest;
use function str_repeat;
/**
* @group mssql
*/
final class ColumnTest extends AbstractColumnTest
{
use TestTrait;
public function testPhpTypeCast(): void
{
$db = $this->getConnection(true);
$command = $db->createCommand();
$schema = $db->getSchema();
$tableSchema = $schema->getTableSchema('type');
$command->insert(
'type',
[
'int_col' => 1,
'char_col' => str_repeat('x', 100),
'char_col3' => null,
'float_col' => 1.234,
'blob_col' => "\x10\x11\x12",
'datetime_col' => '2023-07-11 14:50:00.123',
'bool_col' => false,
'json_col' => [['a' => 1, 'b' => null, 'c' => [1, 3, 5]]],
]
);
$command->execute();
$query = (new Query($db))->from('type')->one();
$this->assertNotNull($tableSchema);
$intColPhpType = $tableSchema->getColumn('int_col')?->phpTypecast($query['int_col']);
$charColPhpType = $tableSchema->getColumn('char_col')?->phpTypecast($query['char_col']);
$charCol3PhpType = $tableSchema->getColumn('char_col3')?->phpTypecast($query['char_col3']);
$floatColPhpType = $tableSchema->getColumn('float_col')?->phpTypecast($query['float_col']);
$blobColPhpType = $tableSchema->getColumn('blob_col')?->phpTypecast($query['blob_col']);
$datetimeColPhpType = $tableSchema->getColumn('datetime_col')?->phpTypecast($query['datetime_col']);
$boolColPhpType = $tableSchema->getColumn('bool_col')?->phpTypecast($query['bool_col']);
$jsonColPhpType = $tableSchema->getColumn('json_col')?->phpTypecast($query['json_col']);
$this->assertSame(1, $intColPhpType);
$this->assertSame(str_repeat('x', 100), $charColPhpType);
$this->assertNull($charCol3PhpType);
$this->assertSame(1.234, $floatColPhpType);
$this->assertSame("\x10\x11\x12", $blobColPhpType);
$this->assertSame('2023-07-11 14:50:00.123', $datetimeColPhpType);
$this->assertFalse($boolColPhpType);
$this->assertSame([['a' => 1, 'b' => null, 'c' => [1, 3, 5]]], $jsonColPhpType);
$db->close();
}
public function testColumnInstance()
{
$db = $this->getConnection(true);
$schema = $db->getSchema();
$tableSchema = $schema->getTableSchema('type');
$this->assertInstanceOf(IntegerColumn::class, $tableSchema->getColumn('int_col'));
$this->assertInstanceOf(StringColumn::class, $tableSchema->getColumn('char_col'));
$this->assertInstanceOf(DoubleColumn::class, $tableSchema->getColumn('float_col'));
$this->assertInstanceOf(BinaryColumn::class, $tableSchema->getColumn('blob_col'));
$this->assertInstanceOf(BooleanColumn::class, $tableSchema->getColumn('bool_col'));
}
/** @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\ColumnProvider::predefinedTypes */
public function testPredefinedType(string $className, string $type, string $phpType)
{
parent::testPredefinedType($className, $type, $phpType);
}
/** @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\ColumnProvider::dbTypecastColumns */
public function testDbTypecastColumns(ColumnInterface $column, array $values)
{
parent::testDbTypecastColumns($column, $values);
}
public function testBinaryColumn()
{
$binaryCol = new BinaryColumn();
$binaryCol->dbType('varbinary');
$this->assertEquals(
new Expression('CONVERT(VARBINARY(MAX), 0x101112)'),
$binaryCol->dbTypecast("\x10\x11\x12"),
);
$this->assertEquals(
new Expression('CONVERT(VARBINARY(MAX), 0x101112)'),
$binaryCol->dbTypecast(new Param("\x10\x11\x12", PDO::PARAM_LOB)),
);
}
}