IndexInputTest.php 2.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php

namespace MongoDB\Tests;

use MongoDB\Model\IndexInput;
use MongoDB\Tests\TestCase;

class IndexInputTest extends TestCase
{
    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     */
    public function testConstructorShouldRequireKey()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
15
        new IndexInput([]);
16 17 18 19 20 21 22
    }

    /**
     * @expectedException MongoDB\Exception\UnexpectedTypeException
     */
    public function testConstructorShouldRequireKeyToBeArrayOrObject()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
23
        new IndexInput(['key' => 'foo']);
24 25 26 27 28 29 30
    }

    /**
     * @expectedException MongoDB\Exception\UnexpectedTypeException
     */
    public function testConstructorShouldRequireKeyOrderToBeScalar()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
31
        new IndexInput(['key' => ['x' => []]]);
32 33 34 35 36 37 38
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     */
    public function testConstructorShouldRequireNamespace()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
39
        new IndexInput(['key' => ['x' => 1]]);
40 41 42 43 44 45 46
    }

    /**
     * @expectedException MongoDB\Exception\UnexpectedTypeException
     */
    public function testConstructorShouldRequireNamespaceToBeString()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
47
        new IndexInput(['key' => ['x' => 1], 'ns' => 1]);
48 49 50 51 52 53 54
    }

    /**
     * @expectedException MongoDB\Exception\UnexpectedTypeException
     */
    public function testConstructorShouldRequireNameToBeString()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
55
        new IndexInput(['key' => ['x' => 1], 'ns' => 'foo.bar', 'name' => 1]);
56 57 58 59 60 61 62
    }

    /**
     * @dataProvider provideExpectedNameAndKey
     */
    public function testNameGeneration($expectedName, array $key)
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
63
        $this->assertSame($expectedName, (string) new IndexInput(['key' => $key, 'ns' => 'foo.bar']));
64 65 66 67
    }

    public function provideExpectedNameAndKey()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
68 69 70 71 72 73 74
        return [
            ['x_1', ['x' => 1]],
            ['x_1_y_-1', ['x' => 1, 'y' => -1]],
            ['loc_2dsphere', ['loc' => '2dsphere']],
            ['loc_2dsphere_x_1', ['loc' => '2dsphere', 'x' => 1]],
            ['doc_text', ['doc' => 'text']],
        ];
75 76 77 78
    }

    public function testBsonSerialization()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
79 80
        $expected = [
            'key' => ['x' => 1],
81 82
            'ns' => 'foo.bar',
            'name' => 'x_1',
Jeremy Mikola's avatar
Jeremy Mikola committed
83
        ];
84

Jeremy Mikola's avatar
Jeremy Mikola committed
85 86
        $indexInput = new IndexInput([
            'key' => ['x' => 1],
87
            'ns' => 'foo.bar',
Jeremy Mikola's avatar
Jeremy Mikola committed
88
        ]);
89

90
        $this->assertInstanceOf('MongoDB\BSON\Serializable', $indexInput);
91 92 93
        $this->assertEquals($expected, $indexInput->bsonSerialize());
    }
}