CollectionFunctionalTest.php 7.6 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\Operation\MapReduce;
12

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

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

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

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

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

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

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

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

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

        return $options;
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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