diff --git a/src/Driver/PgSQL/Statement.php b/src/Driver/PgSQL/Statement.php index 241f50c95c..abfeac939f 100644 --- a/src/Driver/PgSQL/Statement.php +++ b/src/Driver/PgSQL/Statement.php @@ -82,7 +82,12 @@ public function bindValue($param, $value, $type = ParameterType::STRING): bool } if ($type === ParameterType::BOOLEAN) { - $this->parameters[$this->parameterMap[$param]] = (bool) $value === false ? 'f' : 't'; + $booleanValue = null; + if ($value !== null) { + $booleanValue = (bool) $value === false ? 'f' : 't'; + } + + $this->parameters[$this->parameterMap[$param]] = $booleanValue; $this->parameterTypes[$this->parameterMap[$param]] = ParameterType::STRING; } else { $this->parameters[$this->parameterMap[$param]] = $value; diff --git a/tests/Functional/BooleanBindingTest.php b/tests/Functional/BooleanBindingTest.php index e078b448c5..54cbc589de 100644 --- a/tests/Functional/BooleanBindingTest.php +++ b/tests/Functional/BooleanBindingTest.php @@ -9,6 +9,9 @@ use Doctrine\DBAL\Tests\FunctionalTestCase; use Doctrine\DBAL\Tests\TestUtil; +use function assert; +use function is_bool; + class BooleanBindingTest extends FunctionalTestCase { protected function setUp(): void @@ -18,7 +21,7 @@ protected function setUp(): void } $table = new Table('boolean_test_table'); - $table->addColumn('val', 'boolean'); + $table->addColumn('val', 'boolean', ['notnull' => false]); $this->dropAndCreateTable($table); } @@ -28,7 +31,7 @@ protected function tearDown(): void } /** @dataProvider booleanProvider */ - public function testBooleanInsert(bool $input): void + public function testBooleanInsert(?bool $input): void { $queryBuilder = $this->connection->createQueryBuilder(); @@ -37,11 +40,23 @@ public function testBooleanInsert(bool $input): void ])->executeStatement(); self::assertSame(1, $result); + + $valueFromDatabase = $this->connection->createQueryBuilder() + ->select('val')->from('boolean_test_table') + ->executeQuery()->fetchOne(); + + assert($valueFromDatabase === null || is_bool($valueFromDatabase)); + + self::assertSame( + $input, + $this->connection->convertToPHPValue($valueFromDatabase, 'boolean'), + 'Must return from database the same value inserted.', + ); } - /** @return bool[][] */ + /** @return array> */ public static function booleanProvider(): array { - return [[true], [false]]; + return [[true], [false], [null]]; } }