CollectionManagementFunctionalTest.php 4.15 KB
Newer Older
1 2
<?php

3
namespace MongoDB\Tests\Database;
4

5
use MongoDB\Driver\BulkWrite;
6 7
use MongoDB\Model\CollectionInfo;
use InvalidArgumentException;
8 9

/**
10
 * Functional tests for collection management methods.
11
 */
12
class CollectionManagementFunctionalTest extends FunctionalTestCase
13
{
14 15 16 17 18 19 20 21 22 23 24 25
    public function testCreateCollection()
    {
        $that = $this;
        $basicCollectionName = $this->getCollectionName() . '.basic';

        $commandResult = $this->database->createCollection($basicCollectionName);
        $this->assertCommandSucceeded($commandResult);
        $this->assertCollectionExists($basicCollectionName, function(CollectionInfo $info) use ($that) {
            $that->assertFalse($info->isCapped());
        });

        $cappedCollectionName = $this->getCollectionName() . '.capped';
Jeremy Mikola's avatar
Jeremy Mikola committed
26
        $cappedCollectionOptions = [
27 28 29
            'capped' => true,
            'max' => 100,
            'size' => 1048576,
Jeremy Mikola's avatar
Jeremy Mikola committed
30
        ];
31 32 33 34 35 36 37 38 39 40

        $commandResult = $this->database->createCollection($cappedCollectionName, $cappedCollectionOptions);
        $this->assertCommandSucceeded($commandResult);
        $this->assertCollectionExists($cappedCollectionName, function(CollectionInfo $info) use ($that) {
            $that->assertTrue($info->isCapped());
            $that->assertEquals(100, $info->getCappedMax());
            $that->assertEquals(1048576, $info->getCappedSize());
        });
    }

41 42
    public function testDropCollection()
    {
43 44 45 46
        $bulkWrite = new BulkWrite();
        $bulkWrite->insert(['x' => 1]);

        $writeResult = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);
47 48
        $this->assertEquals(1, $writeResult->getInsertedCount());

49
        $commandResult = $this->database->dropCollection($this->getCollectionName());
50 51 52
        $this->assertCommandSucceeded($commandResult);
        $this->assertCollectionCount($this->getNamespace(), 0);
    }
53 54 55

    public function testListCollections()
    {
56 57
        $commandResult = $this->database->createCollection($this->getCollectionName());
        $this->assertCommandSucceeded($commandResult);
58 59 60 61

        $collections = $this->database->listCollections();
        $this->assertInstanceOf('MongoDB\Model\CollectionInfoIterator', $collections);

62 63 64 65 66
        foreach ($collections as $collection) {
            $this->assertInstanceOf('MongoDB\Model\CollectionInfo', $collection);
        }
    }

67 68 69 70 71 72
    public function testListCollectionsWithFilter()
    {
        $commandResult = $this->database->createCollection($this->getCollectionName());
        $this->assertCommandSucceeded($commandResult);

        $collectionName = $this->getCollectionName();
Jeremy Mikola's avatar
Jeremy Mikola committed
73
        $options = ['filter' => ['name' => $collectionName]];
74 75 76 77 78 79 80 81 82 83

        $collections = $this->database->listCollections($options);
        $this->assertInstanceOf('MongoDB\Model\CollectionInfoIterator', $collections);

        foreach ($collections as $collection) {
            $this->assertInstanceOf('MongoDB\Model\CollectionInfo', $collection);
            $this->assertEquals($collectionName, $collection->getName());
        }
    }

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    /**
     * Asserts that a collection with the given name exists in the database.
     *
     * An optional $callback may be provided, which should take a CollectionInfo
     * argument as its first and only parameter. If a CollectionInfo matching
     * the given name is found, it will be passed to the callback, which may
     * perform additional assertions.
     *
     * @param callable $callback
     */
    private function assertCollectionExists($collectionName, $callback = null)
    {
        if ($callback !== null && ! is_callable($callback)) {
            throw new InvalidArgumentException('$callback is not a callable');
        }

        $collections = $this->database->listCollections();

102 103 104
        $foundCollection = null;

        foreach ($collections as $collection) {
105
            if ($collection->getName() === $collectionName) {
106 107 108 109 110
                $foundCollection = $collection;
                break;
            }
        }

111 112 113 114 115
        $this->assertNotNull($foundCollection, sprintf('Found %s collection in the database', $collectionName));

        if ($callback !== null) {
            call_user_func($callback, $foundCollection);
        }
116
    }
117
}