|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Tests\QueryBuilder; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Db\Schema\SchemaBuilderTrait; |
|
8
|
|
|
use Yiisoft\Db\Tests\AbstractQueryBuilderTest; |
|
9
|
|
|
use Yiisoft\Db\Tests\Support\Assert; |
|
10
|
|
|
use Yiisoft\Db\Tests\Support\TestTrait; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @group db |
|
14
|
|
|
*/ |
|
15
|
|
|
final class QueryBuilderTest extends AbstractQueryBuilderTest |
|
16
|
|
|
{ |
|
17
|
|
|
use SchemaBuilderTrait; |
|
18
|
|
|
use TestTrait; |
|
19
|
|
|
|
|
20
|
|
|
public function testAddCommentOnColumn(): void |
|
21
|
|
|
{ |
|
22
|
|
|
$db = $this->getConnection(); |
|
23
|
|
|
|
|
24
|
|
|
$qb = $db->getQueryBuilder(); |
|
25
|
|
|
|
|
26
|
|
|
$this->assertSame( |
|
27
|
|
|
<<<SQL |
|
28
|
|
|
COMMENT ON COLUMN `customer`.`name` IS 'This is name' |
|
29
|
|
|
SQL, |
|
30
|
|
|
$qb->addCommentOnColumn('customer', 'name', 'This is name') |
|
31
|
|
|
); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testAddCommentOnTable(): void |
|
35
|
|
|
{ |
|
36
|
|
|
$db = $this->getConnection(); |
|
37
|
|
|
|
|
38
|
|
|
$qb = $db->getQueryBuilder(); |
|
39
|
|
|
|
|
40
|
|
|
$this->assertSame( |
|
41
|
|
|
<<<SQL |
|
42
|
|
|
COMMENT ON TABLE `customer` IS 'Customer table' |
|
43
|
|
|
SQL, |
|
44
|
|
|
$qb->addCommentOnTable('customer', 'Customer table') |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function testCreateTable(): void |
|
49
|
|
|
{ |
|
50
|
|
|
$this->db = $this->getConnection(); |
|
51
|
|
|
|
|
52
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
53
|
|
|
$expected = <<<SQL |
|
54
|
|
|
CREATE TABLE `test_table` ( |
|
55
|
|
|
\t`id` pk, |
|
56
|
|
|
\t`name` string(255) NOT NULL, |
|
57
|
|
|
\t`email` string(255) NOT NULL, |
|
58
|
|
|
\t`address` string(255) NOT NULL, |
|
59
|
|
|
\t`status` integer NOT NULL, |
|
60
|
|
|
\t`profile_id` integer NOT NULL, |
|
61
|
|
|
\t`created_at` timestamp NOT NULL, |
|
62
|
|
|
\t`updated_at` timestamp NOT NULL |
|
63
|
|
|
) CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB |
|
64
|
|
|
SQL; |
|
65
|
|
|
$columns = [ |
|
66
|
|
|
'id' => $this->primaryKey(5), |
|
67
|
|
|
'name' => $this->string(255)->notNull(), |
|
68
|
|
|
'email' => $this->string(255)->notNull(), |
|
69
|
|
|
'address' => $this->string(255)->notNull(), |
|
70
|
|
|
'status' => $this->integer()->notNull(), |
|
71
|
|
|
'profile_id' => $this->integer()->notNull(), |
|
72
|
|
|
'created_at' => $this->timestamp()->notNull(), |
|
73
|
|
|
'updated_at' => $this->timestamp()->notNull(), |
|
74
|
|
|
]; |
|
75
|
|
|
$options = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; |
|
76
|
|
|
$sql = $qb->createTable('test_table', $columns, $options); |
|
77
|
|
|
|
|
78
|
|
|
Assert::equalsWithoutLE($expected, $sql); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testDropColumn(): void |
|
82
|
|
|
{ |
|
83
|
|
|
$db = $this->getConnection(); |
|
84
|
|
|
|
|
85
|
|
|
$qb = $db->getQueryBuilder(); |
|
86
|
|
|
$sql = $qb->dropColumn('test_table', 'test_column'); |
|
87
|
|
|
|
|
88
|
|
|
$this->assertSame( |
|
89
|
|
|
<<<SQL |
|
90
|
|
|
ALTER TABLE `test_table` DROP COLUMN `test_column` |
|
91
|
|
|
SQL, |
|
92
|
|
|
$sql, |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function testDropCommentFromColumn(): void |
|
97
|
|
|
{ |
|
98
|
|
|
$db = $this->getConnection(); |
|
99
|
|
|
|
|
100
|
|
|
$qb = $db->getQueryBuilder(); |
|
101
|
|
|
$sql = $qb->dropCommentFromColumn('test_table', 'test_column'); |
|
102
|
|
|
|
|
103
|
|
|
$this->assertSame( |
|
104
|
|
|
<<<SQL |
|
105
|
|
|
COMMENT ON COLUMN `test_table`.`test_column` IS NULL |
|
106
|
|
|
SQL, |
|
107
|
|
|
$sql, |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function testRenameTable(): void |
|
112
|
|
|
{ |
|
113
|
|
|
$db = $this->getConnection(); |
|
114
|
|
|
|
|
115
|
|
|
$qb = $db->getQueryBuilder(); |
|
116
|
|
|
$sql = $qb->renameTable('table_from', 'table_to'); |
|
117
|
|
|
|
|
118
|
|
|
$this->assertSame( |
|
119
|
|
|
<<<SQL |
|
120
|
|
|
RENAME TABLE `table_from` TO `table_to` |
|
121
|
|
|
SQL, |
|
122
|
|
|
$sql, |
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|