Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/JWadhams/JsonLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,22 @@ public static function apply($logic = [], $data = [])

$operators = [
'==' => function ($a, $b) {
// Match JavaScript's abstract equality for null: in JS `null` is
// loosely-equal only to `null`/`undefined`, never to 0, false or "".
// PHP's native `==` coerces null to those, so guard it here to keep
// rules evaluating identically to json-logic-js.
if ($a === null || $b === null) {
return $a === null && $b === null;
}
return $a == $b;
},
'===' => function ($a, $b) {
return $a === $b;
},
'!=' => function ($a, $b) {
if ($a === null || $b === null) {
return !($a === null && $b === null);
}
return $a != $b;
},
'!==' => function ($a, $b) {
Expand Down
56 changes: 56 additions & 0 deletions tests/NullLooseEqualityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

use PHPUnit\Framework\TestCase;

/**
* `==` / `!=` must match JavaScript's abstract equality for `null`, so a rule
* evaluates identically in json-logic-js (front end) and this library (back end).
*
* In JS, `null` is loosely-equal only to `null`/`undefined` — never to 0, false
* or "". PHP's native `==` coerces `null` to those, so `null == 0` wrongly
* returned true here while json-logic-js returns false.
*/
class NullLooseEqualityTest extends TestCase
{
/**
* @dataProvider nullEqualityProvider
*/
public function testNullEquality($logic, $expected)
{
$this->assertSame(
$expected,
JWadhams\JsonLogic::apply($logic),
"JsonLogic::apply(" . json_encode($logic) . ") === " . json_encode($expected)
);
}

public function nullEqualityProvider()
{
return [
// null is loosely-equal only to null (JS: null == undefined too, but
// JSON has no undefined — a missing var resolves to null).
'null == null' => [['==' => [null, null]], true],
'null == 0' => [['==' => [null, 0]], false],
'null == 0.0' => [['==' => [null, 0.0]], false],
'null == false' => [['==' => [null, false]], false],
'null == ""' => [['==' => [null, '']], false],
'null == "0"' => [['==' => [null, '0']], false],
'null == []' => [['==' => [null, []]], false],
'0 == null' => [['==' => [0, null]], false],
'false == null' => [['==' => [false, null]], false],

// != is the negation.
'null != null' => [['!=' => [null, null]], false],
'null != 0' => [['!=' => [null, 0]], true],
'null != false' => [['!=' => [null, false]], true],
'0 != null' => [['!=' => [0, null]], true],

// Non-null coercion is unchanged (matches JS already).
'"0" == 0' => [['==' => ['0', 0]], true],
'1 == 1' => [['==' => [1, 1]], true],
'0 == 0' => [['==' => [0, 0]], true],
'false == 0' => [['==' => [false, 0]], true],
'1 != 2' => [['!=' => [1, 2]], true],
];
}
}