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
793bb8bf
Commit
793bb8bf
authored
Apr 13, 2015
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-72: Use model class when listing databases
parent
b5732710
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
195 additions
and
13 deletions
+195
-13
Client.php
src/Client.php
+5
-4
DatabaseInfo.php
src/Model/DatabaseInfo.php
+52
-0
DatabaseInfoIterator.php
src/Model/DatabaseInfoIterator.php
+15
-0
DatabaseInfoLegacyIterator.php
src/Model/DatabaseInfoLegacyIterator.php
+73
-0
ClientFunctionalTest.php
tests/ClientFunctionalTest.php
+50
-9
No files found.
src/Client.php
View file @
793bb8bf
...
...
@@ -7,7 +7,8 @@ use MongoDB\Driver\Cursor;
use
MongoDB\Driver\Manager
;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Driver\WriteConcern
;
use
ArrayIterator
;
use
MongoDB\Model\DatabaseInfoIterator
;
use
MongoDB\Model\DatabaseInfoLegacyIterator
;
use
stdClass
;
use
UnexpectedValueException
;
...
...
@@ -54,7 +55,7 @@ class Client
* List databases.
*
* @see http://docs.mongodb.org/manual/reference/command/listDatabases/
* @return
Traversable
* @return
DatabaseInfoIterator
* @throws UnexpectedValueException if the command result is malformed
*/
public
function
listDatabases
()
...
...
@@ -73,13 +74,13 @@ class Client
$result
[
'databases'
]
);
/* Return a
Traversable
instead of an array in case listDatabases is
/* Return a
n Iterator
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
Arra
yIterator
(
$databases
);
return
new
DatabaseInfoLegac
yIterator
(
$databases
);
}
/**
...
...
src/Model/DatabaseInfo.php
0 → 100644
View file @
793bb8bf
<?php
namespace
MongoDB\Model
;
class
DatabaseInfo
{
private
$empty
;
private
$name
;
private
$sizeOnDisk
;
/**
* Constructor.
*
* @param array $info Database info
*/
public
function
__construct
(
array
$info
)
{
$this
->
name
=
(
string
)
$info
[
'name'
];
$this
->
empty
=
(
boolean
)
$info
[
'empty'
];
$this
->
sizeOnDisk
=
(
integer
)
$info
[
'sizeOnDisk'
];
}
/**
* Return the database name.
*
* @return string
*/
public
function
getName
()
{
return
$this
->
name
;
}
/**
* Return the databases size on disk (in bytes).
*
* @return integer
*/
public
function
getSizeOnDisk
()
{
return
$this
->
sizeOnDisk
;
}
/**
* Return whether the database is empty.
*
* @return boolean
*/
public
function
isEmpty
()
{
return
$this
->
empty
;
}
}
src/Model/DatabaseInfoIterator.php
0 → 100644
View file @
793bb8bf
<?php
namespace
MongoDB\Model
;
use
Iterator
;
interface
DatabaseInfoIterator
extends
Iterator
{
/**
* Return the current element as a DatabaseInfo instance.
*
* @return DatabaseInfo
*/
public
function
current
();
}
src/Model/DatabaseInfoLegacyIterator.php
0 → 100644
View file @
793bb8bf
<?php
namespace
MongoDB\Model
;
class
DatabaseInfoLegacyIterator
implements
DatabaseInfoIterator
{
private
$databases
;
private
$index
=
0
;
/**
* Constructor.
*
* @param array $databases
*/
public
function
__construct
(
array
$databases
)
{
$this
->
databases
=
$databases
;
}
/**
* Return the current element as a DatabaseInfo instance.
*
* @see DatabaseInfoIterator::current()
* @see http://php.net/iterator.current
* @return DatabaseInfo
*/
public
function
current
()
{
return
new
DatabaseInfo
(
current
(
$this
->
databases
));
}
/**
* Return the key of the current element.
*
* @see http://php.net/iterator.key
* @return integer
*/
public
function
key
()
{
return
key
(
$this
->
databases
);
}
/**
* Move forward to next element.
*
* @see http://php.net/iterator.next
*/
public
function
next
()
{
next
(
$this
->
databases
);
}
/**
* Rewind the Iterator to the first element.
*
* @see http://php.net/iterator.rewind
*/
public
function
rewind
()
{
reset
(
$this
->
databases
);
}
/**
* Checks if current position is valid.
*
* @see http://php.net/iterator.valid
* @return boolean
*/
public
function
valid
()
{
return
key
(
$this
->
databases
)
!==
null
;
}
}
tests/ClientFunctionalTest.php
View file @
793bb8bf
...
...
@@ -3,19 +3,30 @@
namespace
MongoDB\Tests
;
use
MongoDB\Client
;
use
MongoDB\Driver\Command
;
use
MongoDB\Model\DatabaseInfo
;
/**
* Functional tests for the Client class.
*/
class
ClientFunctionalTest
extends
FunctionalTestCase
{
private
$client
;
public
function
setUp
()
{
parent
::
setUp
();
$this
->
client
=
new
Client
(
$this
->
getUri
());
$this
->
client
->
dropDatabase
(
$this
->
getDatabaseName
());
}
public
function
testDropDatabase
()
{
$writeResult
=
$this
->
manager
->
executeInsert
(
$this
->
getNamespace
(),
array
(
'x'
=>
1
));
$this
->
assertEquals
(
1
,
$writeResult
->
getInsertedCount
());
$client
=
new
Client
(
$this
->
getUri
());
$commandResult
=
$client
->
dropDatabase
(
$this
->
getDatabaseName
());
$commandResult
=
$this
->
client
->
dropDatabase
(
$this
->
getDatabaseName
());
$this
->
assertCommandSucceeded
(
$commandResult
);
$this
->
assertCollectionCount
(
$this
->
getNamespace
(),
0
);
}
...
...
@@ -25,22 +36,52 @@ class ClientFunctionalTest extends FunctionalTestCase
$writeResult
=
$this
->
manager
->
executeInsert
(
$this
->
getNamespace
(),
array
(
'x'
=>
1
));
$this
->
assertEquals
(
1
,
$writeResult
->
getInsertedCount
());
$client
=
new
Client
(
$this
->
getUri
());
$databases
=
$client
->
listDatabases
();
$databases
=
$this
->
client
->
listDatabases
();
$this
->
assertInstanceOf
(
'MongoDB\Model\DatabaseInfoIterator'
,
$databases
);
foreach
(
$databases
as
$database
)
{
$this
->
assertInstanceOf
(
'MongoDB\Model\DatabaseInfo'
,
$database
);
}
$that
=
$this
;
$this
->
assertDatabaseExists
(
$this
->
getDatabaseName
(),
function
(
DatabaseInfo
$info
)
use
(
$that
)
{
$that
->
assertFalse
(
$info
->
isEmpty
());
$that
->
assertGreaterThan
(
0
,
$info
->
getSizeOnDisk
());
});
}
/**
* Asserts that a database with the given name exists on the server.
*
* An optional $callback may be provided, which should take a DatabaseInfo
* argument as its first and only parameter. If a DatabaseInfo matching
* the given name is found, it will be passed to the callback, which may
* perform additional assertions.
*
* @param callable $callback
*/
private
function
assertDatabaseExists
(
$databaseName
,
$callback
=
null
)
{
if
(
$callback
!==
null
&&
!
is_callable
(
$callback
))
{
throw
new
InvalidArgumentException
(
'$callback is not a callable'
);
}
$
this
->
assertInstanceOf
(
'Traversable'
,
$databases
);
$
databases
=
$this
->
client
->
listDatabases
(
);
$foundDatabase
=
null
;
foreach
(
$databases
as
$database
)
{
if
(
$database
[
'name'
]
===
$this
->
getDatabaseName
()
)
{
if
(
$database
->
getName
()
===
$databaseName
)
{
$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'
);
$this
->
assertNotNull
(
$foundDatabase
,
sprintf
(
'Found %s database on the server'
,
$databaseName
));
if
(
$callback
!==
null
)
{
call_user_func
(
$callback
,
$foundDatabase
);
}
}
}
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