Skip to content

Commit 33158d5

Browse files
authored
Merge pull request #150 from alcaeus/missing-index-info
Expose missing option in indexInfo method
2 parents 079e410 + 02ccc21 commit 33158d5

File tree

3 files changed

+140
-25
lines changed

3 files changed

+140
-25
lines changed

CHANGELOG-1.0.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ All issues and pull requests under this release may be found under the
1010
[1.0.8](https://github.com/alcaeus/mongo-php-adapter/issues?q=milestone%3A1.0.8)
1111
milestone.
1212

13+
* [#150](https://github.com/alcaeus/mongo-php-adapter/pull/150) adds missing
14+
options to `MongoCollection::indexInfo`.
1315
* [#149](https://github.com/alcaeus/mongo-php-adapter/pull/149) fixes calls to
1416
`MongoDB::getDBRef` with references containing a `$db` field with a different
1517
value than the current database.

lib/Mongo/MongoCollection.php

+21-2
Original file line numberDiff line numberDiff line change
@@ -713,8 +713,27 @@ public function getIndexInfo()
713713
'ns' => $indexInfo->getNamespace(),
714714
];
715715

716-
if ($indexInfo->isUnique()) {
717-
$infos['unique'] = true;
716+
$additionalKeys = [
717+
'unique',
718+
'sparse',
719+
'partialFilterExpression',
720+
'expireAfterSeconds',
721+
'storageEngine',
722+
'weights',
723+
'default_language',
724+
'language_override',
725+
'textIndexVersion',
726+
'collation',
727+
'2dsphereIndexVersion',
728+
'bucketSize'
729+
];
730+
731+
foreach ($additionalKeys as $key) {
732+
if (! isset($indexInfo[$key])) {
733+
continue;
734+
}
735+
736+
$infos[$key] = $indexInfo[$key];
718737
}
719738

720739
return $infos;

tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php

+117-23
Original file line numberDiff line numberDiff line change
@@ -1222,36 +1222,130 @@ public function testDeleteIndexesForNonExistingCollection()
12221222
$this->assertSame($expected, $this->getcollection('nonExisting')->deleteIndexes());
12231223
}
12241224

1225-
public function testGetIndexInfo()
1225+
public static function dataGetIndexInfo()
12261226
{
1227-
$collection = $this->getCollection();
1228-
$collection->createIndex(['foo' => 1]);
1229-
$collection->createIndex(['bar' => 1], ['unique' => true]);
1230-
1231-
$expected = [
1232-
[
1233-
'v' => 1,
1234-
'key' => ['_id' => 1],
1235-
'name' => '_id_',
1236-
'ns' => 'mongo-php-adapter.test',
1227+
return [
1228+
'plainIndex' => [
1229+
'expectedIndex' => [
1230+
'v' => 1,
1231+
'key' => ['foo' => 1],
1232+
'name' => 'foo_1',
1233+
'ns' => 'mongo-php-adapter.test',
1234+
],
1235+
'fields' => ['foo' => 1],
1236+
'options' => [],
12371237
],
1238-
[
1239-
'v' => 1,
1240-
'key' => ['foo' => 1],
1241-
'name' => 'foo_1',
1242-
'ns' => 'mongo-php-adapter.test',
1238+
'uniqueIndex' => [
1239+
'expectedIndex' => [
1240+
'v' => 1,
1241+
'key' => ['foo' => 1],
1242+
'name' => 'foo_1',
1243+
'ns' => 'mongo-php-adapter.test',
1244+
'unique' => true,
1245+
],
1246+
'fields' => ['foo' => 1],
1247+
'options' => ['unique' => true],
12431248
],
1244-
[
1245-
'v' => 1,
1246-
'key' => ['bar' => 1],
1247-
'name' => 'bar_1',
1248-
'ns' => 'mongo-php-adapter.test',
1249-
'unique' => true,
1249+
'sparseIndex' => [
1250+
'expectedIndex' => [
1251+
'v' => 1,
1252+
'key' => ['foo' => 1],
1253+
'name' => 'foo_1',
1254+
'ns' => 'mongo-php-adapter.test',
1255+
'sparse' => true,
1256+
],
1257+
'fields' => ['foo' => 1],
1258+
'options' => ['sparse' => true],
1259+
],
1260+
'ttlIndex' => [
1261+
'expectedIndex' => [
1262+
'v' => 1,
1263+
'key' => ['foo' => 1],
1264+
'name' => 'foo_1',
1265+
'ns' => 'mongo-php-adapter.test',
1266+
'expireAfterSeconds' => 86400,
1267+
],
1268+
'fields' => ['foo' => 1],
1269+
'options' => ['expireAfterSeconds' => 86400],
1270+
],
1271+
'textIndex' => [
1272+
'expectedIndex' => [
1273+
'v' => 1,
1274+
'key' => [
1275+
'_fts' => 'text',
1276+
'_ftsx' => 1,
1277+
],
1278+
'name' => 'foo_text',
1279+
'ns' => 'mongo-php-adapter.test',
1280+
'weights' => [
1281+
'foo' => 1,
1282+
],
1283+
'default_language' => 'english',
1284+
'language_override' => 'language',
1285+
'textIndexVersion' => 3,
1286+
],
1287+
'fields' => ['foo' => 'text'],
1288+
'options' => [],
1289+
],
1290+
'partialFilterExpression' => [
1291+
'expectedIndex' => [
1292+
'v' => 1,
1293+
'key' => ['foo' => 1],
1294+
'name' => 'foo_1',
1295+
'ns' => 'mongo-php-adapter.test',
1296+
'partialFilterExpression' => [
1297+
'bar' => ['$gt' => 1],
1298+
],
1299+
],
1300+
'fields' => ['foo' => 1],
1301+
'options' => [
1302+
'partialFilterExpression' => ['bar' => ['$gt' => 1]],
1303+
],
1304+
],
1305+
'geoSpatial' => [
1306+
'expectedIndex' => [
1307+
'v' => 1,
1308+
'key' => ['foo' => '2dsphere'],
1309+
'name' => 'foo_2dsphere',
1310+
'ns' => 'mongo-php-adapter.test',
1311+
'2dsphereIndexVersion' => 3,
1312+
],
1313+
'fields' => ['foo' => '2dsphere'],
1314+
'options' => [],
1315+
],
1316+
'geoHaystack' => [
1317+
'expectedIndex' => [
1318+
'v' => 1,
1319+
'key' => ['foo' => 'geoHaystack', 'bar' => 1],
1320+
'name' => 'foo_geoHaystack_bar_1',
1321+
'ns' => 'mongo-php-adapter.test',
1322+
'bucketSize' => 10,
1323+
],
1324+
'fields' => ['foo' => 'geoHaystack', 'bar' => 1],
1325+
'options' => ['bucketSize' => 10],
12501326
],
12511327
];
1328+
}
1329+
1330+
/**
1331+
* @dataProvider dataGetIndexInfo
1332+
*/
1333+
public function testGetIndexInfo($expectedIndex, $fields, $options)
1334+
{
1335+
$idIndex = [
1336+
'v' => 1,
1337+
'key' => ['_id' => 1],
1338+
'name' => '_id_',
1339+
'ns' => 'mongo-php-adapter.test',
1340+
];
1341+
1342+
$expectedIndexInfo = [$idIndex, $expectedIndex];
1343+
1344+
$collection = $this->getCollection();
1345+
$collection->createIndex($fields, $options);
12521346

12531347
$this->assertEquals(
1254-
$expected,
1348+
$expectedIndexInfo,
12551349
$collection->getIndexInfo()
12561350
);
12571351
}

0 commit comments

Comments
 (0)