Skip to content

Commit a949df5

Browse files
committed
PHPLIB-384: Allow ModifyCollection test to work in sharded environment
1 parent 75cbc78 commit a949df5

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

tests/Database/DatabaseFunctionalTest.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,29 @@ public function testModifyCollection()
161161
$createIndexes = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes);
162162
$createIndexes->execute($this->getPrimaryServer());
163163

164-
$commandResult = $this->database->modifyCollection($this->getCollectionName(), ['index' => ['keyPattern' => ['lastAccess' => 1], 'expireAfterSeconds' => 1000]]);
165-
164+
$commandResult = $this->database->modifyCollection(
165+
$this->getCollectionName(),
166+
['index' => ['keyPattern' => ['lastAccess' => 1], 'expireAfterSeconds' => 1000]],
167+
['typeMap' => ['root' => 'array', 'document' => 'array']]
168+
);
166169
$this->assertCommandSucceeded($commandResult);
167-
$this->assertSame(3, $commandResult['expireAfterSeconds_old']);
168-
$this->assertSame(1000, $commandResult['expireAfterSeconds_new']);
170+
171+
$commandResult = (array) $commandResult;
172+
173+
if (array_key_exists('raw', $commandResult)) {
174+
/* Sharded environment, where we only assert if a shard had a successful update. For
175+
* non-primary shards that don't have chunks for the collection, the result contains a
176+
* "ns does not exist" error. */
177+
foreach ($commandResult['raw'] as $shard) {
178+
if (array_key_exists('ok', $shard) && $shard['ok'] == 1) {
179+
$this->assertSame(3, $shard['expireAfterSeconds_old']);
180+
$this->assertSame(1000, $shard['expireAfterSeconds_new']);
181+
}
182+
}
183+
} else {
184+
$this->assertSame(3, $commandResult['expireAfterSeconds_old']);
185+
$this->assertSame(1000, $commandResult['expireAfterSeconds_new']);
186+
}
169187
}
170188

171189

tests/Operation/ModifyCollectionFunctionalTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,21 @@ public function testCollMod()
2525
);
2626
$result = $modifyCollection->execute($this->getPrimaryServer());
2727

28-
$this->assertSame(3, $result['expireAfterSeconds_old']);
29-
$this->assertSame(1000, $result['expireAfterSeconds_new']);
28+
if (array_key_exists('raw', $result)) {
29+
/* Sharded environment, where we only assert if a shard had a successful update. For
30+
* non-primary shards that don't have chunks for the collection, the result contains a
31+
* "ns does not exist" error. */
32+
foreach ($result['raw'] as $shard) {
33+
$shard = (array) $shard;
34+
35+
if (array_key_exists('ok', $shard) && $shard['ok'] == 1) {
36+
$this->assertSame(3, $shard['expireAfterSeconds_old']);
37+
$this->assertSame(1000, $shard['expireAfterSeconds_new']);
38+
}
39+
}
40+
} else {
41+
$this->assertSame(3, $result['expireAfterSeconds_old']);
42+
$this->assertSame(1000, $result['expireAfterSeconds_new']);
43+
}
3044
}
3145
}

0 commit comments

Comments
 (0)