|
15 | 15 |
|
16 | 16 | use CodeIgniter\Database\BaseBuilder; |
17 | 17 | use CodeIgniter\Database\RawSql; |
| 18 | +use CodeIgniter\Exceptions\InvalidArgumentException; |
18 | 19 | use CodeIgniter\Test\CIUnitTestCase; |
19 | 20 | use CodeIgniter\Test\Mock\MockConnection; |
20 | 21 | use DateTime; |
@@ -351,6 +352,143 @@ public function testOrWhereSameColumn(): void |
351 | 352 | $this->assertSame($expectedBinds, $builder->getBinds()); |
352 | 353 | } |
353 | 354 |
|
| 355 | + #[DataProvider('provideWhereColumnWithOperators')] |
| 356 | + public function testWhereColumnWithOperators(string $first, string $operator): void |
| 357 | + { |
| 358 | + $builder = $this->db->table('users'); |
| 359 | + |
| 360 | + $builder->whereColumn($first, 'updated_at'); |
| 361 | + |
| 362 | + $expectedSQL = sprintf('SELECT * FROM "users" WHERE "created_at" %s "updated_at"', $operator); |
| 363 | + $expectedBinds = []; |
| 364 | + |
| 365 | + $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
| 366 | + $this->assertSame($expectedBinds, $builder->getBinds()); |
| 367 | + } |
| 368 | + |
| 369 | + /** |
| 370 | + * @return iterable<string, array{string, string}> |
| 371 | + */ |
| 372 | + public static function provideWhereColumnWithOperators(): iterable |
| 373 | + { |
| 374 | + return [ |
| 375 | + 'default' => ['created_at', '='], |
| 376 | + '=' => ['created_at =', '='], |
| 377 | + '!=' => ['created_at !=', '!='], |
| 378 | + '<>' => ['created_at <>', '<>'], |
| 379 | + '<' => ['created_at <', '<'], |
| 380 | + '>' => ['created_at >', '>'], |
| 381 | + '<=' => ['created_at <=', '<='], |
| 382 | + '>=' => ['created_at >=', '>='], |
| 383 | + ]; |
| 384 | + } |
| 385 | + |
| 386 | + public function testWhereColumnWithAlias(): void |
| 387 | + { |
| 388 | + $builder = $this->db->table('users u'); |
| 389 | + |
| 390 | + $builder->whereColumn('u.updated_at >', 'u.created_at'); |
| 391 | + |
| 392 | + $expectedSQL = 'SELECT * FROM "users" "u" WHERE "u"."updated_at" > "u"."created_at"'; |
| 393 | + $expectedBinds = []; |
| 394 | + |
| 395 | + $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
| 396 | + $this->assertSame($expectedBinds, $builder->getBinds()); |
| 397 | + } |
| 398 | + |
| 399 | + public function testOrWhereColumn(): void |
| 400 | + { |
| 401 | + $builder = $this->db->table('users'); |
| 402 | + |
| 403 | + $builder->where('active', 1) |
| 404 | + ->orWhereColumn('updated_at >', 'created_at'); |
| 405 | + |
| 406 | + $expectedSQL = 'SELECT * FROM "users" WHERE "active" = 1 OR "updated_at" > "created_at"'; |
| 407 | + $expectedBinds = [ |
| 408 | + 'active' => [ |
| 409 | + 1, |
| 410 | + true, |
| 411 | + ], |
| 412 | + ]; |
| 413 | + |
| 414 | + $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
| 415 | + $this->assertSame($expectedBinds, $builder->getBinds()); |
| 416 | + } |
| 417 | + |
| 418 | + public function testWhereColumnWithGroupedConditions(): void |
| 419 | + { |
| 420 | + $builder = $this->db->table('users'); |
| 421 | + |
| 422 | + $builder->groupStart() |
| 423 | + ->whereColumn('created_at', 'updated_at') |
| 424 | + ->orWhereColumn('updated_at >', 'created_at') |
| 425 | + ->groupEnd() |
| 426 | + ->where('active', 1); |
| 427 | + |
| 428 | + $expectedSQL = 'SELECT * FROM "users" WHERE ( "created_at" = "updated_at" OR "updated_at" > "created_at" ) AND "active" = 1'; |
| 429 | + |
| 430 | + $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
| 431 | + } |
| 432 | + |
| 433 | + public function testWhereColumnNoEscape(): void |
| 434 | + { |
| 435 | + $builder = $this->db->table('users'); |
| 436 | + |
| 437 | + $builder->whereColumn('LOWER(users.email)', 'normalized_email', escape: false); |
| 438 | + |
| 439 | + $expectedSQL = 'SELECT * FROM "users" WHERE LOWER(users.email) = normalized_email'; |
| 440 | + $expectedBinds = []; |
| 441 | + |
| 442 | + $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
| 443 | + $this->assertSame($expectedBinds, $builder->getBinds()); |
| 444 | + } |
| 445 | + |
| 446 | + public function testWhereColumnTreatsSecondArgumentAsColumnName(): void |
| 447 | + { |
| 448 | + $builder = $this->db->table('users'); |
| 449 | + |
| 450 | + $builder->whereColumn('created_at', 'like'); |
| 451 | + |
| 452 | + $expectedSQL = 'SELECT * FROM "users" WHERE "created_at" = "like"'; |
| 453 | + $expectedBinds = []; |
| 454 | + |
| 455 | + $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
| 456 | + $this->assertSame($expectedBinds, $builder->getBinds()); |
| 457 | + } |
| 458 | + |
| 459 | + public function testWhereColumnIgnoresOperatorsInsideFirstArgument(): void |
| 460 | + { |
| 461 | + $builder = $this->db->table('users'); |
| 462 | + |
| 463 | + $builder->whereColumn("JSON_EXTRACT(data, '$.a>b')", 'updated_at', escape: false); |
| 464 | + |
| 465 | + $expectedSQL = 'SELECT * FROM "users" WHERE JSON_EXTRACT(data, \'$.a>b\') = updated_at'; |
| 466 | + $expectedBinds = []; |
| 467 | + |
| 468 | + $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
| 469 | + $this->assertSame($expectedBinds, $builder->getBinds()); |
| 470 | + } |
| 471 | + |
| 472 | + #[DataProvider('provideWhereColumnInvalidColumnThrowInvalidArgumentException')] |
| 473 | + public function testWhereColumnInvalidColumnThrowInvalidArgumentException(string $first, string $second): void |
| 474 | + { |
| 475 | + $this->expectException(InvalidArgumentException::class); |
| 476 | + |
| 477 | + $builder = $this->db->table('users'); |
| 478 | + $builder->whereColumn($first, $second); |
| 479 | + } |
| 480 | + |
| 481 | + /** |
| 482 | + * @return iterable<string, array{string, string}> |
| 483 | + */ |
| 484 | + public static function provideWhereColumnInvalidColumnThrowInvalidArgumentException(): iterable |
| 485 | + { |
| 486 | + return [ |
| 487 | + 'empty first column' => ['', 'updated_at'], |
| 488 | + 'empty second column' => ['created_at =', ''], |
| 489 | + ]; |
| 490 | + } |
| 491 | + |
354 | 492 | public function testWhereIn(): void |
355 | 493 | { |
356 | 494 | $builder = $this->db->table('jobs'); |
|
0 commit comments