Skip to content

Commit ccfe692

Browse files
committed
Fix tests for renaming collection to different database
1 parent 8762274 commit ccfe692

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

tests/Collection/CollectionFunctionalTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Closure;
66
use MongoDB\BSON\Javascript;
77
use MongoDB\Collection;
8+
use MongoDB\Database;
89
use MongoDB\Driver\BulkWrite;
910
use MongoDB\Driver\ReadConcern;
1011
use MongoDB\Driver\ReadPreference;
@@ -352,21 +353,28 @@ public function testRenameToSameDatabase(): void
352353

353354
public function testRenameToDifferentDatabase(): void
354355
{
355-
$toCollectionName = $this->getCollectionName() . '.renamed';
356+
if ($this->isShardedCluster()) {
357+
$this->markTestSkipped('Test does not apply on sharded clusters: need source and target databases to be on the same primary shard.');
358+
}
359+
356360
$toDatabaseName = $this->getDatabaseName() . '_renamed';
361+
$toCollectionName = $this->getCollectionName() . '.renamed';
362+
$toDatabase = new Database($this->manager, $toDatabaseName);
357363
$toCollection = new Collection($this->manager, $toDatabaseName, $toCollectionName);
358364

359365
$writeResult = $this->collection->insertOne(['_id' => 1]);
360366
$this->assertEquals(1, $writeResult->getInsertedCount());
361367

362-
$commandResult = $this->collection->rename($toCollectionName, $toDatabaseName, ['dropTarget' => true]);
368+
$commandResult = $this->collection->rename($toCollectionName, $toDatabaseName);
363369
$this->assertCommandSucceeded($commandResult);
364370
$this->assertCollectionDoesNotExist($this->getCollectionName());
365371
$this->assertCollectionExists($toCollectionName, $toDatabaseName);
366372

367373
$document = $toCollection->findOne();
368374
$this->assertSameDocument(['_id' => 1], $document);
375+
369376
$toCollection->drop();
377+
$toDatabase->drop();
370378
}
371379

372380
public function testWithOptionsInheritsOptions(): void

tests/Database/DatabaseFunctionalTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,13 @@ public function testRenameCollectionToSameDatabase(): void
240240

241241
public function testRenameCollectionToDifferentDatabase(): void
242242
{
243-
$toCollectionName = $this->getCollectionName() . '.renamed';
243+
if ($this->isShardedCluster()) {
244+
$this->markTestSkipped('Test does not apply on sharded clusters: need source and target databases to be on the same primary shard.');
245+
}
246+
244247
$toDatabaseName = $this->getDatabaseName() . '_renamed';
248+
$toCollectionName = $this->getCollectionName() . '.renamed';
249+
$toDatabase = new Database($this->manager, $toDatabaseName);
245250
$toCollection = new Collection($this->manager, $toDatabaseName, $toCollectionName);
246251

247252
$bulkWrite = new BulkWrite();
@@ -253,16 +258,17 @@ public function testRenameCollectionToDifferentDatabase(): void
253258
$commandResult = $this->database->renameCollection(
254259
$this->getCollectionName(),
255260
$toCollectionName,
256-
$toDatabaseName,
257-
['dropTarget' => true]
261+
$toDatabaseName
258262
);
259263
$this->assertCommandSucceeded($commandResult);
260264
$this->assertCollectionDoesNotExist($this->getCollectionName());
261265
$this->assertCollectionExists($toCollectionName, $toDatabaseName);
262266

263267
$document = $toCollection->findOne();
264268
$this->assertSameDocument(['_id' => 1], $document);
269+
265270
$toCollection->drop();
271+
$toDatabase->drop();
266272
}
267273

268274
public function testSelectCollectionInheritsOptions(): void

0 commit comments

Comments
 (0)