Skip to content

Commit 89ce032

Browse files
committed
Remove usage of assertCommandSucceeded
1 parent 021b422 commit 89ce032

10 files changed

+76
-136
lines changed

tests/ClientFunctionalTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ public function testDropDatabase(): void
4747
$writeResult = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);
4848
$this->assertEquals(1, $writeResult->getInsertedCount());
4949

50-
$commandResult = self::commandResult(fn () => $this->client->dropDatabase($this->getDatabaseName()));
51-
$this->assertCommandSucceeded($commandResult);
50+
$this->client->dropDatabase($this->getDatabaseName());
5251
$this->assertCollectionCount($this->getNamespace(), 0);
5352
}
5453

tests/Collection/CollectionFunctionalTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,7 @@ public function testDrop(): void
255255
$writeResult = $this->collection->insertOne(['x' => 1]);
256256
$this->assertEquals(1, $writeResult->getInsertedCount());
257257

258-
$commandResult = self::commandResult(fn () => $this->collection->drop());
259-
$this->assertCommandSucceeded($commandResult);
258+
$this->collection->drop();
260259
$this->assertCollectionDoesNotExist($this->getCollectionName());
261260
}
262261

@@ -332,8 +331,7 @@ public function testRenameToSameDatabase(): void
332331
$writeResult = $this->collection->insertOne(['_id' => 1]);
333332
$this->assertEquals(1, $writeResult->getInsertedCount());
334333

335-
$commandResult = self::commandResult(fn () => $this->collection->rename($toCollectionName, null, ['dropTarget' => true]));
336-
$this->assertCommandSucceeded($commandResult);
334+
$this->collection->rename($toCollectionName, null, ['dropTarget' => true]);
337335
$this->assertCollectionDoesNotExist($this->getCollectionName());
338336
$this->assertCollectionExists($toCollectionName);
339337

@@ -361,8 +359,7 @@ public function testRenameToDifferentDatabase(): void
361359
$writeResult = $this->collection->insertOne(['_id' => 1]);
362360
$this->assertEquals(1, $writeResult->getInsertedCount());
363361

364-
$commandResult = self::commandResult(fn () => $this->collection->rename($toCollectionName, $toDatabaseName));
365-
$this->assertCommandSucceeded($commandResult);
362+
$this->collection->rename($toCollectionName, $toDatabaseName);
366363
$this->assertCollectionDoesNotExist($this->getCollectionName());
367364
$this->assertCollectionExists($toCollectionName, $toDatabaseName);
368365

tests/Database/CollectionManagementFunctionalTest.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ public function testCreateCollection(): void
1616
$that = $this;
1717
$basicCollectionName = $this->getCollectionName() . '.basic';
1818

19-
$commandResult = self::commandResult(fn () => $this->database->createCollection($basicCollectionName));
20-
$this->assertCommandSucceeded($commandResult);
19+
$this->database->createCollection($basicCollectionName);
2120
$this->assertCollectionExists($basicCollectionName, null, function (CollectionInfo $info) use ($that): void {
2221
$that->assertFalse($info->isCapped());
2322
});
@@ -29,8 +28,7 @@ public function testCreateCollection(): void
2928
'size' => 1_048_576,
3029
];
3130

32-
$commandResult = self::commandResult(fn () => $this->database->createCollection($cappedCollectionName, $cappedCollectionOptions));
33-
$this->assertCommandSucceeded($commandResult);
31+
$this->database->createCollection($cappedCollectionName, $cappedCollectionOptions);
3432
$this->assertCollectionExists($cappedCollectionName, null, function (CollectionInfo $info) use ($that): void {
3533
$that->assertTrue($info->isCapped());
3634
$that->assertEquals(100, $info->getCappedMax());
@@ -46,15 +44,13 @@ public function testDropCollection(): void
4644
$writeResult = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);
4745
$this->assertEquals(1, $writeResult->getInsertedCount());
4846

49-
$commandResult = self::commandResult(fn () => $this->database->dropCollection($this->getCollectionName()));
50-
$this->assertCommandSucceeded($commandResult);
47+
$this->database->dropCollection($this->getCollectionName());
5148
$this->assertCollectionCount($this->getNamespace(), 0);
5249
}
5350

5451
public function testListCollections(): void
5552
{
56-
$commandResult = self::commandResult(fn () => $this->database->createCollection($this->getCollectionName()));
57-
$this->assertCommandSucceeded($commandResult);
53+
$this->database->createCollection($this->getCollectionName());
5854

5955
$collections = $this->database->listCollections();
6056
$this->assertInstanceOf(CollectionInfoIterator::class, $collections);
@@ -66,8 +62,7 @@ public function testListCollections(): void
6662

6763
public function testListCollectionsWithFilter(): void
6864
{
69-
$commandResult = self::commandResult(fn () => $this->database->createCollection($this->getCollectionName()));
70-
$this->assertCommandSucceeded($commandResult);
65+
$this->database->createCollection($this->getCollectionName());
7166

7267
$collectionName = $this->getCollectionName();
7368
$options = ['filter' => ['name' => $collectionName]];
@@ -83,8 +78,7 @@ public function testListCollectionsWithFilter(): void
8378

8479
public function testListCollectionNames(): void
8580
{
86-
$commandResult = self::commandResult(fn () => $this->database->createCollection($this->getCollectionName()));
87-
$this->assertCommandSucceeded($commandResult);
81+
$this->database->createCollection($this->getCollectionName());
8882

8983
$collections = $this->database->listCollectionNames();
9084

@@ -95,8 +89,7 @@ public function testListCollectionNames(): void
9589

9690
public function testListCollectionNamesWithFilter(): void
9791
{
98-
$commandResult = self::commandResult(fn () => $this->database->createCollection($this->getCollectionName()));
99-
$this->assertCommandSucceeded($commandResult);
92+
$this->database->createCollection($this->getCollectionName());
10093

10194
$collectionName = $this->getCollectionName();
10295
$options = ['filter' => ['name' => $collectionName]];

tests/Database/DatabaseFunctionalTest.php

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
use PHPUnit\Framework\Attributes\Group;
1717
use TypeError;
1818

19-
use function array_key_exists;
2019
use function current;
20+
use function iterator_to_array;
21+
use function json_encode;
2122

2223
/**
2324
* Functional tests for the Database class.
@@ -139,8 +140,7 @@ public function testDrop(): void
139140
$writeResult = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);
140141
$this->assertEquals(1, $writeResult->getInsertedCount());
141142

142-
$commandResult = self::commandResult(fn () => $this->database->drop());
143-
$this->assertCommandSucceeded($commandResult);
143+
$this->database->drop();
144144
$this->assertCollectionCount($this->getNamespace(), 0);
145145
}
146146

@@ -152,8 +152,7 @@ public function testDropCollection(): void
152152
$writeResult = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);
153153
$this->assertEquals(1, $writeResult->getInsertedCount());
154154

155-
$commandResult = self::commandResult(fn () => $this->database->dropCollection($this->getCollectionName()));
156-
$this->assertCommandSucceeded($commandResult);
155+
$this->database->dropCollection($this->getCollectionName());
157156
$this->assertCollectionDoesNotExist($this->getCollectionName());
158157
}
159158

@@ -183,28 +182,26 @@ public function testModifyCollection(): void
183182
$createIndexes = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes);
184183
$createIndexes->execute($this->getPrimaryServer());
185184

186-
$commandResult = self::commandResult(fn () => $this->database->modifyCollection(
185+
$this->database->modifyCollection(
187186
$this->getCollectionName(),
188187
['index' => ['keyPattern' => ['lastAccess' => 1], 'expireAfterSeconds' => 1000]],
189188
['typeMap' => ['root' => 'array', 'document' => 'array']],
190-
));
191-
$this->assertCommandSucceeded($commandResult);
192-
193-
$commandResult = (array) $commandResult;
194-
195-
if (array_key_exists('raw', $commandResult)) {
196-
/* Sharded environment, where we only assert if a shard had a successful update. For
197-
* non-primary shards that don't have chunks for the collection, the result contains a
198-
* "ns does not exist" error. */
199-
foreach ($commandResult['raw'] as $shard) {
200-
if (array_key_exists('ok', $shard) && $shard['ok'] == 1) {
201-
$this->assertSame(3, $shard['expireAfterSeconds_old']);
202-
$this->assertSame(1000, $shard['expireAfterSeconds_new']);
203-
}
189+
);
190+
191+
$indexes = $this->database->selectCollection($this->getCollectionName())->listIndexes();
192+
$indexes = iterator_to_array($indexes);
193+
$this->assertCount(2, $indexes);
194+
195+
foreach ($indexes as $index) {
196+
switch ($index['key']) {
197+
case ['_id' => 1]:
198+
break;
199+
case ['lastAccess' => 1]:
200+
$this->assertSame(1000, $index['expireAfterSeconds']);
201+
break;
202+
default:
203+
$this->fail('Unexpected index key: ' . json_encode($index->key));
204204
}
205-
} else {
206-
$this->assertSame(3, $commandResult['expireAfterSeconds_old']);
207-
$this->assertSame(1000, $commandResult['expireAfterSeconds_new']);
208205
}
209206
}
210207

@@ -219,13 +216,12 @@ public function testRenameCollectionToSameDatabase(): void
219216
$writeResult = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);
220217
$this->assertEquals(1, $writeResult->getInsertedCount());
221218

222-
$commandResult = self::commandResult(fn () => $this->database->renameCollection(
219+
$this->database->renameCollection(
223220
$this->getCollectionName(),
224221
$toCollectionName,
225222
null,
226223
['dropTarget' => true],
227-
));
228-
$this->assertCommandSucceeded($commandResult);
224+
);
229225
$this->assertCollectionDoesNotExist($this->getCollectionName());
230226
$this->assertCollectionExists($toCollectionName);
231227

@@ -256,12 +252,11 @@ public function testRenameCollectionToDifferentDatabase(): void
256252
$writeResult = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);
257253
$this->assertEquals(1, $writeResult->getInsertedCount());
258254

259-
$commandResult = self::commandResult(fn () => $this->database->renameCollection(
255+
$this->database->renameCollection(
260256
$this->getCollectionName(),
261257
$toCollectionName,
262258
$toDatabaseName,
263-
));
264-
$this->assertCommandSucceeded($commandResult);
259+
);
265260
$this->assertCollectionDoesNotExist($this->getCollectionName());
266261
$this->assertCollectionExists($toCollectionName, $toDatabaseName);
267262

tests/FunctionalTestCase.php

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
use MongoDB\Driver\Command;
1010
use MongoDB\Driver\Exception\CommandException;
1111
use MongoDB\Driver\Manager;
12-
use MongoDB\Driver\Monitoring\CommandFailedEvent;
13-
use MongoDB\Driver\Monitoring\CommandStartedEvent;
14-
use MongoDB\Driver\Monitoring\CommandSubscriber;
15-
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
1612
use MongoDB\Driver\ReadPreference;
1713
use MongoDB\Driver\Server;
1814
use MongoDB\Driver\ServerApi;
@@ -40,13 +36,10 @@
4036
use function is_readable;
4137
use function is_string;
4238
use function key;
43-
use function MongoDB\Driver\Monitoring\addSubscriber;
44-
use function MongoDB\Driver\Monitoring\removeSubscriber;
4539
use function ob_get_clean;
4640
use function ob_start;
4741
use function parse_url;
4842
use function phpinfo;
49-
use function PHPUnit\Framework\assertNull;
5043
use function preg_match;
5144
use function preg_quote;
5245
use function preg_replace;
@@ -200,45 +193,6 @@ protected function assertCollectionExists(string $collectionName, ?string $datab
200193
}
201194
}
202195

203-
/** @param callable():void $closure */
204-
protected static function commandResult(callable $closure): object
205-
{
206-
$subscriber = new class implements CommandSubscriber {
207-
public ?object $result = null;
208-
209-
public function commandFailed(CommandFailedEvent $event): void
210-
{
211-
assertNull($this->result, 'Expected only one command result');
212-
213-
$this->result = $event->getReply();
214-
}
215-
216-
public function commandStarted(CommandStartedEvent $event): void
217-
{
218-
}
219-
220-
public function commandSucceeded(CommandSucceededEvent $event): void
221-
{
222-
assertNull($this->result, 'Expected only one command result');
223-
224-
$this->result = $event->getReply();
225-
}
226-
};
227-
228-
addSubscriber($subscriber);
229-
230-
try {
231-
$return = $closure();
232-
233-
self::assertNull($return, 'Expected void return type');
234-
self::assertNotNull($subscriber->result, 'Expected a command result');
235-
236-
return $subscriber->result;
237-
} finally {
238-
removeSubscriber($subscriber);
239-
}
240-
}
241-
242196
protected function assertCommandSucceeded($document): void
243197
{
244198
$document = is_object($document) ? (array) $document : $document;
@@ -288,10 +242,7 @@ public function configureFailPoint(array|stdClass $command, ?Server $server = nu
288242
$failPointServer = $server ?: $this->getPrimaryServer();
289243

290244
$operation = new DatabaseCommand('admin', $command);
291-
$cursor = self::commandResult(fn () => $operation->execute($failPointServer));
292-
$result = $cursor->toArray()[0];
293-
294-
$this->assertCommandSucceeded($result);
245+
$operation->execute($failPointServer);
295246

296247
// Record the fail point so it can be disabled during tearDown()
297248
$this->configuredFailPoints[] = [$command->configureFailPoint, $failPointServer];

tests/Operation/DropCollectionFunctionalTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use MongoDB\Operation\InsertOne;
77
use MongoDB\Tests\CommandObserver;
88
use PHPUnit\Framework\Attributes\Depends;
9+
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
910

1011
class DropCollectionFunctionalTest extends FunctionalTestCase
1112
{
@@ -36,23 +37,19 @@ public function testDropExistingCollection(): void
3637
$this->assertEquals(1, $writeResult->getInsertedCount());
3738

3839
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
39-
$commandResult = self::commandResult(fn () => $operation->execute($server));
40+
$operation->execute($server);
4041

41-
$this->assertCommandSucceeded($commandResult);
4242
$this->assertCollectionDoesNotExist($this->getCollectionName());
4343
}
4444

4545
#[Depends('testDropExistingCollection')]
46+
#[DoesNotPerformAssertions]
4647
public function testDropNonexistentCollection(): void
4748
{
4849
$this->assertCollectionDoesNotExist($this->getCollectionName());
4950

5051
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
51-
$commandResult = $operation->execute($this->getPrimaryServer());
52-
53-
/* Avoid inspecting the result document as mongos returns {ok:1.0},
54-
* which is inconsistent from the expected mongod response of {ok:0}. */
55-
$this->assertIsObject($commandResult);
52+
$operation->execute($this->getPrimaryServer());
5653
}
5754

5855
public function testSessionOption(): void

tests/Operation/DropIndexesFunctionalTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function testDropOneIndexByName(): void
4848
$this->assertIndexExists('x_1');
4949

5050
$operation = new DropIndexes($this->getDatabaseName(), $this->getCollectionName(), 'x_1');
51-
$this->assertCommandSucceeded(self::commandResult(fn () => $operation->execute($this->getPrimaryServer())));
51+
$operation->execute($this->getPrimaryServer());
5252

5353
$operation = new ListIndexes($this->getDatabaseName(), $this->getCollectionName());
5454
$indexes = $operation->execute($this->getPrimaryServer());
@@ -76,7 +76,7 @@ public function testDropAllIndexesByWildcard(): void
7676
$this->assertIndexExists('y_1');
7777

7878
$operation = new DropIndexes($this->getDatabaseName(), $this->getCollectionName(), '*');
79-
$this->assertCommandSucceeded(self::commandResult(fn () => $operation->execute($this->getPrimaryServer())));
79+
$operation->execute($this->getPrimaryServer());
8080

8181
$operation = new ListIndexes($this->getDatabaseName(), $this->getCollectionName());
8282
$indexes = $operation->execute($this->getPrimaryServer());
@@ -108,7 +108,7 @@ public function testDropByIndexInfo(): void
108108
$this->assertIndexExists('x_1');
109109

110110
$operation = new DropIndexes($this->getDatabaseName(), $this->getCollectionName(), $info);
111-
$this->assertCommandSucceeded(self::commandResult(fn () => $operation->execute($this->getPrimaryServer())));
111+
$operation->execute($this->getPrimaryServer());
112112

113113
$operation = new ListIndexes($this->getDatabaseName(), $this->getCollectionName());
114114
$indexes = $operation->execute($this->getPrimaryServer());

tests/Operation/ModifyCollectionFunctionalTest.php

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

55
use MongoDB\Operation\CreateIndexes;
6+
use MongoDB\Operation\ListIndexes;
67
use MongoDB\Operation\ModifyCollection;
78
use PHPUnit\Framework\Attributes\Group;
89

@@ -29,9 +30,23 @@ public function testCollMod(): void
2930
['index' => ['keyPattern' => ['lastAccess' => 1], 'expireAfterSeconds' => 1000]],
3031
['typeMap' => ['root' => 'array', 'document' => 'array']],
3132
);
32-
$result = $modifyCollection->execute($this->getPrimaryServer());
33+
$modifyCollection->execute($this->getPrimaryServer());
3334

34-
$this->assertSame(3, $result['expireAfterSeconds_old']);
35-
$this->assertSame(1000, $result['expireAfterSeconds_new']);
35+
$listIndexes = new ListIndexes($this->getDatabaseName(), $this->getCollectionName());
36+
$indexes = $listIndexes->execute($this->getPrimaryServer());
37+
$indexes = iterator_to_array($indexes);
38+
$this->assertCount(2, $indexes);
39+
40+
foreach ($indexes as $index) {
41+
switch ($index['key']) {
42+
case ['_id' => 1]:
43+
break;
44+
case ['lastAccess' => 1]:
45+
$this->assertSame(1000, $index['expireAfterSeconds']);
46+
break;
47+
default:
48+
$this->fail('Unexpected index key: ' . json_encode($index->key));
49+
}
50+
}
3651
}
3752
}

0 commit comments

Comments
 (0)