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
6c1d6ac1
Commit
6c1d6ac1
authored
Mar 26, 2015
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-72: Database enumeration method
parent
e9a5e8a7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
0 deletions
+60
-0
Client.php
src/Client.php
+36
-0
ClientFunctionalTest.php
tests/ClientFunctionalTest.php
+24
-0
No files found.
src/Client.php
View file @
6c1d6ac1
...
...
@@ -7,6 +7,9 @@ use MongoDB\Driver\Manager;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Driver\Result
;
use
MongoDB\Driver\WriteConcern
;
use
ArrayIterator
;
use
stdClass
;
use
UnexpectedValueException
;
class
Client
{
...
...
@@ -47,6 +50,39 @@ class Client
return
$this
->
manager
->
executeCommand
(
$databaseName
,
$command
,
$readPreference
);
}
/**
* List databases.
*
* @see http://docs.mongodb.org/manual/reference/command/listDatabases/
* @return Traversable
* @throws UnexpectedValueException if the command result is malformed
*/
public
function
listDatabases
()
{
$command
=
new
Command
(
array
(
'listDatabases'
=>
1
));
$result
=
$this
->
manager
->
executeCommand
(
'admin'
,
$command
);
$result
=
iterator_to_array
(
$result
);
$result
=
current
(
$result
);
if
(
!
isset
(
$result
[
'databases'
])
||
!
is_array
(
$result
[
'databases'
]))
{
throw
new
UnexpectedValueException
(
'listDatabases command did not return a "databases" array'
);
}
$databases
=
array_map
(
function
(
stdClass
$database
)
{
return
(
array
)
$database
;
},
$result
[
'databases'
]
);
/* Return a Traversable instead of an array in case listDatabases is
* eventually changed to return a command cursor, like the collection
* and index enumeration commands. This makes the "totalSize" command
* field inaccessible, but users can manually invoke the command if they
* need that value.
*/
return
new
ArrayIterator
(
$databases
);
}
/**
* Select a database.
*
...
...
tests/ClientFunctionalTest.php
View file @
6c1d6ac1
...
...
@@ -19,4 +19,28 @@ class ClientFunctionalTest extends FunctionalTestCase
$this
->
assertCommandSucceeded
(
$commandResult
);
$this
->
assertCollectionCount
(
$this
->
getNamespace
(),
0
);
}
public
function
testListDatabases
()
{
$writeResult
=
$this
->
manager
->
executeInsert
(
$this
->
getNamespace
(),
array
(
'x'
=>
1
));
$this
->
assertEquals
(
1
,
$writeResult
->
getInsertedCount
());
$client
=
new
Client
(
$this
->
getUri
());
$databases
=
$client
->
listDatabases
();
$this
->
assertInstanceOf
(
'Traversable'
,
$databases
);
$foundDatabase
=
null
;
foreach
(
$databases
as
$database
)
{
if
(
$database
[
'name'
]
===
$this
->
getDatabaseName
())
{
$foundDatabase
=
$database
;
break
;
}
}
$this
->
assertNotNull
(
$foundDatabase
,
'Found test database in list of databases'
);
$this
->
assertFalse
(
$foundDatabase
[
'empty'
],
'Test database is not empty'
);
$this
->
assertGreaterThan
(
0
,
$foundDatabase
[
'sizeOnDisk'
],
'Test database takes up disk space'
);
}
}
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