Skip to content

Commit 4a45833

Browse files
committed
test: add tests for various types of values to where() in QueryBuilder
1 parent 91909c4 commit 4a45833

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

tests/system/Database/Builder/WhereTest.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
use CodeIgniter\Database\RawSql;
1616
use CodeIgniter\Test\CIUnitTestCase;
1717
use CodeIgniter\Test\Mock\MockConnection;
18+
use DateTime;
19+
use Error;
20+
use ErrorException;
1821
use stdClass;
1922

2023
/**
@@ -460,4 +463,119 @@ public function testWhereWithLower()
460463
$expectedSQL = 'SELECT * FROM "jobs" WHERE LOWER(jobs.name) = \'accountant\'';
461464
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
462465
}
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+
}
463581
}

0 commit comments

Comments
 (0)