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