Skip to content

Commit 41892d2

Browse files
committed
Use assertSameDocument to check change stream results
This also removes the hard-coded database and collection names, which are subject to change based on the test suite configuration and getCollectionName() implementation.
1 parent 3a4e920 commit 41892d2

File tree

2 files changed

+61
-47
lines changed

2 files changed

+61
-47
lines changed

tests/DocumentationExamplesTest.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -954,14 +954,16 @@ public function testChangeStreamExample_1_4()
954954
$insertedId = $insertedResult->getInsertedId();
955955
$cursor->next();
956956
$current = $cursor->current();
957-
$expectedChange = (object) [
957+
958+
$expectedChange = [
958959
'_id' => $current->_id,
959960
'operationType' => 'insert',
960-
'fullDocument' => (object) ['_id' => $insertedId, 'x' => 1],
961-
'ns' => (object) ['db' => 'phplib_test', 'coll' => 'inventory'],
962-
'documentKey' => (object) ['_id' => $insertedId]
961+
'fullDocument' => ['_id' => $insertedId, 'x' => 1],
962+
'ns' => ['db' => $this->getDatabaseName(), 'coll' => 'inventory'],
963+
'documentKey' => ['_id' => $insertedId],
963964
];
964-
$this->assertEquals($expectedChange, $current);
965+
966+
$this->assertSameDocument($expectedChange, $current);
965967

966968
// Start Changestream Example 3
967969
$resumeToken = ($current !== null) ? $current->_id : null;
@@ -974,14 +976,16 @@ public function testChangeStreamExample_1_4()
974976
$insertedResult = $db->inventory->insertOne(['x' => 2]);
975977
$insertedId = $insertedResult->getInsertedId();
976978
$cursor->next();
977-
$expectedChange = (object) [
979+
980+
$expectedChange = [
978981
'_id' => $cursor->current()->_id,
979982
'operationType' => 'insert',
980-
'fullDocument' => (object) ['_id' => $insertedId, 'x' => 2],
981-
'ns' => (object) ['db' => 'phplib_test', 'coll' => 'inventory'],
982-
'documentKey' => (object) ['_id' => $insertedId]
983+
'fullDocument' => ['_id' => $insertedId, 'x' => 2],
984+
'ns' => ['db' => $this->getDatabaseName(), 'coll' => 'inventory'],
985+
'documentKey' => ['_id' => $insertedId],
983986
];
984-
$this->assertEquals($expectedChange, $cursor->current());
987+
988+
$this->assertSameDocument($expectedChange, $cursor->current());
985989

986990
// Start Changestream Example 4
987991
$pipeline = [['$match' => ['$or' => [['fullDocument.username' => 'alice'], ['operationType' => 'delete']]]]];

tests/Operation/WatchFunctionalTest.php

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,33 @@ public function testResume()
4141
$this->insertDocument(['_id' => 2, 'x' => 'bar']);
4242

4343
$changeStream->next();
44-
$expectedResult = (object) ([
45-
'_id' => $changeStream->current()->_id,
46-
'operationType' => 'insert',
47-
'fullDocument' => (object) ['_id' => 2, 'x' => 'bar'],
48-
'ns' => (object) ['db' => 'phplib_test', 'coll' => 'WatchFunctionalTest.e68b9f01'],
49-
'documentKey' => (object) ['_id' => 2]
50-
]);
51-
$this->assertEquals($expectedResult, $changeStream->current());
44+
45+
$expectedResult = [
46+
'_id' => $changeStream->current()->_id,
47+
'operationType' => 'insert',
48+
'fullDocument' => ['_id' => 2, 'x' => 'bar'],
49+
'ns' => ['db' => $this->getDatabaseName(), 'coll' => $this->getCollectionName()],
50+
'documentKey' => ['_id' => 2],
51+
];
52+
53+
$this->assertSameDocument($expectedResult, $changeStream->current());
5254

5355
$operation = new DatabaseCommand($this->getDatabaseName(), ["killCursors" => $this->getCollectionName(), "cursors" => [$changeStream->getCursorId()]]);
5456
$operation->execute($this->getPrimaryServer());
5557

5658
$this->insertDocument(['_id' => 3, 'x' => 'baz']);
5759

5860
$changeStream->next();
59-
$expectedResult = (object) ([
60-
'_id' => $changeStream->current()->_id,
61-
'operationType' => 'insert',
62-
'fullDocument' => (object) ['_id' => 3, 'x' => 'baz'],
63-
'ns' => (object) ['db' => 'phplib_test', 'coll' => 'WatchFunctionalTest.e68b9f01'],
64-
'documentKey' => (object) ['_id' => 3]
65-
]);
66-
$this->assertEquals($expectedResult, $changeStream->current());
61+
62+
$expectedResult = [
63+
'_id' => $changeStream->current()->_id,
64+
'operationType' => 'insert',
65+
'fullDocument' => ['_id' => 3, 'x' => 'baz'],
66+
'ns' => ['db' => $this->getDatabaseName(), 'coll' => $this->getCollectionName()],
67+
'documentKey' => ['_id' => 3]
68+
];
69+
70+
$this->assertSameDocument($expectedResult, $changeStream->current());
6771
}
6872

6973
/**
@@ -135,14 +139,16 @@ public function testNoChangeAfterResumeBeforeInsert()
135139
$this->insertDocument(['_id' => 2, 'x' => 'bar']);
136140

137141
$changeStream->next();
138-
$expectedResult = (object) ([
139-
'_id' => $changeStream->current()->_id,
140-
'operationType' => 'insert',
141-
'fullDocument' => (object) ['_id' => 2, 'x' => 'bar'],
142-
'ns' => (object) ['db' => 'phplib_test', 'coll' => 'WatchFunctionalTest.4a554985'],
143-
'documentKey' => (object) ['_id' => 2]
144-
]);
145-
$this->assertEquals($expectedResult, $changeStream->current());
142+
143+
$expectedResult = [
144+
'_id' => $changeStream->current()->_id,
145+
'operationType' => 'insert',
146+
'fullDocument' => ['_id' => 2, 'x' => 'bar'],
147+
'ns' => ['db' => $this->getDatabaseName(), 'coll' => $this->getCollectionName()],
148+
'documentKey' => ['_id' => 2],
149+
];
150+
151+
$this->assertSameDocument($expectedResult, $changeStream->current());
146152

147153
$operation = new DatabaseCommand($this->getDatabaseName(), ["killCursors" => $this->getCollectionName(), "cursors" => [$changeStream->getCursorId()]]);
148154
$operation->execute($this->getPrimaryServer());
@@ -153,14 +159,16 @@ public function testNoChangeAfterResumeBeforeInsert()
153159
$this->insertDocument(['_id' => 3, 'x' => 'baz']);
154160

155161
$changeStream->next();
156-
$expectedResult = (object) ([
157-
'_id' => $changeStream->current()->_id,
158-
'operationType' => 'insert',
159-
'fullDocument' => (object) ['_id' => 3, 'x' => 'baz'],
160-
'ns' => (object) ['db' => 'phplib_test', 'coll' => 'WatchFunctionalTest.4a554985'],
161-
'documentKey' => (object) ['_id' => 3]
162-
]);
163-
$this->assertEquals($expectedResult, $changeStream->current());
162+
163+
$expectedResult = [
164+
'_id' => $changeStream->current()->_id,
165+
'operationType' => 'insert',
166+
'fullDocument' => ['_id' => 3, 'x' => 'baz'],
167+
'ns' => ['db' => $this->getDatabaseName(), 'coll' => $this->getCollectionName()],
168+
'documentKey' => ['_id' => 3],
169+
];
170+
171+
$this->assertSameDocument($expectedResult, $changeStream->current());
164172
}
165173

166174
public function testResumeAfterKillThenNoOperations()
@@ -228,11 +236,13 @@ public function testNonEmptyPipeline()
228236
$this->insertDocument(['_id' => 1]);
229237

230238
$changeStream->next();
231-
$expectedResult = (object) ([
232-
'_id' => $changeStream->current()->_id,
233-
'foo' => [0]
234-
]);
235-
$this->assertEquals($expectedResult, $changeStream->current());
239+
240+
$expectedResult = [
241+
'_id' => $changeStream->current()->_id,
242+
'foo' => [0],
243+
];
244+
245+
$this->assertSameDocument($expectedResult, $changeStream->current());
236246
}
237247

238248
public function testCursorWithEmptyBatchNotClosed()

0 commit comments

Comments
 (0)