Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
4fed85fc
Unverified
Commit
4fed85fc
authored
Aug 09, 2019
by
Andreas Braun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-462: Add compatibility layer for constraint changes
parent
d7aed71c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
38 deletions
+28
-38
DocumentsMatchConstraint.php
tests/SpecTests/DocumentsMatchConstraint.php
+28
-38
No files found.
tests/SpecTests/DocumentsMatchConstraint.php
View file @
4fed85fc
...
@@ -9,6 +9,7 @@ use ArrayObject;
...
@@ -9,6 +9,7 @@ use ArrayObject;
use
InvalidArgumentException
;
use
InvalidArgumentException
;
use
RuntimeException
;
use
RuntimeException
;
use
stdClass
;
use
stdClass
;
use
Symfony\Bridge\PhpUnit\ConstraintTrait
;
/**
/**
* Constraint that checks if one document matches another.
* Constraint that checks if one document matches another.
...
@@ -17,6 +18,8 @@ use stdClass;
...
@@ -17,6 +18,8 @@ use stdClass;
*/
*/
class
DocumentsMatchConstraint
extends
Constraint
class
DocumentsMatchConstraint
extends
Constraint
{
{
use
ConstraintTrait
;
private
$ignoreExtraKeysInRoot
=
false
;
private
$ignoreExtraKeysInRoot
=
false
;
private
$ignoreExtraKeysInEmbedded
=
false
;
private
$ignoreExtraKeysInEmbedded
=
false
;
private
$placeholders
=
[];
private
$placeholders
=
[];
...
@@ -38,50 +41,12 @@ class DocumentsMatchConstraint extends Constraint
...
@@ -38,50 +41,12 @@ class DocumentsMatchConstraint extends Constraint
*/
*/
public
function
__construct
(
$value
,
$ignoreExtraKeysInRoot
=
false
,
$ignoreExtraKeysInEmbedded
=
false
,
array
$placeholders
=
[])
public
function
__construct
(
$value
,
$ignoreExtraKeysInRoot
=
false
,
$ignoreExtraKeysInEmbedded
=
false
,
array
$placeholders
=
[])
{
{
parent
::
__construct
();
$this
->
value
=
$this
->
prepareBSON
(
$value
,
true
,
$this
->
sortKeys
);
$this
->
value
=
$this
->
prepareBSON
(
$value
,
true
,
$this
->
sortKeys
);
$this
->
ignoreExtraKeysInRoot
=
$ignoreExtraKeysInRoot
;
$this
->
ignoreExtraKeysInRoot
=
$ignoreExtraKeysInRoot
;
$this
->
ignoreExtraKeysInEmbedded
=
$ignoreExtraKeysInEmbedded
;
$this
->
ignoreExtraKeysInEmbedded
=
$ignoreExtraKeysInEmbedded
;
$this
->
placeholders
=
$placeholders
;
$this
->
placeholders
=
$placeholders
;
}
}
/**
* Returns a string representation of the constraint.
*
* @return string
*/
public
function
toString
()
{
return
'matches '
.
json_encode
(
$this
->
value
);
}
/**
* Evaluates the constraint for parameter $other. Returns true if the
* constraint is met, false otherwise.
*
* @param mixed $other
* @return boolean
*/
protected
function
matches
(
$other
)
{
/* TODO: If ignoreExtraKeys and sortKeys are both false, then we may be
* able to skip preparation, convert both documents to extended JSON,
* and compare strings.
*
* If ignoreExtraKeys is false and sortKeys is true, we still be able to
* compare JSON strings but will still require preparation to sort keys
* in all documents and sub-documents. */
$other
=
$this
->
prepareBSON
(
$other
,
true
,
$this
->
sortKeys
);
try
{
$this
->
assertEquals
(
$this
->
value
,
$other
,
$this
->
ignoreExtraKeysInRoot
);
}
catch
(
RuntimeException
$e
)
{
return
false
;
}
return
true
;
}
/**
/**
* Compares two documents recursively.
* Compares two documents recursively.
*
*
...
@@ -131,6 +96,31 @@ class DocumentsMatchConstraint extends Constraint
...
@@ -131,6 +96,31 @@ class DocumentsMatchConstraint extends Constraint
}
}
}
}
private
function
doMatches
(
$other
)
{
/* TODO: If ignoreExtraKeys and sortKeys are both false, then we may be
* able to skip preparation, convert both documents to extended JSON,
* and compare strings.
*
* If ignoreExtraKeys is false and sortKeys is true, we still be able to
* compare JSON strings but will still require preparation to sort keys
* in all documents and sub-documents. */
$other
=
$this
->
prepareBSON
(
$other
,
true
,
$this
->
sortKeys
);
try
{
$this
->
assertEquals
(
$this
->
value
,
$other
,
$this
->
ignoreExtraKeysInRoot
);
}
catch
(
RuntimeException
$e
)
{
return
false
;
}
return
true
;
}
private
function
doToString
()
{
return
'matches '
.
json_encode
(
$this
->
value
);
}
/**
/**
* Prepare a BSON document or array for comparison.
* Prepare a BSON document or array for comparison.
*
*
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment