IndexInfoFunctionalTest.php 2.36 KB
Newer Older
1 2 3 4 5 6
<?php

namespace MongoDB\Tests\Model;

use MongoDB\Collection;
use MongoDB\Tests\FunctionalTestCase;
7
use Symfony\Bridge\PhpUnit\SetUpTearDownTrait;
8
use function version_compare;
9 10 11

class IndexInfoFunctionalTest extends FunctionalTestCase
{
12 13
    use SetUpTearDownTrait;

14 15
    private $collection;

16
    private function doSetUp()
17 18 19 20 21 22 23
    {
        parent::setUp();

        $this->collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
        $this->collection->drop();
    }

24
    private function doTearDown()
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    {
        if ($this->hasFailed()) {
            return;
        }

        $this->collection->drop();
    }

    public function testIs2dSphere()
    {
        $indexName = $this->collection->createIndex(['pos' => '2dsphere']);
        $result = $this->collection->listIndexes();

        $result->rewind();
        $result->next();
        $index = $result->current();

        $this->assertEquals($indexName, $index->getName());
        $this->assertTrue($index->is2dSphere());

        $expectedVersion = version_compare($this->getServerVersion(), '3.2.0', '<') ? 2 : 3;
        $this->assertEquals($expectedVersion, $index['2dsphereIndexVersion']);
    }

    public function testIsGeoHaystack()
    {
        $indexName = $this->collection->createIndex(['pos' => 'geoHaystack', 'x' => 1], ['bucketSize' => 5]);
        $result = $this->collection->listIndexes();

        $result->rewind();
        $result->next();
        $index = $result->current();

        $this->assertEquals($indexName, $index->getName());
        $this->assertTrue($index->isGeoHaystack());
        $this->assertEquals(5, $index['bucketSize']);
    }

    public function testIsText()
    {
        $indexName = $this->collection->createIndex(['x' => 'text']);
        $result = $this->collection->listIndexes();

        $result->rewind();
        $result->next();
        $index = $result->current();

        $this->assertEquals($indexName, $index->getName());
        $this->assertTrue($index->isText());
        $this->assertEquals('english', $index['default_language']);
        $this->assertEquals('language', $index['language_override']);

        $expectedVersion = version_compare($this->getServerVersion(), '3.2.0', '<') ? 2 : 3;
        $this->assertEquals($expectedVersion, $index['textIndexVersion']);

        $this->assertSameDocument(['x' => 1], $index['weights']);
    }
}