| Total Complexity | 41 |
| Total Lines | 456 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like CommonQueryBuilderTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CommonQueryBuilderTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | abstract class CommonQueryBuilderTest extends AbstractQueryBuilderTest |
||
| 39 | { |
||
| 40 | use TestTrait; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::addDropChecks() |
||
| 44 | */ |
||
| 45 | public function testAddDropCheck(string $sql, Closure $builder): void |
||
| 46 | { |
||
| 47 | $db = $this->getConnection(); |
||
| 48 | |||
| 49 | $qb = $db->getQueryBuilder(); |
||
| 50 | |||
| 51 | $this->assertSame($db->getQuoter()->quoteSql($sql), $builder($qb)); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::addDropForeignKeys() |
||
| 56 | */ |
||
| 57 | public function testAddDropForeignKey(string $sql, Closure $builder): void |
||
| 58 | { |
||
| 59 | $db = $this->getConnection(); |
||
| 60 | |||
| 61 | $qb = $db->getQueryBuilder(); |
||
| 62 | |||
| 63 | $this->assertSame($db->getQuoter()->quoteSql($sql), $builder($qb)); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::addDropPrimaryKeys() |
||
| 68 | */ |
||
| 69 | public function testAddDropPrimaryKey(string $sql, Closure $builder): void |
||
| 70 | { |
||
| 71 | $db = $this->getConnection(); |
||
| 72 | |||
| 73 | $qb = $db->getQueryBuilder(); |
||
| 74 | |||
| 75 | $this->assertSame($db->getQuoter()->quoteSql($sql), $builder($qb)); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::addDropUniques() |
||
| 80 | */ |
||
| 81 | public function testAddDropUnique(string $sql, Closure $builder): void |
||
| 82 | { |
||
| 83 | $db = $this->getConnection(); |
||
| 84 | |||
| 85 | $qb = $db->getQueryBuilder(); |
||
| 86 | |||
| 87 | $this->assertSame($db->getQuoter()->quoteSql($sql), $builder($qb)); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::alterColumn() |
||
| 92 | */ |
||
| 93 | public function testAlterColumn( |
||
| 94 | string $table, |
||
| 95 | string $column, |
||
| 96 | ColumnSchemaBuilder|string $type, |
||
| 97 | string $expected |
||
| 98 | ): void { |
||
| 99 | $db = $this->getConnection(); |
||
| 100 | |||
| 101 | $qb = $db->getQueryBuilder(); |
||
| 102 | $sql = $qb->alterColumn($table, $column, $type); |
||
| 103 | |||
| 104 | $this->assertSame($expected, $sql); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::batchInsert() |
||
| 109 | */ |
||
| 110 | public function testBatchInsert( |
||
| 111 | string $table, |
||
| 112 | array $columns, |
||
| 113 | array $value, |
||
| 114 | string|null $expected, |
||
| 115 | array $expectedParams = [] |
||
| 116 | ): void { |
||
| 117 | $db = $this->getConnectionWithData(); |
||
| 118 | |||
| 119 | $qb = $db->getQueryBuilder(); |
||
| 120 | $params = []; |
||
| 121 | $sql = $qb->batchInsert($table, $columns, $value, $params); |
||
| 122 | |||
| 123 | $this->assertSame($expected, $sql); |
||
| 124 | $this->assertEquals($expectedParams, $params); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::buildConditions() |
||
| 129 | */ |
||
| 130 | public function testBuildCondition( |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::buildFilterConditions() |
||
| 153 | */ |
||
| 154 | public function testBuildFilterCondition(array $condition, string $expected, array $expectedParams): void |
||
| 155 | { |
||
| 156 | $db = $this->getConnection(); |
||
| 157 | |||
| 158 | $qb = $db->getQueryBuilder(); |
||
| 159 | $query = $this->getQuery($db)->filterWhere($condition); |
||
| 160 | |||
| 161 | [$sql, $params] = $qb->build($query); |
||
| 162 | |||
| 163 | $this->assertSame( |
||
| 164 | 'SELECT *' . ( |
||
| 165 | empty($expected) ? '' : ' WHERE ' . DbHelper::replaceQuotes($expected, $db->getName()) |
||
| 166 | ), |
||
| 167 | $sql, |
||
| 168 | ); |
||
| 169 | $this->assertSame($expectedParams, $params); |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::buildFrom() |
||
| 174 | */ |
||
| 175 | public function testBuildFrom(string $table, string $expected): void |
||
| 176 | { |
||
| 177 | $db = $this->getConnection(); |
||
| 178 | |||
| 179 | $qb = $db->getQueryBuilder(); |
||
| 180 | $params = []; |
||
| 181 | $sql = $qb->buildFrom([$table], $params); |
||
| 182 | $replacedQuotes = DbHelper::replaceQuotes($expected, $db->getName()); |
||
| 183 | |||
| 184 | $this->assertIsString($replacedQuotes); |
||
| 185 | $this->assertSame('FROM ' . $replacedQuotes, $sql); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::buildLikeConditions() |
||
| 190 | * |
||
| 191 | * @throws Exception |
||
| 192 | * @throws InvalidArgumentException |
||
| 193 | * @throws InvalidConfigException |
||
| 194 | * @throws NotSupportedException |
||
| 195 | */ |
||
| 196 | public function testBuildLikeCondition( |
||
| 197 | array|ExpressionInterface $condition, |
||
| 198 | string $expected, |
||
| 199 | array $expectedParams |
||
| 200 | ): void { |
||
| 201 | $db = $this->getConnection(); |
||
| 202 | |||
| 203 | $query = $this->getQuery($db)->where($condition); |
||
| 204 | |||
| 205 | [$sql, $params] = $db->getQueryBuilder()->build($query); |
||
| 206 | |||
| 207 | $replacedQuotes = DbHelper::replaceQuotes($expected, $db->getName()); |
||
| 208 | |||
| 209 | $this->assertIsString($replacedQuotes); |
||
| 210 | $this->assertEquals('SELECT *' . (empty($expected) ? '' : ' WHERE ' . $replacedQuotes), $sql); |
||
| 211 | $this->assertEquals($expectedParams, $params); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::buildWhereExists() |
||
| 216 | */ |
||
| 217 | public function testBuildWhereExists(string $cond, string $expectedQuerySql): void |
||
| 218 | { |
||
| 219 | $db = $this->getConnection(); |
||
| 220 | |||
| 221 | $qb = $db->getQueryBuilder(); |
||
| 222 | $expectedQueryParams = []; |
||
| 223 | $subQuery = $this->getQuery($db)->select('1')->from('Website w'); |
||
| 224 | $query = $this->getQuery($db)->select('id')->from('TotalExample t')->where([$cond, $subQuery]); |
||
| 225 | |||
| 226 | [$actualQuerySql, $actualQueryParams] = $qb->build($query); |
||
| 227 | |||
| 228 | $this->assertSame($expectedQuerySql, $actualQuerySql); |
||
| 229 | $this->assertSame($expectedQueryParams, $actualQueryParams); |
||
| 230 | } |
||
| 231 | |||
| 232 | public function testCheckIntegrity(): void |
||
| 233 | { |
||
| 234 | $db = $this->getConnection(); |
||
| 235 | |||
| 236 | $command = $db->createCommand(); |
||
| 237 | $qb = $db->getQueryBuilder(); |
||
| 238 | $sql = $qb->checkIntegrity('schema', 'table'); |
||
| 239 | |||
| 240 | $this->assertSame(0, $command->setSql($sql)->execute()); |
||
| 241 | } |
||
| 242 | |||
| 243 | public function testCheckIntegrityExecuteException(): void |
||
| 244 | { |
||
| 245 | $db = $this->getConnectionWithData(); |
||
| 246 | |||
| 247 | $command = $db->createCommand(); |
||
| 248 | $qb = $db->getQueryBuilder(); |
||
| 249 | $schemaName = 'dbo'; |
||
| 250 | $tableName = 'T_constraints_3'; |
||
| 251 | $command->setSql($qb->checkIntegrity($schemaName, $tableName, false))->execute(); |
||
| 252 | $command->setSql( |
||
| 253 | <<<SQL |
||
| 254 | INSERT INTO {{{$tableName}}} ([[C_id]], [[C_fk_id_1]], [[C_fk_id_2]]) VALUES (1, 2, 3) |
||
| 255 | SQL |
||
| 256 | )->execute(); |
||
| 257 | $command->setSql($qb->checkIntegrity($schemaName, $tableName))->execute(); |
||
| 258 | |||
| 259 | $this->expectException(IntegrityException::class); |
||
| 260 | |||
| 261 | $command->setSql( |
||
| 262 | <<<SQL |
||
| 263 | INSERT INTO {{{$tableName}}} ([[C_id]], [[C_fk_id_1]], [[C_fk_id_2]]) VALUES (1, 2, 3) |
||
| 264 | SQL |
||
| 265 | )->execute(); |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::createDropIndex() |
||
| 270 | */ |
||
| 271 | public function testCreateDropIndex(string $sql, Closure $builder): void |
||
| 272 | { |
||
| 273 | $db = $this->getConnection(); |
||
| 274 | |||
| 275 | $qb = $db->getQueryBuilder(); |
||
| 276 | |||
| 277 | $this->assertSame($db->getQuoter()->quoteSql($sql), $builder($qb)); |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @throws Exception |
||
| 282 | * @throws InvalidConfigException |
||
| 283 | * @throws Throwable |
||
| 284 | */ |
||
| 285 | public function testCreateTable(): void |
||
| 306 | } |
||
| 307 | |||
| 308 | public function testCreateTableColumnTypes(): void |
||
| 309 | { |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::delete() |
||
| 343 | */ |
||
| 344 | public function testDelete(string $table, array|string $condition, string $expectedSQL, array $expectedParams): void |
||
| 345 | { |
||
| 346 | $db = $this->getConnection(); |
||
| 347 | |||
| 354 | } |
||
| 355 | |||
| 356 | public function testGetColumnType(): void |
||
| 357 | { |
||
| 358 | $db = $this->getConnection(); |
||
| 359 | |||
| 360 | $columnTypes = (new ColumnTypesProvider())->columnTypes($db); |
||
| 361 | $qb = $db->getQueryBuilder(); |
||
| 362 | |||
| 363 | foreach ($columnTypes as $item) { |
||
| 364 | [$column, $builder, $expected] = $item; |
||
| 365 | |||
| 366 | $driverName = $db->getName(); |
||
| 367 | |||
| 368 | if (isset($item[3][$driverName])) { |
||
| 369 | $expectedColumnSchemaBuilder = $item[3][$driverName]; |
||
| 370 | } elseif (isset($item[3]) && !is_array($item[3])) { |
||
| 371 | $expectedColumnSchemaBuilder = $item[3]; |
||
| 372 | } else { |
||
| 373 | $expectedColumnSchemaBuilder = $column; |
||
| 374 | } |
||
| 375 | |||
| 376 | $this->assertSame($expectedColumnSchemaBuilder, $builder->__toString()); |
||
| 377 | $this->assertSame($expected, $qb->getColumnType($column)); |
||
| 378 | $this->assertSame($expected, $qb->getColumnType($builder)); |
||
| 379 | } |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::insert() |
||
| 384 | */ |
||
| 385 | public function testInsert( |
||
| 386 | string $table, |
||
| 387 | array|QueryInterface $columns, |
||
| 388 | array $params, |
||
| 389 | string $expectedSQL, |
||
| 390 | array $expectedParams |
||
| 391 | ): void { |
||
| 392 | $db = $this->getConnection(); |
||
| 393 | |||
| 394 | $qb = $db->getQueryBuilder(); |
||
| 395 | |||
| 396 | $this->assertSame($expectedSQL, $qb->insert($table, $columns, $params)); |
||
| 397 | $this->assertSame($expectedParams, $params); |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::insertEx() |
||
| 402 | */ |
||
| 403 | public function testInsertEx( |
||
| 404 | string $table, |
||
| 405 | array|QueryInterface $columns, |
||
| 406 | array $params, |
||
| 407 | string $expectedSQL, |
||
| 408 | array $expectedParams |
||
| 409 | ): void { |
||
| 410 | $db = $this->getConnectionWithData(); |
||
| 411 | |||
| 412 | $qb = $db->getQueryBuilder(); |
||
| 413 | |||
| 414 | $this->assertSame($expectedSQL, $qb->insertEx($table, $columns, $params)); |
||
| 415 | $this->assertSame($expectedParams, $params); |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @throws Exception |
||
| 420 | * @throws InvalidConfigException |
||
| 421 | * @throws Throwable |
||
| 422 | */ |
||
| 423 | public function testRenameTable(): void |
||
| 424 | { |
||
| 425 | $db = $this->getConnectionWithData(); |
||
| 426 | |||
| 427 | $fromTableName = 'type'; |
||
| 428 | $toTableName = 'new_type'; |
||
| 429 | $command = $db->createCommand(); |
||
| 430 | $qb = $db->getQueryBuilder(); |
||
| 431 | |||
| 432 | if ($db->getSchema()->getTableSchema($toTableName) !== null) { |
||
| 433 | $command->dropTable($toTableName)->execute(); |
||
| 434 | } |
||
| 435 | |||
| 436 | $this->assertNotNull($db->getSchema()->getTableSchema($fromTableName)); |
||
| 437 | $this->assertNull($db->getSchema()->getTableSchema($toTableName)); |
||
| 438 | |||
| 439 | $sql = $qb->renameTable($fromTableName, $toTableName); |
||
| 440 | $command->setSql($sql)->execute(); |
||
| 441 | |||
| 442 | $this->assertNull($db->getSchema()->getTableSchema($fromTableName, true)); |
||
| 443 | $this->assertNotNull($db->getSchema()->getTableSchema($toTableName, true)); |
||
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::update() |
||
| 448 | */ |
||
| 449 | public function testUpdate( |
||
| 450 | string $table, |
||
| 451 | array $columns, |
||
| 452 | array|string $condition, |
||
| 453 | string $expectedSQL, |
||
| 454 | array $expectedParams |
||
| 455 | ): void { |
||
| 456 | $db = $this->getConnection(); |
||
| 457 | |||
| 458 | $qb = $db->getQueryBuilder(); |
||
| 459 | $actualParams = []; |
||
| 460 | |||
| 461 | $this->assertSame($expectedSQL, $qb->update($table, $columns, $condition, $actualParams)); |
||
| 462 | $this->assertSame($expectedParams, $actualParams); |
||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::upsert() |
||
| 467 | * |
||
| 468 | * @throws Exception |
||
| 469 | * @throws JsonException |
||
| 470 | * @throws NotSupportedException |
||
| 471 | */ |
||
| 472 | public function testUpsert( |
||
| 494 | } |
||
| 495 | } |
||
| 496 | } |
||
| 497 |