| Total Complexity | 54 |
| Total Lines | 1048 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| Bugs | 4 | Features | 0 |
Complex classes like QueryBuilderTest 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 QueryBuilderTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | final class QueryBuilderTest extends TestCase |
||
| 24 | { |
||
| 25 | use SchemaBuilderTrait; |
||
| 26 | |||
| 27 | private ConnectionInterface $db; |
||
| 28 | private QueryBuilderInterface $queryBuilder; |
||
| 29 | private Mock $mock; |
||
| 30 | |||
| 31 | public function setUp(): void |
||
| 32 | { |
||
| 33 | parent::setUp(); |
||
| 34 | |||
| 35 | $this->mock = new Mock(); |
||
| 36 | $this->db = $this->mock->connection(); |
||
| 37 | $this->queryBuilder = $this->mock->queryBuilder(); |
||
| 38 | } |
||
| 39 | |||
| 40 | public function tearDown(): void |
||
| 41 | { |
||
| 42 | parent::tearDown(); |
||
| 43 | |||
| 44 | unset($this->queryBuilder, $this->mock); |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::addDropChecks() |
||
| 49 | */ |
||
| 50 | public function testAddDropCheck(string $sql, Closure $builder): void |
||
| 51 | { |
||
| 52 | $this->assertSame($this->mock->quoter()->quoteSql($sql), $builder($this->queryBuilder)); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::addDropForeignKeys() |
||
| 57 | */ |
||
| 58 | public function testAddDropForeignKey(string $sql, Closure $builder): void |
||
| 59 | { |
||
| 60 | $this->assertSame($this->mock->quoter()->quoteSql($sql), $builder($this->queryBuilder)); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::addDropPrimaryKeys() |
||
| 65 | */ |
||
| 66 | public function testAddDropPrimaryKey(string $sql, Closure $builder): void |
||
| 67 | { |
||
| 68 | $this->assertSame($this->mock->quoter()->quoteSql($sql), $builder($this->queryBuilder)); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::addDropUniques() |
||
| 73 | */ |
||
| 74 | public function testAddDropUnique(string $sql, Closure $builder): void |
||
| 75 | { |
||
| 76 | $this->assertSame($this->mock->quoter()->quoteSql($sql), $builder($this->queryBuilder)); |
||
| 77 | } |
||
| 78 | |||
| 79 | public function testAddColumn(): void |
||
| 80 | { |
||
| 81 | $this->assertSame( |
||
| 82 | <<<SQL |
||
| 83 | ALTER TABLE `user` ADD `age` integer |
||
| 84 | SQL, |
||
| 85 | $this->queryBuilder->addColumn('user', 'age', 'integer') |
||
| 86 | ); |
||
| 87 | } |
||
| 88 | |||
| 89 | public function testsAddCommentOnColumn(): void |
||
| 90 | { |
||
| 91 | $this->assertSame( |
||
| 92 | <<<SQL |
||
| 93 | COMMENT ON COLUMN `user`.`name` IS 'This is a comment' |
||
| 94 | SQL, |
||
| 95 | $this->queryBuilder->addCommentOnColumn('user', 'name', 'This is a comment') |
||
| 96 | ); |
||
| 97 | } |
||
| 98 | |||
| 99 | public function testsAddCommentOnTable(): void |
||
| 100 | { |
||
| 101 | $this->assertSame( |
||
| 102 | <<<SQL |
||
| 103 | COMMENT ON TABLE `user` IS 'This is a comment' |
||
| 104 | SQL, |
||
| 105 | $this->queryBuilder->addCommentOnTable('user', 'This is a comment') |
||
| 106 | ); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::alterColumn() |
||
| 111 | */ |
||
| 112 | public function testAlterColumn( |
||
| 113 | string $table, |
||
| 114 | string $column, |
||
| 115 | ColumnSchemaBuilder|string $type, |
||
| 116 | string $expected |
||
| 117 | ): void { |
||
| 118 | $sql = $this->queryBuilder->alterColumn($table, $column, $type); |
||
| 119 | $this->assertSame($expected, $sql); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::batchInsert() |
||
| 124 | */ |
||
| 125 | public function testBatchInsert( |
||
| 126 | string $table, |
||
| 127 | array $columns, |
||
| 128 | array $value, |
||
| 129 | string|null $expected, |
||
| 130 | array $expectedParams = [] |
||
| 131 | ): void { |
||
| 132 | $params = []; |
||
| 133 | $sql = $this->queryBuilder->batchInsert($table, $columns, $value, $params); |
||
| 134 | |||
| 135 | $this->assertSame($expected, $sql); |
||
| 136 | $this->assertSame($expectedParams, $params); |
||
| 137 | } |
||
| 138 | |||
| 139 | public function testBuildColumnsWithString(): void |
||
| 140 | { |
||
| 141 | $columns = '(id)'; |
||
| 142 | |||
| 143 | $this->assertSame($columns, $this->queryBuilder->buildColumns($columns)); |
||
| 144 | } |
||
| 145 | |||
| 146 | public function testBuildColumnsWithArray(): void |
||
| 147 | { |
||
| 148 | $columns = [ |
||
| 149 | 'id', |
||
| 150 | 'name', |
||
| 151 | 'email', |
||
| 152 | 'address', |
||
| 153 | 'status', |
||
| 154 | ]; |
||
| 155 | |||
| 156 | $expected = '`id`, `name`, `email`, `address`, `status`'; |
||
| 157 | |||
| 158 | $this->assertSame($expected, $this->queryBuilder->buildColumns($columns)); |
||
| 159 | } |
||
| 160 | |||
| 161 | public function testBuildColumnsWithExpression(): void |
||
| 162 | { |
||
| 163 | $columns = [ |
||
| 164 | 'id', |
||
| 165 | 'name', |
||
| 166 | 'email', |
||
| 167 | 'address', |
||
| 168 | 'status', |
||
| 169 | new Expression('COUNT(*)'), |
||
| 170 | ]; |
||
| 171 | |||
| 172 | $expected = '`id`, `name`, `email`, `address`, `status`, COUNT(*)'; |
||
| 173 | |||
| 174 | $this->assertSame($expected, $this->queryBuilder->buildColumns($columns)); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::buildConditions() |
||
| 179 | */ |
||
| 180 | public function testBuildCondition( |
||
| 181 | array|ExpressionInterface|string $conditions, |
||
| 182 | string $expected, |
||
| 183 | array $expectedParams = [] |
||
| 184 | ): void { |
||
| 185 | $query = $this->mock->query()->where($conditions); |
||
| 186 | [$sql, $params] = $this->queryBuilder->build($query); |
||
| 187 | |||
| 188 | $this->assertSame( |
||
| 189 | 'SELECT *' . ( |
||
| 190 | empty($expected) ? '' : ' WHERE ' . DbHelper::replaceQuotes( |
||
| 191 | $expected, |
||
| 192 | $this->mock->getDriverName(), |
||
| 193 | ) |
||
| 194 | ), |
||
| 195 | $sql, |
||
| 196 | ); |
||
| 197 | $this->assertSame($expectedParams, $params); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::buildFilterCondition() |
||
| 202 | */ |
||
| 203 | public function testBuildFilterCondition(array $condition, string $expected, array $expectedParams): void |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::buildFrom() |
||
| 222 | */ |
||
| 223 | public function testBuildFrom(string $table, string $expected): void |
||
| 224 | { |
||
| 225 | $params = []; |
||
| 226 | $sql = $this->queryBuilder->buildFrom([$table], $params); |
||
| 227 | $replacedQuotes = DbHelper::replaceQuotes($expected, $this->mock->getDriverName()); |
||
| 228 | |||
| 229 | $this->assertIsString($replacedQuotes); |
||
| 230 | $this->assertSame('FROM ' . $replacedQuotes, $sql); |
||
| 231 | } |
||
| 232 | |||
| 233 | public function testBuildLimit(): void |
||
| 234 | { |
||
| 235 | $query = $this->mock->query()->limit(10); |
||
| 236 | [$sql, $params] = $this->queryBuilder->build($query); |
||
| 237 | |||
| 238 | $this->assertSame('SELECT * LIMIT 10', $sql); |
||
| 239 | $this->assertSame([], $params); |
||
| 240 | } |
||
| 241 | |||
| 242 | public function testBuildOffset(): void |
||
| 243 | { |
||
| 244 | $query = $this->mock->query()->offset(10); |
||
| 245 | [$sql, $params] = $this->queryBuilder->build($query); |
||
| 246 | |||
| 247 | $this->assertSame('SELECT * OFFSET 10', $sql); |
||
| 248 | $this->assertSame([], $params); |
||
| 249 | } |
||
| 250 | |||
| 251 | public function testBuildSelectColumnWithoutParentheses(): void |
||
| 252 | { |
||
| 253 | $params = []; |
||
| 254 | $sql = $this->queryBuilder->buildSelect(['1'], $params); |
||
| 255 | |||
| 256 | $this->assertSame('SELECT `1`', $sql); |
||
| 257 | } |
||
| 258 | |||
| 259 | public function testBuildSelectOptions(): void |
||
| 260 | { |
||
| 261 | $query = $this->mock->query()->selectOption('DISTINCT'); |
||
| 262 | [$sql, $params] = $this->queryBuilder->build($query); |
||
| 263 | |||
| 264 | $this->assertSame('SELECT DISTINCT *', $sql); |
||
| 265 | $this->assertSame([], $params); |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * This test contains three select queries connected with UNION and UNION ALL constructions. |
||
| 270 | * It could be useful to use "phpunit --group=db --filter testBuildUnion" command for run it. |
||
| 271 | */ |
||
| 272 | public function testBuildUnion(): void |
||
| 273 | { |
||
| 274 | $expectedQuerySql = DbHelper::replaceQuotes( |
||
| 275 | <<<SQL |
||
| 276 | (SELECT [[id]] FROM [[TotalExample]] [[t1]] WHERE (w > 0) AND (x < 2)) UNION ( SELECT [[id]] FROM [[TotalTotalExample]] [[t2]] WHERE w > 5 ) UNION ALL ( SELECT [[id]] FROM [[TotalTotalExample]] [[t3]] WHERE w = 3 ) |
||
| 277 | SQL, |
||
| 278 | $this->mock->getDriverName(), |
||
| 279 | ); |
||
| 280 | |||
| 281 | $secondQuery = $this->mock |
||
| 282 | ->query() |
||
| 283 | ->select('id') |
||
| 284 | ->from('TotalTotalExample t2') |
||
| 285 | ->where('w > 5'); |
||
| 286 | |||
| 287 | $thirdQuery = $this->mock |
||
| 288 | ->query() |
||
| 289 | ->select('id') |
||
| 290 | ->from('TotalTotalExample t3') |
||
| 291 | ->where('w = 3'); |
||
| 292 | |||
| 293 | $query = $this->mock |
||
| 294 | ->query() |
||
| 295 | ->select('id') |
||
| 296 | ->from('TotalExample t1') |
||
| 297 | ->where(['and', 'w > 0', 'x < 2']) |
||
| 298 | ->union($secondQuery) |
||
| 299 | ->union($thirdQuery, true); |
||
| 300 | |||
| 301 | [$actualQuerySql, $queryParams] = $this->queryBuilder->build($query); |
||
| 302 | |||
| 303 | $this->assertSame($expectedQuerySql, $actualQuerySql); |
||
| 304 | $this->assertSame([], $queryParams); |
||
| 305 | } |
||
| 306 | |||
| 307 | public function testBuildWithQuery(): void |
||
| 308 | { |
||
| 309 | $expectedQuerySql = DbHelper::replaceQuotes( |
||
| 310 | <<<SQL |
||
| 311 | WITH a1 AS (SELECT [[id]] FROM [[t1]] WHERE expr = 1), a2 AS ((SELECT [[id]] FROM [[t2]] INNER JOIN [[a1]] ON t2.id = a1.id WHERE expr = 2) UNION ( SELECT [[id]] FROM [[t3]] WHERE expr = 3 )) SELECT * FROM [[a2]] |
||
| 312 | SQL, |
||
| 313 | $this->mock->getDriverName(), |
||
| 314 | ); |
||
| 315 | |||
| 316 | $with1Query = $this->mock |
||
| 317 | ->query() |
||
| 318 | ->select('id') |
||
| 319 | ->from('t1') |
||
| 320 | ->where('expr = 1'); |
||
| 321 | |||
| 322 | $with2Query = $this->mock |
||
| 323 | ->query() |
||
| 324 | ->select('id') |
||
| 325 | ->from('t2') |
||
| 326 | ->innerJoin('a1', 't2.id = a1.id') |
||
| 327 | ->where('expr = 2'); |
||
| 328 | |||
| 329 | $with3Query = $this->mock |
||
| 330 | ->query() |
||
| 331 | ->select('id') |
||
| 332 | ->from('t3') |
||
| 333 | ->where('expr = 3'); |
||
| 334 | |||
| 335 | $query = $this->mock |
||
| 336 | ->query() |
||
| 337 | ->withQuery($with1Query, 'a1') |
||
| 338 | ->withQuery($with2Query->union($with3Query), 'a2') |
||
| 339 | ->from('a2'); |
||
| 340 | |||
| 341 | [$actualQuerySql, $queryParams] = $this->queryBuilder->build($query); |
||
| 342 | |||
| 343 | $this->assertSame($expectedQuerySql, $actualQuerySql); |
||
| 344 | $this->assertSame([], $queryParams); |
||
| 345 | } |
||
| 346 | |||
| 347 | public function testBuildWithQueryRecursive(): void |
||
| 348 | { |
||
| 349 | $expectedQuerySql = DbHelper::replaceQuotes( |
||
| 350 | <<<SQL |
||
| 351 | WITH RECURSIVE a1 AS (SELECT [[id]] FROM [[t1]] WHERE expr = 1) SELECT * FROM [[a1]] |
||
| 352 | SQL, |
||
| 353 | $this->mock->getDriverName(), |
||
| 354 | ); |
||
| 355 | |||
| 356 | $with1Query = $this->mock |
||
| 357 | ->query() |
||
| 358 | ->select('id') |
||
| 359 | ->from('t1') |
||
| 360 | ->where('expr = 1'); |
||
| 361 | |||
| 362 | $query = $this->mock |
||
| 363 | ->query() |
||
| 364 | ->withQuery($with1Query, 'a1', true) |
||
| 365 | ->from('a1'); |
||
| 366 | |||
| 367 | [$actualQuerySql, $queryParams] = $this->queryBuilder->build($query); |
||
| 368 | |||
| 369 | $this->assertSame($expectedQuerySql, $actualQuerySql); |
||
| 370 | $this->assertSame([], $queryParams); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::buildWhereExists() |
||
| 375 | */ |
||
| 376 | public function testBuildWhereExists(string $cond, string $expectedQuerySql): void |
||
| 377 | { |
||
| 378 | $expectedQueryParams = []; |
||
| 379 | $subQuery = $this->mock->query()->select('1')->from('Website w'); |
||
| 380 | $query = $this->mock->query()->select('id')->from('TotalExample t')->where([$cond, $subQuery]); |
||
| 381 | |||
| 382 | [$actualQuerySql, $actualQueryParams] = $this->queryBuilder->build($query); |
||
| 383 | |||
| 384 | $this->assertSame($expectedQuerySql, $actualQuerySql); |
||
| 385 | $this->assertSame($expectedQueryParams, $actualQueryParams); |
||
| 386 | } |
||
| 387 | |||
| 388 | public function testBuildWhereExistsWithArrayParameters(): void |
||
| 389 | { |
||
| 390 | $expectedQuerySql = DbHelper::replaceQuotes( |
||
| 391 | <<<SQL |
||
| 392 | SELECT [[id]] FROM [[TotalExample]] [[t]] WHERE (EXISTS (SELECT [[1]] FROM [[Website]] [[w]] WHERE (w.id = t.website_id) AND (([[w]].[[merchant_id]]=:qp0) AND ([[w]].[[user_id]]=:qp1)))) AND ([[t]].[[some_column]]=:qp2) |
||
| 393 | SQL, |
||
| 394 | $this->mock->getDriverName(), |
||
| 395 | ); |
||
| 396 | |||
| 397 | $expectedQueryParams = [':qp0' => 6, ':qp1' => 210, ':qp2' => 'asd']; |
||
| 398 | |||
| 399 | $subQuery = $this->mock |
||
| 400 | ->query() |
||
| 401 | ->select('1') |
||
| 402 | ->from('Website w') |
||
| 403 | ->where('w.id = t.website_id') |
||
| 404 | ->andWhere(['w.merchant_id' => 6, 'w.user_id' => 210]); |
||
| 405 | |||
| 406 | $query = $this->mock |
||
| 407 | ->query() |
||
| 408 | ->select('id') |
||
| 409 | ->from('TotalExample t') |
||
| 410 | ->where(['exists', $subQuery]) |
||
| 411 | ->andWhere(['t.some_column' => 'asd']); |
||
| 412 | |||
| 413 | [$actualQuerySql, $queryParams] = $this->queryBuilder->build($query); |
||
| 414 | |||
| 415 | $this->assertSame($expectedQuerySql, $actualQuerySql); |
||
| 416 | $this->assertSame($expectedQueryParams, $queryParams); |
||
| 417 | } |
||
| 418 | |||
| 419 | public function testBuildWhereExistsWithParameters(): void |
||
| 448 | } |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::createDropIndex() |
||
| 452 | */ |
||
| 453 | public function testCreateDropIndex(string $sql, Closure $builder): void |
||
| 454 | { |
||
| 455 | $this->assertSame($this->mock->quoter()->quoteSql($sql), $builder($this->queryBuilder)); |
||
| 456 | } |
||
| 457 | |||
| 458 | public function testsCreateTable(): void |
||
| 459 | { |
||
| 460 | $expected = DbHelper::replaceQuotes( |
||
| 461 | <<<SQL |
||
| 462 | CREATE TABLE [[test_table]] ( |
||
| 463 | \t[[id]] pk, |
||
| 464 | \t[[name]] string(255) NOT NULL, |
||
| 465 | \t[[email]] string(255) NOT NULL, |
||
| 466 | \t[[address]] string(255) NOT NULL, |
||
| 467 | \t[[status]] integer NOT NULL, |
||
| 468 | \t[[profile_id]] integer NOT NULL, |
||
| 469 | \t[[created_at]] timestamp NOT NULL, |
||
| 470 | \t[[updated_at]] timestamp NOT NULL |
||
| 471 | ) CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB |
||
| 472 | SQL, |
||
| 473 | $this->mock->getDriverName(), |
||
| 474 | ); |
||
| 475 | |||
| 476 | $columns = [ |
||
| 477 | 'id' => $this->primaryKey(5), |
||
| 478 | 'name' => $this->string(255)->notNull(), |
||
| 479 | 'email' => $this->string(255)->notNull(), |
||
| 480 | 'address' => $this->string(255)->notNull(), |
||
| 481 | 'status' => $this->integer()->notNull(), |
||
| 482 | 'profile_id' => $this->integer()->notNull(), |
||
| 483 | 'created_at' => $this->timestamp()->notNull(), |
||
| 484 | 'updated_at' => $this->timestamp()->notNull(), |
||
| 485 | ]; |
||
| 486 | |||
| 487 | $options = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; |
||
| 488 | |||
| 489 | $sql = $this->queryBuilder->createTable('test_table', $columns, $options); |
||
| 490 | |||
| 491 | Assert::equalsWithoutLE($expected, $sql); |
||
| 492 | } |
||
| 493 | |||
| 494 | public function testComplexSelect(): void |
||
| 530 | } |
||
| 531 | |||
| 532 | public function testCreateView(): void |
||
| 533 | { |
||
| 534 | $expected = DbHelper::replaceQuotes( |
||
| 535 | <<<SQL |
||
| 536 | CREATE VIEW [[test_view]] AS SELECT [[id]], [[name]] FROM [[test_table]] |
||
| 537 | SQL, |
||
| 538 | $this->mock->getDriverName(), |
||
| 539 | ); |
||
| 540 | |||
| 541 | $sql = $this->queryBuilder->createView( |
||
| 542 | 'test_view', |
||
| 543 | $this->mock->query()->select(['id', 'name'])->from('test_table'), |
||
| 544 | ); |
||
| 545 | |||
| 546 | $this->assertSame($expected, $sql); |
||
| 547 | } |
||
| 548 | |||
| 549 | public function testCreateViewWithParams(): void |
||
| 550 | { |
||
| 551 | $expected = DbHelper::replaceQuotes( |
||
| 552 | <<<SQL |
||
| 553 | CREATE VIEW `test_view` AS SELECT `id`, `name` FROM `test_table` WHERE `id`=1 |
||
| 554 | SQL, |
||
| 555 | $this->mock->getDriverName(), |
||
| 556 | ); |
||
| 557 | |||
| 558 | $sql = $this->queryBuilder->createView( |
||
| 559 | 'test_view', |
||
| 560 | $this->mock->query()->select(['id', 'name'])->from('test_table')->where(['id' => 1]), |
||
| 561 | ); |
||
| 562 | |||
| 563 | $this->assertSame($expected, $sql); |
||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::delete() |
||
| 568 | */ |
||
| 569 | public function testDelete(string $table, array|string $condition, string $expectedSQL, array $expectedParams): void |
||
| 570 | { |
||
| 571 | $actualParams = []; |
||
| 572 | $actualSQL = $this->queryBuilder->delete($table, $condition, $actualParams); |
||
| 573 | |||
| 574 | $this->assertSame($expectedSQL, $actualSQL); |
||
| 575 | $this->assertSame($expectedParams, $actualParams); |
||
| 576 | } |
||
| 577 | |||
| 578 | public function testDropColumn(): void |
||
| 579 | { |
||
| 580 | $expected = DbHelper::replaceQuotes( |
||
| 581 | <<<SQL |
||
| 582 | ALTER TABLE [[test_table]] DROP COLUMN [[test_column]] |
||
| 583 | SQL, |
||
| 584 | $this->mock->getDriverName(), |
||
| 585 | ); |
||
| 586 | |||
| 587 | $sql = $this->queryBuilder->dropColumn('test_table', 'test_column'); |
||
| 588 | |||
| 589 | $this->assertSame($expected, $sql); |
||
| 590 | } |
||
| 591 | |||
| 592 | public function testdropCommentFromColumn(): void |
||
| 593 | { |
||
| 594 | $expected = DbHelper::replaceQuotes( |
||
| 595 | <<<SQL |
||
| 596 | COMMENT ON COLUMN `test_table`.`test_column` IS NULL |
||
| 597 | SQL, |
||
| 598 | $this->mock->getDriverName(), |
||
| 599 | ); |
||
| 600 | |||
| 601 | $sql = $this->queryBuilder->dropCommentFromColumn('test_table', 'test_column'); |
||
| 602 | |||
| 603 | $this->assertSame($expected, $sql); |
||
| 604 | } |
||
| 605 | |||
| 606 | public function testsdropCommentFromTable(): void |
||
| 607 | { |
||
| 608 | $expected = DbHelper::replaceQuotes( |
||
| 609 | <<<SQL |
||
| 610 | COMMENT ON TABLE `test_table` IS NULL |
||
| 611 | SQL, |
||
| 612 | $this->mock->getDriverName(), |
||
| 613 | ); |
||
| 614 | |||
| 615 | $sql = $this->queryBuilder->dropCommentFromTable('test_table'); |
||
| 616 | |||
| 617 | $this->assertSame($expected, $sql); |
||
| 618 | } |
||
| 619 | |||
| 620 | public function testDropTable(): void |
||
| 632 | } |
||
| 633 | |||
| 634 | public function testDropView(): void |
||
| 635 | { |
||
| 636 | $expected = DbHelper::replaceQuotes( |
||
| 637 | <<<SQL |
||
| 638 | DROP VIEW [[test_view]] |
||
| 639 | SQL, |
||
| 640 | $this->mock->getDriverName(), |
||
| 641 | ); |
||
| 642 | |||
| 643 | $sql = $this->queryBuilder->dropView('test_view'); |
||
| 644 | |||
| 645 | $this->assertSame($expected, $sql); |
||
| 646 | } |
||
| 647 | |||
| 648 | /** |
||
| 649 | * {@see https://github.com/yiisoft/yii2/issues/10869} |
||
| 650 | */ |
||
| 651 | public function testFromIndexHint(): void |
||
| 652 | { |
||
| 653 | $query = $this->mock->query()->from([new Expression('{{%user}} USE INDEX (primary)')]); |
||
| 654 | |||
| 655 | [$sql, $params] = $this->queryBuilder->build($query); |
||
| 656 | |||
| 657 | $expected = DbHelper::replaceQuotes( |
||
| 658 | <<<SQL |
||
| 659 | SELECT * FROM {{%user}} USE INDEX (primary) |
||
| 660 | SQL, |
||
| 661 | $this->mock->getDriverName(), |
||
| 662 | ); |
||
| 663 | |||
| 664 | $this->assertSame($expected, $sql); |
||
| 665 | $this->assertEmpty($params); |
||
| 666 | |||
| 667 | $query = $this->mock |
||
| 668 | ->query() |
||
| 669 | ->from([new Expression('{{user}} {{t}} FORCE INDEX (primary) IGNORE INDEX FOR ORDER BY (i1)')]) |
||
| 670 | ->leftJoin(['p' => 'profile'], 'user.id = profile.user_id USE INDEX (i2)'); |
||
| 671 | |||
| 672 | [$sql, $params] = $this->queryBuilder->build($query); |
||
| 673 | |||
| 674 | $expected = DbHelper::replaceQuotes( |
||
| 675 | <<<SQL |
||
| 676 | SELECT * FROM {{user}} {{t}} FORCE INDEX (primary) IGNORE INDEX FOR ORDER BY (i1) LEFT JOIN [[profile]] [[p]] ON user.id = profile.user_id USE INDEX (i2) |
||
| 677 | SQL, |
||
| 678 | $this->mock->getDriverName(), |
||
| 679 | ); |
||
| 680 | |||
| 681 | $this->assertSame($expected, $sql); |
||
| 682 | $this->assertEmpty($params); |
||
| 683 | } |
||
| 684 | |||
| 685 | public function testFromSubquery(): void |
||
| 686 | { |
||
| 687 | /* subquery */ |
||
| 688 | $subquery = $this->mock->query()->from('user')->where('account_id = accounts.id'); |
||
| 689 | $query = $this->mock->query()->from(['activeusers' => $subquery]); |
||
| 690 | |||
| 691 | /* SELECT * FROM (SELECT * FROM [[user]] WHERE [[active]] = 1) [[activeusers]]; */ |
||
| 692 | [$sql, $params] = $this->queryBuilder->build($query); |
||
| 693 | |||
| 694 | $expected = DbHelper::replaceQuotes( |
||
| 695 | <<<SQL |
||
| 696 | SELECT * FROM (SELECT * FROM [[user]] WHERE account_id = accounts.id) [[activeusers]] |
||
| 697 | SQL, |
||
| 698 | $this->mock->getDriverName(), |
||
| 699 | ); |
||
| 700 | |||
| 701 | $this->assertSame($expected, $sql); |
||
| 702 | $this->assertEmpty($params); |
||
| 703 | |||
| 704 | /* subquery with params */ |
||
| 705 | $subquery = $this->mock->query()->from('user')->where('account_id = :id', ['id' => 1]); |
||
| 706 | $query = $this->mock->query()->from(['activeusers' => $subquery])->where('abc = :abc', ['abc' => 'abc']); |
||
| 707 | |||
| 708 | /* SELECT * FROM (SELECT * FROM [[user]] WHERE [[active]] = 1) [[activeusers]]; */ |
||
| 709 | [$sql, $params] = $this->queryBuilder->build($query); |
||
| 710 | |||
| 711 | $expected = DbHelper::replaceQuotes( |
||
| 712 | <<<SQL |
||
| 713 | SELECT * FROM (SELECT * FROM [[user]] WHERE account_id = :id) [[activeusers]] WHERE abc = :abc |
||
| 714 | SQL, |
||
| 715 | $this->mock->getDriverName(), |
||
| 716 | ); |
||
| 717 | |||
| 718 | $this->assertSame($expected, $sql); |
||
| 719 | $this->assertSame(['abc' => 'abc', 'id' => 1], $params); |
||
| 720 | |||
| 721 | /* simple subquery */ |
||
| 722 | $subquery = '(SELECT * FROM user WHERE account_id = accounts.id)'; |
||
| 723 | $query = $this->mock->query()->from(['activeusers' => $subquery]); |
||
| 724 | |||
| 725 | /* SELECT * FROM (SELECT * FROM [[user]] WHERE [[active]] = 1) [[activeusers]]; */ |
||
| 726 | [$sql, $params] = $this->queryBuilder->build($query); |
||
| 727 | |||
| 728 | $expected = DbHelper::replaceQuotes( |
||
| 729 | <<<SQL |
||
| 730 | SELECT * FROM (SELECT * FROM user WHERE account_id = accounts.id) [[activeusers]] |
||
| 731 | SQL, |
||
| 732 | $this->mock->getDriverName(), |
||
| 733 | ); |
||
| 734 | |||
| 735 | $this->assertSame($expected, $sql); |
||
| 736 | $this->assertEmpty($params); |
||
| 737 | } |
||
| 738 | |||
| 739 | public function testGroupBy(): void |
||
| 809 | } |
||
| 810 | |||
| 811 | /** |
||
| 812 | * {@see https://github.com/yiisoft/yii2/issues/15653} |
||
| 813 | */ |
||
| 814 | public function testIssue15653(): void |
||
| 815 | { |
||
| 816 | $query = $this->mock->query()->from('admin_user')->where(['is_deleted' => false]); |
||
| 817 | $query->where([])->andWhere(['in', 'id', ['1', '0']]); |
||
| 818 | |||
| 819 | [$sql, $params] = $this->queryBuilder->build($query); |
||
| 820 | |||
| 821 | $this->assertSame( |
||
| 822 | DbHelper::replaceQuotes( |
||
| 823 | <<<SQL |
||
| 824 | SELECT * FROM [[admin_user]] WHERE [[id]] IN (:qp0, :qp1) |
||
| 825 | SQL, |
||
| 826 | $this->mock->getDriverName(), |
||
| 827 | ), |
||
| 828 | $sql, |
||
| 829 | ); |
||
| 830 | $this->assertSame([':qp0' => '1', ':qp1' => '0'], $params); |
||
| 831 | } |
||
| 832 | |||
| 833 | public function testOrderBy(): void |
||
| 834 | { |
||
| 835 | /* simple string */ |
||
| 836 | $query = $this->mock->query()->select('*')->from('operations')->orderBy('name ASC, date DESC'); |
||
| 837 | |||
| 838 | [$sql, $params] = $this->queryBuilder->build($query); |
||
| 839 | |||
| 840 | $expected = DbHelper::replaceQuotes( |
||
| 841 | <<<SQL |
||
| 842 | SELECT * FROM [[operations]] ORDER BY [[name]], [[date]] DESC |
||
| 843 | SQL, |
||
| 844 | $this->mock->getDriverName(), |
||
| 845 | ); |
||
| 846 | |||
| 847 | $this->assertSame($expected, $sql); |
||
| 848 | $this->assertEmpty($params); |
||
| 849 | |||
| 850 | /* array syntax */ |
||
| 851 | $query = $this->mock->query()->select('*')->from('operations')->orderBy(['name' => SORT_ASC, 'date' => SORT_DESC]); |
||
| 852 | |||
| 853 | [$sql, $params] = $this->queryBuilder->build($query); |
||
| 854 | |||
| 855 | $expected = DbHelper::replaceQuotes( |
||
| 856 | <<<SQL |
||
| 857 | SELECT * FROM [[operations]] ORDER BY [[name]], [[date]] DESC |
||
| 858 | SQL, |
||
| 859 | $this->mock->getDriverName(), |
||
| 860 | ); |
||
| 861 | |||
| 862 | $this->assertSame($expected, $sql); |
||
| 863 | $this->assertEmpty($params); |
||
| 864 | |||
| 865 | /* expression */ |
||
| 866 | $query = $this->mock |
||
| 867 | ->query() |
||
| 868 | ->select('*') |
||
| 869 | ->from('operations') |
||
| 870 | ->where('account_id = accounts.id') |
||
| 871 | ->orderBy(new Expression('SUBSTR(name, 3, 4) DESC, x ASC')); |
||
| 872 | |||
| 873 | [$sql, $params] = $this->queryBuilder->build($query); |
||
| 874 | |||
| 875 | $expected = DbHelper::replaceQuotes( |
||
| 876 | <<<SQL |
||
| 877 | SELECT * FROM [[operations]] WHERE account_id = accounts.id ORDER BY SUBSTR(name, 3, 4) DESC, x ASC |
||
| 878 | SQL, |
||
| 879 | $this->mock->getDriverName(), |
||
| 880 | ); |
||
| 881 | |||
| 882 | $this->assertSame($expected, $sql); |
||
| 883 | $this->assertEmpty($params); |
||
| 884 | |||
| 885 | /* expression with params */ |
||
| 886 | $query = $this->mock |
||
| 887 | ->query() |
||
| 888 | ->select('*') |
||
| 889 | ->from('operations') |
||
| 890 | ->orderBy(new Expression('SUBSTR(name, 3, :to) DESC, x ASC', [':to' => 4])); |
||
| 891 | |||
| 892 | [$sql, $params] = $this->queryBuilder->build($query); |
||
| 893 | |||
| 894 | $expected = DbHelper::replaceQuotes( |
||
| 895 | <<<SQL |
||
| 896 | SELECT * FROM [[operations]] ORDER BY SUBSTR(name, 3, :to) DESC, x ASC |
||
| 897 | SQL, |
||
| 898 | $this->mock->getDriverName(), |
||
| 899 | ); |
||
| 900 | |||
| 901 | $this->assertSame($expected, $sql); |
||
| 902 | $this->assertSame([':to' => 4], $params); |
||
| 903 | } |
||
| 904 | |||
| 905 | public function testRenameColumn(): void |
||
| 906 | { |
||
| 907 | $sql = $this->queryBuilder->renameColumn('alpha', 'string_identifier', 'string_identifier_test'); |
||
| 908 | $this->assertSame( |
||
| 909 | <<<SQL |
||
| 910 | ALTER TABLE `alpha` RENAME COLUMN `string_identifier` TO `string_identifier_test` |
||
| 911 | SQL, |
||
| 912 | $sql, |
||
| 913 | ); |
||
| 914 | |||
| 915 | $sql = $this->queryBuilder->renameColumn('alpha', 'string_identifier_test', 'string_identifier'); |
||
| 916 | $this->assertSame( |
||
| 917 | <<<SQL |
||
| 918 | ALTER TABLE `alpha` RENAME COLUMN `string_identifier_test` TO `string_identifier` |
||
| 919 | SQL, |
||
| 920 | $sql, |
||
| 921 | ); |
||
| 922 | } |
||
| 923 | |||
| 924 | public function testRenameTable(): void |
||
| 933 | ); |
||
| 934 | } |
||
| 935 | |||
| 936 | public function testSelectExpression(): void |
||
| 937 | { |
||
| 938 | $query = $this->mock->query()->select(new Expression('1 AS ab'))->from('tablename'); |
||
| 939 | |||
| 940 | [$sql, $params] = $this->queryBuilder->build($query); |
||
| 941 | |||
| 942 | $expected = DbHelper::replaceQuotes( |
||
| 943 | <<<SQL |
||
| 944 | SELECT 1 AS ab FROM [[tablename]] |
||
| 945 | SQL, |
||
| 946 | $this->mock->getDriverName(), |
||
| 947 | ); |
||
| 948 | |||
| 949 | $this->assertSame($expected, $sql); |
||
| 950 | $this->assertEmpty($params); |
||
| 951 | |||
| 952 | $query = $this->mock |
||
| 953 | ->query() |
||
| 954 | ->select(new Expression('1 AS ab')) |
||
| 955 | ->addSelect(new Expression('2 AS cd')) |
||
| 956 | ->addSelect(['ef' => new Expression('3')]) |
||
| 957 | ->from('tablename'); |
||
| 958 | |||
| 959 | [$sql, $params] = $this->queryBuilder->build($query); |
||
| 960 | |||
| 961 | $expected = DbHelper::replaceQuotes( |
||
| 962 | <<<SQL |
||
| 963 | SELECT 1 AS ab, 2 AS cd, 3 AS [[ef]] FROM [[tablename]] |
||
| 964 | SQL, |
||
| 965 | $this->mock->getDriverName(), |
||
| 966 | ); |
||
| 967 | |||
| 968 | $this->assertSame($expected, $sql); |
||
| 969 | $this->assertEmpty($params); |
||
| 970 | |||
| 971 | $query = $this->mock |
||
| 972 | ->query() |
||
| 973 | ->select(new Expression('SUBSTR(name, 0, :len)', [':len' => 4])) |
||
| 974 | ->from('tablename'); |
||
| 975 | |||
| 976 | [$sql, $params] = $this->queryBuilder->build($query); |
||
| 977 | |||
| 978 | $expected = DbHelper::replaceQuotes( |
||
| 979 | <<<SQL |
||
| 980 | SELECT SUBSTR(name, 0, :len) FROM [[tablename]] |
||
| 981 | SQL, |
||
| 982 | $this->mock->getDriverName(), |
||
| 983 | ); |
||
| 984 | |||
| 985 | $this->assertSame($expected, $sql); |
||
| 986 | $this->assertSame([':len' => 4], $params); |
||
| 987 | } |
||
| 988 | |||
| 989 | public function testSelectExists(): void |
||
| 990 | { |
||
| 991 | $sql = $this->queryBuilder->selectExists('SELECT 1 FROM `table` WHERE `id` = 1'); |
||
| 992 | |||
| 993 | $this->assertSame('SELECT EXISTS(SELECT 1 FROM `table` WHERE `id` = 1)', $sql); |
||
| 994 | } |
||
| 995 | |||
| 996 | public function testSelectSubquery(): void |
||
| 997 | { |
||
| 998 | $expected = DbHelper::replaceQuotes( |
||
| 999 | <<<SQL |
||
| 1000 | SELECT *, (SELECT COUNT(*) FROM [[operations]] WHERE account_id = accounts.id) AS [[operations_count]] FROM [[accounts]] |
||
| 1001 | SQL, |
||
| 1002 | $this->mock->getDriverName(), |
||
| 1003 | ); |
||
| 1004 | |||
| 1005 | $subquery = $this->mock |
||
| 1006 | ->query() |
||
| 1007 | ->select('COUNT(*)') |
||
| 1008 | ->from('operations') |
||
| 1009 | ->where('account_id = accounts.id'); |
||
| 1010 | |||
| 1011 | $query = $this->mock |
||
| 1012 | ->query() |
||
| 1013 | ->select('*') |
||
| 1014 | ->from('accounts') |
||
| 1015 | ->addSelect(['operations_count' => $subquery]); |
||
| 1016 | |||
| 1017 | [$sql, $params] = $this->queryBuilder->build($query); |
||
| 1018 | |||
| 1019 | $this->assertSame($expected, $sql); |
||
| 1020 | $this->assertEmpty($params); |
||
| 1021 | } |
||
| 1022 | |||
| 1023 | public function testSetConditionClasses(): void |
||
| 1030 | } |
||
| 1031 | |||
| 1032 | public function testSelectExpressionBuilder(): void |
||
| 1033 | { |
||
| 1034 | $this->queryBuilder->setExpressionBuilders(['stdClass' => stdClass::class]); |
||
| 1035 | $dqlBuilder = Assert::getInaccessibleProperty($this->queryBuilder, 'dqlBuilder'); |
||
| 1036 | $expressionBuilders = Assert::getInaccessibleProperty($dqlBuilder, 'expressionBuilders'); |
||
| 1037 | |||
| 1038 | $this->assertSame(stdClass::class, $expressionBuilders['stdClass']); |
||
| 1039 | } |
||
| 1040 | |||
| 1041 | public function testSetSeparator(): void |
||
| 1060 | } |
||
| 1061 | |||
| 1062 | public function testTruncateTable(): void |
||
| 1063 | { |
||
| 1064 | $sql = $this->queryBuilder->truncateTable('table'); |
||
| 1065 | |||
| 1066 | $this->assertSame('TRUNCATE TABLE `table`', $sql); |
||
| 1067 | |||
| 1068 | $sql = $this->queryBuilder->truncateTable('table2'); |
||
| 1069 | |||
| 1070 | $this->assertSame('TRUNCATE TABLE `table2`', $sql); |
||
| 1071 | } |
||
| 1072 | } |
||
| 1073 |