Skip to content

Commit 1765fe3

Browse files
committed
PHPLIB-652: Update unified test runner for change stream tests
This bumps the supported schema version to 1.7, but omits support for 1.6 (see: PHPLIB-718). Also includes spec tests for PHPLIB-828 and PHPLIB-829 and introduces support for Collection::rename() operation. Change stream tests synced with mongodb/specifications@bb7a784. Unified spec tests synced with mongodb/specifications@ff0c705.
1 parent bc60323 commit 1765fe3

11 files changed

+6553
-11
lines changed

tests/UnifiedSpecTests/Context.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,12 @@ public function assertExpectedEventsForClients(array $expectedEventsForClients):
156156

157157
foreach ($expectedEventsForClients as $expectedEventsForClient) {
158158
assertIsObject($expectedEventsForClient);
159-
Util::assertHasOnlyKeys($expectedEventsForClient, ['client', 'events', 'eventType']);
159+
Util::assertHasOnlyKeys($expectedEventsForClient, ['client', 'events', 'eventType', 'ignoreExtraEvents']);
160160

161161
$client = $expectedEventsForClient->client ?? null;
162162
$eventType = $expectedEventsForClient->eventType ?? 'command';
163163
$expectedEvents = $expectedEventsForClient->events ?? null;
164+
$ignoreExtraEvents = $expectedEventsForClient->ignoreExtraEvents ?? false;
164165

165166
assertIsString($client);
166167
assertArrayHasKey($client, $this->eventObserversByClient);
@@ -169,7 +170,7 @@ public function assertExpectedEventsForClients(array $expectedEventsForClients):
169170
assertSame('command', $eventType);
170171
assertIsArray($expectedEvents);
171172

172-
$this->eventObserversByClient[$client]->assert($expectedEvents);
173+
$this->eventObserversByClient[$client]->assert($expectedEvents, $ignoreExtraEvents);
173174
}
174175
}
175176

tests/UnifiedSpecTests/EventObserver.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use function MongoDB\Driver\Monitoring\removeSubscriber;
2323
use function PHPUnit\Framework\assertArrayHasKey;
2424
use function PHPUnit\Framework\assertCount;
25+
use function PHPUnit\Framework\assertGreaterThanOrEqual;
2526
use function PHPUnit\Framework\assertInstanceOf;
2627
use function PHPUnit\Framework\assertIsBool;
2728
use function PHPUnit\Framework\assertIsObject;
@@ -204,9 +205,13 @@ public function getLsidsOnLastTwoCommands(): array
204205
Assert::fail('Not enough CommandStartedEvents observed');
205206
}
206207

207-
public function assert(array $expectedEvents): void
208+
public function assert(array $expectedEvents, bool $ignoreExtraEvents): void
208209
{
209-
assertCount(count($expectedEvents), $this->actualEvents);
210+
if ($ignoreExtraEvents) {
211+
assertGreaterThanOrEqual(count($expectedEvents), count($this->actualEvents));
212+
} else {
213+
assertCount(count($expectedEvents), $this->actualEvents);
214+
}
210215

211216
$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);
212217
$mi->attachIterator(new ArrayIterator($expectedEvents));
@@ -215,6 +220,10 @@ public function assert(array $expectedEvents): void
215220
foreach ($mi as $keys => $events) {
216221
[$expectedEvent, $actualEvent] = $events;
217222

223+
if ($ignoreExtraEvents && $expectedEvent === null) {
224+
break;
225+
}
226+
218227
assertIsObject($expectedEvent);
219228
$expectedEvent = (array) $expectedEvent;
220229
assertCount(1, $expectedEvent);

tests/UnifiedSpecTests/Operation.php

+6
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,12 @@ private function executeForCollection(Collection $collection)
474474
array_diff_key($args, ['map' => 1, 'reduce' => 1, 'out' => 1])
475475
);
476476

477+
case 'rename':
478+
assertArrayHasKey('to', $args);
479+
assertIsString($args['to']);
480+
481+
return $collection->rename($args['to']);
482+
477483
default:
478484
Assert::fail('Unsupported collection operation: ' . $this->name);
479485
}

tests/UnifiedSpecTests/UnifiedSpecTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ class UnifiedSpecTest extends FunctionalTestCase
2121
{
2222
/** @var array */
2323
private static $incompleteTests = [
24+
'change-streams/change-streams: Test with document comment' => 'Not yet implemented (PHPLIB-749)',
25+
'change-streams/change-streams: Test with string comment' => 'Not yet implemented (PHPLIB-749)',
26+
'change-streams/change-streams: Test that comment is set on getMore' => 'Not yet implemented (PHPLIB-749)',
2427
'command-monitoring/pre-42-server-connection-id: command events do not include server connection id' => 'Not yet implemented (PHPC-1899, PHPLIB-718)',
2528
'command-monitoring/server-connection-id: command events include server connection id' => 'Not yet implemented (PHPC-1899, PHPLIB-718)',
2629
// Many load balancer tests use CMAP events and/or assertNumberConnectionsCheckedOut

tests/UnifiedSpecTests/UnifiedTestRunner.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ final class UnifiedTestRunner
5050
public const SERVER_ERROR_UNAUTHORIZED = 13;
5151

5252
public const MIN_SCHEMA_VERSION = '1.0';
53-
public const MAX_SCHEMA_VERSION = '1.5';
53+
54+
// Note: schema version 1.6 is not yet implemented (see: PHPLIB-718)
55+
public const MAX_SCHEMA_VERSION = '1.7';
5456

5557
/** @var MongoDB\Client */
5658
private $internalClient;

tests/UnifiedSpecTests/Util.php

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ final class Util
8787
'find' => ['filter', 'session', 'allowDiskUse', 'allowPartialResults', 'batchSize', 'collation', 'comment', 'cursorType', 'hint', 'limit', 'max', 'maxAwaitTimeMS', 'maxScan', 'maxTimeMS', 'min', 'modifiers', 'noCursorTimeout', 'oplogReplay', 'projection', 'returnKey', 'showRecordId', 'skip', 'snapshot', 'sort'],
8888
'findOne' => ['filter', 'session', 'allowDiskUse', 'allowPartialResults', 'batchSize', 'collation', 'comment', 'cursorType', 'hint', 'max', 'maxAwaitTimeMS', 'maxScan', 'maxTimeMS', 'min', 'modifiers', 'noCursorTimeout', 'oplogReplay', 'projection', 'returnKey', 'showRecordId', 'skip', 'snapshot', 'sort'],
8989
'findOneAndReplace' => ['returnDocument', 'filter', 'replacement', 'session', 'projection', 'returnDocument', 'upsert', 'arrayFilters', 'bypassDocumentValidation', 'collation', 'hint', 'maxTimeMS', 'new', 'remove', 'sort'],
90+
'rename' => ['to'],
9091
'replaceOne' => ['filter', 'replacement', 'session', 'upsert', 'arrayFilters', 'bypassDocumentValidation', 'collation', 'hint'],
9192
'findOneAndUpdate' => ['returnDocument', 'filter', 'update', 'session', 'upsert', 'projection', 'remove', 'arrayFilters', 'bypassDocumentValidation', 'collation', 'hint', 'maxTimeMS', 'sort'],
9293
'updateMany' => ['filter', 'update', 'session', 'upsert', 'arrayFilters', 'bypassDocumentValidation', 'collation', 'hint'],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
{
2+
"description": "change-streams-errors",
3+
"schemaVersion": "1.7",
4+
"createEntities": [
5+
{
6+
"client": {
7+
"id": "client0",
8+
"observeEvents": [
9+
"commandStartedEvent"
10+
],
11+
"ignoreCommandMonitoringEvents": [
12+
"killCursors"
13+
],
14+
"useMultipleMongoses": false
15+
}
16+
},
17+
{
18+
"client": {
19+
"id": "globalClient",
20+
"useMultipleMongoses": false
21+
}
22+
},
23+
{
24+
"database": {
25+
"id": "database0",
26+
"client": "client0",
27+
"databaseName": "database0"
28+
}
29+
},
30+
{
31+
"collection": {
32+
"id": "collection0",
33+
"database": "database0",
34+
"collectionName": "collection0"
35+
}
36+
},
37+
{
38+
"database": {
39+
"id": "globalDatabase0",
40+
"client": "globalClient",
41+
"databaseName": "database0"
42+
}
43+
},
44+
{
45+
"collection": {
46+
"id": "globalCollection0",
47+
"database": "globalDatabase0",
48+
"collectionName": "collection0"
49+
}
50+
}
51+
],
52+
"tests": [
53+
{
54+
"description": "The watch helper must not throw a custom exception when executed against a single server topology, but instead depend on a server error",
55+
"runOnRequirements": [
56+
{
57+
"minServerVersion": "3.6.0",
58+
"topologies": [
59+
"single"
60+
]
61+
}
62+
],
63+
"operations": [
64+
{
65+
"name": "createChangeStream",
66+
"object": "collection0",
67+
"arguments": {
68+
"pipeline": []
69+
},
70+
"expectError": {
71+
"errorCode": 40573
72+
}
73+
}
74+
]
75+
},
76+
{
77+
"description": "Change Stream should error when an invalid aggregation stage is passed in",
78+
"runOnRequirements": [
79+
{
80+
"minServerVersion": "3.6.0",
81+
"topologies": [
82+
"replicaset"
83+
]
84+
}
85+
],
86+
"operations": [
87+
{
88+
"name": "createChangeStream",
89+
"object": "collection0",
90+
"arguments": {
91+
"pipeline": [
92+
{
93+
"$unsupported": "foo"
94+
}
95+
]
96+
},
97+
"expectError": {
98+
"errorCode": 40324
99+
}
100+
}
101+
],
102+
"expectEvents": [
103+
{
104+
"client": "client0",
105+
"ignoreExtraEvents": true,
106+
"events": [
107+
{
108+
"commandStartedEvent": {
109+
"command": {
110+
"aggregate": "collection0",
111+
"cursor": {},
112+
"pipeline": [
113+
{
114+
"$changeStream": {}
115+
},
116+
{
117+
"$unsupported": "foo"
118+
}
119+
]
120+
},
121+
"commandName": "aggregate",
122+
"databaseName": "database0"
123+
}
124+
}
125+
]
126+
}
127+
]
128+
},
129+
{
130+
"description": "Change Stream should error when _id is projected out",
131+
"runOnRequirements": [
132+
{
133+
"minServerVersion": "4.1.11",
134+
"topologies": [
135+
"replicaset",
136+
"sharded-replicaset",
137+
"load-balanced"
138+
]
139+
}
140+
],
141+
"operations": [
142+
{
143+
"name": "createChangeStream",
144+
"object": "collection0",
145+
"arguments": {
146+
"pipeline": [
147+
{
148+
"$project": {
149+
"_id": 0
150+
}
151+
}
152+
]
153+
},
154+
"saveResultAsEntity": "changeStream0"
155+
},
156+
{
157+
"name": "insertOne",
158+
"object": "globalCollection0",
159+
"arguments": {
160+
"document": {
161+
"z": 3
162+
}
163+
}
164+
},
165+
{
166+
"name": "iterateUntilDocumentOrError",
167+
"object": "changeStream0",
168+
"expectError": {
169+
"errorCode": 280
170+
}
171+
}
172+
]
173+
},
174+
{
175+
"description": "change stream errors on ElectionInProgress",
176+
"runOnRequirements": [
177+
{
178+
"minServerVersion": "4.2",
179+
"topologies": [
180+
"replicaset",
181+
"sharded-replicaset",
182+
"load-balanced"
183+
]
184+
}
185+
],
186+
"operations": [
187+
{
188+
"name": "failPoint",
189+
"object": "testRunner",
190+
"arguments": {
191+
"client": "globalClient",
192+
"failPoint": {
193+
"configureFailPoint": "failCommand",
194+
"mode": {
195+
"times": 1
196+
},
197+
"data": {
198+
"failCommands": [
199+
"getMore"
200+
],
201+
"errorCode": 216,
202+
"closeConnection": false
203+
}
204+
}
205+
}
206+
},
207+
{
208+
"name": "createChangeStream",
209+
"object": "collection0",
210+
"arguments": {
211+
"pipeline": []
212+
},
213+
"saveResultAsEntity": "changeStream0"
214+
},
215+
{
216+
"name": "insertOne",
217+
"object": "globalCollection0",
218+
"arguments": {
219+
"document": {
220+
"z": 3
221+
}
222+
}
223+
},
224+
{
225+
"name": "iterateUntilDocumentOrError",
226+
"object": "changeStream0",
227+
"expectError": {
228+
"errorCode": 216
229+
}
230+
}
231+
]
232+
}
233+
]
234+
}

0 commit comments

Comments
 (0)