26
26
use MongoDB \Client ;
27
27
use MongoDB \Collection ;
28
28
use MongoDB \Database ;
29
+ use MongoDB \Driver \Exception \CommandException ;
29
30
use MongoDB \Driver \WriteConcern ;
30
31
use MongoDB \GridFS \Bucket ;
31
32
use MongoDB \Model \CollectionInfo ;
@@ -420,6 +421,27 @@ public function testCreateDocumentSearchIndexes(): void
420
421
$ this ->schemaManager ->createDocumentSearchIndexes (CmsArticle::class);
421
422
}
422
423
424
+ public function testCreateDocumentSearchIndexesNotSupported (): void
425
+ {
426
+ $ exception = $ this ->createSearchIndexCommandException ();
427
+
428
+ $ cmsArticleCollectionName = $ this ->dm ->getClassMetadata (CmsArticle::class)->getCollection ();
429
+ foreach ($ this ->documentCollections as $ collectionName => $ collection ) {
430
+ if ($ collectionName === $ cmsArticleCollectionName ) {
431
+ $ collection
432
+ ->expects ($ this ->once ())
433
+ ->method ('createSearchIndexes ' )
434
+ ->with ($ this ->anything ())
435
+ ->willThrowException ($ exception );
436
+ } else {
437
+ $ collection ->expects ($ this ->never ())->method ('createSearchIndexes ' );
438
+ }
439
+ }
440
+
441
+ $ this ->expectExceptionObject ($ exception );
442
+ $ this ->schemaManager ->createDocumentSearchIndexes (CmsArticle::class);
443
+ }
444
+
423
445
public function testUpdateDocumentSearchIndexes (): void
424
446
{
425
447
$ collectionName = $ this ->dm ->getClassMetadata (CmsArticle::class)->getCollection ();
@@ -443,6 +465,66 @@ public function testUpdateDocumentSearchIndexes(): void
443
465
$ this ->schemaManager ->updateDocumentSearchIndexes (CmsArticle::class);
444
466
}
445
467
468
+ public function testUpdateDocumentSearchIndexesNotSupportedForClassWithoutSearchIndexes (): void
469
+ {
470
+ // Class has no search indexes, so if the server doesn't support them we assume everything is fine
471
+ $ collectionName = $ this ->dm ->getClassMetadata (CmsProduct::class)->getCollection ();
472
+ $ collection = $ this ->documentCollections [$ collectionName ];
473
+ $ collection
474
+ ->expects ($ this ->once ())
475
+ ->method ('listSearchIndexes ' )
476
+ ->willThrowException ($ this ->createSearchIndexCommandException ());
477
+ $ collection
478
+ ->expects ($ this ->never ())
479
+ ->method ('dropSearchIndex ' );
480
+ $ collection
481
+ ->expects ($ this ->never ())
482
+ ->method ('updateSearchIndex ' );
483
+
484
+ $ this ->schemaManager ->updateDocumentSearchIndexes (CmsProduct::class);
485
+ }
486
+
487
+ public function testUpdateDocumentSearchIndexesNotSupportedForClassWithoutSearchIndexesOnOlderServers (): void
488
+ {
489
+ // Class has no search indexes, so if the server doesn't support them we assume everything is fine
490
+ $ collectionName = $ this ->dm ->getClassMetadata (CmsProduct::class)->getCollection ();
491
+ $ collection = $ this ->documentCollections [$ collectionName ];
492
+ $ collection
493
+ ->expects ($ this ->once ())
494
+ ->method ('listSearchIndexes ' )
495
+ ->willThrowException ($ this ->createSearchIndexCommandExceptionForOlderServers ());
496
+ $ collection
497
+ ->expects ($ this ->never ())
498
+ ->method ('dropSearchIndex ' );
499
+ $ collection
500
+ ->expects ($ this ->never ())
501
+ ->method ('updateSearchIndex ' );
502
+
503
+ $ this ->schemaManager ->updateDocumentSearchIndexes (CmsProduct::class);
504
+ }
505
+
506
+ public function testUpdateDocumentSearchIndexesNotSupportedForClassWithSearchIndexes (): void
507
+ {
508
+ $ exception = $ this ->createSearchIndexCommandException ();
509
+
510
+ // This class has search indexes, so we do expect an exception
511
+ $ collectionName = $ this ->dm ->getClassMetadata (CmsArticle::class)->getCollection ();
512
+ $ collection = $ this ->documentCollections [$ collectionName ];
513
+ $ collection
514
+ ->expects ($ this ->once ())
515
+ ->method ('listSearchIndexes ' )
516
+ ->willThrowException ($ exception );
517
+ $ collection
518
+ ->expects ($ this ->never ())
519
+ ->method ('dropSearchIndex ' );
520
+ $ collection
521
+ ->expects ($ this ->never ())
522
+ ->method ('updateSearchIndex ' );
523
+
524
+ $ this ->expectExceptionObject ($ exception );
525
+ $ this ->schemaManager ->updateDocumentSearchIndexes (CmsArticle::class);
526
+ }
527
+
446
528
public function testDeleteDocumentSearchIndexes (): void
447
529
{
448
530
$ collectionName = $ this ->dm ->getClassMetadata (CmsArticle::class)->getCollection ();
@@ -459,6 +541,21 @@ public function testDeleteDocumentSearchIndexes(): void
459
541
$ this ->schemaManager ->deleteDocumentSearchIndexes (CmsArticle::class);
460
542
}
461
543
544
+ public function testDeleteDocumentSearchIndexesNotSupported (): void
545
+ {
546
+ $ collectionName = $ this ->dm ->getClassMetadata (CmsArticle::class)->getCollection ();
547
+ $ collection = $ this ->documentCollections [$ collectionName ];
548
+ $ collection
549
+ ->expects ($ this ->once ())
550
+ ->method ('listSearchIndexes ' )
551
+ ->willThrowException ($ this ->createSearchIndexCommandException ());
552
+ $ collection
553
+ ->expects ($ this ->never ())
554
+ ->method ('dropSearchIndex ' );
555
+
556
+ $ this ->schemaManager ->deleteDocumentSearchIndexes (CmsArticle::class);
557
+ }
558
+
462
559
public function testUpdateValidators (): void
463
560
{
464
561
$ dbCommands = [];
@@ -1239,4 +1336,14 @@ private function writeOptions(array $expectedWriteOptions): Constraint
1239
1336
return true ;
1240
1337
});
1241
1338
}
1339
+
1340
+ private function createSearchIndexCommandException (): CommandException
1341
+ {
1342
+ return new CommandException ('PlanExecutor error during aggregation :: caused by :: Search index commands are only supported with Atlas. ' , 115 );
1343
+ }
1344
+
1345
+ private function createSearchIndexCommandExceptionForOlderServers (): CommandException
1346
+ {
1347
+ return new CommandException ('Unrecognized pipeline stage name: \'$listSearchIndexes \'' , 40234 );
1348
+ }
1242
1349
}
0 commit comments