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
dbc6883d
Commit
dbc6883d
authored
Sep 27, 2016
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Normalize BSON arrays and documents before assertEquals()
parent
ad08e067
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
3 deletions
+48
-3
FunctionalTestCase.php
tests/FunctionalTestCase.php
+48
-3
No files found.
tests/FunctionalTestCase.php
View file @
dbc6883d
...
@@ -6,6 +6,7 @@ use MongoDB\Driver\Command;
...
@@ -6,6 +6,7 @@ use MongoDB\Driver\Command;
use
MongoDB\Driver\Cursor
;
use
MongoDB\Driver\Cursor
;
use
MongoDB\Driver\Manager
;
use
MongoDB\Driver\Manager
;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Model\BSONDocument
;
use
InvalidArgumentException
;
use
InvalidArgumentException
;
use
stdClass
;
use
stdClass
;
use
Traversable
;
use
Traversable
;
...
@@ -42,8 +43,8 @@ abstract class FunctionalTestCase extends TestCase
...
@@ -42,8 +43,8 @@ abstract class FunctionalTestCase extends TestCase
protected
function
assertSameDocument
(
$expectedDocument
,
$actualDocument
)
protected
function
assertSameDocument
(
$expectedDocument
,
$actualDocument
)
{
{
$this
->
assertEquals
(
$this
->
assertEquals
(
is_object
(
$expectedDocument
)
?
(
array
)
$expectedDocument
:
$expectedDocument
,
$this
->
normalizeBSON
(
$expectedDocument
)
,
is_object
(
$actualDocument
)
?
(
array
)
$actualDocument
:
$actualDocument
$this
->
normalizeBSON
(
$actualDocument
)
);
);
}
}
...
@@ -58,7 +59,7 @@ abstract class FunctionalTestCase extends TestCase
...
@@ -58,7 +59,7 @@ abstract class FunctionalTestCase extends TestCase
}
}
$normalizeRootDocuments
=
function
(
$document
)
{
$normalizeRootDocuments
=
function
(
$document
)
{
return
is_object
(
$document
)
?
(
array
)
$document
:
$document
;
return
$this
->
normalizeBSON
(
$document
)
;
};
};
$this
->
assertEquals
(
$this
->
assertEquals
(
...
@@ -85,4 +86,48 @@ abstract class FunctionalTestCase extends TestCase
...
@@ -85,4 +86,48 @@ abstract class FunctionalTestCase extends TestCase
return
$document
[
'version'
];
return
$document
[
'version'
];
}
}
/**
* Normalizes a BSON document or array for use with assertEquals().
*
* The argument will be converted to a BSONArray or BSONDocument based on
* its type and keys. Document fields will be sorted alphabetically. Each
* value within the array or document will then be normalized recursively.
*
* @param array|object $bson
* @return BSONDocument|BSONArray
* @throws InvalidArgumentException if $bson is not an array or object
*/
private
function
normalizeBSON
(
$bson
)
{
if
(
!
is_array
(
$bson
)
&&
!
is_object
(
$bson
))
{
throw
new
InvalidArgumentException
(
'$bson is not an array or object'
);
}
if
(
$bson
instanceof
BSONArray
||
(
is_array
(
$bson
)
&&
$bson
===
array_values
(
$bson
)))
{
if
(
!
$bson
instanceof
BSONArray
)
{
$bson
=
new
BSONArray
(
$bson
);
}
}
else
{
if
(
!
$bson
instanceof
BSONDocument
)
{
$bson
=
new
BSONDocument
((
array
)
$bson
);
}
$bson
->
ksort
();
}
foreach
(
$bson
as
$key
=>
$value
)
{
if
(
$value
instanceof
BSONArray
||
(
is_array
(
$value
)
&&
$value
===
array_values
(
$value
)))
{
$bson
[
$key
]
=
$this
->
normalizeBSON
(
$value
);
continue
;
}
if
(
$value
instanceof
stdClass
||
$value
instanceof
BSONDocument
||
is_array
(
$value
))
{
$bson
[
$key
]
=
$this
->
normalizeBSON
(
$value
);
continue
;
}
}
return
$bson
;
}
}
}
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