Commit 980746df authored by Katherine Walker's avatar Katherine Walker

PHPLIB-226: Implement ArrayAccess for CollectionInfo and DatabaseInfo

parent 47616117
......@@ -17,6 +17,9 @@
namespace MongoDB\Model;
use MongoDB\Exception\BadMethodCallException;
use ArrayAccess;
/**
* Collection information model class.
*
......@@ -28,7 +31,7 @@ namespace MongoDB\Model;
* @see \MongoDB\Database::listCollections()
* @see https://github.com/mongodb/specifications/blob/master/source/enumerate-collections.rst
*/
class CollectionInfo
class CollectionInfo implements ArrayAccess
{
private $info;
......@@ -102,4 +105,50 @@ class CollectionInfo
{
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__);
}
}
......@@ -17,6 +17,8 @@
namespace MongoDB\Model;
use MongoDB\Exception\BadMethodCallException;
use ArrayAccess;
/**
* Database information model class.
*
......@@ -27,7 +29,7 @@ namespace MongoDB\Model;
* @see \MongoDB\Client::listDatabases()
* @see http://docs.mongodb.org/manual/reference/command/listDatabases/
*/
class DatabaseInfo
class DatabaseInfo implements ArrayAccess
{
private $info;
......@@ -81,4 +83,50 @@ class DatabaseInfo
{
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__);
}
}
......@@ -2,6 +2,7 @@
namespace MongoDB\Tests\Model;
use MongoDB\Exception\BadMethodCallException;
use MongoDB\Model\CollectionInfo;
use MongoDB\Tests\TestCase;
......@@ -50,4 +51,28 @@ class CollectionInfoTest extends TestCase
$info = new CollectionInfo($expectedInfo);
$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']);
}
}
......@@ -2,6 +2,7 @@
namespace MongoDB\Tests\Model;
use MongoDB\Exception\BadMethodCallException;
use MongoDB\Model\DatabaseInfo;
use MongoDB\Tests\TestCase;
......@@ -39,4 +40,28 @@ class DatabaseInfoTest extends TestCase
$info = new DatabaseInfo($expectedInfo);
$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']);
}
}
......@@ -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()
{
$server = $this->getPrimaryServer();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment