diff --git a/src/Query/Expression/BinaryOperatorExpression.php b/src/Query/Expression/BinaryOperatorExpression.php index 13c45b32..a7cb1365 100644 --- a/src/Query/Expression/BinaryOperatorExpression.php +++ b/src/Query/Expression/BinaryOperatorExpression.php @@ -69,8 +69,8 @@ public function __construct( */ public function negate() { - $this->negated = true; - $this->negatedInt = 1; + $this->negated = !$this->negated; + $this->negatedInt = $this->negated ? 1 : 0; } /** diff --git a/tests/EndToEndTest.php b/tests/EndToEndTest.php index 3b6ddf09..1dad87e3 100644 --- a/tests/EndToEndTest.php +++ b/tests/EndToEndTest.php @@ -1262,6 +1262,42 @@ public function testNegateOperationWithOr() $this->assertSame([['count' => 9]], $query->fetchAll(\PDO::FETCH_ASSOC)); } + public function testNullEvaluation() + { + $pdo = self::getConnectionToFullDB(false); + + // case 1, where console value is null + $query = $pdo->prepare("SELECT COUNT(*) as 'count' FROM `video_game_characters` WHERE (:console IS NULL AND `console` = 'gameboy') OR NOT (:console IS NULL)"); + $query->bindValue(':console', NULL); + $query->execute(); + $this->assertSame([['count' => 1]], $query->fetchAll(\PDO::FETCH_ASSOC)); + + // case 2, where console value is not null + $query = $pdo->prepare("SELECT COUNT(*) as 'count' FROM `video_game_characters` WHERE (:console IS NULL AND `console` = 'gameboy') OR NOT (:console IS NULL)"); + $query->bindValue(':console', 'all'); + $query->execute(); + $this->assertSame([['count' => 16]], $query->fetchAll(\PDO::FETCH_ASSOC)); + } + + public function testNullWithDoubleNegativeEvaluation() + { + $pdo = self::getConnectionToFullDB(false); + + //case 1, where console value is not null + $query = $pdo->prepare("SELECT COUNT(*) as 'count' FROM `video_game_characters` WHERE (:console IS NOT NULL AND `console` = :console) OR NOT (:console IS NOT NULL)"); + $query->bindValue(':console', 'gameboy'); + $query->execute(); + + $this->assertSame([['count' => 1]], $query->fetchAll(\PDO::FETCH_ASSOC)); + + //case 1, where console value is null + $query = $pdo->prepare("SELECT COUNT(*) as 'count' FROM `video_game_characters` WHERE (:console IS NOT NULL AND `console` = :console) OR NOT (:console IS NOT NULL)"); + $query->bindValue(':console', NULL); + $query->execute(); + + $this->assertSame([['count' => 16]], $query->fetchAll(\PDO::FETCH_ASSOC)); + } + private static function getPdo(string $connection_string, bool $strict_mode = false) : \PDO { $options = $strict_mode ? [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET sql_mode="STRICT_ALL_TABLES"'] : [];