Commit 82a44c5f authored by Jeremy Mikola's avatar Jeremy Mikola

PHPLIB-133: Support typeMap option in FindOne

parent 5f24d92f
......@@ -3,6 +3,7 @@
namespace MongoDB\Operation;
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentTypeException;
/**
* Operation for finding a single document with the find command.
......@@ -15,6 +16,7 @@ use MongoDB\Driver\Server;
class FindOne implements Executable
{
private $find;
private $options;
/**
* Constructs a find command for finding a single document.
......@@ -42,6 +44,8 @@ class FindOne implements Executable
* "$orderby" also exists in the modifiers document, this option will
* take precedence.
*
* * typeMap (array): Type map for BSON deserialization.
*
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param array|object $filter Query by which to filter documents
......@@ -50,12 +54,18 @@ class FindOne implements Executable
*/
public function __construct($databaseName, $collectionName, $filter, array $options = array())
{
if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
throw new InvalidArgumentTypeException('"typeMap" option', $options['typeMap'], 'array');
}
$this->find = new Find(
$databaseName,
$collectionName,
$filter,
array('limit' => -1) + $options
);
$this->options = $options;
}
/**
......@@ -68,6 +78,11 @@ class FindOne implements Executable
public function execute(Server $server)
{
$cursor = $this->find->execute($server);
if (isset($this->options['typeMap'])) {
$cursor->setTypeMap($this->options['typeMap']);
}
$document = current($cursor->toArray());
return ($document === false) ? null : $document;
......
<?php
namespace MongoDB\Tests\Operation;
use MongoDB\Driver\BulkWrite;
use MongoDB\Operation\FindOne;
class FindOneFunctionalTest extends FunctionalTestCase
{
/**
* @dataProvider provideTypeMapOptionsAndExpectedDocument
*/
public function testTypeMapOption(array $typeMap, $expectedDocument)
{
$this->createFixtures(1);
$operation = new FindOne($this->getDatabaseName(), $this->getCollectionName(), [], ['typeMap' => $typeMap]);
$document = $operation->execute($this->getPrimaryServer());
$this->assertEquals($expectedDocument, $document);
}
public function provideTypeMapOptionsAndExpectedDocument()
{
return [
[
['root' => 'array', 'document' => 'array'],
['_id' => 1, 'x' => ['foo' => 'bar']],
],
[
['root' => 'object', 'document' => 'array'],
(object) ['_id' => 1, 'x' => ['foo' => 'bar']],
],
[
['root' => 'array', 'document' => 'stdClass'],
['_id' => 1, 'x' => (object) ['foo' => 'bar']],
],
];
}
/**
* Create data fixtures.
*
* @param integer $n
*/
private function createFixtures($n)
{
$bulkWrite = new BulkWrite(true);
for ($i = 1; $i <= $n; $i++) {
$bulkWrite->insert([
'_id' => $i,
'x' => (object) ['foo' => 'bar'],
]);
}
$result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);
$this->assertEquals($n, $result->getInsertedCount());
}
}
<?php
namespace MongoDB\Tests\Operation;
use MongoDB\Operation\FindOne;
class FindOneTest extends TestCase
{
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidConstructorTypeMapOptions
*/
public function testConstructorTypeMapOption($typeMap)
{
new FindOne($this->getDatabaseName(), $this->getCollectionName(), [], ['typeMap' => $typeMap]);
}
public function provideInvalidConstructorTypeMapOptions()
{
return $this->wrapValuesForDataProvider($this->getInvalidArrayValues());
}
}
......@@ -20,6 +20,11 @@ abstract class TestCase extends BaseTestCase
return $this->wrapValuesForDataProvider($this->getInvalidBooleanValues());
}
protected function getInvalidArrayValues()
{
return array(123, 3.14, 'foo', true, new stdClass);
}
protected function getInvalidBooleanValues()
{
return array(123, 3.14, 'foo', array(), new stdClass);
......
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