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
76e995bc
Commit
76e995bc
authored
Jul 16, 2018
by
Derick Rethans
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-376: countDocuments() throws an exception if no documents are found
parent
f4b5a6f4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
1 deletion
+38
-1
CountDocuments.php
src/Operation/CountDocuments.php
+8
-1
CountDocumentsFunctionalTest.php
tests/Operation/CountDocumentsFunctionalTest.php
+30
-0
No files found.
src/Operation/CountDocuments.php
View file @
76e995bc
...
...
@@ -152,8 +152,15 @@ class CountDocuments implements Executable
}
$cursor
=
$server
->
executeReadCommand
(
$this
->
databaseName
,
new
Command
(
$this
->
createCommandDocument
()),
$this
->
createOptions
());
$
result
=
current
(
$cursor
->
toArray
()
);
$
allResults
=
$cursor
->
toArray
(
);
/* If there are no documents to count, the aggregation pipeline has no items to group, and
* hence the result is an empty array (PHPLIB-376) */
if
(
count
(
$allResults
)
==
0
)
{
return
0
;
}
$result
=
current
(
$allResults
);
if
(
!
isset
(
$result
->
n
)
||
!
(
is_integer
(
$result
->
n
)
||
is_float
(
$result
->
n
)))
{
throw
new
UnexpectedValueException
(
'count command did not return a numeric "n" value'
);
}
...
...
tests/Operation/CountDocumentsFunctionalTest.php
0 → 100644
View file @
76e995bc
<?php
namespace
MongoDB\Tests\Operation
;
use
MongoDB\Operation\CountDocuments
;
use
MongoDB\Operation\InsertMany
;
use
stdClass
;
class
CountDocumentsFunctionalTest
extends
FunctionalTestCase
{
public
function
testEmptyCollection
()
{
$operation
=
new
CountDocuments
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[]);
$this
->
assertSame
(
0
,
$operation
->
execute
(
$this
->
getPrimaryServer
()));
}
public
function
testNonEmptyCollection
()
{
$insertMany
=
new
InsertMany
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[
[
'x'
=>
1
],
[
'x'
=>
2
],
[
'y'
=>
3
],
[
'z'
=>
4
],
]);
$insertMany
->
execute
(
$this
->
getPrimaryServer
());
$operation
=
new
CountDocuments
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[]);
$this
->
assertSame
(
4
,
$operation
->
execute
(
$this
->
getPrimaryServer
()));
}
}
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