Skip to content

Commit 5ed3a1a

Browse files
committed
Refactor command logging for spec tests and fix docs
Merged from #645. * Don't collect unexpected extra commands in spec tests: as discussed in #642 (comment), it makes more sense to not collect commands that we're not going to be asserting against instead of dropping them later. * While the docs for the aggregation helpers in `MongoDB\Database` and `MongoDB\Collection` have been updated with respect to read concerns and write stages, the documentation in the `MongoDB\Operation\Aggregate` wasn't. This is done in this PR.
2 parents ebc4a99 + 2e954b4 commit 5ed3a1a

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

src/Operation/Aggregate.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ class Aggregate implements Executable
6363
* * batchSize (integer): The number of documents to return per batch.
6464
*
6565
* * bypassDocumentValidation (boolean): If true, allows the write to
66-
* circumvent document level validation. This only applies when the $out
67-
* stage is specified.
66+
* circumvent document level validation. This only applies when an $out
67+
* or $merge stage is specified.
6868
*
6969
* For servers < 3.2, this option is ignored as document level validation
7070
* is not available.
@@ -87,15 +87,14 @@ class Aggregate implements Executable
8787
* * maxTimeMS (integer): The maximum amount of time to allow the query to
8888
* run.
8989
*
90-
* * readConcern (MongoDB\Driver\ReadConcern): Read concern. Note that a
91-
* "majority" read concern is not compatible with the $out stage.
90+
* * readConcern (MongoDB\Driver\ReadConcern): Read concern.
9291
*
9392
* This is not supported for server versions < 3.2 and will result in an
9493
* exception at execution time if used.
9594
*
9695
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
9796
*
98-
* This option is ignored if the $out stage is specified.
97+
* This option is ignored if an $out or $merge stage is specified.
9998
*
10099
* * session (MongoDB\Driver\Session): Client session.
101100
*
@@ -111,7 +110,7 @@ class Aggregate implements Executable
111110
* mongod/mongos upgrades.
112111
*
113112
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern. This only
114-
* applies when the $out stage is specified.
113+
* applies when an $out or $merge stage is specified.
115114
*
116115
* This is not supported for server versions < 3.4 and will result in an
117116
* exception at execution time if used.
@@ -298,10 +297,10 @@ public function execute(Server $server)
298297
* Create the aggregate command.
299298
*
300299
* @param Server $server
301-
* @param boolean $hasOutStage
300+
* @param boolean $hasWriteStage
302301
* @return Command
303302
*/
304-
private function createCommand(Server $server, $hasOutStage)
303+
private function createCommand(Server $server, $hasWriteStage)
305304
{
306305
$cmd = [
307306
'aggregate' => isset($this->collectionName) ? $this->collectionName : 1,
@@ -337,10 +336,10 @@ private function createCommand(Server $server, $hasOutStage)
337336
}
338337

339338
if ($this->options['useCursor']) {
340-
/* Ignore batchSize if pipeline includes an $out stage, as no
341-
* documents will be returned and sending a batchSize of zero could
342-
* prevent the pipeline from executing at all. */
343-
$cmd['cursor'] = isset($this->options["batchSize"]) && ! $hasOutStage
339+
/* Ignore batchSize if pipeline includes an $out or $merge stage, as
340+
* no documents will be returned and sending a batchSize of zero
341+
* could prevent the pipeline from executing at all. */
342+
$cmd['cursor'] = isset($this->options["batchSize"]) && !$hasWriteStage
344343
? ['batchSize' => $this->options["batchSize"]]
345344
: new stdClass;
346345
}

tests/SpecTests/CommandExpectations.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public static function fromTransactions(array $expectedEvents)
9191
*/
9292
public function commandFailed(CommandFailedEvent $event)
9393
{
94-
if ($this->ignoreCommandFailed) {
94+
if ($this->ignoreCommandFailed || ($this->ignoreExtraEvents && count($this->actualEvents) === count($this->expectedEvents))) {
9595
return;
9696
}
9797

@@ -105,7 +105,7 @@ public function commandFailed(CommandFailedEvent $event)
105105
*/
106106
public function commandStarted(CommandStartedEvent $event)
107107
{
108-
if ($this->ignoreCommandStarted) {
108+
if ($this->ignoreCommandStarted || ($this->ignoreExtraEvents && count($this->actualEvents) === count($this->expectedEvents))) {
109109
return;
110110
}
111111

@@ -119,7 +119,7 @@ public function commandStarted(CommandStartedEvent $event)
119119
*/
120120
public function commandSucceeded(CommandSucceededEvent $event)
121121
{
122-
if ($this->ignoreCommandSucceeded) {
122+
if ($this->ignoreCommandSucceeded || ($this->ignoreExtraEvents && count($this->actualEvents) === count($this->expectedEvents))) {
123123
return;
124124
}
125125

@@ -150,15 +150,11 @@ public function stopMonitoring()
150150
*/
151151
public function assert(FunctionalTestCase $test, Context $context)
152152
{
153-
$actualEvents = $this->ignoreExtraEvents
154-
? array_slice($this->actualEvents, 0, count($this->expectedEvents))
155-
: $this->actualEvents;
156-
157-
$test->assertCount(count($this->expectedEvents), $actualEvents);
153+
$test->assertCount(count($this->expectedEvents), $this->actualEvents);
158154

159155
$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);
160156
$mi->attachIterator(new ArrayIterator($this->expectedEvents));
161-
$mi->attachIterator(new ArrayIterator($actualEvents));
157+
$mi->attachIterator(new ArrayIterator($this->actualEvents));
162158

163159
foreach ($mi as $events) {
164160
list($expectedEventAndClass, $actualEvent) = $events;

0 commit comments

Comments
 (0)