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

3
namespace MongoDB\Tests\Database;
4

5
use InvalidArgumentException;
6
use MongoDB\Driver\BulkWrite;
7
use MongoDB\Model\CollectionInfo;
8 9 10 11
use MongoDB\Model\CollectionInfoIterator;
use function call_user_func;
use function is_callable;
use function sprintf;
12 13

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

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

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

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

45 46
    public function testDropCollection()
    {
47 48 49 50
        $bulkWrite = new BulkWrite();
        $bulkWrite->insert(['x' => 1]);

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

53
        $commandResult = $this->database->dropCollection($this->getCollectionName());
54 55 56
        $this->assertCommandSucceeded($commandResult);
        $this->assertCollectionCount($this->getNamespace(), 0);
    }
57 58 59

    public function testListCollections()
    {
60 61
        $commandResult = $this->database->createCollection($this->getCollectionName());
        $this->assertCommandSucceeded($commandResult);
62 63

        $collections = $this->database->listCollections();
64
        $this->assertInstanceOf(CollectionInfoIterator::class, $collections);
65

66
        foreach ($collections as $collection) {
67
            $this->assertInstanceOf(CollectionInfo::class, $collection);
68 69 70
        }
    }

71 72 73 74 75 76
    public function testListCollectionsWithFilter()
    {
        $commandResult = $this->database->createCollection($this->getCollectionName());
        $this->assertCommandSucceeded($commandResult);

        $collectionName = $this->getCollectionName();
Jeremy Mikola's avatar
Jeremy Mikola committed
77
        $options = ['filter' => ['name' => $collectionName]];
78 79

        $collections = $this->database->listCollections($options);
80
        $this->assertInstanceOf(CollectionInfoIterator::class, $collections);
81 82

        foreach ($collections as $collection) {
83
            $this->assertInstanceOf(CollectionInfo::class, $collection);
84 85 86 87
            $this->assertEquals($collectionName, $collection->getName());
        }
    }

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    /**
     * 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();

106 107 108
        $foundCollection = null;

        foreach ($collections as $collection) {
109
            if ($collection->getName() === $collectionName) {
110 111 112 113 114
                $foundCollection = $collection;
                break;
            }
        }

115 116 117 118 119
        $this->assertNotNull($foundCollection, sprintf('Found %s collection in the database', $collectionName));

        if ($callback !== null) {
            call_user_func($callback, $foundCollection);
        }
120
    }
121
}