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

namespace MongoDB\Tests\Collection;

5
use MongoDB\BSON\Javascript;
6
use MongoDB\Collection;
7
use MongoDB\Driver\BulkWrite;
8
use MongoDB\Driver\ReadConcern;
9 10
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
11
use MongoDB\Exception\InvalidArgumentException;
12
use MongoDB\Operation\Count;
13
use MongoDB\Operation\MapReduce;
14 15
use MongoDB\Tests\CommandObserver;
use stdClass;
16

17 18 19 20 21
/**
 * Functional tests for the Collection class.
 */
class CollectionFunctionalTest extends FunctionalTestCase
{
22
    /**
23
     * @dataProvider provideInvalidDatabaseAndCollectionNames
24
     */
25
    public function testConstructorDatabaseNameArgument($databaseName)
26
    {
27
        $this->expectException(InvalidArgumentException::class);
28
        // TODO: Move to unit test once ManagerInterface can be mocked (PHPC-378)
29
        new Collection($this->manager, $databaseName, $this->getCollectionName());
30 31
    }

32 33 34 35 36
    /**
     * @dataProvider provideInvalidDatabaseAndCollectionNames
     */
    public function testConstructorCollectionNameArgument($collectionName)
    {
37
        $this->expectException(InvalidArgumentException::class);
38 39 40 41 42
        // TODO: Move to unit test once ManagerInterface can be mocked (PHPC-378)
        new Collection($this->manager, $this->getDatabaseName(), $collectionName);
    }

    public function provideInvalidDatabaseAndCollectionNames()
43
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
44 45 46 47
        return [
            [null],
            [''],
        ];
48 49
    }

50 51 52 53 54
    /**
     * @dataProvider provideInvalidConstructorOptions
     */
    public function testConstructorOptionTypeChecks(array $options)
    {
55
        $this->expectException(InvalidArgumentException::class);
56
        new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName(), $options);
57 58 59 60 61 62
    }

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

63 64 65 66
        foreach ($this->getInvalidReadConcernValues() as $value) {
            $options[][] = ['readConcern' => $value];
        }

67 68 69 70
        foreach ($this->getInvalidReadPreferenceValues() as $value) {
            $options[][] = ['readPreference' => $value];
        }

71 72 73 74
        foreach ($this->getInvalidArrayValues() as $value) {
            $options[][] = ['typeMap' => $value];
        }

75 76 77 78 79 80 81
        foreach ($this->getInvalidWriteConcernValues() as $value) {
            $options[][] = ['writeConcern' => $value];
        }

        return $options;
    }

82 83 84 85 86
    public function testGetManager()
    {
        $this->assertSame($this->manager, $this->collection->getManager());
    }

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    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());
    }

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    public function testCreateIndexSplitsCommandOptions()
    {
        if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
            $this->markTestSkipped('Sessions are not supported');
        }

        (new CommandObserver)->observe(
            function() {
                $this->collection->createIndex(
                    ['x' => 1],
                    [
                        'maxTimeMS' => 1000,
                        'session' => $this->manager->startSession(),
                        'sparse' => true,
                        'unique' => true,
                        'writeConcern' => new WriteConcern(1),
                    ]
                );
            },
            function(stdClass $command) {
                $this->assertObjectHasAttribute('lsid', $command);
                $this->assertObjectHasAttribute('maxTimeMS', $command);
                $this->assertObjectHasAttribute('writeConcern', $command);
                $this->assertObjectHasAttribute('sparse', $command->indexes[0]);
                $this->assertObjectHasAttribute('unique', $command->indexes[0]);
            }
        );
    }

136 137
    public function testDrop()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
138
        $writeResult = $this->collection->insertOne(['x' => 1]);
139 140 141 142 143 144
        $this->assertEquals(1, $writeResult->getInsertedCount());

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

146 147 148 149 150
    /**
     * @todo Move this to a unit test once Manager can be mocked
     */
    public function testDropIndexShouldNotAllowWildcardCharacter()
    {
151
        $this->expectException(InvalidArgumentException::class);
152 153 154
        $this->collection->dropIndex('*');
    }

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    public function testExplain()
    {
        if (version_compare($this->getServerVersion(), '3.0.0', '<')) {
            $this->markTestSkipped('Explain command is not supported');
        }

        $this->createFixtures(3);

        $operation = new Count($this->getDatabaseName(), $this->getCollectionName(), ['x' => ['$gte' => 1]], []);

        $result = $this->collection->explain($operation);

        $this->assertArrayHasKey('queryPlanner', $result);
    }

170 171 172 173
    public function testFindOne()
    {
        $this->createFixtures(5);

Jeremy Mikola's avatar
Jeremy Mikola committed
174 175
        $filter = ['_id' => ['$lt' => 5]];
        $options = [
176
            'skip' => 1,
Jeremy Mikola's avatar
Jeremy Mikola committed
177 178
            'sort' => ['x' => -1],
        ];
179

180
        $expected = ['_id' => 3, 'x' => 33];
181

182
        $this->assertSameDocument($expected, $this->collection->findOne($filter, $options));
183 184
    }

185
    public function testWithOptionsInheritsOptions()
186 187
    {
        $collectionOptions = [
188
            'readConcern' => new ReadConcern(ReadConcern::LOCAL),
189
            'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
190
            'typeMap' => ['root' => 'array'],
191 192 193
            'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
        ];

194
        $collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName(), $collectionOptions);
195 196 197
        $clone = $collection->withOptions();
        $debug = $clone->__debugInfo();

198 199 200
        $this->assertSame($this->manager, $debug['manager']);
        $this->assertSame($this->getDatabaseName(), $debug['databaseName']);
        $this->assertSame($this->getCollectionName(), $debug['collectionName']);
201 202
        $this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
        $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
203 204
        $this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
        $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
205 206
        $this->assertInternalType('array', $debug['typeMap']);
        $this->assertSame(['root' => 'array'], $debug['typeMap']);
207 208 209 210
        $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }

211
    public function testWithOptionsPassesOptions()
212 213
    {
        $collectionOptions = [
214
            'readConcern' => new ReadConcern(ReadConcern::LOCAL),
215
            'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
216
            'typeMap' => ['root' => 'array'],
217 218 219 220 221 222
            'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
        ];

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

223 224
        $this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
        $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
225 226
        $this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
        $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
227 228
        $this->assertInternalType('array', $debug['typeMap']);
        $this->assertSame(['root' => 'array'], $debug['typeMap']);
229 230 231 232
        $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    public function testMapReduce()
    {
        $this->createFixtures(3);

        $map = new Javascript('function() { emit(1, this.x); }');
        $reduce = new Javascript('function(key, values) { return Array.sum(values); }');
        $out = ['inline' => 1];

        $result = $this->collection->mapReduce($map, $reduce, $out);

        $this->assertInstanceOf('MongoDB\MapReduceResult', $result);
        $expected = [
            [ '_id' => 1.0, 'value' => 66.0 ],
        ];

        $this->assertSameDocuments($expected, $result);

        $this->assertGreaterThanOrEqual(0, $result->getExecutionTimeMS());
        $this->assertNotEmpty($result->getCounts());
    }

254 255 256 257 258 259 260
    /**
     * Create data fixtures.
     *
     * @param integer $n
     */
    private function createFixtures($n)
    {
261
        $bulkWrite = new BulkWrite(['ordered' => true]);
262 263

        for ($i = 1; $i <= $n; $i++) {
Jeremy Mikola's avatar
Jeremy Mikola committed
264
            $bulkWrite->insert([
265 266
                '_id' => $i,
                'x' => (integer) ($i . $i),
Jeremy Mikola's avatar
Jeremy Mikola committed
267
            ]);
268 269 270 271 272 273
        }

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

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