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
a3449c6a
Commit
a3449c6a
authored
Sep 28, 2016
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #254
parents
765ff28c
b251eb26
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
75 additions
and
16 deletions
+75
-16
FunctionalTestCase.php
tests/FunctionalTestCase.php
+56
-3
BucketFunctionalTest.php
tests/GridFS/BucketFunctionalTest.php
+10
-2
FunctionalTestCase.php
tests/GridFS/FunctionalTestCase.php
+1
-1
SpecFunctionalTest.php
tests/GridFS/SpecFunctionalTest.php
+8
-10
No files found.
tests/FunctionalTestCase.php
View file @
a3449c6a
...
...
@@ -6,6 +6,8 @@ use MongoDB\Driver\Command;
use
MongoDB\Driver\Cursor
;
use
MongoDB\Driver\Manager
;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Model\BSONDocument
;
use
InvalidArgumentException
;
use
stdClass
;
use
Traversable
;
...
...
@@ -38,11 +40,18 @@ abstract class FunctionalTestCase extends TestCase
$this
->
assertEquals
(
1
,
$document
[
'ok'
]);
}
protected
function
assertSameObjectID
(
$expectedObjectID
,
$actualObjectID
)
{
$this
->
assertInstanceOf
(
'MongoDB\BSON\ObjectID'
,
$expectedObjectID
);
$this
->
assertInstanceOf
(
'MongoDB\BSON\ObjectID'
,
$actualObjectID
);
$this
->
assertEquals
((
string
)
$expectedObjectID
,
(
string
)
$actualObjectID
);
}
protected
function
assertSameDocument
(
$expectedDocument
,
$actualDocument
)
{
$this
->
assertEquals
(
is_object
(
$expectedDocument
)
?
(
array
)
$expectedDocument
:
$expectedDocument
,
is_object
(
$actualDocument
)
?
(
array
)
$actualDocument
:
$actualDocument
\MongoDB\BSON\toJSON
(
\MongoDB\BSON\fromPHP
(
$this
->
normalizeBSON
(
$expectedDocument
)))
,
\MongoDB\BSON\toJSON
(
\MongoDB\BSON\fromPHP
(
$this
->
normalizeBSON
(
$actualDocument
)))
);
}
...
...
@@ -57,7 +66,7 @@ abstract class FunctionalTestCase extends TestCase
}
$normalizeRootDocuments
=
function
(
$document
)
{
return
is_object
(
$document
)
?
(
array
)
$document
:
$document
;
return
\MongoDB\BSON\toJSON
(
\MongoDB\BSON\fromPHP
(
$this
->
normalizeBSON
(
$document
)))
;
};
$this
->
assertEquals
(
...
...
@@ -84,4 +93,48 @@ abstract class FunctionalTestCase extends TestCase
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
;
}
}
tests/GridFS/BucketFunctionalTest.php
View file @
a3449c6a
...
...
@@ -174,6 +174,7 @@ class BucketFunctionalTest extends FunctionalTestCase
$id
=
$this
->
bucket
->
uploadFromStream
(
'filename'
,
$this
->
createStream
(
$input
));
$destination
=
$this
->
createStream
();
$this
->
bucket
->
downloadToStream
(
$id
,
$destination
);
rewind
(
$destination
);
$this
->
assertStreamContents
(
$input
,
$destination
);
}
...
...
@@ -208,30 +209,37 @@ class BucketFunctionalTest extends FunctionalTestCase
$destination
=
$this
->
createStream
();
$this
->
bucket
->
downloadToStreamByName
(
'filename'
,
$destination
);
rewind
(
$destination
);
$this
->
assertStreamContents
(
'baz'
,
$destination
);
$destination
=
$this
->
createStream
();
$this
->
bucket
->
downloadToStreamByName
(
'filename'
,
$destination
,
[
'revision'
=>
-
3
]);
rewind
(
$destination
);
$this
->
assertStreamContents
(
'foo'
,
$destination
);
$destination
=
$this
->
createStream
();
$this
->
bucket
->
downloadToStreamByName
(
'filename'
,
$destination
,
[
'revision'
=>
-
2
]);
rewind
(
$destination
);
$this
->
assertStreamContents
(
'bar'
,
$destination
);
$destination
=
$this
->
createStream
();
$this
->
bucket
->
downloadToStreamByName
(
'filename'
,
$destination
,
[
'revision'
=>
-
1
]);
rewind
(
$destination
);
$this
->
assertStreamContents
(
'baz'
,
$destination
);
$destination
=
$this
->
createStream
();
$this
->
bucket
->
downloadToStreamByName
(
'filename'
,
$destination
,
[
'revision'
=>
0
]);
rewind
(
$destination
);
$this
->
assertStreamContents
(
'foo'
,
$destination
);
$destination
=
$this
->
createStream
();
$this
->
bucket
->
downloadToStreamByName
(
'filename'
,
$destination
,
[
'revision'
=>
1
]);
rewind
(
$destination
);
$this
->
assertStreamContents
(
'bar'
,
$destination
);
$destination
=
$this
->
createStream
();
$this
->
bucket
->
downloadToStreamByName
(
'filename'
,
$destination
,
[
'revision'
=>
2
]);
rewind
(
$destination
);
$this
->
assertStreamContents
(
'baz'
,
$destination
);
}
...
...
@@ -331,7 +339,7 @@ class BucketFunctionalTest extends FunctionalTestCase
$fileDocument
=
$this
->
bucket
->
getFileDocumentForStream
(
$stream
);
$this
->
assert
Equals
(
$id
,
$fileDocument
->
_id
);
$this
->
assert
SameObjectID
(
$id
,
$fileDocument
->
_id
);
$this
->
assertSame
(
'filename'
,
$fileDocument
->
filename
);
$this
->
assertSame
(
6
,
$fileDocument
->
length
);
$this
->
assertSameDocument
(
$metadata
,
$fileDocument
->
metadata
);
...
...
@@ -363,7 +371,7 @@ class BucketFunctionalTest extends FunctionalTestCase
$id
=
$this
->
bucket
->
uploadFromStream
(
'filename'
,
$this
->
createStream
(
'foobar'
));
$stream
=
$this
->
bucket
->
openDownloadStream
(
$id
);
$this
->
assert
Equals
(
$id
,
$this
->
bucket
->
getFileIdForStream
(
$stream
));
$this
->
assert
SameObjectID
(
$id
,
$this
->
bucket
->
getFileIdForStream
(
$stream
));
}
public
function
testGetFileIdForStreamWithWritableStream
()
...
...
tests/GridFS/FunctionalTestCase.php
View file @
a3449c6a
...
...
@@ -38,7 +38,7 @@ abstract class FunctionalTestCase extends BaseFunctionalTestCase
{
$this
->
assertInternalType
(
'resource'
,
$stream
);
$this
->
assertSame
(
'stream'
,
get_resource_type
(
$stream
));
$this
->
assertEquals
(
$expectedContents
,
stream_get_contents
(
$stream
,
-
1
,
.
0
));
$this
->
assertEquals
(
$expectedContents
,
stream_get_contents
(
$stream
));
}
/**
...
...
tests/GridFS/SpecFunctionalTest.php
View file @
a3449c6a
...
...
@@ -97,21 +97,19 @@ class SpecFunctionalTest extends FunctionalTestCase
foreach
(
$mi
as
$documents
)
{
list
(
$expectedDocument
,
$actualDocument
)
=
$documents
;
array_walk
(
$expectedDocument
,
function
(
&
$value
)
use
(
$actualResult
)
{
if
(
$value
===
'*result'
)
{
$value
=
$actualResult
;
foreach
(
$expectedDocument
as
$key
=>
$value
)
{
if
(
!
is_string
(
$value
)
)
{
continue
;
}
});
array_walk
(
$expectedDocument
,
function
(
&
$value
,
$key
)
use
(
$actualDocument
)
{
if
(
!
is_string
(
$value
))
{
return
;
if
(
$value
===
'*result'
)
{
$expectedDocument
[
$key
]
=
$actualResult
;
}
if
(
!
strncmp
(
$value
,
'*actual_'
,
8
))
{
$value
=
$actualDocument
[
$key
];
$expectedDocument
[
$key
]
=
$actualDocument
[
$key
];
}
}
});
$this
->
assertSameDocument
(
$expectedDocument
,
$actualDocument
);
}
...
...
@@ -154,7 +152,7 @@ class SpecFunctionalTest extends FunctionalTestCase
if
(
isset
(
$value
[
'$date'
]))
{
// TODO: This is necessary until PHPC-536 is implemented
$milliseconds
=
floor
((
new
DateTime
(
$value
[
'$date'
]))
->
format
(
'U.u'
)
*
1000
);
$value
=
new
UTCDateTime
(
$milliseconds
);
$value
=
new
UTCDateTime
(
(
int
)
$milliseconds
);
return
;
}
...
...
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