Skip to content

Commit 25c92ff

Browse files
authored
Merge pull request #61 from vimeo/allows-double-negative
ensures double negative is evaluated correctly
2 parents 4cb334b + 1c9d7de commit 25c92ff

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/Query/Expression/BinaryOperatorExpression.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public function __construct(
6969
*/
7070
public function negate()
7171
{
72-
$this->negated = true;
73-
$this->negatedInt = 1;
72+
$this->negated = !$this->negated;
73+
$this->negatedInt = $this->negated ? 1 : 0;
7474
}
7575

7676
/**

tests/EndToEndTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,42 @@ public function testNegateOperationWithOr()
12621262
$this->assertSame([['count' => 9]], $query->fetchAll(\PDO::FETCH_ASSOC));
12631263
}
12641264

1265+
public function testNullEvaluation()
1266+
{
1267+
$pdo = self::getConnectionToFullDB(false);
1268+
1269+
// case 1, where console value is null
1270+
$query = $pdo->prepare("SELECT COUNT(*) as 'count' FROM `video_game_characters` WHERE (:console IS NULL AND `console` = 'gameboy') OR NOT (:console IS NULL)");
1271+
$query->bindValue(':console', NULL);
1272+
$query->execute();
1273+
$this->assertSame([['count' => 1]], $query->fetchAll(\PDO::FETCH_ASSOC));
1274+
1275+
// case 2, where console value is not null
1276+
$query = $pdo->prepare("SELECT COUNT(*) as 'count' FROM `video_game_characters` WHERE (:console IS NULL AND `console` = 'gameboy') OR NOT (:console IS NULL)");
1277+
$query->bindValue(':console', 'all');
1278+
$query->execute();
1279+
$this->assertSame([['count' => 16]], $query->fetchAll(\PDO::FETCH_ASSOC));
1280+
}
1281+
1282+
public function testNullWithDoubleNegativeEvaluation()
1283+
{
1284+
$pdo = self::getConnectionToFullDB(false);
1285+
1286+
//case 1, where console value is not null
1287+
$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)");
1288+
$query->bindValue(':console', 'gameboy');
1289+
$query->execute();
1290+
1291+
$this->assertSame([['count' => 1]], $query->fetchAll(\PDO::FETCH_ASSOC));
1292+
1293+
//case 1, where console value is null
1294+
$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)");
1295+
$query->bindValue(':console', NULL);
1296+
$query->execute();
1297+
1298+
$this->assertSame([['count' => 16]], $query->fetchAll(\PDO::FETCH_ASSOC));
1299+
}
1300+
12651301
private static function getPdo(string $connection_string, bool $strict_mode = false) : \PDO
12661302
{
12671303
$options = $strict_mode ? [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET sql_mode="STRICT_ALL_TABLES"'] : [];

0 commit comments

Comments
 (0)