Skip to content

Commit 6811bbd

Browse files
NHZEXliu21st
authored andcommitted
迁移测试 ModelFieldType
1 parent 4c39281 commit 6811bbd

File tree

7 files changed

+159
-32
lines changed

7 files changed

+159
-32
lines changed

src/db/connector/Pgsql.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,28 @@ protected function supportSavepoint(): bool
111111
return true;
112112
}
113113

114+
protected function getFieldType(string $type): string
115+
{
116+
// 将字段类型转换为小写以进行比较
117+
$type = strtolower($type);
118+
119+
return match (true) {
120+
str_starts_with($type, 'set') => 'set',
121+
str_starts_with($type, 'enum') => 'enum',
122+
str_starts_with($type, 'bigint'),
123+
str_contains($type, 'numeric') => 'bigint',
124+
str_contains($type, 'float') || str_contains($type, 'double') ||
125+
str_contains($type, 'decimal') || str_contains($type, 'real') ||
126+
str_contains($type, 'int') || str_contains($type, 'serial') ||
127+
str_contains($type, 'bit') => 'int',
128+
str_contains($type, 'bool') => 'bool',
129+
str_starts_with($type, 'timestamp') => 'timestamp',
130+
str_starts_with($type, 'datetime') => 'datetime',
131+
str_starts_with($type, 'date') => 'date',
132+
default => 'string',
133+
};
134+
}
135+
114136
public function insert(BaseQuery $query, bool $getLastInsID = false)
115137
{
116138
// 分析查询表达式

tests/TestCaseBase.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use think\db\ConnectionInterface;
1111
use think\db\connector\Pgsql;
1212
use think\facade\Db;
13+
use think\Model;
1314
use function version_compare;
1415

1516
/**
@@ -21,6 +22,15 @@ class TestCaseBase extends TestCase
2122
protected static string $connectName;
2223
protected bool $isPgScriptInstalled = false;
2324

25+
protected static function initModelSupport(): void
26+
{
27+
// todo 需要一个重置能力更安全
28+
Model::maker(function (Model $model) {
29+
$model->setConnection(static::$connectName);
30+
var_dump('maker:' . __FUNCTION__ . '-' . $model::class . '-' . spl_object_id($model));
31+
});
32+
}
33+
2434
public function __get(string $name)
2535
{
2636
if ($name === 'connectName') {
@@ -55,6 +65,18 @@ protected static function compatibleInsertAll(BaseQuery $query, array $data): vo
5565
}
5666
}
5767

68+
protected static function compatibleModelInsertAll(Model $query, array $data): void
69+
{
70+
if ($query->getConnection() === 'pgsql') {
71+
// 当前驱动批量插入不兼容,会产生类型错误,修复后可以移除兼容性
72+
foreach ($data as $datum) {
73+
(clone $query)->insert($datum);
74+
}
75+
} else {
76+
$query->insertAll($data);
77+
}
78+
}
79+
5880
protected function proxyAssertMatchesRegularExpression(string $pattern, string $string, string $message = '')
5981
{
6082
if (version_compare(Version::id(), '9.1', '>=')) {
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Phinx\Migration\AbstractMigration;
6+
7+
final class ModelFieldType extends AbstractMigration
8+
{
9+
/**
10+
* Change Method.
11+
*
12+
* Write your reversible migrations using this method.
13+
*
14+
* More information on writing migrations is available here:
15+
* https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
16+
*
17+
* Remember to call "create()" or "update()" and NOT "save()" when working
18+
* with the Table class.
19+
*/
20+
public function change(): void
21+
{
22+
$adapterType = $this->getAdapter()->getAdapterType();
23+
24+
$this
25+
->table('test_field_type', ['id' => false, 'primary_key' => ['id']])
26+
->addColumn(
27+
'id',
28+
'integer',
29+
[
30+
'identity' => true,
31+
'signed' => false, // MySQL用UNSIGNED, PostgreSQL需要容错
32+
'null' => false,
33+
]
34+
)
35+
->addColumn(
36+
't_json',
37+
'json',
38+
[
39+
'null' => true,
40+
'default' => null,
41+
])
42+
->addColumn(
43+
't_php',
44+
'text',
45+
[ // 改用text类型更通用
46+
'limit' => 512,
47+
'null' => true,
48+
'default' => null,
49+
]
50+
)
51+
->addColumn(
52+
'bigint',
53+
$adapterType === 'pgsql' ? 'decimal' : 'biginteger',
54+
[
55+
'signed' => false,
56+
'null' => true,
57+
'default' => null,
58+
'after' => 't_php', // 可选字段排序
59+
'precision' => $adapterType === 'pgsql' ? 20 : null, // PG BIGINT 最大19位
60+
]
61+
)
62+
->create();
63+
}
64+
}

tests/orm/ModelFieldTypeTest.php renamed to tests/orm/ModelFieldTypeBase.php

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,49 @@
33

44
namespace tests\orm;
55

6-
use PHPUnit\Framework\TestCase;
76
use tests\stubs\FieldTypeModel;
87
use tests\stubs\TestFieldJsonDTO;
98
use tests\stubs\TestFieldPhpDTO;
10-
use think\facade\Db;
9+
use tests\TestCaseBase;
1110

12-
class ModelFieldTypeTest extends TestCase
11+
class ModelFieldTypeBase extends TestCaseBase
1312
{
14-
public static function setUpBeforeClass(): void
13+
protected function provideTestData(): array
1514
{
16-
Db::execute('DROP TABLE IF EXISTS `test_field_type`;');
17-
Db::execute(
18-
<<<SQL
19-
CREATE TABLE `test_field_type` (
20-
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
21-
`t_json` json DEFAULT NULL,
22-
`t_php` varchar(512) DEFAULT NULL,
23-
`bigint` bigint UNSIGNED DEFAULT NULL
24-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
25-
SQL
26-
);
27-
}
28-
29-
public function testFieldTypeSelect()
30-
{
31-
$data = [
15+
return [
3216
['id' => 1, 't_json' => '{"num1": 1, "str1": "a"}', 't_php' => (string) (new TestFieldPhpDTO(1, 'a')), 'bigint' => '0'],
3317
['id' => 2, 't_json' => '{"num1": 2, "str1": "b"}', 't_php' => (string) (new TestFieldPhpDTO(2, 'b')), 'bigint' => '244791959321042944'],
3418
['id' => 3, 't_json' => '{"num1": 3, "str1": "c"}', 't_php' => (string) (new TestFieldPhpDTO(3, 'c')), 'bigint' => '18374686479671623679'],
3519
];
20+
}
21+
22+
public static function setUpBeforeClass(): void
23+
{
24+
parent::setUpBeforeClass();
25+
26+
self::initModelSupport();
27+
}
3628

37-
(new FieldTypeModel())->insertAll($data);
29+
public function testInitData(): array
30+
{
31+
$this->db->execute('TRUNCATE TABLE test_field_type;');
32+
33+
$data = $this->provideTestData();
34+
self::compatibleModelInsertAll(new FieldTypeModel(), $data);
35+
36+
return $data;
37+
}
38+
39+
/**
40+
* @depends testInitData
41+
*/
42+
public function testFieldTypeSelect(array $data)
43+
{
44+
var_dump($this->db->getTableFieldsInfo('test_field_type'));
45+
var_dump($this->db->getSchemaInfo('test_field_type'));
46+
var_dump($this->db->getFieldBindType('bigint'));
3847

39-
$result = Db::table('test_field_type')->select();
48+
$result = $this->db->table('test_field_type')->setFieldType(['bigint' => 'string'])->select();
4049
$this->assertNotEmpty($result->count());
4150
foreach ($data as $index => $item) {
4251
$this->assertEquals($item, $result[$index]);
@@ -52,7 +61,7 @@ public function testFieldTypeSelect()
5261
}
5362

5463
/**
55-
* @depends testFieldTypeSelect
64+
* @depends testInitData
5665
*/
5766
public function testFieldReadAndWrite()
5867
{
@@ -69,9 +78,6 @@ public function testFieldReadAndWrite()
6978
$this->assertEquals($result->id, $result->t_php->getId());
7079
}
7180

72-
/**
73-
* @depends testFieldTypeSelect
74-
*/
7581
public function testFieldReadInvalid()
7682
{
7783

tests/orm/ModelOneToOneBase.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use tests\stubs\ProfileModel;
77
use tests\stubs\UserModel;
88
use tests\TestCaseBase;
9-
use think\Model;
109

1110
/**
1211
* 模型一对一关联
@@ -17,11 +16,7 @@ public static function setUpBeforeClass(): void
1716
{
1817
parent::setUpBeforeClass();
1918

20-
// todo 需要一个重置能力更安全
21-
Model::maker(function (Model $model) {
22-
$model->setConnection(static::$connectName);
23-
var_dump('maker:' . __FUNCTION__ . '-' . $model::class . '-' . spl_object_id($model));
24-
});
19+
self::initModelSupport();
2520
}
2621

2722
public function setUp(): void
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace tests\orm;
5+
6+
class MysqlModelFieldTypeTest extends ModelFieldTypeBase
7+
{
8+
protected static string $connectName = 'mysql';
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace tests\orm;
5+
6+
class PgsqlModelFieldTypeTest extends ModelFieldTypeBase
7+
{
8+
protected static string $connectName = 'pgsql';
9+
}

0 commit comments

Comments
 (0)