Skip to content

Commit 7417ecf

Browse files
committed
Merge pull request #514
2 parents 99453ef + fe26083 commit 7417ecf

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/Collection.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,16 +277,17 @@ public function count($filter = [], array $options = [])
277277
* @see CreateIndexes::__construct() for supported command options
278278
* @param array|object $key Document containing fields mapped to values,
279279
* which denote order or an index type
280-
* @param array $options Index options
280+
* @param array $options Index and command options
281281
* @return string The name of the created index
282282
* @throws UnsupportedException if options are not supported by the selected server
283283
* @throws InvalidArgumentException for parameter/option parsing errors
284284
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
285285
*/
286286
public function createIndex($key, array $options = [])
287287
{
288-
$indexOptions = array_diff_key($options, ['maxTimeMS' => 1, 'writeConcern' => 1]);
289-
$commandOptions = array_intersect_key($options, ['maxTimeMS' => 1, 'writeConcern' => 1]);
288+
$commandOptionKeys = ['maxTimeMS' => 1, 'session' => 1, 'writeConcern' => 1];
289+
$indexOptions = array_diff_key($options, $commandOptionKeys);
290+
$commandOptions = array_intersect_key($options, $commandOptionKeys);
290291

291292
return current($this->createIndexes([['key' => $key] + $indexOptions], $commandOptions));
292293
}

tests/Collection/CollectionFunctionalTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use MongoDB\Driver\ReadPreference;
1010
use MongoDB\Driver\WriteConcern;
1111
use MongoDB\Operation\MapReduce;
12+
use MongoDB\Tests\CommandObserver;
13+
use stdClass;
1214

1315
/**
1416
* Functional tests for the Collection class.
@@ -100,6 +102,35 @@ public function testGetNamespace()
100102
$this->assertEquals($this->getNamespace(), $this->collection->getNamespace());
101103
}
102104

105+
public function testCreateIndexSplitsCommandOptions()
106+
{
107+
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
108+
$this->markTestSkipped('Sessions are not supported');
109+
}
110+
111+
(new CommandObserver)->observe(
112+
function() {
113+
$this->collection->createIndex(
114+
['x' => 1],
115+
[
116+
'maxTimeMS' => 1000,
117+
'session' => $this->manager->startSession(),
118+
'sparse' => true,
119+
'unique' => true,
120+
'writeConcern' => new WriteConcern(1),
121+
]
122+
);
123+
},
124+
function(stdClass $command) {
125+
$this->assertObjectHasAttribute('lsid', $command);
126+
$this->assertObjectHasAttribute('maxTimeMS', $command);
127+
$this->assertObjectHasAttribute('writeConcern', $command);
128+
$this->assertObjectHasAttribute('sparse', $command->indexes[0]);
129+
$this->assertObjectHasAttribute('unique', $command->indexes[0]);
130+
}
131+
);
132+
}
133+
103134
public function testDrop()
104135
{
105136
$writeResult = $this->collection->insertOne(['x' => 1]);

0 commit comments

Comments
 (0)