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

namespace MongoDB\Tests\Collection;

5 6
use MongoDB\Driver\BulkWrite;

7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**
 * Functional tests for the Collection class.
 */
class CollectionFunctionalTest extends FunctionalTestCase
{
    public function testDrop()
    {
        $writeResult = $this->collection->insertOne(array('x' => 1));
        $this->assertEquals(1, $writeResult->getInsertedCount());

        $commandResult = $this->collection->drop();
        $this->assertCommandSucceeded($commandResult);
        $this->assertCollectionCount($this->getNamespace(), 0);
    }
21 22 23 24 25 26 27 28 29 30 31

    public function testFindOne()
    {
        $this->createFixtures(5);

        $filter = array('_id' => array('$lt' => 5));
        $options = array(
            'skip' => 1,
            'sort' => array('x' => -1),
        );

32
        $expected = (object) array('_id' => 3, 'x' => 33);
33

34
        $this->assertEquals($expected, $this->collection->findOne($filter, $options));
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    }

    /**
     * Create data fixtures.
     *
     * @param integer $n
     */
    private function createFixtures($n)
    {
        $bulkWrite = new BulkWrite(true);

        for ($i = 1; $i <= $n; $i++) {
            $bulkWrite->insert(array(
                '_id' => $i,
                'x' => (integer) ($i . $i),
            ));
        }

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

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