Skip to content

Commit 50ef5ed

Browse files
committed
Refactor functional tests to use DropCollection
1 parent ed4cfa1 commit 50ef5ed

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

tests/Collection/CrudSpec/AggregateFunctionalTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use MongoDB\Collection;
66
use MongoDB\Driver\ReadPreference;
7+
use MongoDB\Operation\DropCollection;
78

89
/**
910
* CRUD spec functional tests for aggregate().
@@ -48,7 +49,8 @@ public function testAggregateWithOut()
4849
}
4950

5051
$outputCollection = new Collection($this->manager, $this->getNamespace() . '_output');
51-
$this->dropCollectionIfItExists($outputCollection);
52+
$operation = new DropCollection($this->getDatabaseName(), $outputCollection->getCollectionName());
53+
$operation->execute($this->getPrimaryServer());
5254

5355
$this->collection->aggregate(
5456
array(
@@ -66,6 +68,7 @@ public function testAggregateWithOut()
6668
$this->assertSameDocuments($expected, $outputCollection->find());
6769

6870
// Manually clean up our output collection
69-
$this->dropCollectionIfItExists($outputCollection);
71+
$operation = new DropCollection($this->getDatabaseName(), $outputCollection->getCollectionName());
72+
$operation->execute($this->getPrimaryServer());
7073
}
7174
}

tests/Collection/FunctionalTestCase.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace MongoDB\Tests\Collection;
44

55
use MongoDB\Collection;
6-
use MongoDB\Database;
6+
use MongoDB\Operation\DropCollection;
77
use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase;
88

99
/**
@@ -18,7 +18,8 @@ public function setUp()
1818
parent::setUp();
1919

2020
$this->collection = new Collection($this->manager, $this->getNamespace());
21-
$this->dropCollectionIfItExists($this->collection);
21+
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
22+
$operation->execute($this->getPrimaryServer());
2223
}
2324

2425
public function tearDown()
@@ -27,21 +28,7 @@ public function tearDown()
2728
return;
2829
}
2930

30-
$this->dropCollectionIfItExists($this->collection);
31-
}
32-
33-
/**
34-
* Drop the collection if it exists.
35-
*
36-
* @param Collection $collection
37-
*/
38-
protected function dropCollectionIfItExists(Collection $collection)
39-
{
40-
$database = new Database($this->manager, $collection->getDatabaseName());
41-
$collections = $database->listCollections(array('filter' => array('name' => $collection->getCollectionName())));
42-
43-
if (iterator_count($collections) > 0) {
44-
$this->assertCommandSucceeded($collection->drop());
45-
}
31+
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
32+
$operation->execute($this->getPrimaryServer());
4633
}
4734
}

tests/FunctionalTestCase.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ protected function assertSameDocuments(array $expectedDocuments, $actualDocument
6868
);
6969
}
7070

71+
protected function getPrimaryServer()
72+
{
73+
return $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
74+
}
75+
7176
protected function getServerVersion(ReadPreference $readPreference = null)
7277
{
7378
$cursor = $this->manager->executeCommand(

tests/Operation/FunctionalTestCase.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,31 @@
22

33
namespace MongoDB\Tests\Operation;
44

5+
use MongoDB\Collection;
56
use MongoDB\Driver\ReadPreference;
7+
use MongoDB\Operation\DropCollection;
68
use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase;
79

810
/**
911
* Base class for Operation functional tests.
1012
*/
1113
abstract class FunctionalTestCase extends BaseFunctionalTestCase
1214
{
13-
public function getPrimaryServer()
15+
public function setUp()
1416
{
15-
return $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
17+
parent::setUp();
18+
19+
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
20+
$operation->execute($this->getPrimaryServer());
21+
}
22+
23+
public function tearDown()
24+
{
25+
if ($this->hasFailed()) {
26+
return;
27+
}
28+
29+
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
30+
$operation->execute($this->getPrimaryServer());
1631
}
1732
}

0 commit comments

Comments
 (0)