DropCollectionFunctionalTest.php 1.77 KB
Newer Older
1 2 3 4 5
<?php

namespace MongoDB\Tests\Operation;

use MongoDB\Operation\DropCollection;
6
use MongoDB\Operation\InsertOne;
7 8 9 10 11 12
use MongoDB\Operation\ListCollections;

class DropCollectionFunctionalTest extends FunctionalTestCase
{
    public function testDropExistingCollection()
    {
13 14 15 16
        $server = $this->getPrimaryServer();

        $insertOne = new InsertOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1]);
        $writeResult = $insertOne->execute($server);
17 18 19 20 21
        $this->assertEquals(1, $writeResult->getInsertedCount());

        $operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
        $operation->execute($server);

22
        $this->assertCollectionDoesNotExist($this->getCollectionName());
23 24 25 26 27 28 29
    }

    /**
     * @depends testDropExistingCollection
     */
    public function testDropNonexistentCollection()
    {
30
        $this->assertCollectionDoesNotExist($this->getCollectionName());
31 32

        $operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
33
        $operation->execute($this->getPrimaryServer());
34 35 36 37 38 39 40 41
    }

    /**
     * Asserts that a collection with the given name does not exist on the
     * server.
     *
     * @param string $collectionName
     */
42
    private function assertCollectionDoesNotExist($collectionName)
43
    {
44 45
        $operation = new ListCollections($this->getDatabaseName());
        $collections = $operation->execute($this->getPrimaryServer());
46 47 48 49 50 51 52 53 54 55

        $foundCollection = null;

        foreach ($collections as $collection) {
            if ($collection->getName() === $collectionName) {
                $foundCollection = $collection;
                break;
            }
        }

56
        $this->assertNull($foundCollection, sprintf('Collection %s exists', $collectionName));
57 58
    }
}