FunctionalTestCase.php 1.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?php

namespace MongoDB\Tests\Collection;

use MongoDB\Collection;
use MongoDB\Database;
use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase;

/**
 * Base class for Collection functional tests.
 */
abstract class FunctionalTestCase extends BaseFunctionalTestCase
{
    protected $collection;

    public function setUp()
    {
        parent::setUp();

        $this->collection = new Collection($this->manager, $this->getNamespace());
        $this->dropCollectionIfItExists($this->collection);
    }

24 25 26 27 28 29 30 31 32
    public function tearDown()
    {
        if ($this->hasFailed()) {
            return;
        }

        $this->dropCollectionIfItExists($this->collection);
    }

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    /**
     * Drop the collection if it exists.
     *
     * @param Collection $collection
     */
    protected function dropCollectionIfItExists(Collection $collection)
    {
        $database = new Database($this->manager, $collection->getDatabaseName());
        $collections = $database->listCollections(array('filter' => array('name' => $collection->getCollectionName())));

        if (iterator_count($collections) > 0) {
            $this->assertCommandSucceeded($collection->drop());
        }
    }
}