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
aee5a0c6
Commit
aee5a0c6
authored
Sep 28, 2015
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-123: Do not throw when listing indexes on nonexistent collection
parent
3961ce21
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
1 deletion
+63
-1
ListIndexes.php
src/Operation/ListIndexes.php
+18
-1
ListIndexesFunctionalTest.php
tests/Operation/ListIndexesFunctionalTest.php
+45
-0
No files found.
src/Operation/ListIndexes.php
View file @
aee5a0c6
...
...
@@ -5,8 +5,10 @@ namespace MongoDB\Operation;
use
MongoDB\Driver\Command
;
use
MongoDB\Driver\Query
;
use
MongoDB\Driver\Server
;
use
MongoDB\Driver\Exception\RuntimeException
;
use
MongoDB\Model\IndexInfoIterator
;
use
MongoDB\Model\IndexInfoIteratorIterator
;
use
EmptyIterator
;
/**
* Operation for the listIndexes command.
...
...
@@ -17,6 +19,8 @@ use MongoDB\Model\IndexInfoIteratorIterator;
*/
class
ListIndexes
implements
Executable
{
private
static
$errorCodeDatabaseNotFound
=
60
;
private
static
$errorCodeNamespaceNotFound
=
26
;
private
static
$wireVersionForCommand
=
3
;
private
$databaseName
;
...
...
@@ -75,7 +79,20 @@ class ListIndexes implements Executable
$cmd
[
'maxTimeMS'
]
=
$this
->
options
[
'maxTimeMS'
];
}
try
{
$cursor
=
$server
->
executeCommand
(
$this
->
databaseName
,
new
Command
(
$cmd
));
}
catch
(
RuntimeException
$e
)
{
/* The server may return an error if the collection does not exist.
* Check for possible error codes (see: SERVER-20463) and return an
* empty iterator instead of throwing.
*/
if
(
$e
->
getCode
()
===
self
::
$errorCodeNamespaceNotFound
||
$e
->
getCode
()
===
self
::
$errorCodeDatabaseNotFound
)
{
return
new
IndexInfoIteratorIterator
(
new
EmptyIterator
);
}
throw
$e
;
}
$cursor
->
setTypeMap
(
array
(
'root'
=>
'array'
,
'document'
=>
'array'
));
return
new
IndexInfoIteratorIterator
(
$cursor
);
...
...
tests/Operation/ListIndexesFunctionalTest.php
0 → 100644
View file @
aee5a0c6
<?php
namespace
MongoDB\Tests\Operation
;
use
MongoDB\Driver\Server
;
use
MongoDB\Operation\DropCollection
;
use
MongoDB\Operation\ListIndexes
;
class
ListIndexesFunctionalTest
extends
FunctionalTestCase
{
public
function
testListIndexesForNewlyCreatedCollection
()
{
$server
=
$this
->
getPrimaryServer
();
$operation
=
new
DropCollection
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
());
$operation
->
execute
(
$server
);
$writeResult
=
$this
->
manager
->
executeInsert
(
$this
->
getNamespace
(),
[
'x'
=>
1
]);
$this
->
assertEquals
(
1
,
$writeResult
->
getInsertedCount
());
$operation
=
new
ListIndexes
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
());
// Convert the CursorInfoIterator to an array since we cannot rewind its cursor
$indexes
=
iterator_to_array
(
$operation
->
execute
(
$server
));
$this
->
assertCount
(
1
,
$indexes
);
foreach
(
$indexes
as
$index
)
{
$this
->
assertInstanceOf
(
'MongoDB\Model\IndexInfo'
,
$index
);
$this
->
assertEquals
([
'_id'
=>
1
],
$index
->
getKey
());
}
}
public
function
testListIndexesForNonexistentCollection
()
{
$server
=
$this
->
getPrimaryServer
();
$operation
=
new
DropCollection
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
());
$operation
->
execute
(
$server
);
$operation
=
new
ListIndexes
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
());
$indexes
=
$operation
->
execute
(
$server
);
$this
->
assertCount
(
0
,
$indexes
);
}
}
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