IndexInputTest.php 2.61 KB
Newer Older
1 2
<?php

3
namespace MongoDB\Tests\Model;
4 5 6

use MongoDB\Model\IndexInput;
use MongoDB\Tests\TestCase;
7
use stdClass;
8 9 10 11 12 13 14 15

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

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

    /**
28
     * @expectedException MongoDB\Exception\InvalidArgumentException
29
     * @dataProvider provideInvalidFieldOrderValues
30
     */
31
    public function testConstructorShouldRequireKeyFieldOrderToBeNumericOrString($order)
32
    {
33 34 35 36 37 38
        new IndexInput(['key' => ['x' => $order]]);
    }

    public function provideInvalidFieldOrderValues()
    {
        return $this->wrapValuesForDataProvider([true, [], new stdClass]);
39 40 41 42 43 44 45
    }

    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     */
    public function testConstructorShouldRequireNamespace()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
46
        new IndexInput(['key' => ['x' => 1]]);
47 48 49
    }

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

    /**
58
     * @expectedException MongoDB\Exception\InvalidArgumentException
59 60 61
     */
    public function testConstructorShouldRequireNameToBeString()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
62
        new IndexInput(['key' => ['x' => 1], 'ns' => 'foo.bar', 'name' => 1]);
63 64 65 66 67 68 69
    }

    /**
     * @dataProvider provideExpectedNameAndKey
     */
    public function testNameGeneration($expectedName, array $key)
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
70
        $this->assertSame($expectedName, (string) new IndexInput(['key' => $key, 'ns' => 'foo.bar']));
71 72 73 74
    }

    public function provideExpectedNameAndKey()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
75 76 77 78 79 80 81
        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']],
        ];
82 83 84 85
    }

    public function testBsonSerialization()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
86 87
        $expected = [
            'key' => ['x' => 1],
88 89
            'ns' => 'foo.bar',
            'name' => 'x_1',
Jeremy Mikola's avatar
Jeremy Mikola committed
90
        ];
91

Jeremy Mikola's avatar
Jeremy Mikola committed
92 93
        $indexInput = new IndexInput([
            'key' => ['x' => 1],
94
            'ns' => 'foo.bar',
Jeremy Mikola's avatar
Jeremy Mikola committed
95
        ]);
96

97
        $this->assertInstanceOf('MongoDB\BSON\Serializable', $indexInput);
98 99 100
        $this->assertEquals($expected, $indexInput->bsonSerialize());
    }
}