|
15 | 15 | use CodeIgniter\Database\RawSql; |
16 | 16 | use CodeIgniter\Test\CIUnitTestCase; |
17 | 17 | use CodeIgniter\Test\Mock\MockConnection; |
| 18 | +use DateTime; |
| 19 | +use Error; |
| 20 | +use ErrorException; |
18 | 21 | use stdClass; |
19 | 22 |
|
20 | 23 | /** |
@@ -460,4 +463,119 @@ public function testWhereWithLower() |
460 | 463 | $expectedSQL = 'SELECT * FROM "jobs" WHERE LOWER(jobs.name) = \'accountant\''; |
461 | 464 | $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
462 | 465 | } |
| 466 | + |
| 467 | + public function testWhereValueIsString() |
| 468 | + { |
| 469 | + $builder = $this->db->table('users'); |
| 470 | + |
| 471 | + $builder->where('id', '1'); |
| 472 | + |
| 473 | + $expectedSQL = <<<'SQL' |
| 474 | + SELECT * FROM "users" WHERE "id" = '1' |
| 475 | + SQL; |
| 476 | + $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
| 477 | + } |
| 478 | + |
| 479 | + public function testWhereValueIsFloat() |
| 480 | + { |
| 481 | + $builder = $this->db->table('users'); |
| 482 | + |
| 483 | + $builder->where('id', 1.234); |
| 484 | + |
| 485 | + $expectedSQL = <<<'SQL' |
| 486 | + SELECT * FROM "users" WHERE "id" = 1.234 |
| 487 | + SQL; |
| 488 | + $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
| 489 | + } |
| 490 | + |
| 491 | + public function testWhereValueIsTrue() |
| 492 | + { |
| 493 | + $builder = $this->db->table('users'); |
| 494 | + |
| 495 | + $builder->where('id', true); |
| 496 | + |
| 497 | + $expectedSQL = 'SELECT * FROM "users" WHERE "id" = 1'; |
| 498 | + $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
| 499 | + } |
| 500 | + |
| 501 | + public function testWhereValueIsFalse() |
| 502 | + { |
| 503 | + $builder = $this->db->table('users'); |
| 504 | + |
| 505 | + $builder->where('id', false); |
| 506 | + |
| 507 | + $expectedSQL = 'SELECT * FROM "users" WHERE "id" = 0'; |
| 508 | + $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
| 509 | + } |
| 510 | + |
| 511 | + public function testWhereValueIsArray() |
| 512 | + { |
| 513 | + $builder = $this->db->table('users'); |
| 514 | + |
| 515 | + $builder->where('id', ['a', 'b']); |
| 516 | + |
| 517 | + // SQL syntax error |
| 518 | + $expectedSQL = <<<'SQL' |
| 519 | + SELECT * FROM "users" WHERE "id" = ('a','b') |
| 520 | + SQL; |
| 521 | + $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
| 522 | + } |
| 523 | + |
| 524 | + public function testWhereValueIsArrayOfArray() |
| 525 | + { |
| 526 | + $this->expectException(ErrorException::class); |
| 527 | + $this->expectExceptionMessage('Array to string conversion'); |
| 528 | + |
| 529 | + $builder = $this->db->table('users'); |
| 530 | + |
| 531 | + $builder->where('id', [['a', 'b'], ['c', 'd']]); |
| 532 | + |
| 533 | + $builder->getCompiledSelect(); |
| 534 | + } |
| 535 | + |
| 536 | + public function testWhereValueIsArrayOfObject() |
| 537 | + { |
| 538 | + $this->expectException(Error::class); |
| 539 | + $this->expectExceptionMessage('Object of class stdClass could not be converted to string'); |
| 540 | + |
| 541 | + $builder = $this->db->table('users'); |
| 542 | + |
| 543 | + $builder->where('id', [(object) ['a' => 'b'], (object) ['c' => 'd']]); |
| 544 | + |
| 545 | + $builder->getCompiledSelect(); |
| 546 | + } |
| 547 | + |
| 548 | + public function testWhereValueIsNull() |
| 549 | + { |
| 550 | + $builder = $this->db->table('users'); |
| 551 | + |
| 552 | + $builder->where('id', null); |
| 553 | + |
| 554 | + $expectedSQL = 'SELECT * FROM "users" WHERE "id" IS NULL'; |
| 555 | + $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
| 556 | + } |
| 557 | + |
| 558 | + public function testWhereValueIsStdClass() |
| 559 | + { |
| 560 | + $this->expectException(Error::class); |
| 561 | + $this->expectExceptionMessage('Object of class stdClass could not be converted to string'); |
| 562 | + |
| 563 | + $builder = $this->db->table('users'); |
| 564 | + |
| 565 | + $builder->where('id', (object) ['a' => 'b']); |
| 566 | + |
| 567 | + $builder->getCompiledSelect(); |
| 568 | + } |
| 569 | + |
| 570 | + public function testWhereValueIsDateTime() |
| 571 | + { |
| 572 | + $this->expectException(Error::class); |
| 573 | + $this->expectExceptionMessage('Object of class DateTime could not be converted to string'); |
| 574 | + |
| 575 | + $builder = $this->db->table('users'); |
| 576 | + |
| 577 | + $builder->where('id', new DateTime('2022-02-19 12:00')); |
| 578 | + |
| 579 | + $builder->getCompiledSelect(); |
| 580 | + } |
463 | 581 | } |
0 commit comments