IndexInfoTest.php 4.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php

namespace MongoDB\Tests;

use MongoDB\Model\IndexInfo;
use MongoDB\Tests\TestCase;

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

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

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

        $this->assertSame(1, $info->getVersion());
Jeremy Mikola's avatar
Jeremy Mikola committed
39
        $this->assertSame(['y' => 1], $info->getKey());
40 41 42 43 44 45 46 47 48
        $this->assertSame('y_sparse', $info->getName());
        $this->assertSame('foo.bar', $info->getNamespace());
        $this->assertTrue($info->isSparse());
        $this->assertFalse($info->isTtl());
        $this->assertFalse($info->isUnique());
    }

    public function testUniqueIndex()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
49
        $info = new IndexInfo([
50
            'v' => 1,
Jeremy Mikola's avatar
Jeremy Mikola committed
51
            'key' => ['z' => 1],
52 53 54
            'name' => 'z_unique',
            'ns' => 'foo.bar',
            'unique' => true,
Jeremy Mikola's avatar
Jeremy Mikola committed
55
        ]);
56 57

        $this->assertSame(1, $info->getVersion());
Jeremy Mikola's avatar
Jeremy Mikola committed
58
        $this->assertSame(['z' => 1], $info->getKey());
59 60 61 62 63 64 65 66 67
        $this->assertSame('z_unique', $info->getName());
        $this->assertSame('foo.bar', $info->getNamespace());
        $this->assertFalse($info->isSparse());
        $this->assertFalse($info->isTtl());
        $this->assertTrue($info->isUnique());
    }

    public function testTtlIndex()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
68
        $info = new IndexInfo([
69
            'v' => 1,
Jeremy Mikola's avatar
Jeremy Mikola committed
70
            'key' => ['z' => 1],
71 72 73
            'name' => 'z_unique',
            'ns' => 'foo.bar',
            'expireAfterSeconds' => 100,
Jeremy Mikola's avatar
Jeremy Mikola committed
74
        ]);
75 76

        $this->assertSame(1, $info->getVersion());
Jeremy Mikola's avatar
Jeremy Mikola committed
77
        $this->assertSame(['z' => 1], $info->getKey());
78 79 80 81 82 83 84 85
        $this->assertSame('z_unique', $info->getName());
        $this->assertSame('foo.bar', $info->getNamespace());
        $this->assertFalse($info->isSparse());
        $this->assertTrue($info->isTtl());
        $this->assertFalse($info->isUnique());
        $this->assertTrue(isset($info['expireAfterSeconds']));
        $this->assertSame(100, $info['expireAfterSeconds']);
    }
86 87 88

    public function testDebugInfo()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
89
        $expectedInfo = [
90
            'v' => 1,
Jeremy Mikola's avatar
Jeremy Mikola committed
91
            'key' => ['x' => 1],
92 93
            'name' => 'x_1',
            'ns' => 'foo.bar',
Jeremy Mikola's avatar
Jeremy Mikola committed
94
        ];
95 96 97 98

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

100 101 102 103 104 105 106 107 108 109 110 111 112 113
    public function testImplementsArrayAccess()
    {
        $info = new IndexInfo([
            'v' => 1,
            'key' => ['x' => 1],
            'name' => 'x_1',
            'ns' => 'foo.bar',
        ]);

        $this->assertInstanceOf('ArrayAccess', $info);
        $this->assertTrue(isset($info['name']));
        $this->assertSame('x_1', $info['name']);
    }

114 115 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
    /**
     * @expectedException MongoDB\Exception\BadMethodCallException
     * @expectedExceptionMessage MongoDB\Model\IndexInfo is immutable
     */
    public function testOffsetSetCannotBeCalled()
    {
        $info = new IndexInfo([
            'v' => 1,
            'key' => ['x' => 1],
            'name' => 'x_1',
            'ns' => 'foo.bar',
        ]);

        $info['v'] = 2;
    }

    /**
     * @expectedException MongoDB\Exception\BadMethodCallException
     * @expectedExceptionMessage MongoDB\Model\IndexInfo is immutable
     */
    public function testOffsetUnsetCannotBeCalled()
    {
        $info = new IndexInfo([
            'v' => 1,
            'key' => ['x' => 1],
            'name' => 'x_1',
            'ns' => 'foo.bar',
        ]);

        unset($info['v']);
    }
145
}