CollectionFunctionalTest.php 6.88 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
    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
18
     * @dataProvider provideInvalidDatabaseAndCollectionNames
19
     */
20
    public function testConstructorDatabaseNameArgument($databaseName)
21 22
    {
        // TODO: Move to unit test once ManagerInterface can be mocked (PHPC-378)
23
        new Collection($this->manager, $databaseName, $this->getCollectionName());
24 25
    }

26 27 28 29 30 31 32 33 34 35 36
    /**
     * @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()
37
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
38 39 40 41
        return [
            [null],
            [''],
        ];
42 43
    }

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

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

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

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

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

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

        return $options;
    }

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

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

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

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

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

120 121 122 123
    public function testFindOne()
    {
        $this->createFixtures(5);

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

130
        $expected = ['_id' => 3, 'x' => 33];
131

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

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

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

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

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

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

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

183 184 185 186 187 188 189
    /**
     * Create data fixtures.
     *
     * @param integer $n
     */
    private function createFixtures($n)
    {
190
        $bulkWrite = new BulkWrite(['ordered' => true]);
191 192

        for ($i = 1; $i <= $n; $i++) {
Jeremy Mikola's avatar
Jeremy Mikola committed
193
            $bulkWrite->insert([
194 195
                '_id' => $i,
                'x' => (integer) ($i . $i),
Jeremy Mikola's avatar
Jeremy Mikola committed
196
            ]);
197 198 199 200 201 202
        }

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

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