CollectionFunctionalTest.php 6.15 KB
Newer Older
1 2 3 4
<?php

namespace MongoDB\Tests\Collection;

5
use MongoDB\Collection;
6
use MongoDB\Driver\BulkWrite;
7
use MongoDB\Driver\ReadConcern;
8 9
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
10

11 12 13 14 15
/**
 * Functional tests for the Collection class.
 */
class CollectionFunctionalTest extends FunctionalTestCase
{
16 17 18 19 20 21 22 23 24 25 26 27
    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     * @dataProvider provideInvalidNamespaceValues
     */
    public function testConstructorNamespaceArgument($namespace)
    {
        // TODO: Move to unit test once ManagerInterface can be mocked (PHPC-378)
        new Collection($this->manager, $namespace);
    }

    public function provideInvalidNamespaceValues()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
28 29 30 31 32 33 34
        return [
            [null],
            [''],
            ['db_collection'],
            ['db'],
            ['.collection'],
        ];
35 36
    }

37
    /**
38
     * @expectedException MongoDB\Exception\InvalidArgumentException
39 40 41 42 43 44 45 46 47 48 49
     * @dataProvider provideInvalidConstructorOptions
     */
    public function testConstructorOptionTypeChecks(array $options)
    {
        new Collection($this->manager, $this->getNamespace(), $options);
    }

    public function provideInvalidConstructorOptions()
    {
        $options = [];

50 51 52 53
        foreach ($this->getInvalidReadConcernValues() as $value) {
            $options[][] = ['readConcern' => $value];
        }

54 55 56 57
        foreach ($this->getInvalidReadPreferenceValues() as $value) {
            $options[][] = ['readPreference' => $value];
        }

58 59 60 61
        foreach ($this->getInvalidArrayValues() as $value) {
            $options[][] = ['typeMap' => $value];
        }

62 63 64 65 66 67 68
        foreach ($this->getInvalidWriteConcernValues() as $value) {
            $options[][] = ['writeConcern' => $value];
        }

        return $options;
    }

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    public function testToString()
    {
        $this->assertEquals($this->getNamespace(), (string) $this->collection);
    }

    public function getGetCollectionName()
    {
        $this->assertEquals($this->getCollectionName(), $this->collection->getCollectionName());
    }

    public function getGetDatabaseName()
    {
        $this->assertEquals($this->getDatabaseName(), $this->collection->getDatabaseName());
    }

    public function testGetNamespace()
    {
        $this->assertEquals($this->getNamespace(), $this->collection->getNamespace());
    }

89 90
    public function testDrop()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
91
        $writeResult = $this->collection->insertOne(['x' => 1]);
92 93 94 95 96 97
        $this->assertEquals(1, $writeResult->getInsertedCount());

        $commandResult = $this->collection->drop();
        $this->assertCommandSucceeded($commandResult);
        $this->assertCollectionCount($this->getNamespace(), 0);
    }
98

99 100 101 102 103 104 105 106 107
    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     * @todo Move this to a unit test once Manager can be mocked
     */
    public function testDropIndexShouldNotAllowWildcardCharacter()
    {
        $this->collection->dropIndex('*');
    }

108 109 110 111
    public function testFindOne()
    {
        $this->createFixtures(5);

Jeremy Mikola's avatar
Jeremy Mikola committed
112 113
        $filter = ['_id' => ['$lt' => 5]];
        $options = [
114
            'skip' => 1,
Jeremy Mikola's avatar
Jeremy Mikola committed
115 116
            'sort' => ['x' => -1],
        ];
117

Jeremy Mikola's avatar
Jeremy Mikola committed
118
        $expected = (object) ['_id' => 3, 'x' => 33];
119

120
        $this->assertEquals($expected, $this->collection->findOne($filter, $options));
121 122
    }

123 124 125
    public function testWithOptionsInheritsReadPreferenceAndWriteConcern()
    {
        $collectionOptions = [
126
            'readConcern' => new ReadConcern(ReadConcern::LOCAL),
127
            'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
128
            'typeMap' => ['root' => 'array'],
129 130 131 132 133 134 135
            'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
        ];

        $collection = new Collection($this->manager, $this->getNamespace(), $collectionOptions);
        $clone = $collection->withOptions();
        $debug = $clone->__debugInfo();

136 137
        $this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
        $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
138 139
        $this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
        $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
140 141
        $this->assertInternalType('array', $debug['typeMap']);
        $this->assertSame(['root' => 'array'], $debug['typeMap']);
142 143 144 145 146 147 148
        $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }

    public function testWithOptionsPassesReadPreferenceAndWriteConcern()
    {
        $collectionOptions = [
149
            'readConcern' => new ReadConcern(ReadConcern::LOCAL),
150
            'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
151
            'typeMap' => ['root' => 'array'],
152 153 154 155 156 157
            'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
        ];

        $clone = $this->collection->withOptions($collectionOptions);
        $debug = $clone->__debugInfo();

158 159
        $this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
        $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
160 161
        $this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
        $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
162 163
        $this->assertInternalType('array', $debug['typeMap']);
        $this->assertSame(['root' => 'array'], $debug['typeMap']);
164 165 166 167
        $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }

168 169 170 171 172 173 174
    /**
     * Create data fixtures.
     *
     * @param integer $n
     */
    private function createFixtures($n)
    {
175
        $bulkWrite = new BulkWrite(['ordered' => true]);
176 177

        for ($i = 1; $i <= $n; $i++) {
Jeremy Mikola's avatar
Jeremy Mikola committed
178
            $bulkWrite->insert([
179 180
                '_id' => $i,
                'x' => (integer) ($i . $i),
Jeremy Mikola's avatar
Jeremy Mikola committed
181
            ]);
182 183 184 185 186 187
        }

        $result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);

        $this->assertEquals($n, $result->getInsertedCount());
    }
188
}