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

3
namespace MongoDB\Tests\Model;
4

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

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

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

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

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

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

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

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

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

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

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

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

89
        $this->assertInstanceOf(Serializable::class, $indexInput);
90 91 92
        $this->assertEquals($expected, $indexInput->bsonSerialize());
    }
}