Commit 7417ecf1 authored by Jeremy Mikola's avatar Jeremy Mikola

Merge pull request #514

parents 99453efa fe26083c
......@@ -277,7 +277,7 @@ class Collection
* @see CreateIndexes::__construct() for supported command options
* @param array|object $key Document containing fields mapped to values,
* which denote order or an index type
* @param array $options Index options
* @param array $options Index and command options
* @return string The name of the created index
* @throws UnsupportedException if options are not supported by the selected server
* @throws InvalidArgumentException for parameter/option parsing errors
......@@ -285,8 +285,9 @@ class Collection
*/
public function createIndex($key, array $options = [])
{
$indexOptions = array_diff_key($options, ['maxTimeMS' => 1, 'writeConcern' => 1]);
$commandOptions = array_intersect_key($options, ['maxTimeMS' => 1, 'writeConcern' => 1]);
$commandOptionKeys = ['maxTimeMS' => 1, 'session' => 1, 'writeConcern' => 1];
$indexOptions = array_diff_key($options, $commandOptionKeys);
$commandOptions = array_intersect_key($options, $commandOptionKeys);
return current($this->createIndexes([['key' => $key] + $indexOptions], $commandOptions));
}
......
......@@ -9,6 +9,8 @@ use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use MongoDB\Operation\MapReduce;
use MongoDB\Tests\CommandObserver;
use stdClass;
/**
* Functional tests for the Collection class.
......@@ -100,6 +102,35 @@ class CollectionFunctionalTest extends FunctionalTestCase
$this->assertEquals($this->getNamespace(), $this->collection->getNamespace());
}
public function testCreateIndexSplitsCommandOptions()
{
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
$this->markTestSkipped('Sessions are not supported');
}
(new CommandObserver)->observe(
function() {
$this->collection->createIndex(
['x' => 1],
[
'maxTimeMS' => 1000,
'session' => $this->manager->startSession(),
'sparse' => true,
'unique' => true,
'writeConcern' => new WriteConcern(1),
]
);
},
function(stdClass $command) {
$this->assertObjectHasAttribute('lsid', $command);
$this->assertObjectHasAttribute('maxTimeMS', $command);
$this->assertObjectHasAttribute('writeConcern', $command);
$this->assertObjectHasAttribute('sparse', $command->indexes[0]);
$this->assertObjectHasAttribute('unique', $command->indexes[0]);
}
);
}
public function testDrop()
{
$writeResult = $this->collection->insertOne(['x' => 1]);
......
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