Commit 50ef5ed6 authored by Jeremy Mikola's avatar Jeremy Mikola

Refactor functional tests to use DropCollection

parent ed4cfa1c
...@@ -4,6 +4,7 @@ namespace MongoDB\Tests\Collection\CrudSpec; ...@@ -4,6 +4,7 @@ namespace MongoDB\Tests\Collection\CrudSpec;
use MongoDB\Collection; use MongoDB\Collection;
use MongoDB\Driver\ReadPreference; use MongoDB\Driver\ReadPreference;
use MongoDB\Operation\DropCollection;
/** /**
* CRUD spec functional tests for aggregate(). * CRUD spec functional tests for aggregate().
...@@ -48,7 +49,8 @@ class AggregateFunctionalTest extends FunctionalTestCase ...@@ -48,7 +49,8 @@ class AggregateFunctionalTest extends FunctionalTestCase
} }
$outputCollection = new Collection($this->manager, $this->getNamespace() . '_output'); $outputCollection = new Collection($this->manager, $this->getNamespace() . '_output');
$this->dropCollectionIfItExists($outputCollection); $operation = new DropCollection($this->getDatabaseName(), $outputCollection->getCollectionName());
$operation->execute($this->getPrimaryServer());
$this->collection->aggregate( $this->collection->aggregate(
array( array(
...@@ -66,6 +68,7 @@ class AggregateFunctionalTest extends FunctionalTestCase ...@@ -66,6 +68,7 @@ class AggregateFunctionalTest extends FunctionalTestCase
$this->assertSameDocuments($expected, $outputCollection->find()); $this->assertSameDocuments($expected, $outputCollection->find());
// Manually clean up our output collection // Manually clean up our output collection
$this->dropCollectionIfItExists($outputCollection); $operation = new DropCollection($this->getDatabaseName(), $outputCollection->getCollectionName());
$operation->execute($this->getPrimaryServer());
} }
} }
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
namespace MongoDB\Tests\Collection; namespace MongoDB\Tests\Collection;
use MongoDB\Collection; use MongoDB\Collection;
use MongoDB\Database; use MongoDB\Operation\DropCollection;
use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase; use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase;
/** /**
...@@ -18,7 +18,8 @@ abstract class FunctionalTestCase extends BaseFunctionalTestCase ...@@ -18,7 +18,8 @@ abstract class FunctionalTestCase extends BaseFunctionalTestCase
parent::setUp(); parent::setUp();
$this->collection = new Collection($this->manager, $this->getNamespace()); $this->collection = new Collection($this->manager, $this->getNamespace());
$this->dropCollectionIfItExists($this->collection); $operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
$operation->execute($this->getPrimaryServer());
} }
public function tearDown() public function tearDown()
...@@ -27,21 +28,7 @@ abstract class FunctionalTestCase extends BaseFunctionalTestCase ...@@ -27,21 +28,7 @@ abstract class FunctionalTestCase extends BaseFunctionalTestCase
return; return;
} }
$this->dropCollectionIfItExists($this->collection); $operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
} $operation->execute($this->getPrimaryServer());
/**
* 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());
}
} }
} }
...@@ -68,6 +68,11 @@ abstract class FunctionalTestCase extends TestCase ...@@ -68,6 +68,11 @@ abstract class FunctionalTestCase extends TestCase
); );
} }
protected function getPrimaryServer()
{
return $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
}
protected function getServerVersion(ReadPreference $readPreference = null) protected function getServerVersion(ReadPreference $readPreference = null)
{ {
$cursor = $this->manager->executeCommand( $cursor = $this->manager->executeCommand(
......
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
namespace MongoDB\Tests\Operation; namespace MongoDB\Tests\Operation;
use MongoDB\Collection;
use MongoDB\Driver\ReadPreference; use MongoDB\Driver\ReadPreference;
use MongoDB\Operation\DropCollection;
use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase; use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase;
/** /**
...@@ -10,8 +12,21 @@ use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase; ...@@ -10,8 +12,21 @@ use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase;
*/ */
abstract class FunctionalTestCase extends BaseFunctionalTestCase abstract class FunctionalTestCase extends BaseFunctionalTestCase
{ {
public function getPrimaryServer() public function setUp()
{ {
return $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY)); parent::setUp();
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
$operation->execute($this->getPrimaryServer());
}
public function tearDown()
{
if ($this->hasFailed()) {
return;
}
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
$operation->execute($this->getPrimaryServer());
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment