CreateIndexesFunctionalTest.php 7.42 KB
Newer Older
1 2 3 4
<?php

namespace MongoDB\Tests\Operation;

5
use InvalidArgumentException;
6
use MongoDB\Driver\Exception\RuntimeException;
7 8 9
use MongoDB\Model\IndexInfo;
use MongoDB\Operation\CreateIndexes;
use MongoDB\Operation\ListIndexes;
10
use MongoDB\Tests\CommandObserver;
11 12 13 14
use function call_user_func;
use function is_callable;
use function sprintf;
use function version_compare;
15 16 17 18 19 20 21 22 23 24 25

class CreateIndexesFunctionalTest extends FunctionalTestCase
{
    public function testCreateSparseUniqueIndex()
    {
        $indexes = [['key' => ['x' => 1], 'sparse' => true, 'unique' => true]];

        $operation = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes);
        $createdIndexNames = $operation->execute($this->getPrimaryServer());

        $this->assertSame('x_1', $createdIndexNames[0]);
26
        $this->assertIndexExists('x_1', function (IndexInfo $info) {
27 28 29 30 31 32 33 34 35 36 37 38 39 40
            $this->assertTrue($info->isSparse());
            $this->assertTrue($info->isUnique());
            $this->assertFalse($info->isTtl());
        });
    }

    public function testCreateCompoundIndex()
    {
        $indexes = [['key' => ['y' => -1, 'z' => 1]]];

        $operation = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes);
        $createdIndexNames = $operation->execute($this->getPrimaryServer());

        $this->assertSame('y_-1_z_1', $createdIndexNames[0]);
41
        $this->assertIndexExists('y_-1_z_1', function (IndexInfo $info) {
42 43 44 45 46 47 48 49 50 51 52 53 54 55
            $this->assertFalse($info->isSparse());
            $this->assertFalse($info->isUnique());
            $this->assertFalse($info->isTtl());
        });
    }

    public function testCreateGeospatialIndex()
    {
        $indexes = [['key' => ['g' => '2dsphere', 'z' => 1]]];

        $operation = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes);
        $createdIndexNames = $operation->execute($this->getPrimaryServer());

        $this->assertSame('g_2dsphere_z_1', $createdIndexNames[0]);
56
        $this->assertIndexExists('g_2dsphere_z_1', function (IndexInfo $info) {
57 58 59 60 61 62 63 64 65 66 67 68 69 70
            $this->assertFalse($info->isSparse());
            $this->assertFalse($info->isUnique());
            $this->assertFalse($info->isTtl());
        });
    }

    public function testCreateTTLIndex()
    {
        $indexes = [['key' => ['t' => 1], 'expireAfterSeconds' => 0, 'name' => 'my_ttl']];

        $operation = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes);
        $createdIndexNames = $operation->execute($this->getPrimaryServer());

        $this->assertSame('my_ttl', $createdIndexNames[0]);
71
        $this->assertIndexExists('my_ttl', function (IndexInfo $info) {
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
            $this->assertFalse($info->isSparse());
            $this->assertFalse($info->isUnique());
            $this->assertTrue($info->isTtl());
        });
    }

    public function testCreateIndexes()
    {
        $expectedNames = ['x_1', 'y_-1_z_1', 'g_2dsphere_z_1', 'my_ttl'];

        $indexes = [
            ['key' => ['x' => 1], 'sparse' => true, 'unique' => true],
            ['key' => ['y' => -1, 'z' => 1]],
            ['key' => ['g' => '2dsphere', 'z' => 1]],
            ['key' => ['t' => 1], 'expireAfterSeconds' => 0, 'name' => 'my_ttl'],
        ];

        $operation = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes);
        $createdIndexNames = $operation->execute($this->getPrimaryServer());

        $this->assertSame($expectedNames, $createdIndexNames);

94
        $this->assertIndexExists('x_1', function (IndexInfo $info) {
95 96 97 98 99
            $this->assertTrue($info->isSparse());
            $this->assertTrue($info->isUnique());
            $this->assertFalse($info->isTtl());
        });

100
        $this->assertIndexExists('y_-1_z_1', function (IndexInfo $info) {
101 102 103 104 105
            $this->assertFalse($info->isSparse());
            $this->assertFalse($info->isUnique());
            $this->assertFalse($info->isTtl());
        });

106
        $this->assertIndexExists('g_2dsphere_z_1', function (IndexInfo $info) {
107 108 109 110 111
            $this->assertFalse($info->isSparse());
            $this->assertFalse($info->isUnique());
            $this->assertFalse($info->isTtl());
        });

112
        $this->assertIndexExists('my_ttl', function (IndexInfo $info) {
113 114 115 116 117 118 119 120 121 122 123 124 125 126
            $this->assertFalse($info->isSparse());
            $this->assertFalse($info->isUnique());
            $this->assertTrue($info->isTtl());
        });
    }

    public function testCreateConflictingIndexesWithCommand()
    {
        $indexes = [
            ['key' => ['x' => 1], 'sparse' => true, 'unique' => false],
            ['key' => ['x' => 1], 'sparse' => false, 'unique' => true],
        ];

        $operation = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes);
127

128
        $this->expectException(RuntimeException::class);
129
        $operation->execute($this->getPrimaryServer());
130 131
    }

132 133
    public function testDefaultWriteConcernIsOmitted()
    {
134 135
        (new CommandObserver())->observe(
            function () {
136 137 138 139 140 141 142 143 144
                $operation = new CreateIndexes(
                    $this->getDatabaseName(),
                    $this->getCollectionName(),
                    [['key' => ['x' => 1]]],
                    ['writeConcern' => $this->createDefaultWriteConcern()]
                );

                $operation->execute($this->getPrimaryServer());
            },
145
            function (array $event) {
146
                $this->assertObjectNotHasAttribute('writeConcern', $event['started']->getCommand());
147 148 149 150
            }
        );
    }

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

157 158
        (new CommandObserver())->observe(
            function () {
159 160 161 162 163 164 165 166 167
                $operation = new CreateIndexes(
                    $this->getDatabaseName(),
                    $this->getCollectionName(),
                    [['key' => ['x' => 1]]],
                    ['session' => $this->createSession()]
                );

                $operation->execute($this->getPrimaryServer());
            },
168
            function (array $event) {
169
                $this->assertObjectHasAttribute('lsid', $event['started']->getCommand());
170 171 172 173
            }
        );
    }

174 175 176 177 178 179 180 181
    /**
     * Asserts that an index with the given name exists for the collection.
     *
     * An optional $callback may be provided, which should take an IndexInfo
     * argument as its first and only parameter. If an IndexInfo matching the
     * given name is found, it will be passed to the callback, which may perform
     * additional assertions.
     *
182
     * @param string   $indexName
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
     * @param callable $callback
     */
    private function assertIndexExists($indexName, $callback = null)
    {
        if ($callback !== null && ! is_callable($callback)) {
            throw new InvalidArgumentException('$callback is not a callable');
        }

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

        $foundIndex = null;

        foreach ($indexes as $index) {
            if ($index->getName() === $indexName) {
                $foundIndex = $index;
                break;
            }
        }

203
        $this->assertNotNull($foundIndex, sprintf('Index %s does not exist', $indexName));
204 205 206 207 208 209

        if ($callback !== null) {
            call_user_func($callback, $foundIndex);
        }
    }
}