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

3
namespace MongoDB\Tests\Database;
4

5 6
use MongoDB\Model\CollectionInfo;
use InvalidArgumentException;
7 8

/**
9
 * Functional tests for collection management methods.
10
 */
11
class CollectionManagementFunctionalTest extends FunctionalTestCase
12
{
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    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';
        $cappedCollectionOptions = array(
            'capped' => true,
            'max' => 100,
            'size' => 1048576,
        );

        $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());
        });
    }

40 41 42 43 44
    public function testDropCollection()
    {
        $writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1));
        $this->assertEquals(1, $writeResult->getInsertedCount());

45
        $commandResult = $this->database->dropCollection($this->getCollectionName());
46 47 48
        $this->assertCommandSucceeded($commandResult);
        $this->assertCollectionCount($this->getNamespace(), 0);
    }
49 50 51

    public function testListCollections()
    {
52 53
        $commandResult = $this->database->createCollection($this->getCollectionName());
        $this->assertCommandSucceeded($commandResult);
54 55 56 57

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

58 59 60 61 62
        foreach ($collections as $collection) {
            $this->assertInstanceOf('MongoDB\Model\CollectionInfo', $collection);
        }
    }

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    public function testListCollectionsWithFilter()
    {
        $commandResult = $this->database->createCollection($this->getCollectionName());
        $this->assertCommandSucceeded($commandResult);

        $collectionName = $this->getCollectionName();
        $options = array('filter' => array('name' => $collectionName));

        $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());
        }
    }

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

98 99 100
        $foundCollection = null;

        foreach ($collections as $collection) {
101
            if ($collection->getName() === $collectionName) {
102 103 104 105 106
                $foundCollection = $collection;
                break;
            }
        }

107 108 109 110 111
        $this->assertNotNull($foundCollection, sprintf('Found %s collection in the database', $collectionName));

        if ($callback !== null) {
            call_user_func($callback, $foundCollection);
        }
112
    }
113
}