1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Tests\Command; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Db\Exception\Exception; |
8
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
9
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
10
|
|
|
use Yiisoft\Db\Tests\AbstractCommandTest; |
11
|
|
|
use Yiisoft\Db\Tests\Support\Assert; |
12
|
|
|
use Yiisoft\Db\Tests\Support\DbHelper; |
13
|
|
|
use Yiisoft\Db\Tests\Support\TestTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @group db |
17
|
|
|
* |
18
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
19
|
|
|
*/ |
20
|
|
|
final class CommandTest extends AbstractCommandTest |
21
|
|
|
{ |
22
|
|
|
use TestTrait; |
23
|
|
|
|
24
|
|
|
public function testAddDefaultValue(): void |
25
|
|
|
{ |
26
|
|
|
$db = $this->getConnection(); |
27
|
|
|
|
28
|
|
|
$command = $db->createCommand(); |
29
|
|
|
|
30
|
|
|
$this->expectException(NotSupportedException::class); |
31
|
|
|
$this->expectExceptionMessage( |
32
|
|
|
'Yiisoft\Db\Tests\Support\Stub\DDLQueryBuilder does not support adding default value constraints.' |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
$command->addDefaultValue('name', 'table', 'column', 'value'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testBatchInsert(): void |
39
|
|
|
{ |
40
|
|
|
$db = $this->getConnection(); |
41
|
|
|
|
42
|
|
|
$command = $db->createCommand(); |
43
|
|
|
|
44
|
|
|
$this->expectException(NotSupportedException::class); |
45
|
|
|
$this->expectExceptionMessage( |
46
|
|
|
'Yiisoft\Db\Tests\Support\Stub\Schema::loadTableSchema() is not supported by core-db.' |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
$command->batchInsert('table', ['column1', 'column2'], [['value1', 'value2'], ['value3', 'value4']]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testCheckIntegrity(): void |
53
|
|
|
{ |
54
|
|
|
$db = $this->getConnection(); |
55
|
|
|
|
56
|
|
|
$command = $db->createCommand(); |
57
|
|
|
|
58
|
|
|
$this->expectException(NotSupportedException::class); |
59
|
|
|
$this->expectExceptionMessage( |
60
|
|
|
'Yiisoft\Db\Tests\Support\Stub\DDLQueryBuilder does not support enabling/disabling integrity check.' |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$command->checkIntegrity('schema', 'table')->execute(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testCreateTable(): void |
67
|
|
|
{ |
68
|
|
|
$this->db = $this->getConnection(); |
69
|
|
|
|
70
|
|
|
$command = $this->db->createCommand(); |
71
|
|
|
|
72
|
|
|
$expected = <<<SQL |
73
|
|
|
CREATE TABLE [test_table] ( |
74
|
|
|
\t[id] pk, |
75
|
|
|
\t[name] string(255) NOT NULL, |
76
|
|
|
\t[email] string(255) NOT NULL, |
77
|
|
|
\t[address] string(255) NOT NULL, |
78
|
|
|
\t[status] integer NOT NULL, |
79
|
|
|
\t[profile_id] integer NOT NULL, |
80
|
|
|
\t[created_at] timestamp NOT NULL, |
81
|
|
|
\t[updated_at] timestamp NOT NULL |
82
|
|
|
) CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB |
83
|
|
|
SQL; |
84
|
|
|
$columns = [ |
85
|
|
|
'id' => $this->primaryKey(5), |
86
|
|
|
'name' => $this->string(255)->notNull(), |
87
|
|
|
'email' => $this->string(255)->notNull(), |
88
|
|
|
'address' => $this->string(255)->notNull(), |
89
|
|
|
'status' => $this->integer()->notNull(), |
90
|
|
|
'profile_id' => $this->integer()->notNull(), |
91
|
|
|
'created_at' => $this->timestamp()->notNull(), |
92
|
|
|
'updated_at' => $this->timestamp()->notNull(), |
93
|
|
|
]; |
94
|
|
|
$options = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; |
95
|
|
|
$sql = $command->createTable('test_table', $columns, $options)->getSql(); |
96
|
|
|
|
97
|
|
|
Assert::equalsWithoutLE($expected, $sql); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testDropDefaultValue(): void |
101
|
|
|
{ |
102
|
|
|
$db = $this->getConnection(); |
103
|
|
|
|
104
|
|
|
$command = $db->createCommand(); |
105
|
|
|
|
106
|
|
|
$this->expectException(NotSupportedException::class); |
107
|
|
|
$this->expectExceptionMessage( |
108
|
|
|
'Yiisoft\Db\Tests\Support\Stub\DDLQueryBuilder does not support dropping default value constraints.' |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$command->dropDefaultValue('column', 'table'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testExecute(): void |
115
|
|
|
{ |
116
|
|
|
$db = $this->getConnection(); |
117
|
|
|
|
118
|
|
|
$command = $db->createCommand(); |
119
|
|
|
|
120
|
|
|
$this->expectException(NotSupportedException::class); |
121
|
|
|
$this->expectExceptionMessage( |
122
|
|
|
'Yiisoft\Db\Tests\Support\Stub\Command::internalExecute() is not supported by core-db.' |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
$command->createTable('customer', ['id' => 'pk'])->execute(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testInsert(): void |
129
|
|
|
{ |
130
|
|
|
$db = $this->getConnection(); |
131
|
|
|
|
132
|
|
|
$command = $db->createCommand(); |
133
|
|
|
|
134
|
|
|
$this->expectException(NotSupportedException::class); |
135
|
|
|
$this->expectExceptionMessage( |
136
|
|
|
'Yiisoft\Db\Tests\Support\Stub\Schema::loadTableSchema() is not supported by core-db.' |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
$command->insert('customer', ['email' => '[email protected]', 'name' => 'test', 'address' => 'test address']); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testQuery(): void |
143
|
|
|
{ |
144
|
|
|
$db = $this->getConnection('customer'); |
145
|
|
|
|
146
|
|
|
$command = $db->createCommand(); |
147
|
|
|
$command->setSql( |
148
|
|
|
<<<SQL |
149
|
|
|
SELECT * FROM customer |
150
|
|
|
SQL |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$this->expectException(NotSupportedException::class); |
154
|
|
|
$this->expectExceptionMessage( |
155
|
|
|
'Yiisoft\Db\Tests\Support\Stub\Command::internalExecute() is not supported by core-db.' |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
$command->query(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function testQueryAll(): void |
162
|
|
|
{ |
163
|
|
|
$db = $this->getConnection('customer'); |
164
|
|
|
|
165
|
|
|
$command = $db->createCommand(); |
166
|
|
|
$command->setSql( |
167
|
|
|
<<<SQL |
168
|
|
|
SELECT * FROM {{customer}} |
169
|
|
|
SQL, |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
$this->expectException(NotSupportedException::class); |
173
|
|
|
$this->expectExceptionMessage( |
174
|
|
|
'Yiisoft\Db\Tests\Support\Stub\Command::internalExecute() is not supported by core-db.' |
175
|
|
|
); |
176
|
|
|
|
177
|
|
|
$command->queryAll(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function testRenameTable(): void |
181
|
|
|
{ |
182
|
|
|
$db = $this->getConnection(); |
183
|
|
|
|
184
|
|
|
$sql = $db->createCommand()->renameTable('table', 'newname')->getSql(); |
185
|
|
|
|
186
|
|
|
$this->assertSame( |
187
|
|
|
DbHelper::replaceQuotes( |
188
|
|
|
<<<SQL |
189
|
|
|
RENAME TABLE [[table]] TO [[newname]] |
190
|
|
|
SQL, |
191
|
|
|
$db->getName(), |
192
|
|
|
), |
193
|
|
|
$sql, |
194
|
|
|
); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function testResetSequence(): void |
198
|
|
|
{ |
199
|
|
|
$db = $this->getConnection(); |
200
|
|
|
|
201
|
|
|
$this->expectException(NotSupportedException::class); |
202
|
|
|
$this->expectExceptionMessage( |
203
|
|
|
'Yiisoft\Db\Tests\Support\Stub\DMLQueryBuilder does not support resetting sequence.' |
204
|
|
|
); |
205
|
|
|
|
206
|
|
|
$db->createCommand()->resetSequence('table', 5); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @dataProvider \Yiisoft\Db\Tests\Provider\CommandProvider::upsert() |
211
|
|
|
* |
212
|
|
|
* @throws Exception |
213
|
|
|
* @throws InvalidConfigException |
214
|
|
|
* @throws NotSupportedException |
215
|
|
|
*/ |
216
|
|
|
public function testUpsert(array $firstData, array $secondData): void |
|
|
|
|
217
|
|
|
{ |
218
|
|
|
$db = $this->getConnection(); |
219
|
|
|
|
220
|
|
|
$command = $db->createCommand(); |
221
|
|
|
|
222
|
|
|
$this->expectException(NotSupportedException::class); |
223
|
|
|
$this->expectExceptionMessage( |
224
|
|
|
'Yiisoft\Db\Tests\Support\Stub\DMLQueryBuilder does not support upsert.' |
225
|
|
|
); |
226
|
|
|
|
227
|
|
|
$command->upsert('table', $firstData); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.