-
Notifications
You must be signed in to change notification settings - Fork 266
PHPLIB-1569: Implement $$matchAsDocument and $$matchAsRoot #1508
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
Changes from all commits
d790e01
86f4e9b
329262d
b1b84ff
2be678e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
namespace MongoDB\Tests\UnifiedSpecTests\Constraint; | ||
|
||
use LogicException; | ||
use MongoDB\BSON\Document; | ||
use MongoDB\BSON\Serializable; | ||
use MongoDB\BSON\Type; | ||
use MongoDB\Model\BSONArray; | ||
|
@@ -25,10 +26,13 @@ | |
use function is_int; | ||
use function is_object; | ||
use function ltrim; | ||
use function PHPUnit\Framework\assertInstanceOf; | ||
use function PHPUnit\Framework\assertIsBool; | ||
use function PHPUnit\Framework\assertIsString; | ||
use function PHPUnit\Framework\assertJson; | ||
use function PHPUnit\Framework\assertMatchesRegularExpression; | ||
use function PHPUnit\Framework\assertNotNull; | ||
use function PHPUnit\Framework\assertStringStartsWith; | ||
use function PHPUnit\Framework\assertThat; | ||
use function PHPUnit\Framework\containsOnly; | ||
use function PHPUnit\Framework\isInstanceOf; | ||
|
@@ -39,6 +43,7 @@ | |
use function sprintf; | ||
use function str_starts_with; | ||
use function strrchr; | ||
use function trim; | ||
|
||
/** | ||
* Constraint that checks if one value matches another. | ||
|
@@ -263,6 +268,35 @@ private function assertMatchesOperator(BSONDocument $operator, $actual, string $ | |
return; | ||
} | ||
|
||
if ($name === '$$matchAsDocument') { | ||
assertInstanceOf(BSONDocument::class, $operator['$$matchAsDocument'], '$$matchAsDocument requires a BSON document'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I originally thought this has to do with the PHPLIB typeMap but forgot that it is actually due to the |
||
assertIsString($actual, '$$matchAsDocument requires actual value to be a JSON string'); | ||
assertJson($actual, '$$matchAsDocument requires actual value to be a JSON string'); | ||
|
||
/* Note: assertJson() accepts array and scalar values, but the spec | ||
* assumes that the JSON string will yield a document. */ | ||
assertStringStartsWith('{', trim($actual), '$$matchAsDocument requires actual value to be a JSON string denoting an object'); | ||
|
||
$actualDocument = Document::fromJSON($actual)->toPHP(); | ||
$constraint = new Matches($operator['$$matchAsDocument'], $this->entityMap, allowExtraRootKeys: false); | ||
|
||
if (! $constraint->evaluate($actualDocument, '', true)) { | ||
self::failAt(sprintf('%s did not match: %s', (new Exporter())->shortenedExport($actual), $constraint->additionalFailureDescription(null)), $keyPath); | ||
} | ||
|
||
return; | ||
} | ||
|
||
if ($name === '$$matchAsRoot') { | ||
$constraint = new Matches($operator['$$matchAsRoot'], $this->entityMap, allowExtraRootKeys: true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per the UTF spec,
|
||
|
||
if (! $constraint->evaluate($actual, '', true)) { | ||
self::failAt(sprintf('$actual did not match as root-level document: %s', $constraint->additionalFailureDescription(null)), $keyPath); | ||
} | ||
|
||
return; | ||
} | ||
|
||
if ($name === '$$matchesEntity') { | ||
assertNotNull($this->entityMap, '$$matchesEntity requires EntityMap'); | ||
assertIsString($operator['$$matchesEntity'], '$$matchesEntity requires string'); | ||
|
Uh oh!
There was an error while loading. Please reload this page.