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
3db125f1
Commit
3db125f1
authored
Mar 18, 2015
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #5
parents
27543788
ea6da09f
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
683 additions
and
542 deletions
+683
-542
Client.php
src/Client.php
+14
-2
Collection.php
src/Collection.php
+596
-538
Database.php
src/Database.php
+49
-1
CollectionTest.php
tests/CollectionTest.php
+24
-1
No files found.
src/Client.php
View file @
3db125f1
...
@@ -2,9 +2,10 @@
...
@@ -2,9 +2,10 @@
namespace
MongoDB
;
namespace
MongoDB
;
use
MongoDB\Driver\Manager
;
use
MongoDB\Database
;
use
MongoDB\Collection
;
use
MongoDB\Collection
;
use
MongoDB\Database
;
use
MongoDB\Driver\Manager
;
use
MongoDB\Driver\Result
;
class
Client
class
Client
{
{
...
@@ -29,6 +30,17 @@ class Client
...
@@ -29,6 +30,17 @@ class Client
$this
->
manager
=
new
Manager
(
$uri
,
$options
,
$driverOptions
);
$this
->
manager
=
new
Manager
(
$uri
,
$options
,
$driverOptions
);
}
}
/**
* Drop a database.
*
* @param string $databaseName
* @return Result
*/
public
function
dropDatabase
(
$databaseName
)
{
// TODO
}
/**
/**
* Select a database
* Select a database
*
*
...
...
src/Collection.php
View file @
3db125f1
...
@@ -65,226 +65,39 @@ class Collection
...
@@ -65,226 +65,39 @@ class Collection
}
}
/**
/**
* Performs a find (query) on the collection
* Runs an aggregation framework pipeline
*
* NOTE: The return value of this method depends on your MongoDB server version
* @see http://docs.mongodb.org/manual/core/read-operations-introduction/
* and possibly options.
* @see Collection::getFindOptions() for supported $options
* MongoDB 2.6 (and later) will return a Cursor by default
*
* MongoDB pre 2.6 will return an ArrayIterator
* @param array $filter The find query to execute
* @param array $options Additional options
* @return Result
*/
public
function
find
(
array
$filter
=
array
(),
array
$options
=
array
())
{
$options
=
array_merge
(
$this
->
getFindOptions
(),
$options
);
$query
=
$this
->
_buildQuery
(
$filter
,
$options
);
$cursor
=
$this
->
manager
->
executeQuery
(
$this
->
ns
,
$query
,
$this
->
rp
);
return
$cursor
;
}
/**
* Performs a find (query) on the collection, returning at most one result
*
* @see http://docs.mongodb.org/manual/core/read-operations-introduction/
* @see Collection::getFindOptions() for supported $options
*
* @param array $filter The find query to execute
* @param array $options Additional options
* @return array|false The matched document, or false on failure
*/
public
function
findOne
(
array
$filter
=
array
(),
array
$options
=
array
())
{
$options
=
array_merge
(
$this
->
getFindOptions
(),
array
(
"limit"
=>
1
),
$options
);
$query
=
$this
->
_buildQuery
(
$filter
,
$options
);
$cursor
=
$this
->
manager
->
executeQuery
(
$this
->
ns
,
$query
,
$this
->
rp
);
$array
=
iterator_to_array
(
$cursor
);
if
(
$array
)
{
return
$array
[
0
];
}
return
false
;
}
/**
* Retrieves all find options with their default values.
*
*
* @return array of Collection::find() options
* @see http://docs.mongodb.org/manual/reference/command/aggregate/
*/
* @see Collection::getAggregateOptions() for supported $options
public
function
getFindOptions
()
{
return
array
(
/**
* Get partial results from a mongos if some shards are down (instead of throwing an error).
*
* @see http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-query
*/
"allowPartialResults"
=>
false
,
/**
* The number of documents to return per batch.
*
* @see http://docs.mongodb.org/manual/reference/method/cursor.batchSize/
*/
"batchSize"
=>
101
,
/**
* Attaches a comment to the query. If $comment also exists
* in the modifiers document, the comment field overwrites $comment.
*
* @see http://docs.mongodb.org/manual/reference/operator/meta/comment/
*/
"comment"
=>
""
,
/**
* Indicates the type of cursor to use. This value includes both
* the tailable and awaitData options.
* The default is Collection::CURSOR_TYPE_NON_TAILABLE.
*
* @see http://docs.mongodb.org/manual/reference/operator/meta/comment/
*/
"cursorType"
=>
self
::
CURSOR_TYPE_NON_TAILABLE
,
/**
* The maximum number of documents to return.
*
* @see http://docs.mongodb.org/manual/reference/method/cursor.limit/
*/
"limit"
=>
0
,
/**
* The maximum amount of time to allow the query to run. If $maxTimeMS also exists
* in the modifiers document, the maxTimeMS field overwrites $maxTimeMS.
*
* @see http://docs.mongodb.org/manual/reference/operator/meta/maxTimeMS/
*/
"maxTimeMS"
=>
0
,
/**
* Meta-operators modifying the output or behavior of a query.
*
* @see http://docs.mongodb.org/manual/reference/operator/query-modifier/
*/
"modifiers"
=>
array
(),
/**
* The server normally times out idle cursors after an inactivity period (10 minutes)
* to prevent excess memory use. Set this option to prevent that.
*
* @see http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-query
*/
"noCursorTimeout"
=>
false
,
/**
* Internal replication use only - driver should not set
*
* @see http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-query
* @internal
*/
"oplogReplay"
=>
false
,
/**
* Limits the fields to return for all matching documents.
*
* @see http://docs.mongodb.org/manual/tutorial/project-fields-from-query-results/
*/
"projection"
=>
array
(),
/**
* The number of documents to skip before returning.
*
* @see http://docs.mongodb.org/manual/reference/method/cursor.skip/
*/
"skip"
=>
0
,
/**
* The order in which to return matching documents. If $orderby also exists
* in the modifiers document, the sort field overwrites $orderby.
*
* @see http://docs.mongodb.org/manual/reference/method/cursor.sort/
*/
"sort"
=>
array
(),
);
}
/**
* Constructs the Query Wire Protocol field 'flags' based on $options
* provided to other helpers
*
*
* @param array $
options
* @param array $
pipeline The pipeline to execute
* @
return integer OP_QUERY Wire Protocol flag
s
* @
param array $options Additional option
s
* @
internal
* @
return Iterator
*/
*/
final
protected
function
_opQueryFlags
(
$options
)
public
function
aggregate
(
array
$pipeline
,
array
$options
=
array
()
)
{
{
$flags
=
0
;
$options
=
array_merge
(
$this
->
getAggregateOptions
(),
$options
);
$options
=
$this
->
_massageAggregateOptions
(
$options
);
$flags
|=
$options
[
"allowPartialResults"
]
?
self
::
QUERY_FLAG_PARTIAL
:
0
;
$cmd
=
array
(
$flags
|=
$options
[
"cursorType"
]
?
$options
[
"cursorType"
]
:
0
;
"aggregate"
=>
$this
->
collname
,
$flags
|=
$options
[
"oplogReplay"
]
?
self
::
QUERY_FLAG_OPLOG_REPLY
:
0
;
"pipeline"
=>
$pipeline
,
$flags
|=
$options
[
"noCursorTimeout"
]
?
self
::
QUERY_FLAG_NO_CURSOR_TIMEOUT
:
0
;
)
+
$options
;
return
$flags
;
}
/**
$result
=
$this
->
_runCommand
(
$this
->
dbname
,
$cmd
);
* Helper to build a Query object
$doc
=
$result
->
toArray
();
*
if
(
isset
(
$cmd
[
"cursor"
])
&&
$cmd
[
"cursor"
])
{
* @param array $filter the query document
return
$result
;
* @param array $options query/protocol options
}
else
{
* @return Query
if
(
$doc
[
"ok"
])
{
* @internal
return
new
\ArrayIterator
(
$doc
[
"result"
]);
*/
}
final
protected
function
_buildQuery
(
$filter
,
$options
)
{
if
(
$options
[
"comment"
])
{
$options
[
"modifiers"
][
'$comment'
]
=
$options
[
"comment"
];
}
if
(
$options
[
"maxTimeMS"
])
{
$options
[
"modifiers"
][
'$maxTimeMS'
]
=
$options
[
"maxTimeMS"
];
}
if
(
$options
[
"sort"
])
{
$options
[
'$orderby'
]
=
$options
[
"sort"
];
}
}
$flags
=
$this
->
_opQueryFlags
(
$options
);
throw
$this
->
_generateCommandException
(
$doc
);
$options
[
"cursorFlags"
]
=
$flags
;
$query
=
new
Query
(
$filter
,
$options
);
return
$query
;
}
/**
* Retrieves all Write options with their default values.
*
* @return array of available Write options
*/
public
function
getWriteOptions
()
{
return
array
(
"ordered"
=>
false
,
"upsert"
=>
false
,
"limit"
=>
1
,
);
}
/**
* Retrieves all Bulk Write options with their default values.
*
* @return array of available Bulk Write options
*/
public
function
getBulkOptions
()
{
return
array
(
"ordered"
=>
false
,
);
}
}
/**
/**
...
@@ -403,311 +216,298 @@ class Collection
...
@@ -403,311 +216,298 @@ class Collection
}
}
/**
/**
* Inserts the provided document
* Counts all documents matching $filter
* If no $filter provided, returns the numbers of documents in the collection
*
*
* @see http://docs.mongodb.org/manual/reference/command/insert/
* @see http://docs.mongodb.org/manual/reference/command/count/
* @see Collection::getCountOptions() for supported $options
*
*
* @param array $
document The document to insert
* @param array $
filter The find query to execute
* @param array $options
Additional options
* @param array $options Additional options
* @return
InsertOneResult
* @return
integer
*/
*/
public
function
insertOne
(
array
$document
)
public
function
count
(
array
$filter
=
array
(),
array
$options
=
array
()
)
{
{
$options
=
array_merge
(
$this
->
getWriteOptions
());
$cmd
=
array
(
"count"
=>
$this
->
collname
,
$bulk
=
new
BulkWrite
(
$options
[
"ordered"
]);
"query"
=>
$filter
,
$id
=
$bulk
->
insert
(
$document
);
)
+
$options
;
$wr
=
$this
->
manager
->
executeBulkWrite
(
$this
->
ns
,
$bulk
,
$this
->
wc
);
return
new
InsertOneResult
(
$wr
,
$id
);
$doc
=
$this
->
_runCommand
(
$this
->
dbname
,
$cmd
)
->
toArray
();
if
(
$doc
[
"ok"
])
{
return
$doc
[
"n"
];
}
throw
$this
->
_generateCommandException
(
$doc
);
}
}
/**
/**
* Inserts the provided documents
* Create a single index in the collection.
*
* @see http://docs.mongodb.org/manual/reference/command/insert/
*
*
* @param array $documents The documents to insert
* @see http://docs.mongodb.org/manual/reference/command/createIndexes/
* @return InsertManyResult
* @see http://docs.mongodb.org/manual/reference/method/db.collection.createIndex/
* @param array|object $keys
* @param array $options
* @return string The name of the created index
*/
*/
public
function
insertMany
(
array
$documents
)
public
function
createIndex
(
$keys
,
array
$options
=
array
()
)
{
{
$options
=
array_merge
(
$this
->
getWriteOptions
());
// TODO
$bulk
=
new
BulkWrite
(
$options
[
"ordered"
]);
$insertedIds
=
array
();
foreach
(
$documents
as
$i
=>
$document
)
{
$insertedId
=
$bulk
->
insert
(
$document
);
if
(
$insertedId
!==
null
)
{
$insertedIds
[
$i
]
=
$insertedId
;
}
}
$writeResult
=
$this
->
manager
->
executeBulkWrite
(
$this
->
ns
,
$bulk
,
$this
->
wc
);
return
new
InsertManyResult
(
$writeResult
,
$insertedIds
);
}
}
/**
/**
* Internal helper for delete one/many documents
* Create multiple indexes in the collection.
* @internal
*
* TODO: decide if $models should be an array of associative arrays, using
* createIndex()'s parameter names as keys, or tuples, using parameters in
* order (e.g. [keys, options]).
*
* @see http://docs.mongodb.org/manual/reference/command/createIndexes/
* @see http://docs.mongodb.org/manual/reference/method/db.collection.createIndex/
* @param array $models
* @return string[] The names of the created indexes
*/
*/
final
protected
function
_delete
(
$filter
,
$limit
=
1
)
public
function
createIndexes
(
array
$models
)
{
{
$options
=
array_merge
(
$this
->
getWriteOptions
(),
array
(
"limit"
=>
$limit
));
// TODO
$bulk
=
new
BulkWrite
(
$options
[
"ordered"
]);
$bulk
->
delete
(
$filter
,
$options
);
return
$this
->
manager
->
executeBulkWrite
(
$this
->
ns
,
$bulk
,
$this
->
wc
);
}
}
/**
/**
* Deletes a document matching the $filter criteria.
* Deletes a document matching the $filter criteria.
* NOTE: Will delete
at most ONE document
matching $filter
* NOTE: Will delete
ALL documents
matching $filter
*
*
* @see http://docs.mongodb.org/manual/reference/command/delete/
* @see http://docs.mongodb.org/manual/reference/command/delete/
*
*
* @param array $filter The $filter criteria to delete
* @param array $filter The $filter criteria to delete
* @return DeleteResult
* @return DeleteResult
*/
*/
public
function
delete
One
(
array
$filter
)
public
function
delete
Many
(
array
$filter
)
{
{
$wr
=
$this
->
_delete
(
$filter
);
$wr
=
$this
->
_delete
(
$filter
,
0
);
return
new
DeleteResult
(
$wr
);
return
new
DeleteResult
(
$wr
);
}
}
/**
/**
* Deletes a document matching the $filter criteria.
* Deletes a document matching the $filter criteria.
* NOTE: Will delete
ALL documents
matching $filter
* NOTE: Will delete
at most ONE document
matching $filter
*
*
* @see http://docs.mongodb.org/manual/reference/command/delete/
* @see http://docs.mongodb.org/manual/reference/command/delete/
*
*
* @param array $filter The $filter criteria to delete
* @param array $filter The $filter criteria to delete
* @return DeleteResult
* @return DeleteResult
*/
*/
public
function
delete
Many
(
array
$filter
)
public
function
delete
One
(
array
$filter
)
{
{
$wr
=
$this
->
_delete
(
$filter
,
0
);
$wr
=
$this
->
_delete
(
$filter
);
return
new
DeleteResult
(
$wr
);
return
new
DeleteResult
(
$wr
);
}
}
/**
/**
* Internal helper for replacing/updating one/many documents
* Finds the distinct values for a specified field across the collection
* @internal
*
* @see http://docs.mongodb.org/manual/reference/command/distinct/
* @see Collection::getDistinctOptions() for supported $options
*
* @param string $fieldName The fieldname to use
* @param array $filter The find query to execute
* @param array $options Additional options
* @return integer
*/
*/
p
rotected
function
_update
(
$filter
,
$update
,
$options
)
p
ublic
function
distinct
(
$fieldName
,
array
$filter
=
array
(),
array
$options
=
array
()
)
{
{
$options
=
array_merge
(
$this
->
getWriteOptions
(),
$options
);
$options
=
array_merge
(
$this
->
getDistinctOptions
(),
$options
);
$cmd
=
array
(
"distinct"
=>
$this
->
collname
,
"key"
=>
$fieldName
,
"query"
=>
$filter
,
)
+
$options
;
$bulk
=
new
BulkWrite
(
$options
[
"ordered"
]);
$doc
=
$this
->
_runCommand
(
$this
->
dbname
,
$cmd
)
->
toArray
();
$bulk
->
update
(
$filter
,
$update
,
$options
);
if
(
$doc
[
"ok"
])
{
return
$this
->
manager
->
executeBulkWrite
(
$this
->
ns
,
$bulk
,
$this
->
wc
);
return
$doc
[
"values"
];
}
throw
$this
->
_generateCommandException
(
$doc
);
}
}
/**
/**
* Replace one document
* Drop this collection.
*
* @see http://docs.mongodb.org/manual/reference/command/update/
* @see Collection::getWriteOptions() for supported $options
*
*
* @param array $filter The document to be replaced
* @return Result
* @param array $update The document to replace with
* @param array $options Additional options
* @return UpdateResult
*/
*/
public
function
replaceOne
(
array
$filter
,
array
$update
,
array
$options
=
array
()
)
public
function
drop
(
)
{
{
if
(
key
(
$update
)[
0
]
==
'$'
)
{
// TODO
throw
new
\InvalidArgumentException
(
"First key in
\$
update must NOT be a
\$
operator"
);
}
$wr
=
$this
->
_update
(
$filter
,
$update
,
$options
);
return
new
UpdateResult
(
$wr
);
}
}
/**
/**
* Update one document
* Drop a single index in the collection.
* NOTE: Will update at most ONE document matching $filter
*
* @see http://docs.mongodb.org/manual/reference/command/update/
* @see Collection::getWriteOptions() for supported $options
*
*
* @param array $filter The document to be replaced
* @see http://docs.mongodb.org/manual/reference/command/dropIndexes/
* @param array $update An array of update operators to apply to the document
* @see http://docs.mongodb.org/manual/reference/method/db.collection.dropIndex/
* @param array $options Additional options
* @param string $indexName
* @return UpdateResult
* @return Result
* @throws InvalidArgumentException if "*" is specified
*/
*/
public
function
updateOne
(
array
$filter
,
array
$update
,
array
$options
=
array
()
)
public
function
dropIndex
(
$indexName
)
{
{
if
(
key
(
$update
)[
0
]
!=
'$'
)
{
// TODO
throw
new
\InvalidArgumentException
(
"First key in
\$
update must be a
\$
operator"
);
}
$wr
=
$this
->
_update
(
$filter
,
$update
,
$options
);
return
new
UpdateResult
(
$wr
);
}
}
/**
/**
* Update one document
* Drop all indexes in the collection.
* NOTE: Will update ALL documents matching $filter
*
* @see http://docs.mongodb.org/manual/reference/command/update/
* @see Collection::getWriteOptions() for supported $options
*
*
* @param array $filter The document to be replaced
* @see http://docs.mongodb.org/manual/reference/command/dropIndexes/
* @param array $update An array of update operators to apply to the document
* @see http://docs.mongodb.org/manual/reference/method/db.collection.dropIndexes/
* @param array $options Additional options
* @return Result
* @return UpdateResult
*/
*/
public
function
updateMany
(
array
$filter
,
$update
,
array
$options
=
array
()
)
public
function
dropIndexes
(
)
{
{
$wr
=
$this
->
_update
(
$filter
,
$update
,
$options
+
array
(
"limit"
=>
0
));
// TODO
return
new
UpdateResult
(
$wr
);
}
}
/**
/**
* Counts all documents matching $filter
* Performs a find (query) on the collection
* If no $filter provided, returns the numbers of documents in the collection
*
*
* @see http://docs.mongodb.org/manual/
reference/command/count
/
* @see http://docs.mongodb.org/manual/
core/read-operations-introduction
/
* @see Collection::get
Count
Options() for supported $options
* @see Collection::get
Find
Options() for supported $options
*
*
* @param array $filter The find query to execute
* @param array $filter
The find query to execute
* @param array $options Additional options
* @param array $options
Additional options
* @return
integer
* @return
Result
*/
*/
public
function
count
(
array
$filter
=
array
(),
array
$options
=
array
())
public
function
find
(
array
$filter
=
array
(),
array
$options
=
array
())
{
{
$cmd
=
array
(
$options
=
array_merge
(
$this
->
getFindOptions
(),
$options
);
"count"
=>
$this
->
collname
,
"query"
=>
$filter
,
)
+
$options
;
$
doc
=
$this
->
_runCommand
(
$this
->
dbname
,
$cmd
)
->
toArray
(
);
$
query
=
$this
->
_buildQuery
(
$filter
,
$options
);
if
(
$doc
[
"ok"
])
{
return
$doc
[
"n"
]
;
$cursor
=
$this
->
manager
->
executeQuery
(
$this
->
ns
,
$query
,
$this
->
rp
)
;
}
throw
$this
->
_generateCommandException
(
$doc
)
;
return
$cursor
;
}
}
/**
/**
*
Retrieves all count options with their default values.
*
Performs a find (query) on the collection, returning at most one result
*
*
* @return array of Collection::count() options
* @see http://docs.mongodb.org/manual/core/read-operations-introduction/
* @see Collection::getFindOptions() for supported $options
*
* @param array $filter The find query to execute
* @param array $options Additional options
* @return array|false The matched document, or false on failure
*/
*/
public
function
getCountOptions
(
)
public
function
findOne
(
array
$filter
=
array
(),
array
$options
=
array
()
)
{
{
return
array
(
$options
=
array_merge
(
$this
->
getFindOptions
(),
array
(
"limit"
=>
1
),
$options
);
/**
* The index to use.
*
* @see http://docs.mongodb.org/manual/reference/command/count/
*/
"hint"
=>
""
,
// string or document
/**
$query
=
$this
->
_buildQuery
(
$filter
,
$options
);
* The maximum number of documents to count.
*
* @see http://docs.mongodb.org/manual/reference/command/count/
*/
"limit"
=>
0
,
/**
$cursor
=
$this
->
manager
->
executeQuery
(
$this
->
ns
,
$query
,
$this
->
rp
);
* The maximum amount of time to allow the query to run.
*
* @see http://docs.mongodb.org/manual/reference/command/count/
*/
"maxTimeMS"
=>
0
,
/**
$array
=
iterator_to_array
(
$cursor
);
* The number of documents to skip before returning the documents.
if
(
$array
)
{
*
return
$array
[
0
];
* @see http://docs.mongodb.org/manual/reference/command/count/
}
*/
"skip"
=>
0
,
return
false
;
);
}
}
/**
/**
* Finds
the distinct values for a specified field across the collection
* Finds
a single document and deletes it, returning the original.
*
*
* @see http://docs.mongodb.org/manual/reference/command/
distinct
/
* @see http://docs.mongodb.org/manual/reference/command/
findAndModify
/
* @see Collection::get
DistinctOptions
() for supported $options
* @see Collection::get
FindOneAndDelete
() for supported $options
*
*
* @param string $fieldName The fieldname to use
* @param array $filter The $filter criteria to search for
* @param array $filter The find query to execute
* @param array $options Additional options
* @param array $options Additional options
* @return array The original document
* @return integer
*/
*/
public
function
distinct
(
$fieldName
,
array
$filter
=
array
()
,
array
$options
=
array
())
public
function
findOneAndDelete
(
array
$filter
,
array
$options
=
array
())
{
{
$options
=
array_merge
(
$this
->
getDistinctOptions
(),
$options
);
$options
=
array_merge
(
$this
->
getFindOneAndDeleteOptions
(),
$options
);
$options
=
$this
->
_massageFindAndModifyOptions
(
$options
);
$cmd
=
array
(
$cmd
=
array
(
"distinct"
=>
$this
->
collname
,
"findandmodify"
=>
$this
->
collname
,
"key"
=>
$fieldName
,
"query"
=>
$filter
,
"query"
=>
$filter
,
)
+
$options
;
)
+
$options
;
$doc
=
$this
->
_runCommand
(
$this
->
dbname
,
$cmd
)
->
toArray
();
$doc
=
$this
->
_runCommand
(
$this
->
dbname
,
$cmd
)
->
toArray
();
if
(
$doc
[
"ok"
])
{
if
(
$doc
[
"ok"
])
{
return
$doc
[
"value
s
"
];
return
$doc
[
"value"
];
}
}
throw
$this
->
_generateCommandException
(
$doc
);
throw
$this
->
_generateCommandException
(
$doc
);
}
}
/**
/**
* Retrieves all distinct options with their default values.
* Finds a single document and replaces it, returning either the original or the replaced document
* By default, returns the original document.
* To return the new document set:
* $options = array("returnDocument" => Collection::FIND_ONE_AND_RETURN_AFTER);
*
*
* @return array of Collection::distinct() options
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
* @see Collection::getFindOneAndReplace() for supported $options
*
* @param array $filter The $filter criteria to search for
* @param array $replacement The document to replace with
* @param array $options Additional options
* @return array
*/
*/
public
function
getDistinctOptions
(
)
public
function
findOneAndReplace
(
array
$filter
,
array
$replacement
,
array
$options
=
array
()
)
{
{
return
array
(
if
(
key
(
$replacement
)[
0
]
==
'$'
)
{
/**
throw
new
\InvalidArgumentException
(
"First key in
\$
replacement must NOT be a
\$
operator"
);
* The maximum amount of time to allow the query to run. The default is infinite.
}
*
* @see http://docs.mongodb.org/manual/reference/command/distinct/
$options
=
array_merge
(
$this
->
getFindOneAndReplaceOptions
(),
$options
);
*/
$options
=
$this
->
_massageFindAndModifyOptions
(
$options
,
$replacement
);
"maxTimeMS"
=>
0
,
);
$cmd
=
array
(
"findandmodify"
=>
$this
->
collname
,
"query"
=>
$filter
,
)
+
$options
;
$doc
=
$this
->
_runCommand
(
$this
->
dbname
,
$cmd
)
->
toArray
();
if
(
$doc
[
"ok"
])
{
return
$doc
[
"value"
];
}
throw
$this
->
_generateCommandException
(
$doc
);
}
}
/**
/**
* Runs an aggregation framework pipeline
* Finds a single document and updates it, returning either the original or the updated document
* NOTE: The return value of this method depends on your MongoDB server version
* By default, returns the original document.
* and possibly options.
* To return the new document set:
* MongoDB 2.6 (and later) will return a Cursor by default
* $options = array("returnDocument" => Collection::FIND_ONE_AND_RETURN_AFTER);
* MongoDB pre 2.6 will return an ArrayIterator
*
*
* @see http://docs.mongodb.org/manual/reference/command/aggregate/
* @see Collection::getAggregateOptions() for supported $options
*
*
* @param array $pipeline The pipeline to execute
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
* @see Collection::getFindOneAndUpdate() for supported $options
*
* @param array $filter The $filter criteria to search for
* @param array $update An array of update operators to apply to the document
* @param array $options Additional options
* @param array $options Additional options
* @return
Iterator
* @return
array
*/
*/
public
function
aggregate
(
array
$pipelin
e
,
array
$options
=
array
())
public
function
findOneAndUpdate
(
array
$filter
,
array
$updat
e
,
array
$options
=
array
())
{
{
$options
=
array_merge
(
$this
->
getAggregateOptions
(),
$options
);
if
(
key
(
$update
)[
0
]
!=
'$'
)
{
$options
=
$this
->
_massageAggregateOptions
(
$options
);
throw
new
\InvalidArgumentException
(
"First key in
\$
update must be a
\$
operator"
);
}
$options
=
array_merge
(
$this
->
getFindOneAndUpdateOptions
(),
$options
);
$options
=
$this
->
_massageFindAndModifyOptions
(
$options
,
$update
);
$cmd
=
array
(
$cmd
=
array
(
"
aggregate
"
=>
$this
->
collname
,
"
findandmodify
"
=>
$this
->
collname
,
"
pipeline"
=>
$pipeline
,
"
query"
=>
$filter
,
)
+
$options
;
)
+
$options
;
$result
=
$this
->
_runCommand
(
$this
->
dbname
,
$cmd
);
$doc
=
$this
->
_runCommand
(
$this
->
dbname
,
$cmd
)
->
toArray
();
$doc
=
$result
->
toArray
();
if
(
$doc
[
"ok"
])
{
if
(
isset
(
$cmd
[
"cursor"
])
&&
$cmd
[
"cursor"
])
{
return
$doc
[
"value"
];
return
$result
;
}
else
{
if
(
$doc
[
"ok"
])
{
return
new
\ArrayIterator
(
$doc
[
"result"
]);
}
}
}
throw
$this
->
_generateCommandException
(
$doc
);
throw
$this
->
_generateCommandException
(
$doc
);
...
@@ -763,44 +563,90 @@ class Collection
...
@@ -763,44 +563,90 @@ class Collection
}
}
/**
/**
* Internal helper for massaging aggregate options
* Retrieves all Bulk Write options with their default values.
* @internal
*
* @return array of available Bulk Write options
*/
*/
p
rotected
function
_massageAggregateOptions
(
$options
)
p
ublic
function
getBulkOptions
(
)
{
{
if
(
$options
[
"useCursor"
])
{
return
array
(
$options
[
"cursor"
]
=
array
(
"batchSize"
=>
$options
[
"batchSize"
]);
"ordered"
=>
false
,
}
);
unset
(
$options
[
"useCursor"
],
$options
[
"batchSize"
]);
return
$options
;
}
}
/**
/**
*
Finds a single document and deletes it, returning the original.
*
Returns the CollectionName this object operates on
*
*
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
* @return string
* @see Collection::getFindOneAndDelete() for supported $options
*/
public
function
getCollectionName
()
{
return
$this
->
collname
;
}
/**
* Retrieves all count options with their default values.
*
*
* @param array $filter The $filter criteria to search for
* @return array of Collection::count() options
* @param array $options Additional options
* @return array The original document
*/
*/
public
function
findOneAndDelete
(
array
$filter
,
array
$options
=
array
()
)
public
function
getCountOptions
(
)
{
{
$options
=
array_merge
(
$this
->
getFindOneAndDeleteOptions
(),
$options
);
return
array
(
$options
=
$this
->
_massageFindAndModifyOptions
(
$options
);
/**
$cmd
=
array
(
* The index to use.
"findandmodify"
=>
$this
->
collname
,
*
"query"
=>
$filter
,
* @see http://docs.mongodb.org/manual/reference/command/count/
)
+
$options
;
*/
"hint"
=>
""
,
// string or document
$doc
=
$this
->
_runCommand
(
$this
->
dbname
,
$cmd
)
->
toArray
();
/**
if
(
$doc
[
"ok"
])
{
* The maximum number of documents to count.
return
$doc
[
"value"
];
*
}
* @see http://docs.mongodb.org/manual/reference/command/count/
*/
"limit"
=>
0
,
throw
$this
->
_generateCommandException
(
$doc
);
/**
* The maximum amount of time to allow the query to run.
*
* @see http://docs.mongodb.org/manual/reference/command/count/
*/
"maxTimeMS"
=>
0
,
/**
* The number of documents to skip before returning the documents.
*
* @see http://docs.mongodb.org/manual/reference/command/count/
*/
"skip"
=>
0
,
);
}
/**
* Returns the DatabaseName this object operates on
*
* @return string
*/
public
function
getDatabaseName
()
{
return
$this
->
dbname
;
}
/**
* Retrieves all distinct options with their default values.
*
* @return array of Collection::distinct() options
*/
public
function
getDistinctOptions
()
{
return
array
(
/**
* The maximum amount of time to allow the query to run. The default is infinite.
*
* @see http://docs.mongodb.org/manual/reference/command/distinct/
*/
"maxTimeMS"
=>
0
,
);
}
}
/**
/**
...
@@ -835,42 +681,6 @@ class Collection
...
@@ -835,42 +681,6 @@ class Collection
);
);
}
}
/**
* Finds a single document and replaces it, returning either the original or the replaced document
* By default, returns the original document.
* To return the new document set:
* $options = array("returnDocument" => Collection::FIND_ONE_AND_RETURN_AFTER);
*
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
* @see Collection::getFindOneAndReplace() for supported $options
*
* @param array $filter The $filter criteria to search for
* @param array $replacement The document to replace with
* @param array $options Additional options
* @return array
*/
public
function
findOneAndReplace
(
array
$filter
,
array
$replacement
,
array
$options
=
array
())
{
if
(
key
(
$replacement
)[
0
]
==
'$'
)
{
throw
new
\InvalidArgumentException
(
"First key in
\$
replacement must NOT be a
\$
operator"
);
}
$options
=
array_merge
(
$this
->
getFindOneAndReplaceOptions
(),
$options
);
$options
=
$this
->
_massageFindAndModifyOptions
(
$options
,
$replacement
);
$cmd
=
array
(
"findandmodify"
=>
$this
->
collname
,
"query"
=>
$filter
,
)
+
$options
;
$doc
=
$this
->
_runCommand
(
$this
->
dbname
,
$cmd
)
->
toArray
();
if
(
$doc
[
"ok"
])
{
return
$doc
[
"value"
];
}
throw
$this
->
_generateCommandException
(
$doc
);
}
/**
/**
* Retrieves all findOneAndReplace options with their default values.
* Retrieves all findOneAndReplace options with their default values.
*
*
...
@@ -919,43 +729,6 @@ class Collection
...
@@ -919,43 +729,6 @@ class Collection
);
);
}
}
/**
* Finds a single document and updates it, returning either the original or the updated document
* By default, returns the original document.
* To return the new document set:
* $options = array("returnDocument" => Collection::FIND_ONE_AND_RETURN_AFTER);
*
*
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
* @see Collection::getFindOneAndUpdate() for supported $options
*
* @param array $filter The $filter criteria to search for
* @param array $update An array of update operators to apply to the document
* @param array $options Additional options
* @return array
*/
public
function
findOneAndUpdate
(
array
$filter
,
array
$update
,
array
$options
=
array
())
{
if
(
key
(
$update
)[
0
]
!=
'$'
)
{
throw
new
\InvalidArgumentException
(
"First key in
\$
update must be a
\$
operator"
);
}
$options
=
array_merge
(
$this
->
getFindOneAndUpdateOptions
(),
$options
);
$options
=
$this
->
_massageFindAndModifyOptions
(
$options
,
$update
);
$cmd
=
array
(
"findandmodify"
=>
$this
->
collname
,
"query"
=>
$filter
,
)
+
$options
;
$doc
=
$this
->
_runCommand
(
$this
->
dbname
,
$cmd
)
->
toArray
();
if
(
$doc
[
"ok"
])
{
return
$doc
[
"value"
];
}
throw
$this
->
_generateCommandException
(
$doc
);
}
/**
/**
* Retrieves all findOneAndUpdate options with their default values.
* Retrieves all findOneAndUpdate options with their default values.
*
*
...
@@ -1004,29 +777,287 @@ class Collection
...
@@ -1004,29 +777,287 @@ class Collection
}
}
/**
/**
* Internal helper for massaging findandmodify options
* Retrieves all find options with their default values.
* @internal
*
* @return array of Collection::find() options
*/
*/
final
protected
function
_massageFindAndModifyOptions
(
$options
,
$update
=
array
()
)
public
function
getFindOptions
(
)
{
{
$ret
=
array
(
return
array
(
"sort"
=>
$options
[
"sort"
],
/**
"new"
=>
isset
(
$options
[
"returnDocument"
])
?
$options
[
"returnDocument"
]
==
self
::
FIND_ONE_AND_RETURN_AFTER
:
false
,
* Get partial results from a mongos if some shards are down (instead of throwing an error).
"fields"
=>
$options
[
"projection"
],
*
"upsert"
=>
isset
(
$options
[
"upsert"
])
?
$options
[
"upsert"
]
:
false
,
* @see http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-query
);
*/
if
(
$update
)
{
"allowPartialResults"
=>
false
,
$ret
[
"update"
]
=
$update
;
}
else
{
$ret
[
"remove"
]
=
true
;
}
return
$ret
;
}
/**
/**
* Internal helper for throwing an exception with error message
* The number of documents to return per batch.
* @internal
*
* @see http://docs.mongodb.org/manual/reference/method/cursor.batchSize/
*/
"batchSize"
=>
101
,
/**
* Attaches a comment to the query. If $comment also exists
* in the modifiers document, the comment field overwrites $comment.
*
* @see http://docs.mongodb.org/manual/reference/operator/meta/comment/
*/
"comment"
=>
""
,
/**
* Indicates the type of cursor to use. This value includes both
* the tailable and awaitData options.
* The default is Collection::CURSOR_TYPE_NON_TAILABLE.
*
* @see http://docs.mongodb.org/manual/reference/operator/meta/comment/
*/
"cursorType"
=>
self
::
CURSOR_TYPE_NON_TAILABLE
,
/**
* The maximum number of documents to return.
*
* @see http://docs.mongodb.org/manual/reference/method/cursor.limit/
*/
"limit"
=>
0
,
/**
* The maximum amount of time to allow the query to run. If $maxTimeMS also exists
* in the modifiers document, the maxTimeMS field overwrites $maxTimeMS.
*
* @see http://docs.mongodb.org/manual/reference/operator/meta/maxTimeMS/
*/
"maxTimeMS"
=>
0
,
/**
* Meta-operators modifying the output or behavior of a query.
*
* @see http://docs.mongodb.org/manual/reference/operator/query-modifier/
*/
"modifiers"
=>
array
(),
/**
* The server normally times out idle cursors after an inactivity period (10 minutes)
* to prevent excess memory use. Set this option to prevent that.
*
* @see http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-query
*/
"noCursorTimeout"
=>
false
,
/**
* Internal replication use only - driver should not set
*
* @see http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-query
* @internal
*/
"oplogReplay"
=>
false
,
/**
* Limits the fields to return for all matching documents.
*
* @see http://docs.mongodb.org/manual/tutorial/project-fields-from-query-results/
*/
"projection"
=>
array
(),
/**
* The number of documents to skip before returning.
*
* @see http://docs.mongodb.org/manual/reference/method/cursor.skip/
*/
"skip"
=>
0
,
/**
* The order in which to return matching documents. If $orderby also exists
* in the modifiers document, the sort field overwrites $orderby.
*
* @see http://docs.mongodb.org/manual/reference/method/cursor.sort/
*/
"sort"
=>
array
(),
);
}
/**
* Retrieves all Write options with their default values.
*
* @return array of available Write options
*/
public
function
getWriteOptions
()
{
return
array
(
"ordered"
=>
false
,
"upsert"
=>
false
,
"limit"
=>
1
,
);
}
/**
* Inserts the provided documents
*
* @see http://docs.mongodb.org/manual/reference/command/insert/
*
* @param array $documents The documents to insert
* @return InsertManyResult
*/
public
function
insertMany
(
array
$documents
)
{
$options
=
array_merge
(
$this
->
getWriteOptions
());
$bulk
=
new
BulkWrite
(
$options
[
"ordered"
]);
$insertedIds
=
array
();
foreach
(
$documents
as
$i
=>
$document
)
{
$insertedId
=
$bulk
->
insert
(
$document
);
if
(
$insertedId
!==
null
)
{
$insertedIds
[
$i
]
=
$insertedId
;
}
}
$writeResult
=
$this
->
manager
->
executeBulkWrite
(
$this
->
ns
,
$bulk
,
$this
->
wc
);
return
new
InsertManyResult
(
$writeResult
,
$insertedIds
);
}
/**
* Inserts the provided document
*
* @see http://docs.mongodb.org/manual/reference/command/insert/
*
* @param array $document The document to insert
* @param array $options Additional options
* @return InsertOneResult
*/
public
function
insertOne
(
array
$document
)
{
$options
=
array_merge
(
$this
->
getWriteOptions
());
$bulk
=
new
BulkWrite
(
$options
[
"ordered"
]);
$id
=
$bulk
->
insert
(
$document
);
$wr
=
$this
->
manager
->
executeBulkWrite
(
$this
->
ns
,
$bulk
,
$this
->
wc
);
return
new
InsertOneResult
(
$wr
,
$id
);
}
/**
* Returns information for all indexes in the collection.
*
* @see http://docs.mongodb.org/manual/reference/command/listIndexes/
* @see http://docs.mongodb.org/manual/reference/method/db.collection.getIndexes/
* @return Result
*/
public
function
listIndexes
()
{
// TODO
}
/**
* Replace one document
*
* @see http://docs.mongodb.org/manual/reference/command/update/
* @see Collection::getWriteOptions() for supported $options
*
* @param array $filter The document to be replaced
* @param array $update The document to replace with
* @param array $options Additional options
* @return UpdateResult
*/
public
function
replaceOne
(
array
$filter
,
array
$update
,
array
$options
=
array
())
{
if
(
key
(
$update
)[
0
]
==
'$'
)
{
throw
new
\InvalidArgumentException
(
"First key in
\$
update must NOT be a
\$
operator"
);
}
$wr
=
$this
->
_update
(
$filter
,
$update
,
$options
);
return
new
UpdateResult
(
$wr
);
}
/**
* Update one document
* NOTE: Will update ALL documents matching $filter
*
* @see http://docs.mongodb.org/manual/reference/command/update/
* @see Collection::getWriteOptions() for supported $options
*
* @param array $filter The document to be replaced
* @param array $update An array of update operators to apply to the document
* @param array $options Additional options
* @return UpdateResult
*/
public
function
updateMany
(
array
$filter
,
$update
,
array
$options
=
array
())
{
$wr
=
$this
->
_update
(
$filter
,
$update
,
$options
+
array
(
"limit"
=>
0
));
return
new
UpdateResult
(
$wr
);
}
/**
* Update one document
* NOTE: Will update at most ONE document matching $filter
*
* @see http://docs.mongodb.org/manual/reference/command/update/
* @see Collection::getWriteOptions() for supported $options
*
* @param array $filter The document to be replaced
* @param array $update An array of update operators to apply to the document
* @param array $options Additional options
* @return UpdateResult
*/
public
function
updateOne
(
array
$filter
,
array
$update
,
array
$options
=
array
())
{
if
(
key
(
$update
)[
0
]
!=
'$'
)
{
throw
new
\InvalidArgumentException
(
"First key in
\$
update must be a
\$
operator"
);
}
$wr
=
$this
->
_update
(
$filter
,
$update
,
$options
);
return
new
UpdateResult
(
$wr
);
}
/**
* Helper to build a Query object
*
* @param array $filter the query document
* @param array $options query/protocol options
* @return Query
* @internal
*/
final
protected
function
_buildQuery
(
$filter
,
$options
)
{
if
(
$options
[
"comment"
])
{
$options
[
"modifiers"
][
'$comment'
]
=
$options
[
"comment"
];
}
if
(
$options
[
"maxTimeMS"
])
{
$options
[
"modifiers"
][
'$maxTimeMS'
]
=
$options
[
"maxTimeMS"
];
}
if
(
$options
[
"sort"
])
{
$options
[
'$orderby'
]
=
$options
[
"sort"
];
}
$flags
=
$this
->
_opQueryFlags
(
$options
);
$options
[
"cursorFlags"
]
=
$flags
;
$query
=
new
Query
(
$filter
,
$options
);
return
$query
;
}
/**
* Internal helper for delete one/many documents
* @internal
*/
final
protected
function
_delete
(
$filter
,
$limit
=
1
)
{
$options
=
array_merge
(
$this
->
getWriteOptions
(),
array
(
"limit"
=>
$limit
));
$bulk
=
new
BulkWrite
(
$options
[
"ordered"
]);
$bulk
->
delete
(
$filter
,
$options
);
return
$this
->
manager
->
executeBulkWrite
(
$this
->
ns
,
$bulk
,
$this
->
wc
);
}
/**
* Internal helper for throwing an exception with error message
* @internal
*/
*/
final
protected
function
_generateCommandException
(
$doc
)
final
protected
function
_generateCommandException
(
$doc
)
{
{
...
@@ -1038,33 +1069,60 @@ class Collection
...
@@ -1038,33 +1069,60 @@ class Collection
}
}
/**
/**
* Internal helper for
running a command
* Internal helper for
massaging aggregate options
* @internal
* @internal
*/
*/
final
protected
function
_runCommand
(
$dbname
,
array
$cmd
,
ReadPreference
$rp
=
null
)
protected
function
_massageAggregateOptions
(
$options
)
{
{
//var_dump(\BSON\toJSON(\BSON\fromArray($cmd)));
if
(
$options
[
"useCursor"
])
{
$command
=
new
Command
(
$cmd
);
$options
[
"cursor"
]
=
array
(
"batchSize"
=>
$options
[
"batchSize"
]);
return
$this
->
manager
->
executeCommand
(
$dbname
,
$command
,
$rp
);
}
unset
(
$options
[
"useCursor"
],
$options
[
"batchSize"
]);
return
$options
;
}
}
/**
/**
* Returns the CollectionName this object operates on
* Constructs the Query Wire Protocol field 'flags' based on $options
* provided to other helpers
*
*
* @return string
* @param array $options
* @return integer OP_QUERY Wire Protocol flags
* @internal
*/
*/
public
function
getCollectionName
(
)
final
protected
function
_opQueryFlags
(
$options
)
{
{
return
$this
->
collname
;
$flags
=
0
;
$flags
|=
$options
[
"allowPartialResults"
]
?
self
::
QUERY_FLAG_PARTIAL
:
0
;
$flags
|=
$options
[
"cursorType"
]
?
$options
[
"cursorType"
]
:
0
;
$flags
|=
$options
[
"oplogReplay"
]
?
self
::
QUERY_FLAG_OPLOG_REPLY
:
0
;
$flags
|=
$options
[
"noCursorTimeout"
]
?
self
::
QUERY_FLAG_NO_CURSOR_TIMEOUT
:
0
;
return
$flags
;
}
}
/**
/**
* Returns the DatabaseName this object operates on
* Internal helper for running a command
*
* @internal
* @return string
*/
*/
public
function
getDatabaseName
(
)
final
protected
function
_runCommand
(
$dbname
,
array
$cmd
,
ReadPreference
$rp
=
null
)
{
{
return
$this
->
dbname
;
//var_dump(\BSON\toJSON(\BSON\fromArray($cmd)));
$command
=
new
Command
(
$cmd
);
return
$this
->
manager
->
executeCommand
(
$dbname
,
$command
,
$rp
);
}
/**
* Internal helper for replacing/updating one/many documents
* @internal
*/
protected
function
_update
(
$filter
,
$update
,
$options
)
{
$options
=
array_merge
(
$this
->
getWriteOptions
(),
$options
);
$bulk
=
new
BulkWrite
(
$options
[
"ordered"
]);
$bulk
->
update
(
$filter
,
$update
,
$options
);
return
$this
->
manager
->
executeBulkWrite
(
$this
->
ns
,
$bulk
,
$this
->
wc
);
}
}
}
}
src/Database.php
View file @
3db125f1
...
@@ -2,8 +2,9 @@
...
@@ -2,8 +2,9 @@
namespace
MongoDB
;
namespace
MongoDB
;
use
MongoDB\Driver\Manager
;
use
MongoDB\Collection
;
use
MongoDB\Collection
;
use
MongoDB\Driver\Manager
;
use
MongoDB\Driver\Result
;
class
Database
class
Database
{
{
...
@@ -32,6 +33,53 @@ class Database
...
@@ -32,6 +33,53 @@ class Database
$this
->
rp
=
$rp
;
$this
->
rp
=
$rp
;
}
}
/**
* Create a new collection explicitly.
*
* @see http://docs.mongodb.org/manual/reference/command/create/
* @see http://docs.mongodb.org/manual/reference/method/db.createCollection/
* @param string $collectionName
* @param array $options
* @return Result
*/
public
function
createCollection
(
$collectionName
,
array
$options
=
array
())
{
// TODO
}
/**
* Drop this database.
*
* @return Result
*/
public
function
drop
()
{
// TODO
}
/**
* Drop a collection within this database.
*
* @param string $collectionName
* @return Result
*/
public
function
dropCollection
(
$collectionName
)
{
// TODO
}
/**
* Returns information for all collections in this database.
*
* @see http://docs.mongodb.org/manual/reference/command/listCollections/
* @param array $options
* @return Result
*/
public
function
listCollections
(
array
$options
=
array
())
{
// TODO
}
/**
/**
* Select a specific collection in this database
* Select a specific collection in this database
*
*
...
...
tests/CollectionTest.php
View file @
3db125f1
...
@@ -6,7 +6,7 @@ use MongoDB\Driver\Manager;
...
@@ -6,7 +6,7 @@ use MongoDB\Driver\Manager;
class
CollectionTest
extends
PHPUnit_Framework_TestCase
{
class
CollectionTest
extends
PHPUnit_Framework_TestCase
{
function
setUp
()
{
function
setUp
()
{
require
__DIR__
.
"/"
.
"utils.inc"
;
require
_once
__DIR__
.
"/"
.
"utils.inc"
;
$this
->
faker
=
Faker\Factory
::
create
();
$this
->
faker
=
Faker\Factory
::
create
();
$this
->
faker
->
seed
(
1234
);
$this
->
faker
->
seed
(
1234
);
...
@@ -44,5 +44,28 @@ class CollectionTest extends PHPUnit_Framework_TestCase {
...
@@ -44,5 +44,28 @@ class CollectionTest extends PHPUnit_Framework_TestCase {
}
}
$this
->
assertEquals
(
0
,
$n
);
$this
->
assertEquals
(
0
,
$n
);
}
}
public
function
testMethodOrder
()
{
$class
=
new
ReflectionClass
(
'MongoDB\Collection'
);
$filters
=
array
(
'public'
=>
ReflectionMethod
::
IS_PUBLIC
,
'protected'
=>
ReflectionMethod
::
IS_PROTECTED
,
'private'
=>
ReflectionMethod
::
IS_PRIVATE
,
);
foreach
(
$filters
as
$visibility
=>
$filter
)
{
$methods
=
array_map
(
function
(
ReflectionMethod
$method
)
{
return
$method
->
getName
();
},
$class
->
getMethods
(
$filter
)
);
$sortedMethods
=
$methods
;
sort
(
$sortedMethods
);
$this
->
assertEquals
(
$methods
,
$sortedMethods
,
sprintf
(
'%s methods are declared alphabetically'
,
ucfirst
(
$visibility
)));
}
}
}
}
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