Skip to content

Commit 8bfce9b

Browse files
tanlisujmikola
andauthored
Apply suggestions from code review
Co-authored-by: Jeremy Mikola <jmikola@gmail.com>
1 parent 0726d26 commit 8bfce9b

File tree

6 files changed

+16
-25
lines changed

6 files changed

+16
-25
lines changed

src/Collection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,7 @@ public function mapReduce(JavascriptInterface $map, JavascriptInterface $reduce,
10201020
public function rename(string $toCollectionName, ?string $toDatabaseName = null, array $options = [])
10211021
{
10221022
if (! isset($toDatabaseName)) {
1023-
$toDatabaseName = $this->getDatabaseName();
1023+
$toDatabaseName = $this->databaseName;
10241024
}
10251025

10261026
if (! isset($options['typeMap'])) {
@@ -1033,7 +1033,7 @@ public function rename(string $toCollectionName, ?string $toDatabaseName = null,
10331033
$options['writeConcern'] = $this->writeConcern;
10341034
}
10351035

1036-
$operation = new RenameCollection($this->getDatabaseName(), $this->getCollectionName(), $toDatabaseName, $toCollectionName, $options);
1036+
$operation = new RenameCollection($this->databaseName, $this->collectionName, $toDatabaseName, $toCollectionName, $options);
10371037

10381038
return $operation->execute($server);
10391039
}

src/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ public function renameCollection(string $fromCollectionName, string $toCollectio
500500
$options['writeConcern'] = $this->writeConcern;
501501
}
502502

503-
$operation = new RenameCollection($this->getDatabaseName(), $fromCollectionName, $toDatabaseName, $toCollectionName, $options);
503+
$operation = new RenameCollection($this->databaseName, $fromCollectionName, $toDatabaseName, $toCollectionName, $options);
504504

505505
return $operation->execute($server);
506506
}

src/Operation/RenameCollection.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,16 @@ public function execute(Server $server)
125125
throw UnsupportedException::writeConcernNotSupportedInTransaction();
126126
}
127127

128-
$commandOptions = [
128+
$cmd = [
129129
'renameCollection' => $this->fromNamespace,
130130
'to' => $this->toNamespace,
131131
];
132132

133133
if (isset($this->options['dropTarget'])) {
134-
$commandOptions['dropTarget'] = $this->options['dropTarget'];
134+
$cmd['dropTarget'] = $this->options['dropTarget'];
135135
}
136136

137-
$command = new Command($commandOptions);
138-
139-
$cursor = $server->executeWriteCommand('admin', $command, $this->createOptions());
137+
$cursor = $server->executeWriteCommand('admin', new Command($cmd), $this->createOptions());
140138

141139
if (isset($this->options['typeMap'])) {
142140
$cursor->setTypeMap($this->options['typeMap']);

tests/Collection/CollectionFunctionalTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,15 +346,14 @@ public function testRenameToSameDatabase(): void
346346
$this->assertCollectionDoesNotExist($this->getCollectionName());
347347
$this->assertCollectionExists($toCollectionName);
348348

349-
$document = $toCollection->findOne();
350-
$this->assertSameDocument(['_id' => 1], $document);
349+
$this->assertSameDocument(['_id' => 1], $toCollection->findOne());
351350
$toCollection->drop();
352351
}
353352

354353
public function testRenameToDifferentDatabase(): void
355354
{
356355
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.');
356+
$this->markTestSkipped('TODO: mongos requires the target database to exist');
358357
}
359358

360359
$toDatabaseName = $this->getDatabaseName() . '_renamed';
@@ -370,10 +369,8 @@ public function testRenameToDifferentDatabase(): void
370369
$this->assertCollectionDoesNotExist($this->getCollectionName());
371370
$this->assertCollectionExists($toCollectionName, $toDatabaseName);
372371

373-
$document = $toCollection->findOne();
374-
$this->assertSameDocument(['_id' => 1], $document);
372+
$this->assertSameDocument(['_id' => 1], $toCollection->findOne());
375373

376-
$toCollection->drop();
377374
$toDatabase->drop();
378375
}
379376

tests/Database/DatabaseFunctionalTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,7 @@ public function testRenameCollectionToSameDatabase(): void
233233
$this->assertCollectionDoesNotExist($this->getCollectionName());
234234
$this->assertCollectionExists($toCollectionName);
235235

236-
$document = $toCollection->findOne();
237-
$this->assertSameDocument(['_id' => 1], $document);
236+
$this->assertSameDocument(['_id' => 1], $toCollection->findOne());
238237
$toCollection->drop();
239238
}
240239

@@ -264,10 +263,8 @@ public function testRenameCollectionToDifferentDatabase(): void
264263
$this->assertCollectionDoesNotExist($this->getCollectionName());
265264
$this->assertCollectionExists($toCollectionName, $toDatabaseName);
266265

267-
$document = $toCollection->findOne();
268-
$this->assertSameDocument(['_id' => 1], $document);
266+
$this->assertSameDocument(['_id' => 1], $toCollection->findOne());
269267

270-
$toCollection->drop();
271268
$toDatabase->drop();
272269
}
273270

tests/Operation/RenameCollectionFunctionalTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function (array $event): void {
6969
);
7070
}
7171

72-
public function testRenameExistingCollection(): void
72+
public function testRenameCollectionToNonexistentTarget(): void
7373
{
7474
$server = $this->getPrimaryServer();
7575

@@ -90,11 +90,10 @@ public function testRenameExistingCollection(): void
9090
$this->assertCollectionExists($this->toCollectionName);
9191

9292
$operation = new FindOne($this->getDatabaseName(), $this->toCollectionName, []);
93-
$cursor = $operation->execute($server);
94-
$this->assertSameDocument(['_id' => 1], $cursor);
93+
$this->assertSameDocument(['_id' => 1], $operation->execute($server));
9594
}
9695

97-
public function testRenameExistingCollectionExistingTarget(): void
96+
public function testRenameCollectionExistingTarget(): void
9897
{
9998
$server = $this->getPrimaryServer();
10099

@@ -114,7 +113,7 @@ public function testRenameExistingCollectionExistingTarget(): void
114113
$this->getDatabaseName(),
115114
$this->toCollectionName
116115
);
117-
$commandResult = $operation->execute($server);
116+
$operation->execute($server);
118117
}
119118

120119
/**
@@ -133,7 +132,7 @@ public function testRenameNonexistentCollection(): void
133132
$this->toCollectionName,
134133
['writeConcern' => $this->createDefaultWriteConcern()]
135134
);
136-
$commandResult = $operation->execute($this->getPrimaryServer());
135+
$operation->execute($this->getPrimaryServer());
137136
}
138137

139138
public function testSessionOption(): void

0 commit comments

Comments
 (0)