IndexInfoTest.php 6.95 KB
Newer Older
1 2
<?php

3
namespace MongoDB\Tests\Model;
4

5
use MongoDB\Exception\BadMethodCallException;
6 7 8 9 10 11 12
use MongoDB\Model\IndexInfo;
use MongoDB\Tests\TestCase;

class IndexInfoTest extends TestCase
{
    public function testBasicIndex()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
13
        $info = new IndexInfo([
14
            'v' => 1,
Jeremy Mikola's avatar
Jeremy Mikola committed
15
            'key' => ['x' => 1],
16 17
            'name' => 'x_1',
            'ns' => 'foo.bar',
Jeremy Mikola's avatar
Jeremy Mikola committed
18
        ]);
19 20

        $this->assertSame(1, $info->getVersion());
Jeremy Mikola's avatar
Jeremy Mikola committed
21
        $this->assertSame(['x' => 1], $info->getKey());
22 23
        $this->assertSame('x_1', $info->getName());
        $this->assertSame('foo.bar', $info->getNamespace());
24 25
        $this->assertFalse($info->is2dSphere());
        $this->assertFalse($info->isGeoHaystack());
26
        $this->assertFalse($info->isSparse());
27
        $this->assertFalse($info->isText());
28 29 30 31 32 33
        $this->assertFalse($info->isTtl());
        $this->assertFalse($info->isUnique());
    }

    public function testSparseIndex()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
34
        $info = new IndexInfo([
35
            'v' => 1,
Jeremy Mikola's avatar
Jeremy Mikola committed
36
            'key' => ['y' => 1],
37 38 39
            'name' => 'y_sparse',
            'ns' => 'foo.bar',
            'sparse' => true,
Jeremy Mikola's avatar
Jeremy Mikola committed
40
        ]);
41 42

        $this->assertSame(1, $info->getVersion());
Jeremy Mikola's avatar
Jeremy Mikola committed
43
        $this->assertSame(['y' => 1], $info->getKey());
44 45
        $this->assertSame('y_sparse', $info->getName());
        $this->assertSame('foo.bar', $info->getNamespace());
46 47
        $this->assertFalse($info->is2dSphere());
        $this->assertFalse($info->isGeoHaystack());
48
        $this->assertTrue($info->isSparse());
49
        $this->assertFalse($info->isText());
50 51 52 53 54 55
        $this->assertFalse($info->isTtl());
        $this->assertFalse($info->isUnique());
    }

    public function testUniqueIndex()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
56
        $info = new IndexInfo([
57
            'v' => 1,
Jeremy Mikola's avatar
Jeremy Mikola committed
58
            'key' => ['z' => 1],
59 60 61
            'name' => 'z_unique',
            'ns' => 'foo.bar',
            'unique' => true,
Jeremy Mikola's avatar
Jeremy Mikola committed
62
        ]);
63 64

        $this->assertSame(1, $info->getVersion());
Jeremy Mikola's avatar
Jeremy Mikola committed
65
        $this->assertSame(['z' => 1], $info->getKey());
66 67
        $this->assertSame('z_unique', $info->getName());
        $this->assertSame('foo.bar', $info->getNamespace());
68 69
        $this->assertFalse($info->is2dSphere());
        $this->assertFalse($info->isGeoHaystack());
70
        $this->assertFalse($info->isSparse());
71
        $this->assertFalse($info->isText());
72 73 74 75 76 77
        $this->assertFalse($info->isTtl());
        $this->assertTrue($info->isUnique());
    }

    public function testTtlIndex()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
78
        $info = new IndexInfo([
79
            'v' => 1,
Jeremy Mikola's avatar
Jeremy Mikola committed
80
            'key' => ['z' => 1],
81 82 83
            'name' => 'z_unique',
            'ns' => 'foo.bar',
            'expireAfterSeconds' => 100,
Jeremy Mikola's avatar
Jeremy Mikola committed
84
        ]);
85 86

        $this->assertSame(1, $info->getVersion());
Jeremy Mikola's avatar
Jeremy Mikola committed
87
        $this->assertSame(['z' => 1], $info->getKey());
88 89
        $this->assertSame('z_unique', $info->getName());
        $this->assertSame('foo.bar', $info->getNamespace());
90 91
        $this->assertFalse($info->is2dSphere());
        $this->assertFalse($info->isGeoHaystack());
92
        $this->assertFalse($info->isSparse());
93
        $this->assertFalse($info->isText());
94 95
        $this->assertTrue($info->isTtl());
        $this->assertFalse($info->isUnique());
96
        $this->assertArrayHasKey('expireAfterSeconds', $info);
97 98
        $this->assertSame(100, $info['expireAfterSeconds']);
    }
99 100 101

    public function testDebugInfo()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
102
        $expectedInfo = [
103
            'v' => 1,
Jeremy Mikola's avatar
Jeremy Mikola committed
104
            'key' => ['x' => 1],
105 106
            'name' => 'x_1',
            'ns' => 'foo.bar',
Jeremy Mikola's avatar
Jeremy Mikola committed
107
        ];
108 109 110 111

        $info = new IndexInfo($expectedInfo);
        $this->assertSame($expectedInfo, $info->__debugInfo());
    }
112

113 114 115 116 117 118 119 120 121 122
    public function testImplementsArrayAccess()
    {
        $info = new IndexInfo([
            'v' => 1,
            'key' => ['x' => 1],
            'name' => 'x_1',
            'ns' => 'foo.bar',
        ]);

        $this->assertInstanceOf('ArrayAccess', $info);
123
        $this->assertArrayHasKey('name', $info);
124 125 126
        $this->assertSame('x_1', $info['name']);
    }

127 128 129 130 131 132 133 134 135
    public function testOffsetSetCannotBeCalled()
    {
        $info = new IndexInfo([
            'v' => 1,
            'key' => ['x' => 1],
            'name' => 'x_1',
            'ns' => 'foo.bar',
        ]);

136 137
        $this->expectException(BadMethodCallException::class);
        $this->expectExceptionMessage('MongoDB\Model\IndexInfo is immutable');
138 139 140 141 142 143 144 145 146 147 148 149
        $info['v'] = 2;
    }

    public function testOffsetUnsetCannotBeCalled()
    {
        $info = new IndexInfo([
            'v' => 1,
            'key' => ['x' => 1],
            'name' => 'x_1',
            'ns' => 'foo.bar',
        ]);

150 151
        $this->expectException(BadMethodCallException::class);
        $this->expectExceptionMessage('MongoDB\Model\IndexInfo is immutable');
152 153
        unset($info['v']);
    }
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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

    public function testIs2dSphere()
    {
        $info = new IndexInfo([
            'v' => 2,
            'key' => ['pos' => '2dsphere'],
            'name' => 'pos_2dsphere',
            'ns' => 'foo.bar',
        ]);

        $this->assertSame(2, $info->getVersion());
        $this->assertSame(['pos' => '2dsphere'], $info->getKey());
        $this->assertSame('pos_2dsphere', $info->getName());
        $this->assertSame('foo.bar', $info->getNamespace());
        $this->assertTrue($info->is2dSphere());
        $this->assertFalse($info->isGeoHaystack());
        $this->assertFalse($info->isSparse());
        $this->assertFalse($info->isText());
        $this->assertFalse($info->isTtl());
        $this->assertFalse($info->isUnique());
    }

    public function testIsGeoHaystack()
    {
        $info = new IndexInfo([
            'v' => 2,
            'key' => ['pos2' => 'geoHaystack', 'x' => 1],
            'name' => 'pos2_geoHaystack_x_1',
            'ns' => 'foo.bar',
        ]);

        $this->assertSame(2, $info->getVersion());
        $this->assertSame(['pos2' => 'geoHaystack', 'x' => 1], $info->getKey());
        $this->assertSame('pos2_geoHaystack_x_1', $info->getName());
        $this->assertSame('foo.bar', $info->getNamespace());
        $this->assertFalse($info->is2dSphere());
        $this->assertTrue($info->isGeoHaystack());
        $this->assertFalse($info->isSparse());
        $this->assertFalse($info->isText());
        $this->assertFalse($info->isTtl());
        $this->assertFalse($info->isUnique());
    }

    public function testIsText()
    {
        $info = new IndexInfo([
            'v' => 2,
            'key' => ['_fts' => 'text', '_ftsx' => 1],
            'name' => 'title_text_description_text',
            'ns' => 'foo.bar',
        ]);

        $this->assertSame(2, $info->getVersion());
        $this->assertSame(['_fts' => 'text', '_ftsx' => 1], $info->getKey());
        $this->assertSame('title_text_description_text', $info->getName());
        $this->assertSame('foo.bar', $info->getNamespace());
        $this->assertFalse($info->is2dSphere());
        $this->assertFalse($info->isGeoHaystack());
        $this->assertFalse($info->isSparse());
        $this->assertTrue($info->isText());
        $this->assertFalse($info->isTtl());
        $this->assertFalse($info->isUnique());
    }
217
}