PHPLIB-499: Fall back to provided namespace if index info doesn't contain it

parent 150177a5
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
namespace MongoDB\Model; namespace MongoDB\Model;
use IteratorIterator; use IteratorIterator;
use Traversable;
use function array_key_exists;
/** /**
* IndexInfoIterator for both listIndexes command and legacy query results. * IndexInfoIterator for both listIndexes command and legacy query results.
...@@ -34,6 +36,19 @@ use IteratorIterator; ...@@ -34,6 +36,19 @@ use IteratorIterator;
*/ */
class IndexInfoIteratorIterator extends IteratorIterator implements IndexInfoIterator class IndexInfoIteratorIterator extends IteratorIterator implements IndexInfoIterator
{ {
/** @var string|null $ns */
private $ns;
/**
* @param string|null $ns
*/
public function __construct(Traversable $iterator, $ns = null)
{
parent::__construct($iterator);
$this->ns = $ns;
}
/** /**
* Return the current element as an IndexInfo instance. * Return the current element as an IndexInfo instance.
* *
...@@ -43,6 +58,12 @@ class IndexInfoIteratorIterator extends IteratorIterator implements IndexInfoIte ...@@ -43,6 +58,12 @@ class IndexInfoIteratorIterator extends IteratorIterator implements IndexInfoIte
*/ */
public function current() public function current()
{ {
return new IndexInfo(parent::current()); $info = parent::current();
if (! array_key_exists('ns', $info) && $this->ns !== null) {
$info['ns'] = $this->ns;
}
return new IndexInfo($info);
} }
} }
...@@ -149,6 +149,6 @@ class ListIndexes implements Executable ...@@ -149,6 +149,6 @@ class ListIndexes implements Executable
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']); $cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
return new IndexInfoIteratorIterator(new CachingIterator($cursor)); return new IndexInfoIteratorIterator(new CachingIterator($cursor), $this->databaseName . '.' . $this->collectionName);
} }
} }
...@@ -31,6 +31,7 @@ class ListIndexesFunctionalTest extends FunctionalTestCase ...@@ -31,6 +31,7 @@ class ListIndexesFunctionalTest extends FunctionalTestCase
foreach ($indexes as $index) { foreach ($indexes as $index) {
$this->assertInstanceOf(IndexInfo::class, $index); $this->assertInstanceOf(IndexInfo::class, $index);
$this->assertEquals(['_id' => 1], $index->getKey()); $this->assertEquals(['_id' => 1], $index->getKey());
$this->assertSame($this->getNamespace(), $index->getNamespace());
} }
} }
......
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