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

3
namespace MongoDB\Tests\Model;
4

5
use MongoDB\Exception\InvalidArgumentException;
6 7
use MongoDB\Model\IndexInput;
use MongoDB\Tests\TestCase;
8
use stdClass;
9 10 11 12 13

class IndexInputTest extends TestCase
{
    public function testConstructorShouldRequireKey()
    {
14
        $this->expectException(InvalidArgumentException::class);
Jeremy Mikola's avatar
Jeremy Mikola committed
15
        new IndexInput([]);
16 17 18 19
    }

    public function testConstructorShouldRequireKeyToBeArrayOrObject()
    {
20
        $this->expectException(InvalidArgumentException::class);
Jeremy Mikola's avatar
Jeremy Mikola committed
21
        new IndexInput(['key' => 'foo']);
22 23 24
    }

    /**
25
     * @dataProvider provideInvalidFieldOrderValues
26
     */
27
    public function testConstructorShouldRequireKeyFieldOrderToBeNumericOrString($order)
28
    {
29
        $this->expectException(InvalidArgumentException::class);
30 31 32 33 34 35
        new IndexInput(['key' => ['x' => $order]]);
    }

    public function provideInvalidFieldOrderValues()
    {
        return $this->wrapValuesForDataProvider([true, [], new stdClass]);
36 37 38 39
    }

    public function testConstructorShouldRequireNamespace()
    {
40
        $this->expectException(InvalidArgumentException::class);
Jeremy Mikola's avatar
Jeremy Mikola committed
41
        new IndexInput(['key' => ['x' => 1]]);
42 43 44 45
    }

    public function testConstructorShouldRequireNamespaceToBeString()
    {
46
        $this->expectException(InvalidArgumentException::class);
Jeremy Mikola's avatar
Jeremy Mikola committed
47
        new IndexInput(['key' => ['x' => 1], 'ns' => 1]);
48 49 50 51
    }

    public function testConstructorShouldRequireNameToBeString()
    {
52
        $this->expectException(InvalidArgumentException::class);
Jeremy Mikola's avatar
Jeremy Mikola committed
53
        new IndexInput(['key' => ['x' => 1], 'ns' => 'foo.bar', 'name' => 1]);
54 55 56 57 58 59 60
    }

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

    public function provideExpectedNameAndKey()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
66 67 68 69 70 71 72
        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']],
        ];
73 74 75 76
    }

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

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

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