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
798d510a
Commit
798d510a
authored
Jul 17, 2018
by
Derick Rethans
Browse files
Options
Browse Files
Download
Plain Diff
Merged pull request #566
parents
f4b5a6f4
da70fdca
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
3 deletions
+40
-3
CountDocuments.php
src/Operation/CountDocuments.php
+8
-1
CountDocumentsFunctionalTest.php
tests/Operation/CountDocumentsFunctionalTest.php
+30
-0
CountFunctionalTest.php
tests/Operation/CountFunctionalTest.php
+2
-2
No files found.
src/Operation/CountDocuments.php
View file @
798d510a
...
@@ -152,8 +152,15 @@ class CountDocuments implements Executable
...
@@ -152,8 +152,15 @@ class CountDocuments implements Executable
}
}
$cursor
=
$server
->
executeReadCommand
(
$this
->
databaseName
,
new
Command
(
$this
->
createCommandDocument
()),
$this
->
createOptions
());
$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
)))
{
if
(
!
isset
(
$result
->
n
)
||
!
(
is_integer
(
$result
->
n
)
||
is_float
(
$result
->
n
)))
{
throw
new
UnexpectedValueException
(
'count command did not return a numeric "n" value'
);
throw
new
UnexpectedValueException
(
'count command did not return a numeric "n" value'
);
}
}
...
...
tests/Operation/CountDocumentsFunctionalTest.php
0 → 100644
View file @
798d510a
<?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
()));
}
}
tests/Operation/CountFunctionalTest.php
View file @
798d510a
...
@@ -55,7 +55,7 @@ class CountFunctionalTest extends FunctionalTestCase
...
@@ -55,7 +55,7 @@ class CountFunctionalTest extends FunctionalTestCase
foreach
(
$hintsUsingSparseIndex
as
$hint
)
{
foreach
(
$hintsUsingSparseIndex
as
$hint
)
{
$operation
=
new
Count
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$filter
,
[
'hint'
=>
$hint
]);
$operation
=
new
Count
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$filter
,
[
'hint'
=>
$hint
]);
$this
->
assert
Equals
(
2
,
$operation
->
execute
(
$this
->
getPrimaryServer
()));
$this
->
assert
Same
(
2
,
$operation
->
execute
(
$this
->
getPrimaryServer
()));
}
}
$hintsNotUsingSparseIndex
=
[
$hintsNotUsingSparseIndex
=
[
...
@@ -66,7 +66,7 @@ class CountFunctionalTest extends FunctionalTestCase
...
@@ -66,7 +66,7 @@ class CountFunctionalTest extends FunctionalTestCase
foreach
(
$hintsNotUsingSparseIndex
as
$hint
)
{
foreach
(
$hintsNotUsingSparseIndex
as
$hint
)
{
$operation
=
new
Count
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$filter
,
[
'hint'
=>
$hint
]);
$operation
=
new
Count
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
$filter
,
[
'hint'
=>
$hint
]);
$this
->
assert
Equals
(
3
,
$operation
->
execute
(
$this
->
getPrimaryServer
()));
$this
->
assert
Same
(
3
,
$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