Skip to content

Commit 23e32dd

Browse files
committed
Merge pull request #718
2 parents 150177a + a2f87dc commit 23e32dd

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/Model/IndexInfoIteratorIterator.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
namespace MongoDB\Model;
1919

2020
use IteratorIterator;
21+
use Traversable;
22+
use function array_key_exists;
2123

2224
/**
2325
* IndexInfoIterator for both listIndexes command and legacy query results.
@@ -34,6 +36,19 @@
3436
*/
3537
class IndexInfoIteratorIterator extends IteratorIterator implements IndexInfoIterator
3638
{
39+
/** @var string|null $ns */
40+
private $ns;
41+
42+
/**
43+
* @param string|null $ns
44+
*/
45+
public function __construct(Traversable $iterator, $ns = null)
46+
{
47+
parent::__construct($iterator);
48+
49+
$this->ns = $ns;
50+
}
51+
3752
/**
3853
* Return the current element as an IndexInfo instance.
3954
*
@@ -43,6 +58,12 @@ class IndexInfoIteratorIterator extends IteratorIterator implements IndexInfoIte
4358
*/
4459
public function current()
4560
{
46-
return new IndexInfo(parent::current());
61+
$info = parent::current();
62+
63+
if (! array_key_exists('ns', $info) && $this->ns !== null) {
64+
$info['ns'] = $this->ns;
65+
}
66+
67+
return new IndexInfo($info);
4768
}
4869
}

src/Operation/ListIndexes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,6 @@ private function executeCommand(Server $server)
149149

150150
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
151151

152-
return new IndexInfoIteratorIterator(new CachingIterator($cursor));
152+
return new IndexInfoIteratorIterator(new CachingIterator($cursor), $this->databaseName . '.' . $this->collectionName);
153153
}
154154
}

tests/Operation/ListIndexesFunctionalTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function testListIndexesForNewlyCreatedCollection()
3131
foreach ($indexes as $index) {
3232
$this->assertInstanceOf(IndexInfo::class, $index);
3333
$this->assertEquals(['_id' => 1], $index->getKey());
34+
$this->assertSame($this->getNamespace(), $index->getNamespace());
3435
}
3536
}
3637

0 commit comments

Comments
 (0)