IndexManagementFunctionalTest.php 6.73 KB
Newer Older
1 2
<?php

3
namespace MongoDB\Tests\Collection;
4

5 6
use MongoDB\Model\IndexInfo;
use InvalidArgumentException;
7

8 9 10 11 12 13
/**
 * Functional tests for index management methods.
 *
 * @see https://github.com/mongodb/specifications/blob/master/source/index-management.rst
 */
class IndexManagementFunctionalTest extends FunctionalTestCase
14
{
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    public function testCreateIndex()
    {
        $that = $this;

        $this->assertSame('x_1', $this->collection->createIndex(array('x' => 1), array('sparse' => true, 'unique' => true)));
        $this->assertIndexExists('x_1', function(IndexInfo $info) use ($that) {
            $that->assertTrue($info->isSparse());
            $that->assertTrue($info->isUnique());
            $that->assertFalse($info->isTtl());
        });

        $this->assertSame('y_-1_z_1', $this->collection->createIndex(array('y' => -1, 'z' => 1)));
        $this->assertIndexExists('y_-1_z_1', function(IndexInfo $info) use ($that) {
            $that->assertFalse($info->isSparse());
            $that->assertFalse($info->isUnique());
            $that->assertFalse($info->isTtl());
        });

        $this->assertSame('g_2dsphere_z_1', $this->collection->createIndex(array('g' => '2dsphere', 'z' => 1)));
        $this->assertIndexExists('g_2dsphere_z_1', function(IndexInfo $info) use ($that) {
            $that->assertFalse($info->isSparse());
            $that->assertFalse($info->isUnique());
            $that->assertFalse($info->isTtl());
        });

40 41
        $this->assertSame('my_ttl', $this->collection->createIndex(array('t' => 1), array('expireAfterSeconds' => 0, 'name' => 'my_ttl')));
        $this->assertIndexExists('my_ttl', function(IndexInfo $info) use ($that) {
42 43 44 45 46 47 48 49 50 51
            $that->assertFalse($info->isSparse());
            $that->assertFalse($info->isUnique());
            $that->assertTrue($info->isTtl());
        });
    }

    public function testCreateIndexes()
    {
        $that = $this;

52
        $expectedNames = array('x_1', 'y_-1_z_1', 'g_2dsphere_z_1', 'my_ttl');
53 54 55 56 57

        $indexes = array(
            array('key' => array('x' => 1), 'sparse' => true, 'unique' => true),
            array('key' => array('y' => -1, 'z' => 1)),
            array('key' => array('g' => '2dsphere', 'z' => 1)),
58
            array('key' => array('t' => 1), 'expireAfterSeconds' => 0, 'name' => 'my_ttl'),
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        );

        $this->assertSame($expectedNames, $this->collection->createIndexes($indexes));

        $this->assertIndexExists('x_1', function(IndexInfo $info) use ($that) {
            $that->assertTrue($info->isSparse());
            $that->assertTrue($info->isUnique());
            $that->assertFalse($info->isTtl());
        });

        $this->assertIndexExists('y_-1_z_1', function(IndexInfo $info) use ($that) {
            $that->assertFalse($info->isSparse());
            $that->assertFalse($info->isUnique());
            $that->assertFalse($info->isTtl());
        });

        $this->assertIndexExists('g_2dsphere_z_1', function(IndexInfo $info) use ($that) {
            $that->assertFalse($info->isSparse());
            $that->assertFalse($info->isUnique());
            $that->assertFalse($info->isTtl());
        });

81
        $this->assertIndexExists('my_ttl', function(IndexInfo $info) use ($that) {
82 83 84 85 86 87
            $that->assertFalse($info->isSparse());
            $that->assertFalse($info->isUnique());
            $that->assertTrue($info->isTtl());
        });
    }

88 89 90 91 92
    public function testCreateIndexesWithEmptyInputIsNop()
    {
        $this->assertSame(array(), $this->collection->createIndexes(array()));
    }

93 94 95 96 97 98 99 100 101 102 103 104 105
    public function testDropIndex()
    {
        $this->assertSame('x_1', $this->collection->createIndex(array('x' => 1)));
        $this->assertIndexExists('x_1');
        $this->assertCommandSucceeded($this->collection->dropIndex('x_1'));

        foreach ($this->collection->listIndexes() as $index) {
            if ($index->getName() === 'x_1') {
                $this->fail('The "x_1" index should have been deleted');
            }
        }
    }

106 107 108 109 110 111 112 113 114 115
    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     */
    public function testDropIndexShouldNotAllowEmptyIndexName()
    {
        $this->assertSame('x_1', $this->collection->createIndex(array('x' => 1)));
        $this->assertIndexExists('x_1');
        $this->collection->dropIndex('');
    }

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     */
    public function testDropIndexShouldNotAllowWildcardCharacter()
    {
        $this->assertSame('x_1', $this->collection->createIndex(array('x' => 1)));
        $this->assertIndexExists('x_1');
        $this->collection->dropIndex('*');
    }

    public function testDropIndexes()
    {
        $this->assertSame('x_1', $this->collection->createIndex(array('x' => 1)));
        $this->assertSame('y_1', $this->collection->createIndex(array('y' => 1)));
        $this->assertIndexExists('x_1');
        $this->assertIndexExists('y_1');
        $this->assertCommandSucceeded($this->collection->dropIndexes());

        foreach ($this->collection->listIndexes() as $index) {
            if ($index->getName() === 'x_1') {
                $this->fail('The "x_1" index should have been deleted');
            }

            if ($index->getName() === 'y_1') {
                $this->fail('The "y_1" index should have been deleted');
            }
        }
    }

    public function testListIndexes()
    {
        $this->assertSame('x_1', $this->collection->createIndex(array('x' => 1)));

        $indexes = $this->collection->listIndexes();
        $this->assertInstanceOf('MongoDB\Model\IndexInfoIterator', $indexes);

        foreach ($indexes as $index) {
            $this->assertInstanceOf('MongoDB\Model\IndexInfo', $index);
        }
    }

    /**
     * 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.
     *
     * @param callable $callback
     */
    private function assertIndexExists($indexName, $callback = null)
    {
        if ($callback !== null && ! is_callable($callback)) {
            throw new InvalidArgumentException('$callback is not a callable');
        }

        $indexes = $this->collection->listIndexes();

        $foundIndex = null;

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

        $this->assertNotNull($foundIndex, sprintf('Found %s index for the collection', $indexName));

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