ListIndexesFunctionalTest.php 2.43 KB
Newer Older
1 2 3 4
<?php

namespace MongoDB\Tests\Operation;

5 6
use MongoDB\Model\IndexInfo;
use MongoDB\Model\IndexInfoIterator;
7
use MongoDB\Operation\DropCollection;
8
use MongoDB\Operation\InsertOne;
9
use MongoDB\Operation\ListIndexes;
10
use MongoDB\Tests\CommandObserver;
11
use function version_compare;
12 13 14 15 16 17

class ListIndexesFunctionalTest extends FunctionalTestCase
{
    public function testListIndexesForNewlyCreatedCollection()
    {
        $operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
18
        $operation->execute($this->getPrimaryServer());
19

20
        $insertOne = new InsertOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1]);
21
        $writeResult = $insertOne->execute($this->getPrimaryServer());
22 23 24
        $this->assertEquals(1, $writeResult->getInsertedCount());

        $operation = new ListIndexes($this->getDatabaseName(), $this->getCollectionName());
25 26
        $indexes = $operation->execute($this->getPrimaryServer());

27
        $this->assertInstanceOf(IndexInfoIterator::class, $indexes);
28

29 30 31
        $this->assertCount(1, $indexes);

        foreach ($indexes as $index) {
32
            $this->assertInstanceOf(IndexInfo::class, $index);
33
            $this->assertEquals(['_id' => 1], $index->getKey());
34
            $this->assertSame($this->getNamespace(), $index->getNamespace());
35 36 37 38 39 40
        }
    }

    public function testListIndexesForNonexistentCollection()
    {
        $operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
41
        $operation->execute($this->getPrimaryServer());
42 43

        $operation = new ListIndexes($this->getDatabaseName(), $this->getCollectionName());
44
        $indexes = $operation->execute($this->getPrimaryServer());
45 46 47

        $this->assertCount(0, $indexes);
    }
48 49 50 51 52 53 54

    public function testSessionOption()
    {
        if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
            $this->markTestSkipped('Sessions are not supported');
        }

55 56
        (new CommandObserver())->observe(
            function () {
57 58 59 60 61 62 63 64
                $operation = new ListIndexes(
                    $this->getDatabaseName(),
                    $this->getCollectionName(),
                    ['session' => $this->createSession()]
                );

                $operation->execute($this->getPrimaryServer());
            },
65
            function (array $event) {
66
                $this->assertObjectHasAttribute('lsid', $event['started']->getCommand());
67 68 69
            }
        );
    }
70
}