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
82a44c5f
Commit
82a44c5f
authored
Oct 09, 2015
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-133: Support typeMap option in FindOne
parent
5f24d92f
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
103 additions
and
0 deletions
+103
-0
FindOne.php
src/Operation/FindOne.php
+15
-0
FindOneFunctionalTest.php
tests/Operation/FindOneFunctionalTest.php
+61
-0
FindOneTest.php
tests/Operation/FindOneTest.php
+22
-0
TestCase.php
tests/Operation/TestCase.php
+5
-0
No files found.
src/Operation/FindOne.php
View file @
82a44c5f
...
@@ -3,6 +3,7 @@
...
@@ -3,6 +3,7 @@
namespace
MongoDB\Operation
;
namespace
MongoDB\Operation
;
use
MongoDB\Driver\Server
;
use
MongoDB\Driver\Server
;
use
MongoDB\Exception\InvalidArgumentTypeException
;
/**
/**
* Operation for finding a single document with the find command.
* Operation for finding a single document with the find command.
...
@@ -15,6 +16,7 @@ use MongoDB\Driver\Server;
...
@@ -15,6 +16,7 @@ use MongoDB\Driver\Server;
class
FindOne
implements
Executable
class
FindOne
implements
Executable
{
{
private
$find
;
private
$find
;
private
$options
;
/**
/**
* Constructs a find command for finding a single document.
* Constructs a find command for finding a single document.
...
@@ -42,6 +44,8 @@ class FindOne implements Executable
...
@@ -42,6 +44,8 @@ class FindOne implements Executable
* "$orderby" also exists in the modifiers document, this option will
* "$orderby" also exists in the modifiers document, this option will
* take precedence.
* take precedence.
*
*
* * typeMap (array): Type map for BSON deserialization.
*
* @param string $databaseName Database name
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param string $collectionName Collection name
* @param array|object $filter Query by which to filter documents
* @param array|object $filter Query by which to filter documents
...
@@ -50,12 +54,18 @@ class FindOne implements Executable
...
@@ -50,12 +54,18 @@ class FindOne implements Executable
*/
*/
public
function
__construct
(
$databaseName
,
$collectionName
,
$filter
,
array
$options
=
array
())
public
function
__construct
(
$databaseName
,
$collectionName
,
$filter
,
array
$options
=
array
())
{
{
if
(
isset
(
$options
[
'typeMap'
])
&&
!
is_array
(
$options
[
'typeMap'
]))
{
throw
new
InvalidArgumentTypeException
(
'"typeMap" option'
,
$options
[
'typeMap'
],
'array'
);
}
$this
->
find
=
new
Find
(
$this
->
find
=
new
Find
(
$databaseName
,
$databaseName
,
$collectionName
,
$collectionName
,
$filter
,
$filter
,
array
(
'limit'
=>
-
1
)
+
$options
array
(
'limit'
=>
-
1
)
+
$options
);
);
$this
->
options
=
$options
;
}
}
/**
/**
...
@@ -68,6 +78,11 @@ class FindOne implements Executable
...
@@ -68,6 +78,11 @@ class FindOne implements Executable
public
function
execute
(
Server
$server
)
public
function
execute
(
Server
$server
)
{
{
$cursor
=
$this
->
find
->
execute
(
$server
);
$cursor
=
$this
->
find
->
execute
(
$server
);
if
(
isset
(
$this
->
options
[
'typeMap'
]))
{
$cursor
->
setTypeMap
(
$this
->
options
[
'typeMap'
]);
}
$document
=
current
(
$cursor
->
toArray
());
$document
=
current
(
$cursor
->
toArray
());
return
(
$document
===
false
)
?
null
:
$document
;
return
(
$document
===
false
)
?
null
:
$document
;
...
...
tests/Operation/FindOneFunctionalTest.php
0 → 100644
View file @
82a44c5f
<?php
namespace
MongoDB\Tests\Operation
;
use
MongoDB\Driver\BulkWrite
;
use
MongoDB\Operation\FindOne
;
class
FindOneFunctionalTest
extends
FunctionalTestCase
{
/**
* @dataProvider provideTypeMapOptionsAndExpectedDocument
*/
public
function
testTypeMapOption
(
array
$typeMap
,
$expectedDocument
)
{
$this
->
createFixtures
(
1
);
$operation
=
new
FindOne
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[],
[
'typeMap'
=>
$typeMap
]);
$document
=
$operation
->
execute
(
$this
->
getPrimaryServer
());
$this
->
assertEquals
(
$expectedDocument
,
$document
);
}
public
function
provideTypeMapOptionsAndExpectedDocument
()
{
return
[
[
[
'root'
=>
'array'
,
'document'
=>
'array'
],
[
'_id'
=>
1
,
'x'
=>
[
'foo'
=>
'bar'
]],
],
[
[
'root'
=>
'object'
,
'document'
=>
'array'
],
(
object
)
[
'_id'
=>
1
,
'x'
=>
[
'foo'
=>
'bar'
]],
],
[
[
'root'
=>
'array'
,
'document'
=>
'stdClass'
],
[
'_id'
=>
1
,
'x'
=>
(
object
)
[
'foo'
=>
'bar'
]],
],
];
}
/**
* Create data fixtures.
*
* @param integer $n
*/
private
function
createFixtures
(
$n
)
{
$bulkWrite
=
new
BulkWrite
(
true
);
for
(
$i
=
1
;
$i
<=
$n
;
$i
++
)
{
$bulkWrite
->
insert
([
'_id'
=>
$i
,
'x'
=>
(
object
)
[
'foo'
=>
'bar'
],
]);
}
$result
=
$this
->
manager
->
executeBulkWrite
(
$this
->
getNamespace
(),
$bulkWrite
);
$this
->
assertEquals
(
$n
,
$result
->
getInsertedCount
());
}
}
tests/Operation/FindOneTest.php
0 → 100644
View file @
82a44c5f
<?php
namespace
MongoDB\Tests\Operation
;
use
MongoDB\Operation\FindOne
;
class
FindOneTest
extends
TestCase
{
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidConstructorTypeMapOptions
*/
public
function
testConstructorTypeMapOption
(
$typeMap
)
{
new
FindOne
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[],
[
'typeMap'
=>
$typeMap
]);
}
public
function
provideInvalidConstructorTypeMapOptions
()
{
return
$this
->
wrapValuesForDataProvider
(
$this
->
getInvalidArrayValues
());
}
}
tests/Operation/TestCase.php
View file @
82a44c5f
...
@@ -20,6 +20,11 @@ abstract class TestCase extends BaseTestCase
...
@@ -20,6 +20,11 @@ abstract class TestCase extends BaseTestCase
return
$this
->
wrapValuesForDataProvider
(
$this
->
getInvalidBooleanValues
());
return
$this
->
wrapValuesForDataProvider
(
$this
->
getInvalidBooleanValues
());
}
}
protected
function
getInvalidArrayValues
()
{
return
array
(
123
,
3.14
,
'foo'
,
true
,
new
stdClass
);
}
protected
function
getInvalidBooleanValues
()
protected
function
getInvalidBooleanValues
()
{
{
return
array
(
123
,
3.14
,
'foo'
,
array
(),
new
stdClass
);
return
array
(
123
,
3.14
,
'foo'
,
array
(),
new
stdClass
);
...
...
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