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
980746df
Commit
980746df
authored
Apr 02, 2018
by
Katherine Walker
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-226: Implement ArrayAccess for CollectionInfo and DatabaseInfo
parent
47616117
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
173 additions
and
2 deletions
+173
-2
CollectionInfo.php
src/Model/CollectionInfo.php
+50
-1
DatabaseInfo.php
src/Model/DatabaseInfo.php
+49
-1
CollectionInfoTest.php
tests/Model/CollectionInfoTest.php
+25
-0
DatabaseInfoTest.php
tests/Model/DatabaseInfoTest.php
+25
-0
ListCollectionsFunctionalTest.php
tests/Operation/ListCollectionsFunctionalTest.php
+24
-0
No files found.
src/Model/CollectionInfo.php
View file @
980746df
...
@@ -17,6 +17,9 @@
...
@@ -17,6 +17,9 @@
namespace
MongoDB\Model
;
namespace
MongoDB\Model
;
use
MongoDB\Exception\BadMethodCallException
;
use
ArrayAccess
;
/**
/**
* Collection information model class.
* Collection information model class.
*
*
...
@@ -28,7 +31,7 @@ namespace MongoDB\Model;
...
@@ -28,7 +31,7 @@ namespace MongoDB\Model;
* @see \MongoDB\Database::listCollections()
* @see \MongoDB\Database::listCollections()
* @see https://github.com/mongodb/specifications/blob/master/source/enumerate-collections.rst
* @see https://github.com/mongodb/specifications/blob/master/source/enumerate-collections.rst
*/
*/
class
CollectionInfo
class
CollectionInfo
implements
ArrayAccess
{
{
private
$info
;
private
$info
;
...
@@ -102,4 +105,50 @@ class CollectionInfo
...
@@ -102,4 +105,50 @@ class CollectionInfo
{
{
return
!
empty
(
$this
->
info
[
'options'
][
'capped'
]);
return
!
empty
(
$this
->
info
[
'options'
][
'capped'
]);
}
}
/**
* Check whether a field exists in the collection information.
*
* @see http://php.net/arrayaccess.offsetexists
* @param mixed $key
* @return boolean
*/
public
function
offsetExists
(
$key
)
{
return
array_key_exists
(
$key
,
$this
->
info
);
}
/**
* Return the field's value from the collection information.
*
* @see http://php.net/arrayaccess.offsetget
* @param mixed $key
* @return mixed
*/
public
function
offsetGet
(
$key
)
{
return
$this
->
info
[
$key
];
}
/**
* Not supported.
*
* @see http://php.net/arrayaccess.offsetset
* @throws BadMethodCallException
*/
public
function
offsetSet
(
$key
,
$value
)
{
throw
BadMethodCallException
::
classIsImmutable
(
__CLASS__
);
}
/**
* Not supported.
*
* @see http://php.net/arrayaccess.offsetunset
* @throws BadMethodCallException
*/
public
function
offsetUnset
(
$key
)
{
throw
BadMethodCallException
::
classIsImmutable
(
__CLASS__
);
}
}
}
src/Model/DatabaseInfo.php
View file @
980746df
...
@@ -17,6 +17,8 @@
...
@@ -17,6 +17,8 @@
namespace
MongoDB\Model
;
namespace
MongoDB\Model
;
use
MongoDB\Exception\BadMethodCallException
;
use
ArrayAccess
;
/**
/**
* Database information model class.
* Database information model class.
*
*
...
@@ -27,7 +29,7 @@ namespace MongoDB\Model;
...
@@ -27,7 +29,7 @@ namespace MongoDB\Model;
* @see \MongoDB\Client::listDatabases()
* @see \MongoDB\Client::listDatabases()
* @see http://docs.mongodb.org/manual/reference/command/listDatabases/
* @see http://docs.mongodb.org/manual/reference/command/listDatabases/
*/
*/
class
DatabaseInfo
class
DatabaseInfo
implements
ArrayAccess
{
{
private
$info
;
private
$info
;
...
@@ -81,4 +83,50 @@ class DatabaseInfo
...
@@ -81,4 +83,50 @@ class DatabaseInfo
{
{
return
(
boolean
)
$this
->
info
[
'empty'
];
return
(
boolean
)
$this
->
info
[
'empty'
];
}
}
/**
* Check whether a field exists in the database information.
*
* @see http://php.net/arrayaccess.offsetexists
* @param mixed $key
* @return boolean
*/
public
function
offsetExists
(
$key
)
{
return
array_key_exists
(
$key
,
$this
->
info
);
}
/**
* Return the field's value from the database information.
*
* @see http://php.net/arrayaccess.offsetget
* @param mixed $key
* @return mixed
*/
public
function
offsetGet
(
$key
)
{
return
$this
->
info
[
$key
];
}
/**
* Not supported.
*
* @see http://php.net/arrayaccess.offsetset
* @throws BadMethodCallException
*/
public
function
offsetSet
(
$key
,
$value
)
{
throw
BadMethodCallException
::
classIsImmutable
(
__CLASS__
);
}
/**
* Not supported.
*
* @see http://php.net/arrayaccess.offsetunset
* @throws BadMethodCallException
*/
public
function
offsetUnset
(
$key
)
{
throw
BadMethodCallException
::
classIsImmutable
(
__CLASS__
);
}
}
}
tests/Model/CollectionInfoTest.php
View file @
980746df
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
namespace
MongoDB\Tests\Model
;
namespace
MongoDB\Tests\Model
;
use
MongoDB\Exception\BadMethodCallException
;
use
MongoDB\Model\CollectionInfo
;
use
MongoDB\Model\CollectionInfo
;
use
MongoDB\Tests\TestCase
;
use
MongoDB\Tests\TestCase
;
...
@@ -50,4 +51,28 @@ class CollectionInfoTest extends TestCase
...
@@ -50,4 +51,28 @@ class CollectionInfoTest extends TestCase
$info
=
new
CollectionInfo
(
$expectedInfo
);
$info
=
new
CollectionInfo
(
$expectedInfo
);
$this
->
assertSame
(
$expectedInfo
,
$info
->
__debugInfo
());
$this
->
assertSame
(
$expectedInfo
,
$info
->
__debugInfo
());
}
}
public
function
testImplementsArrayAccess
()
{
$info
=
new
CollectionInfo
([
'name'
=>
'foo'
]);
$this
->
assertInstanceOf
(
'ArrayAccess'
,
$info
);
$this
->
assertArrayHasKey
(
'name'
,
$info
);
$this
->
assertSame
(
'foo'
,
$info
[
'name'
]);
}
public
function
testOffsetSetCannotBeCalled
()
{
$info
=
new
CollectionInfo
([
'name'
=>
'foo'
,
'options'
=>
[
'capped'
=>
true
,
'size'
=>
1048576
]]);
$this
->
expectException
(
BadMethodCallException
::
class
);
$this
->
expectExceptionMessage
(
'MongoDB\Model\CollectionInfo is immutable'
);
$info
[
'options'
]
=
[
'capped'
=>
false
];
}
public
function
testOffsetUnsetCannotBeCalled
()
{
$info
=
new
CollectionInfo
([
'name'
=>
'foo'
,
'options'
=>
[
'capped'
=>
true
,
'size'
=>
1048576
]]);
$this
->
expectException
(
BadMethodCallException
::
class
);
$this
->
expectExceptionMessage
(
'MongoDB\Model\CollectionInfo is immutable'
);
unset
(
$info
[
'options'
]);
}
}
}
tests/Model/DatabaseInfoTest.php
View file @
980746df
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
namespace
MongoDB\Tests\Model
;
namespace
MongoDB\Tests\Model
;
use
MongoDB\Exception\BadMethodCallException
;
use
MongoDB\Model\DatabaseInfo
;
use
MongoDB\Model\DatabaseInfo
;
use
MongoDB\Tests\TestCase
;
use
MongoDB\Tests\TestCase
;
...
@@ -39,4 +40,28 @@ class DatabaseInfoTest extends TestCase
...
@@ -39,4 +40,28 @@ class DatabaseInfoTest extends TestCase
$info
=
new
DatabaseInfo
(
$expectedInfo
);
$info
=
new
DatabaseInfo
(
$expectedInfo
);
$this
->
assertSame
(
$expectedInfo
,
$info
->
__debugInfo
());
$this
->
assertSame
(
$expectedInfo
,
$info
->
__debugInfo
());
}
}
public
function
testImplementsArrayAccess
()
{
$info
=
new
DatabaseInfo
([
'name'
=>
'foo'
]);
$this
->
assertInstanceOf
(
'ArrayAccess'
,
$info
);
$this
->
assertArrayHasKey
(
'name'
,
$info
);
$this
->
assertSame
(
'foo'
,
$info
[
'name'
]);
}
public
function
testOffsetSetCannotBeCalled
()
{
$info
=
new
DatabaseInfo
([
'name'
=>
'foo'
,
'sizeOnDisk'
=>
1048576
,
'empty'
=>
false
]);
$this
->
expectException
(
BadMethodCallException
::
class
);
$this
->
expectExceptionMessage
(
'MongoDB\Model\DatabaseInfo is immutable'
);
$info
[
'empty'
]
=
true
;
}
public
function
testOffsetUnsetCannotBeCalled
()
{
$info
=
new
DatabaseInfo
([
'name'
=>
'foo'
,
'sizeOnDisk'
=>
1048576
,
'empty'
=>
false
]);
$this
->
expectException
(
BadMethodCallException
::
class
);
$this
->
expectExceptionMessage
(
'MongoDB\Model\DatabaseInfo is immutable'
);
unset
(
$info
[
'empty'
]);
}
}
}
tests/Operation/ListCollectionsFunctionalTest.php
View file @
980746df
...
@@ -34,6 +34,30 @@ class ListCollectionsFunctionalTest extends FunctionalTestCase
...
@@ -34,6 +34,30 @@ class ListCollectionsFunctionalTest extends FunctionalTestCase
}
}
}
}
public
function
testIdIndexAndInfo
()
{
if
(
version_compare
(
$this
->
getServerVersion
(),
'3.4.0'
,
'<'
))
{
$this
->
markTestSkipped
(
'idIndex and info are not supported'
);
}
$server
=
$this
->
getPrimaryServer
();
$insertOne
=
new
InsertOne
(
$this
->
getDatabaseName
(),
$this
->
getCollectionName
(),
[
'x'
=>
1
]);
$writeResult
=
$insertOne
->
execute
(
$server
);
$this
->
assertEquals
(
1
,
$writeResult
->
getInsertedCount
());
$operation
=
new
ListCollections
(
$this
->
getDatabaseName
(),
[
'filter'
=>
[
'name'
=>
$this
->
getCollectionName
()]]);
$collections
=
$operation
->
execute
(
$server
);
$this
->
assertInstanceOf
(
'MongoDB\Model\CollectionInfoIterator'
,
$collections
);
foreach
(
$collections
as
$collection
)
{
$this
->
assertInstanceOf
(
'MongoDB\Model\CollectionInfo'
,
$collection
);
$this
->
assertArrayHasKey
(
'readOnly'
,
$collection
[
'info'
]);
$this
->
assertEquals
([
'v'
=>
2
,
'key'
=>
[
'_id'
=>
1
],
'name'
=>
'_id_'
,
'ns'
=>
$this
->
getNamespace
()],
$collection
[
'idIndex'
]);
}
}
public
function
testListCollectionsForNonexistentDatabase
()
public
function
testListCollectionsForNonexistentDatabase
()
{
{
$server
=
$this
->
getPrimaryServer
();
$server
=
$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