CollectionFunctionalTest.php 7.63 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\MapReduce;
13

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

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

    public function provideInvalidDatabaseAndCollectionNames()
40
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
41 42 43 44
        return [
            [null],
            [''],
        ];
45 46
    }

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

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

60 61 62 63
        foreach ($this->getInvalidReadConcernValues() as $value) {
            $options[][] = ['readConcern' => $value];
        }

64 65 66 67
        foreach ($this->getInvalidReadPreferenceValues() as $value) {
            $options[][] = ['readPreference' => $value];
        }

68 69 70 71
        foreach ($this->getInvalidArrayValues() as $value) {
            $options[][] = ['typeMap' => $value];
        }

72 73 74 75 76 77 78
        foreach ($this->getInvalidWriteConcernValues() as $value) {
            $options[][] = ['writeConcern' => $value];
        }

        return $options;
    }

79 80 81 82 83
    public function testGetManager()
    {
        $this->assertSame($this->manager, $this->collection->getManager());
    }

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

104 105
    public function testDrop()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
106
        $writeResult = $this->collection->insertOne(['x' => 1]);
107 108 109 110 111 112
        $this->assertEquals(1, $writeResult->getInsertedCount());

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

114 115 116 117 118
    /**
     * @todo Move this to a unit test once Manager can be mocked
     */
    public function testDropIndexShouldNotAllowWildcardCharacter()
    {
119
        $this->expectException(InvalidArgumentException::class);
120 121 122
        $this->collection->dropIndex('*');
    }

123 124 125 126
    public function testFindOne()
    {
        $this->createFixtures(5);

Jeremy Mikola's avatar
Jeremy Mikola committed
127 128
        $filter = ['_id' => ['$lt' => 5]];
        $options = [
129
            'skip' => 1,
Jeremy Mikola's avatar
Jeremy Mikola committed
130 131
            'sort' => ['x' => -1],
        ];
132

133
        $expected = ['_id' => 3, 'x' => 33];
134

135
        $this->assertSameDocument($expected, $this->collection->findOne($filter, $options));
136 137
    }

138
    public function testWithOptionsInheritsOptions()
139 140
    {
        $collectionOptions = [
141
            'readConcern' => new ReadConcern(ReadConcern::LOCAL),
142
            'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
143
            'typeMap' => ['root' => 'array'],
144 145 146
            'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
        ];

147
        $collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName(), $collectionOptions);
148 149 150
        $clone = $collection->withOptions();
        $debug = $clone->__debugInfo();

151 152 153
        $this->assertSame($this->manager, $debug['manager']);
        $this->assertSame($this->getDatabaseName(), $debug['databaseName']);
        $this->assertSame($this->getCollectionName(), $debug['collectionName']);
154 155
        $this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
        $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
156 157
        $this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
        $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
158 159
        $this->assertInternalType('array', $debug['typeMap']);
        $this->assertSame(['root' => 'array'], $debug['typeMap']);
160 161 162 163
        $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }

164
    public function testWithOptionsPassesOptions()
165 166
    {
        $collectionOptions = [
167
            'readConcern' => new ReadConcern(ReadConcern::LOCAL),
168
            'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
169
            'typeMap' => ['root' => 'array'],
170 171 172 173 174 175
            'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
        ];

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

176 177
        $this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
        $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
178 179
        $this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
        $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
180 181
        $this->assertInternalType('array', $debug['typeMap']);
        $this->assertSame(['root' => 'array'], $debug['typeMap']);
182 183 184 185
        $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    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());
    }

207 208 209 210 211 212 213
    /**
     * Create data fixtures.
     *
     * @param integer $n
     */
    private function createFixtures($n)
    {
214
        $bulkWrite = new BulkWrite(['ordered' => true]);
215 216

        for ($i = 1; $i <= $n; $i++) {
Jeremy Mikola's avatar
Jeremy Mikola committed
217
            $bulkWrite->insert([
218 219
                '_id' => $i,
                'x' => (integer) ($i . $i),
Jeremy Mikola's avatar
Jeremy Mikola committed
220
            ]);
221 222 223 224 225 226
        }

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

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