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
3130ae38
Commit
3130ae38
authored
Oct 05, 2015
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #34
parents
3961ce21
14667e36
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
114 additions
and
2 deletions
+114
-2
DropCollection.php
src/Operation/DropCollection.php
+6
-1
ListIndexes.php
src/Operation/ListIndexes.php
+18
-1
ListCollectionsFunctionalTest.php
tests/Operation/ListCollectionsFunctionalTest.php
+45
-0
ListIndexesFunctionalTest.php
tests/Operation/ListIndexesFunctionalTest.php
+45
-0
No files found.
src/Operation/DropCollection.php
View file @
3130ae38
...
...
@@ -17,6 +17,7 @@ use MongoDB\Exception\RuntimeException;
*/
class
DropCollection
implements
Executable
{
private
static
$errorMessageNamespaceNotFound
=
'ns not found'
;
private
$databaseName
;
private
$collectionName
;
...
...
@@ -44,7 +45,11 @@ class DropCollection implements Executable
try
{
$cursor
=
$server
->
executeCommand
(
$this
->
databaseName
,
new
Command
(
array
(
'drop'
=>
$this
->
collectionName
)));
}
catch
(
DriverRuntimeException
$e
)
{
if
(
$e
->
getMessage
()
===
'ns not found'
)
{
/* The server may return an error if the collection does not exist.
* Check for an error message (unfortunately, there isn't a code)
* and NOP instead of throwing.
*/
if
(
$e
->
getMessage
()
===
self
::
$errorMessageNamespaceNotFound
)
{
return
(
object
)
[
'ok'
=>
0
,
'errmsg'
=>
'ns not found'
];
}
...
...
src/Operation/ListIndexes.php
View file @
3130ae38
...
...
@@ -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/ListCollectionsFunctionalTest.php
0 → 100644
View file @
3130ae38
<?php
namespace
MongoDB\Tests\Operation
;
use
MongoDB\Driver\Server
;
use
MongoDB\Operation\DropDatabase
;
use
MongoDB\Operation\ListCollections
;
class
ListCollectionsFunctionalTest
extends
FunctionalTestCase
{
public
function
testListCollectionsForNewlyCreatedDatabase
()
{
$server
=
$this
->
getPrimaryServer
();
$operation
=
new
DropDatabase
(
$this
->
getDatabaseName
());
$operation
->
execute
(
$server
);
$writeResult
=
$this
->
manager
->
executeInsert
(
$this
->
getNamespace
(),
[
'x'
=>
1
]);
$this
->
assertEquals
(
1
,
$writeResult
->
getInsertedCount
());
$operation
=
new
ListCollections
(
$this
->
getDatabaseName
(),
[
'filter'
=>
[
'name'
=>
$this
->
getCollectionName
()]]);
// Convert the CollectionInfoIterator to an array since we cannot rewind its cursor
$collections
=
iterator_to_array
(
$operation
->
execute
(
$server
));
$this
->
assertCount
(
1
,
$collections
);
foreach
(
$collections
as
$collection
)
{
$this
->
assertInstanceOf
(
'MongoDB\Model\CollectionInfo'
,
$collection
);
$this
->
assertEquals
(
$this
->
getCollectionName
(),
$collection
->
getName
());
}
}
public
function
testListCollectionsForNonexistentDatabase
()
{
$server
=
$this
->
getPrimaryServer
();
$operation
=
new
DropDatabase
(
$this
->
getDatabaseName
());
$operation
->
execute
(
$server
);
$operation
=
new
ListCollections
(
$this
->
getDatabaseName
());
$collections
=
$operation
->
execute
(
$server
);
$this
->
assertCount
(
0
,
$collections
);
}
}
tests/Operation/ListIndexesFunctionalTest.php
0 → 100644
View file @
3130ae38
<?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