From 052717b3d2069474ca20eb659e2532a947337577 Mon Sep 17 00:00:00 2001 From: Hedy Tang Date: Wed, 13 Aug 2025 15:46:27 -0400 Subject: [PATCH] fix the negation operations in fake sql --- .../Expression/BinaryOperatorEvaluator.php | 18 ++++++++--------- tests/EndToEndTest.php | 20 +++++++++++++++++++ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/Processor/Expression/BinaryOperatorEvaluator.php b/src/Processor/Expression/BinaryOperatorEvaluator.php index 3770a951..f8fc9a19 100644 --- a/src/Processor/Expression/BinaryOperatorEvaluator.php +++ b/src/Processor/Expression/BinaryOperatorEvaluator.php @@ -152,7 +152,7 @@ public static function evaluate( return !$expr->negatedInt; } - return $l_value == $r_value ? 1 : 0 ^ $expr->negatedInt; + return ($l_value == $r_value ? 1 : 0 ) ^ $expr->negatedInt; case '<>': case '!=': @@ -165,35 +165,35 @@ public static function evaluate( return $expr->negatedInt; } - return $l_value != $r_value ? 1 : 0 ^ $expr->negatedInt; + return ($l_value != $r_value ? 1 : 0) ^ $expr->negatedInt; case '>': if ($as_string) { return (string) $l_value > (string) $r_value ? 1 : 0 ^ $expr->negatedInt; } - return (float) $l_value > (float) $r_value ? 1 : 0 ^ $expr->negatedInt; + return ((float) $l_value > (float) $r_value ? 1 : 0 ) ^ $expr->negatedInt; // no break case '>=': if ($as_string) { - return (string) $l_value >= (string) $r_value ? 1 : 0 ^ $expr->negatedInt; + return ((string) $l_value >= (string) $r_value ? 1 : 0) ^ $expr->negatedInt; } - return (float) $l_value >= (float) $r_value ? 1 : 0 ^ $expr->negatedInt; + return ((float) $l_value >= (float) $r_value ? 1 : 0) ^ $expr->negatedInt; case '<': if ($as_string) { - return (string) $l_value < (string) $r_value ? 1 : 0 ^ $expr->negatedInt; + return ((string) $l_value < (string) $r_value ? 1 : 0) ^ $expr->negatedInt; } - return (float) $l_value < (float) $r_value ? 1 : 0 ^ $expr->negatedInt; + return ((float) $l_value < (float) $r_value ? 1 : 0) ^ $expr->negatedInt; case '<=': if ($as_string) { - return (string) $l_value <= (string) $r_value ? 1 : 0 ^ $expr->negatedInt; + return ((string) $l_value <= (string) $r_value ? 1 : 0) ^ $expr->negatedInt; } - return (float) $l_value <= (float) $r_value ? 1 : 0 ^ $expr->negatedInt; + return ((float) $l_value <= (float) $r_value ? 1 : 0) ^ $expr->negatedInt; } // PHPCS thinks there's a fallthrough here, but there provably is not diff --git a/tests/EndToEndTest.php b/tests/EndToEndTest.php index d2cd95a3..2191e511 100644 --- a/tests/EndToEndTest.php +++ b/tests/EndToEndTest.php @@ -1214,6 +1214,26 @@ public function testSelectNullableFields() $query->fetch(\PDO::FETCH_ASSOC) ); } + + public function testNegateOperation() + { + // greater than + $pdo = self::getConnectionToFullDB(false); + $query = $pdo->prepare("SELECT COUNT(*) as 'count' FROM `video_game_characters` WHERE `console` = :console AND NOT (`powerups` > :powerups)"); + $query->bindValue(':console', 'nes'); + $query->bindValue(':powerups', 3); + $query->execute(); + + $this->assertSame([['count' => 8]], $query->fetchAll(\PDO::FETCH_ASSOC)); + + // equals + $query = $pdo->prepare("SELECT COUNT(*) as 'count' FROM `video_game_characters` WHERE `console` = :console AND NOT (`powerups` = :powerups)"); + $query->bindValue(':console', 'nes'); + $query->bindValue(':powerups', 0); + $query->execute(); + + $this->assertSame([['count' => 2]], $query->fetchAll(\PDO::FETCH_ASSOC)); + } private static function getPdo(string $connection_string, bool $strict_mode = false) : \PDO {