CollectionFunctionalTest.php 6.58 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    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());
    }

96 97
    public function testDrop()
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
98
        $writeResult = $this->collection->insertOne(['x' => 1]);
99 100 101 102 103 104
        $this->assertEquals(1, $writeResult->getInsertedCount());

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

106 107 108 109 110 111 112 113 114
    /**
     * @expectedException MongoDB\Exception\InvalidArgumentException
     * @todo Move this to a unit test once Manager can be mocked
     */
    public function testDropIndexShouldNotAllowWildcardCharacter()
    {
        $this->collection->dropIndex('*');
    }

115 116 117 118
    public function testFindOne()
    {
        $this->createFixtures(5);

Jeremy Mikola's avatar
Jeremy Mikola committed
119 120
        $filter = ['_id' => ['$lt' => 5]];
        $options = [
121
            'skip' => 1,
Jeremy Mikola's avatar
Jeremy Mikola committed
122 123
            'sort' => ['x' => -1],
        ];
124

125
        $expected = ['_id' => 3, 'x' => 33];
126

127
        $this->assertSameDocument($expected, $this->collection->findOne($filter, $options));
128 129
    }

130 131 132
    public function testWithOptionsInheritsReadPreferenceAndWriteConcern()
    {
        $collectionOptions = [
133
            'readConcern' => new ReadConcern(ReadConcern::LOCAL),
134
            'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
135
            'typeMap' => ['root' => 'array'],
136 137 138
            'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
        ];

139
        $collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName(), $collectionOptions);
140 141 142
        $clone = $collection->withOptions();
        $debug = $clone->__debugInfo();

143 144
        $this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
        $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
145 146
        $this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
        $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
147 148
        $this->assertInternalType('array', $debug['typeMap']);
        $this->assertSame(['root' => 'array'], $debug['typeMap']);
149 150 151 152 153 154 155
        $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }

    public function testWithOptionsPassesReadPreferenceAndWriteConcern()
    {
        $collectionOptions = [
156
            'readConcern' => new ReadConcern(ReadConcern::LOCAL),
157
            'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
158
            'typeMap' => ['root' => 'array'],
159 160 161 162 163 164
            'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
        ];

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

165 166
        $this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
        $this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
167 168
        $this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
        $this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
169 170
        $this->assertInternalType('array', $debug['typeMap']);
        $this->assertSame(['root' => 'array'], $debug['typeMap']);
171 172 173 174
        $this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
        $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
    }

175 176 177 178 179 180 181
    /**
     * Create data fixtures.
     *
     * @param integer $n
     */
    private function createFixtures($n)
    {
182
        $bulkWrite = new BulkWrite(['ordered' => true]);
183 184

        for ($i = 1; $i <= $n; $i++) {
Jeremy Mikola's avatar
Jeremy Mikola committed
185
            $bulkWrite->insert([
186 187
                '_id' => $i,
                'x' => (integer) ($i . $i),
Jeremy Mikola's avatar
Jeremy Mikola committed
188
            ]);
189 190 191 192 193 194
        }

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

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