Skip to content

PHPLIB-831: Allow $$exists within $$unsetOrMatches #909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2022
Merged
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
15 changes: 15 additions & 0 deletions tests/UnifiedSpecTests/Constraint/Matches.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use function is_float;
use function is_int;
use function is_object;
use function ltrim;
use function PHPUnit\Framework\assertIsBool;
use function PHPUnit\Framework\assertIsString;
use function PHPUnit\Framework\assertMatchesRegularExpression;
Expand All @@ -37,6 +38,7 @@
use function range;
use function sprintf;
use function strpos;
use function strrchr;

use const PHP_INT_SIZE;

Expand Down Expand Up @@ -240,6 +242,19 @@ private function assertMatchesOperator(BSONDocument $operator, $actual, string $
{
$name = self::getOperatorName($operator);

if ($name === '$$exists') {
assertIsBool($operator['$$exists'], '$$exists requires bool');

/* If we get to this point, the field itself must already exist so
* we need only fail if $$exists is false. */
if ($operator['$$exists'] === false) {
$key = ltrim(strrchr($keyPath, '.'), '.');
self::failAt(sprintf('$actual has unexpected key "%s"', $key), $keyPath);
}

return;
}

if ($name === '$$type') {
assertThat(
$operator['$$type'],
Expand Down
20 changes: 20 additions & 0 deletions tests/UnifiedSpecTests/Constraint/MatchesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,26 @@ public function testOperatorUnsetOrMatches(): void
$this->assertResult(false, $c, ['x' => ['y' => 1, 'z' => 2]], 'value does not match (embedded)');
}

public function testOperatorUnsetOrMatchesWithNestedOperator(): void
{
// Nested $$unsetOrMatches is redundant, but should behave the same as if it was omitted
$c = new Matches(['x' => ['$$unsetOrMatches' => ['$$unsetOrMatches' => ['y' => 1]]]]);
$this->assertResult(true, $c, new stdClass(), 'missing value is considered unset (embedded)');
$this->assertResult(false, $c, ['x' => null], 'null value is not considered unset (embedded)');
$this->assertResult(true, $c, ['x' => ['y' => 1]], 'value matches (embedded)');
$this->assertResult(false, $c, ['x' => ['y' => 1, 'z' => 2]], 'value does not match (embedded)');

$c = new Matches(['x' => ['$$unsetOrMatches' => ['$$exists' => true]]]);
$this->assertResult(true, $c, new stdClass(), 'missing value is considered unset (embedded)');
$this->assertResult(true, $c, ['x' => null], 'null value is not considered unset (embedded)');
$this->assertResult(true, $c, ['x' => ['y' => 1]], 'non-null value is not considered unset (embedded)');

$c = new Matches(['x' => ['$$unsetOrMatches' => ['$$exists' => false]]]);
$this->assertResult(true, $c, new stdClass(), 'missing value is considered unset (embedded)');
$this->assertResult(false, $c, ['x' => null], 'null value is not considered unset (embedded)');
$this->assertResult(false, $c, ['x' => ['y' => 1]], 'non-null value is not considered unset (embedded)');
}

public function testOperatorSessionLsid(): void
{
$session = $this->manager->startSession();
Expand Down