Skip to content

Commit 07262f8

Browse files
committed
Move assertCollectionDoesNotExist() and assertCollectionExists() to FunctionalTestCase
1 parent b35fe4d commit 07262f8

File tree

6 files changed

+63
-150
lines changed

6 files changed

+63
-150
lines changed

tests/Collection/CollectionFunctionalTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@ public function testRename(): void
345345

346346
$commandResult = $this->collection->rename($this->getDatabaseName() . '.' . $renamedCollection);
347347
$this->assertCommandSucceeded($commandResult);
348+
$this->assertCollectionDoesNotExist($this->getCollectionName());
349+
$this->assertCollectionExists($renamedCollection);
348350

349351
$operation = new FindOne($this->getDatabaseName(), $renamedCollection, []);
350352
$cursor = $operation->execute($this->getPrimaryServer());

tests/Database/CollectionManagementFunctionalTest.php

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@
22

33
namespace MongoDB\Tests\Database;
44

5-
use InvalidArgumentException;
65
use MongoDB\Driver\BulkWrite;
76
use MongoDB\Model\CollectionInfo;
87
use MongoDB\Model\CollectionInfoIterator;
98

10-
use function call_user_func;
11-
use function is_callable;
12-
use function sprintf;
13-
149
/**
1510
* Functional tests for collection management methods.
1611
*/
@@ -112,38 +107,4 @@ public function testListCollectionNamesWithFilter(): void
112107
$this->assertEquals($collectionName, $collection);
113108
}
114109
}
115-
116-
/**
117-
* Asserts that a collection with the given name exists in the database.
118-
*
119-
* An optional $callback may be provided, which should take a CollectionInfo
120-
* argument as its first and only parameter. If a CollectionInfo matching
121-
* the given name is found, it will be passed to the callback, which may
122-
* perform additional assertions.
123-
*
124-
* @param callable $callback
125-
*/
126-
private function assertCollectionExists($collectionName, ?callable $callback = null): void
127-
{
128-
if ($callback !== null && ! is_callable($callback)) {
129-
throw new InvalidArgumentException('$callback is not a callable');
130-
}
131-
132-
$collections = $this->database->listCollections();
133-
134-
$foundCollection = null;
135-
136-
foreach ($collections as $collection) {
137-
if ($collection->getName() === $collectionName) {
138-
$foundCollection = $collection;
139-
break;
140-
}
141-
}
142-
143-
$this->assertNotNull($foundCollection, sprintf('Found %s collection in the database', $collectionName));
144-
145-
if ($callback !== null) {
146-
call_user_func($callback, $foundCollection);
147-
}
148-
}
149110
}

tests/FunctionalTestCase.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@
1616
use MongoDB\Operation\CreateCollection;
1717
use MongoDB\Operation\DatabaseCommand;
1818
use MongoDB\Operation\DropCollection;
19+
use MongoDB\Operation\ListCollections;
1920
use stdClass;
2021
use UnexpectedValueException;
2122

2223
use function array_merge;
24+
use function call_user_func;
2325
use function count;
2426
use function current;
2527
use function explode;
2628
use function getenv;
2729
use function implode;
2830
use function is_array;
31+
use function is_callable;
2932
use function is_object;
3033
use function is_string;
3134
use function key;
@@ -147,6 +150,64 @@ protected function assertCollectionCount($namespace, $count): void
147150
$this->assertEquals($count, $document['n']);
148151
}
149152

153+
/**
154+
* Asserts that a collection with the given name does not exist on the
155+
* server.
156+
*
157+
* @param string $collectionName
158+
*/
159+
protected function assertCollectionDoesNotExist(string $collectionName): void
160+
{
161+
$operation = new ListCollections($this->getDatabaseName());
162+
$collections = $operation->execute($this->getPrimaryServer());
163+
164+
$foundCollection = null;
165+
166+
foreach ($collections as $collection) {
167+
if ($collection->getName() === $collectionName) {
168+
$foundCollection = $collection;
169+
break;
170+
}
171+
}
172+
173+
$this->assertNull($foundCollection, sprintf('Collection %s exists', $collectionName));
174+
}
175+
176+
/**
177+
* Asserts that a collection with the given name exists on the server.
178+
*
179+
* An optional $callback may be provided, which should take a CollectionInfo
180+
* argument as its first and only parameter. If a CollectionInfo matching
181+
* the given name is found, it will be passed to the callback, which may
182+
* perform additional assertions.
183+
*
184+
* @param callable $callback
185+
*/
186+
protected function assertCollectionExists($collectionName, ?callable $callback = null): void
187+
{
188+
if ($callback !== null && ! is_callable($callback)) {
189+
throw new InvalidArgumentException('$callback is not a callable');
190+
}
191+
192+
$operation = new ListCollections($this->getDatabaseName());
193+
$collections = $operation->execute($this->getPrimaryServer());
194+
195+
$foundCollection = null;
196+
197+
foreach ($collections as $collection) {
198+
if ($collection->getName() === $collectionName) {
199+
$foundCollection = $collection;
200+
break;
201+
}
202+
}
203+
204+
$this->assertNotNull($foundCollection, sprintf('Found %s collection in the database', $collectionName));
205+
206+
if ($callback !== null) {
207+
call_user_func($callback, $foundCollection);
208+
}
209+
}
210+
150211
protected function assertCommandSucceeded($document): void
151212
{
152213
$document = is_object($document) ? (array) $document : $document;

tests/GridFS/BucketFunctionalTest.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use MongoDB\GridFS\Exception\StreamException;
1414
use MongoDB\Model\BSONDocument;
1515
use MongoDB\Model\IndexInfo;
16-
use MongoDB\Operation\ListCollections;
1716
use MongoDB\Operation\ListIndexes;
1817
use PHPUnit\Framework\Error\Warning;
1918

@@ -773,29 +772,6 @@ public function testDanglingOpenWritableStream(): void
773772
$this->assertSame('', $output);
774773
}
775774

776-
/**
777-
* Asserts that a collection with the given name does not exist on the
778-
* server.
779-
*
780-
* @param string $collectionName
781-
*/
782-
private function assertCollectionDoesNotExist(string $collectionName): void
783-
{
784-
$operation = new ListCollections($this->getDatabaseName());
785-
$collections = $operation->execute($this->getPrimaryServer());
786-
787-
$foundCollection = null;
788-
789-
foreach ($collections as $collection) {
790-
if ($collection->getName() === $collectionName) {
791-
$foundCollection = $collection;
792-
break;
793-
}
794-
}
795-
796-
$this->assertNull($foundCollection, sprintf('Collection %s exists', $collectionName));
797-
}
798-
799775
/**
800776
* Asserts that an index with the given name exists for the collection.
801777
*

tests/Operation/DropCollectionFunctionalTest.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
use MongoDB\Operation\DropCollection;
66
use MongoDB\Operation\InsertOne;
7-
use MongoDB\Operation\ListCollections;
87
use MongoDB\Tests\CommandObserver;
98

10-
use function sprintf;
119
use function version_compare;
1210

1311
class DropCollectionFunctionalTest extends FunctionalTestCase
@@ -81,27 +79,4 @@ function (array $event): void {
8179
}
8280
);
8381
}
84-
85-
/**
86-
* Asserts that a collection with the given name does not exist on the
87-
* server.
88-
*
89-
* @param string $collectionName
90-
*/
91-
private function assertCollectionDoesNotExist(string $collectionName): void
92-
{
93-
$operation = new ListCollections($this->getDatabaseName());
94-
$collections = $operation->execute($this->getPrimaryServer());
95-
96-
$foundCollection = null;
97-
98-
foreach ($collections as $collection) {
99-
if ($collection->getName() === $collectionName) {
100-
$foundCollection = $collection;
101-
break;
102-
}
103-
}
104-
105-
$this->assertNull($foundCollection, sprintf('Collection %s exists', $collectionName));
106-
}
10782
}

tests/Operation/RenameCollectionFunctionalTest.php

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@
66
use MongoDB\Operation\DropCollection;
77
use MongoDB\Operation\FindOne;
88
use MongoDB\Operation\InsertOne;
9-
use MongoDB\Operation\ListCollections;
109
use MongoDB\Operation\RenameCollection;
1110
use MongoDB\Tests\CommandObserver;
1211

13-
use function call_user_func;
14-
use function is_callable;
15-
use function sprintf;
1612
use function version_compare;
1713

1814
class RenameCollectionFunctionalTest extends FunctionalTestCase
@@ -132,62 +128,4 @@ public function tearDown(): void
132128

133129
parent::tearDown();
134130
}
135-
136-
/**
137-
* Asserts that a collection with the given name does not exist on the
138-
* server.
139-
*
140-
* @param string $collectionName
141-
*/
142-
private function assertCollectionDoesNotExist(string $collectionName): void
143-
{
144-
$operation = new ListCollections($this->getDatabaseName());
145-
$collections = $operation->execute($this->getPrimaryServer());
146-
147-
$foundCollection = null;
148-
149-
foreach ($collections as $collection) {
150-
if ($collection->getName() === $collectionName) {
151-
$foundCollection = $collection;
152-
break;
153-
}
154-
}
155-
156-
$this->assertNull($foundCollection, sprintf('Collection %s exists', $collectionName));
157-
}
158-
159-
/**
160-
* Asserts that a collection with the given name exists on the server.
161-
*
162-
* An optional $callback may be provided, which should take a CollectionInfo
163-
* argument as its first and only parameter. If a CollectionInfo matching
164-
* the given name is found, it will be passed to the callback, which may
165-
* perform additional assertions.
166-
*
167-
* @param callable $callback
168-
*/
169-
private function assertCollectionExists($collectionName, ?callable $callback = null): void
170-
{
171-
if ($callback !== null && ! is_callable($callback)) {
172-
throw new InvalidArgumentException('$callback is not a callable');
173-
}
174-
175-
$operation = new ListCollections($this->getDatabaseName());
176-
$collections = $operation->execute($this->getPrimaryServer());
177-
178-
$foundCollection = null;
179-
180-
foreach ($collections as $collection) {
181-
if ($collection->getName() === $collectionName) {
182-
$foundCollection = $collection;
183-
break;
184-
}
185-
}
186-
187-
$this->assertNotNull($foundCollection, sprintf('Found %s collection in the database', $collectionName));
188-
189-
if ($callback !== null) {
190-
call_user_func($callback, $foundCollection);
191-
}
192-
}
193131
}

0 commit comments

Comments
 (0)