Skip to content

Commit 50533da

Browse files
committed
Test read-write aggregation pipeline
1 parent 0c09739 commit 50533da

File tree

1 file changed

+54
-36
lines changed

1 file changed

+54
-36
lines changed

tests/Collection/CollectionFunctionalTest.php

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use MongoDB\Collection;
88
use MongoDB\Database;
99
use MongoDB\Driver\BulkWrite;
10+
use MongoDB\Driver\Exception\CommandException;
1011
use MongoDB\Driver\ReadConcern;
1112
use MongoDB\Driver\ReadPreference;
1213
use MongoDB\Driver\WriteConcern;
@@ -21,7 +22,7 @@
2122
use function call_user_func;
2223
use function is_scalar;
2324
use function json_encode;
24-
use function strchr;
25+
use function str_contains;
2526
use function usort;
2627
use function version_compare;
2728

@@ -447,16 +448,28 @@ public function testMapReduce(): void
447448
public function collectionMethodClosures()
448449
{
449450
return [
450-
[
451+
'read-only aggregate' => [
451452
function ($collection, $session, $options = []): void {
452453
$collection->aggregate(
453454
[['$match' => ['_id' => ['$lt' => 3]]]],
454455
['session' => $session] + $options
455456
);
457+
}, 'r',
458+
],
459+
460+
'read-write aggregate' => [
461+
function ($collection, $session, $options = []): void {
462+
$collection->aggregate(
463+
[
464+
['$match' => ['_id' => ['$lt' => 3]]],
465+
['$merge' => $collection . '_out'],
466+
],
467+
['session' => $session] + $options
468+
);
456469
}, 'rw',
457470
],
458471

459-
[
472+
'bulkWrite insertOne' => [
460473
function ($collection, $session, $options = []): void {
461474
$collection->bulkWrite(
462475
[['insertOne' => [['test' => 'foo']]]],
@@ -466,7 +479,7 @@ function ($collection, $session, $options = []): void {
466479
],
467480

468481
/* Disabled, as count command can't be used in transactions
469-
[
482+
'count' => [
470483
function($collection, $session, $options = []) {
471484
$collection->count(
472485
[],
@@ -476,7 +489,7 @@ function($collection, $session, $options = []) {
476489
],
477490
*/
478491

479-
[
492+
'countDocuments' => [
480493
function ($collection, $session, $options = []): void {
481494
$collection->countDocuments(
482495
[],
@@ -486,7 +499,7 @@ function ($collection, $session, $options = []): void {
486499
],
487500

488501
/* Disabled, as it's illegal to use createIndex command in transactions
489-
[
502+
'createIndex' => [
490503
function($collection, $session, $options = []) {
491504
$collection->createIndex(
492505
['test' => 1],
@@ -496,7 +509,7 @@ function($collection, $session, $options = []) {
496509
],
497510
*/
498511

499-
[
512+
'deleteMany' => [
500513
function ($collection, $session, $options = []): void {
501514
$collection->deleteMany(
502515
['test' => 'foo'],
@@ -505,7 +518,7 @@ function ($collection, $session, $options = []): void {
505518
}, 'w',
506519
],
507520

508-
[
521+
'deleteOne' => [
509522
function ($collection, $session, $options = []): void {
510523
$collection->deleteOne(
511524
['test' => 'foo'],
@@ -514,7 +527,7 @@ function ($collection, $session, $options = []): void {
514527
}, 'w',
515528
],
516529

517-
[
530+
'distinct' => [
518531
function ($collection, $session, $options = []): void {
519532
$collection->distinct(
520533
'_id',
@@ -525,7 +538,7 @@ function ($collection, $session, $options = []): void {
525538
],
526539

527540
/* Disabled, as it's illegal to use drop command in transactions
528-
[
541+
'drop' => [
529542
function($collection, $session, $options = []) {
530543
$collection->drop(
531544
['session' => $session] + $options
@@ -535,7 +548,7 @@ function($collection, $session, $options = []) {
535548
*/
536549

537550
/* Disabled, as it's illegal to use dropIndexes command in transactions
538-
[
551+
'dropIndex' => [
539552
function($collection, $session, $options = []) {
540553
$collection->dropIndex(
541554
'_id_1',
@@ -545,7 +558,7 @@ function($collection, $session, $options = []) {
545558
], */
546559

547560
/* Disabled, as it's illegal to use dropIndexes command in transactions
548-
[
561+
'dropIndexes' => [
549562
function($collection, $session, $options = []) {
550563
$collection->dropIndexes(
551564
['session' => $session] + $options
@@ -555,7 +568,7 @@ function($collection, $session, $options = []) {
555568
*/
556569

557570
/* Disabled, as count command can't be used in transactions
558-
[
571+
'estimatedDocumentCount' => [
559572
function($collection, $session, $options = []) {
560573
$collection->estimatedDocumentCount(
561574
['session' => $session] + $options
@@ -564,7 +577,7 @@ function($collection, $session, $options = []) {
564577
],
565578
*/
566579

567-
[
580+
'find' => [
568581
function ($collection, $session, $options = []): void {
569582
$collection->find(
570583
['test' => 'foo'],
@@ -573,7 +586,7 @@ function ($collection, $session, $options = []): void {
573586
}, 'r',
574587
],
575588

576-
[
589+
'findOne' => [
577590
function ($collection, $session, $options = []): void {
578591
$collection->findOne(
579592
['test' => 'foo'],
@@ -582,7 +595,7 @@ function ($collection, $session, $options = []): void {
582595
}, 'r',
583596
],
584597

585-
[
598+
'findOneAndDelete' => [
586599
function ($collection, $session, $options = []): void {
587600
$collection->findOneAndDelete(
588601
['test' => 'foo'],
@@ -591,7 +604,7 @@ function ($collection, $session, $options = []): void {
591604
}, 'w',
592605
],
593606

594-
[
607+
'findOneAndReplace' => [
595608
function ($collection, $session, $options = []): void {
596609
$collection->findOneAndReplace(
597610
['test' => 'foo'],
@@ -601,7 +614,7 @@ function ($collection, $session, $options = []): void {
601614
}, 'w',
602615
],
603616

604-
[
617+
'findOneAndUpdate' => [
605618
function ($collection, $session, $options = []): void {
606619
$collection->findOneAndUpdate(
607620
['test' => 'foo'],
@@ -611,7 +624,7 @@ function ($collection, $session, $options = []): void {
611624
}, 'w',
612625
],
613626

614-
[
627+
'insertMany' => [
615628
function ($collection, $session, $options = []): void {
616629
$collection->insertMany(
617630
[
@@ -623,7 +636,7 @@ function ($collection, $session, $options = []): void {
623636
}, 'w',
624637
],
625638

626-
[
639+
'insertOne' => [
627640
function ($collection, $session, $options = []): void {
628641
$collection->insertOne(
629642
['test' => 'foo'],
@@ -633,7 +646,7 @@ function ($collection, $session, $options = []): void {
633646
],
634647

635648
/* Disabled, as it's illegal to use listIndexes command in transactions
636-
[
649+
'listIndexes' => [
637650
function($collection, $session, $options = []) {
638651
$collection->listIndexes(
639652
['session' => $session] + $options
@@ -643,7 +656,7 @@ function($collection, $session, $options = []) {
643656
*/
644657

645658
/* Disabled, as it's illegal to use mapReduce command in transactions
646-
[
659+
'mapReduce' => [
647660
function($collection, $session, $options = []) {
648661
$collection->mapReduce(
649662
new \MongoDB\BSON\Javascript('function() { emit(this.state, this.pop); }'),
@@ -655,7 +668,7 @@ function($collection, $session, $options = []) {
655668
],
656669
*/
657670

658-
[
671+
'replaceOne' => [
659672
function ($collection, $session, $options = []): void {
660673
$collection->replaceOne(
661674
['test' => 'foo'],
@@ -665,7 +678,7 @@ function ($collection, $session, $options = []): void {
665678
}, 'w',
666679
],
667680

668-
[
681+
'updateMany' => [
669682
function ($collection, $session, $options = []): void {
670683
$collection->updateMany(
671684
['test' => 'foo'],
@@ -675,7 +688,7 @@ function ($collection, $session, $options = []): void {
675688
}, 'w',
676689
],
677690

678-
[
691+
'updateOne' => [
679692
function ($collection, $session, $options = []): void {
680693
$collection->updateOne(
681694
['test' => 'foo'],
@@ -686,7 +699,7 @@ function ($collection, $session, $options = []): void {
686699
],
687700

688701
/* Disabled, as it's illegal to use change streams in transactions
689-
[
702+
'watch' => [
690703
function($collection, $session, $options = []) {
691704
$collection->watch(
692705
[],
@@ -698,32 +711,28 @@ function($collection, $session, $options = []) {
698711
];
699712
}
700713

701-
public function collectionReadMethodClosures()
714+
public function collectionReadMethodClosures(): array
702715
{
703716
return array_filter(
704717
$this->collectionMethodClosures(),
705718
function ($rw) {
706-
if (strchr($rw[1], 'r') !== false) {
707-
return true;
708-
}
719+
return str_contains($rw[1], 'r');
709720
}
710721
);
711722
}
712723

713-
public function collectionWriteMethodClosures()
724+
public function collectionWriteMethodClosures(): array
714725
{
715726
return array_filter(
716727
$this->collectionMethodClosures(),
717728
function ($rw) {
718-
if (strchr($rw[1], 'w') !== false) {
719-
return true;
720-
}
729+
return str_contains($rw[1], 'w');
721730
}
722731
);
723732
}
724733

725734
/** @dataProvider collectionMethodClosures */
726-
public function testMethodDoesNotInheritReadWriteConcernInTranasaction(Closure $method): void
735+
public function testMethodDoesNotInheritReadWriteConcernInTransaction(Closure $method): void
727736
{
728737
$this->skipIfTransactionsAreNotSupported();
729738

@@ -739,7 +748,16 @@ public function testMethodDoesNotInheritReadWriteConcernInTranasaction(Closure $
739748

740749
(new CommandObserver())->observe(
741750
function () use ($method, $collection, $session): void {
742-
call_user_func($method, $collection, $session);
751+
try {
752+
call_user_func($method, $collection, $session);
753+
} catch (CommandException $exception) {
754+
// Write aggregates are not supported in transactions
755+
if ($exception->getResultDocument()->codeName === 'OperationNotSupportedInTransaction') {
756+
$this->markTestSkipped('OperationNotSupportedInTransaction: ' . $exception->getMessage());
757+
}
758+
759+
throw $exception;
760+
}
743761
},
744762
function (array $event): void {
745763
$this->assertObjectNotHasAttribute('writeConcern', $event['started']->getCommand());

0 commit comments

Comments
 (0)