From 9c50247b3525524a78914ee25ae97bc865a3a92d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Wed, 5 Nov 2025 14:11:18 -0500 Subject: [PATCH 1/4] PHPLIB-1689 Make the Atlas Search exception more specific --- src/Exception/SearchNotSupportedException.php | 31 +++++++++++++++++++ src/Operation/Aggregate.php | 12 ++++++- src/Operation/CreateSearchIndexes.php | 12 ++++++- tests/Collection/CollectionFunctionalTest.php | 27 ++++++++++++++++ 4 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 src/Exception/SearchNotSupportedException.php diff --git a/src/Exception/SearchNotSupportedException.php b/src/Exception/SearchNotSupportedException.php new file mode 100644 index 000000000..2c1312b31 --- /dev/null +++ b/src/Exception/SearchNotSupportedException.php @@ -0,0 +1,31 @@ +getCode(), $e); + } + + public static function isSearchNotSupportedError(Throwable $e): bool + { + if (! $e instanceof ServerException) { + return false; + } + + return in_array($e->getCode(), [ + 59, // MongoDB 4 to 6, 7-community: no such command: 'createSearchIndexes' + 40324, // MongoDB 4 to 6: Unrecognized pipeline stage name: '$listSearchIndexes' + 115, // MongoDB 7-ent: Search index commands are only supported with Atlas. + 6047401, // MongoDB 7: $listSearchIndexes stage is only allowed on MongoDB Atlas + 31082, // MongoDB 8: Using Atlas Search Database Commands and the $listSearchIndexes aggregation stage requires additional configuration. + ], true); + } +} diff --git a/src/Operation/Aggregate.php b/src/Operation/Aggregate.php index b5da6470c..4d120f9c4 100644 --- a/src/Operation/Aggregate.php +++ b/src/Operation/Aggregate.php @@ -21,12 +21,14 @@ use MongoDB\Driver\Command; use MongoDB\Driver\CursorInterface; use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException; +use MongoDB\Driver\Exception\ServerException; use MongoDB\Driver\ReadConcern; use MongoDB\Driver\ReadPreference; use MongoDB\Driver\Server; use MongoDB\Driver\Session; use MongoDB\Driver\WriteConcern; use MongoDB\Exception\InvalidArgumentException; +use MongoDB\Exception\SearchNotSupportedException; use MongoDB\Exception\UnexpectedValueException; use MongoDB\Exception\UnsupportedException; use MongoDB\Model\CodecCursor; @@ -233,7 +235,15 @@ public function execute(Server $server): CursorInterface $this->createCommandOptions(), ); - $cursor = $this->executeCommand($server, $command); + try { + $cursor = $this->executeCommand($server, $command); + } catch (ServerException $exception) { + if (SearchNotSupportedException::isSearchNotSupportedError($exception)) { + throw SearchNotSupportedException::create($exception); + } + + throw $exception; + } if (isset($this->options['codec'])) { return CodecCursor::fromCursor($cursor, $this->options['codec']); diff --git a/src/Operation/CreateSearchIndexes.php b/src/Operation/CreateSearchIndexes.php index d21ed9428..0b5b59573 100644 --- a/src/Operation/CreateSearchIndexes.php +++ b/src/Operation/CreateSearchIndexes.php @@ -19,8 +19,10 @@ use MongoDB\Driver\Command; use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException; +use MongoDB\Driver\Exception\ServerException; use MongoDB\Driver\Server; use MongoDB\Exception\InvalidArgumentException; +use MongoDB\Exception\SearchNotSupportedException; use MongoDB\Exception\UnsupportedException; use MongoDB\Model\SearchIndexInput; @@ -83,7 +85,15 @@ public function execute(Server $server): array $cmd['comment'] = $this->options['comment']; } - $cursor = $server->executeCommand($this->databaseName, new Command($cmd)); + try { + $cursor = $server->executeCommand($this->databaseName, new Command($cmd)); + } catch (ServerException $exception) { + if (SearchNotSupportedException::isSearchNotSupportedError($exception)) { + throw SearchNotSupportedException::create($exception); + } + + throw $exception; + } /** @var object{indexesCreated: list} $result */ $result = current($cursor->toArray()); diff --git a/tests/Collection/CollectionFunctionalTest.php b/tests/Collection/CollectionFunctionalTest.php index 95659381c..31d965a99 100644 --- a/tests/Collection/CollectionFunctionalTest.php +++ b/tests/Collection/CollectionFunctionalTest.php @@ -13,6 +13,7 @@ use MongoDB\Driver\ReadPreference; use MongoDB\Driver\WriteConcern; use MongoDB\Exception\InvalidArgumentException; +use MongoDB\Exception\SearchNotSupportedException; use MongoDB\Exception\UnsupportedException; use MongoDB\Operation\Count; use MongoDB\Tests\CommandObserver; @@ -807,6 +808,32 @@ public function testListSearchIndexesInheritTypeMap(): void $this->assertIsArray($indexes[0]); } + public function testListSearchIndexesNotSupportedException(): void + { + if (self::isAtlas()) { + self::markTestSkipped('Atlas Search is supported on Atlas'); + } + + $collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName()); + + $this->expectException(SearchNotSupportedException::class); + + $collection->listSearchIndexes(); + } + + public function testCreateSearchIndexNotSupportedException(): void + { + if (self::isAtlas()) { + self::markTestSkipped('Atlas Search is supported on Atlas'); + } + + $collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName()); + + $this->expectException(SearchNotSupportedException::class); + + $collection->createSearchIndex(['mappings' => ['dynamic' => false]], ['name' => 'test-search-index']); + } + /** * Create data fixtures. */ From f0aac733dfa9aa834397d120f09f000007a68492 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Tue, 18 Nov 2025 18:00:53 +0100 Subject: [PATCH 2/4] Use error message when error code is not specific to atlas search on old server versions --- phpunit.xml.dist | 1 + .../AtlasSearchNotSupportedException.php | 40 ++++++++++++ src/Exception/SearchNotSupportedException.php | 31 ---------- src/Operation/Aggregate.php | 6 +- src/Operation/CreateSearchIndexes.php | 6 +- tests/Collection/CollectionFunctionalTest.php | 27 -------- tests/ExamplesTest.php | 4 +- .../AtlasSearchNotSupportedExceptionTest.php | 62 +++++++++++++++++++ tests/FunctionalTestCase.php | 4 +- 9 files changed, 111 insertions(+), 70 deletions(-) create mode 100644 src/Exception/AtlasSearchNotSupportedException.php delete mode 100644 src/Exception/SearchNotSupportedException.php create mode 100644 tests/Exception/AtlasSearchNotSupportedExceptionTest.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index b3b46625b..be9b7cd48 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -16,6 +16,7 @@ + diff --git a/src/Exception/AtlasSearchNotSupportedException.php b/src/Exception/AtlasSearchNotSupportedException.php new file mode 100644 index 000000000..b65b1620b --- /dev/null +++ b/src/Exception/AtlasSearchNotSupportedException.php @@ -0,0 +1,40 @@ +getCode() === 31082 ? $e->getMessage() : 'Using Atlas Search Database Commands and the $listSearchIndexes aggregation stage requires additional configuration. Please connect to Atlas or an AtlasCLI local deployment to enable. For more information on how to connect, see https://dochub.mongodb.org/core/atlas-cli-deploy-local-reqs'; + + return new self($message, $e->getCode(), $e); + } + + /** @internal */ + public static function isAtlasSearchNotSupportedError(Throwable $e): bool + { + if (! $e instanceof ServerException) { + return false; + } + + return match ($e->getCode()) { + // MongoDB 8: Using Atlas Search Database Commands and the $listSearchIndexes aggregation stage requires additional configuration. + 31082 => true, + // MongoDB 7: $listSearchIndexes stage is only allowed on MongoDB Atlas + 6047401 => true, + // MongoDB 7-ent: Search index commands are only supported with Atlas. + 115 => true, + // MongoDB 4 to 6, 7-community + 59 => 'no such command: \'createSearchIndexes\'' === $e->getMessage(), + // MongoDB 4 to 6 + 40324 => 'Unrecognized pipeline stage name: \'$listSearchIndexes\'' === $e->getMessage(), + // Not an Atlas Search error + default => false, + }; + } +} diff --git a/src/Exception/SearchNotSupportedException.php b/src/Exception/SearchNotSupportedException.php deleted file mode 100644 index 2c1312b31..000000000 --- a/src/Exception/SearchNotSupportedException.php +++ /dev/null @@ -1,31 +0,0 @@ -getCode(), $e); - } - - public static function isSearchNotSupportedError(Throwable $e): bool - { - if (! $e instanceof ServerException) { - return false; - } - - return in_array($e->getCode(), [ - 59, // MongoDB 4 to 6, 7-community: no such command: 'createSearchIndexes' - 40324, // MongoDB 4 to 6: Unrecognized pipeline stage name: '$listSearchIndexes' - 115, // MongoDB 7-ent: Search index commands are only supported with Atlas. - 6047401, // MongoDB 7: $listSearchIndexes stage is only allowed on MongoDB Atlas - 31082, // MongoDB 8: Using Atlas Search Database Commands and the $listSearchIndexes aggregation stage requires additional configuration. - ], true); - } -} diff --git a/src/Operation/Aggregate.php b/src/Operation/Aggregate.php index 4d120f9c4..6827bb07f 100644 --- a/src/Operation/Aggregate.php +++ b/src/Operation/Aggregate.php @@ -27,8 +27,8 @@ use MongoDB\Driver\Server; use MongoDB\Driver\Session; use MongoDB\Driver\WriteConcern; +use MongoDB\Exception\AtlasSearchNotSupportedException; use MongoDB\Exception\InvalidArgumentException; -use MongoDB\Exception\SearchNotSupportedException; use MongoDB\Exception\UnexpectedValueException; use MongoDB\Exception\UnsupportedException; use MongoDB\Model\CodecCursor; @@ -238,8 +238,8 @@ public function execute(Server $server): CursorInterface try { $cursor = $this->executeCommand($server, $command); } catch (ServerException $exception) { - if (SearchNotSupportedException::isSearchNotSupportedError($exception)) { - throw SearchNotSupportedException::create($exception); + if (AtlasSearchNotSupportedException::isAtlasSearchNotSupportedError($exception)) { + throw AtlasSearchNotSupportedException::create($exception); } throw $exception; diff --git a/src/Operation/CreateSearchIndexes.php b/src/Operation/CreateSearchIndexes.php index 0b5b59573..2738b7291 100644 --- a/src/Operation/CreateSearchIndexes.php +++ b/src/Operation/CreateSearchIndexes.php @@ -21,8 +21,8 @@ use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException; use MongoDB\Driver\Exception\ServerException; use MongoDB\Driver\Server; +use MongoDB\Exception\AtlasSearchNotSupportedException; use MongoDB\Exception\InvalidArgumentException; -use MongoDB\Exception\SearchNotSupportedException; use MongoDB\Exception\UnsupportedException; use MongoDB\Model\SearchIndexInput; @@ -88,8 +88,8 @@ public function execute(Server $server): array try { $cursor = $server->executeCommand($this->databaseName, new Command($cmd)); } catch (ServerException $exception) { - if (SearchNotSupportedException::isSearchNotSupportedError($exception)) { - throw SearchNotSupportedException::create($exception); + if (AtlasSearchNotSupportedException::isAtlasSearchNotSupportedError($exception)) { + throw AtlasSearchNotSupportedException::create($exception); } throw $exception; diff --git a/tests/Collection/CollectionFunctionalTest.php b/tests/Collection/CollectionFunctionalTest.php index 31d965a99..95659381c 100644 --- a/tests/Collection/CollectionFunctionalTest.php +++ b/tests/Collection/CollectionFunctionalTest.php @@ -13,7 +13,6 @@ use MongoDB\Driver\ReadPreference; use MongoDB\Driver\WriteConcern; use MongoDB\Exception\InvalidArgumentException; -use MongoDB\Exception\SearchNotSupportedException; use MongoDB\Exception\UnsupportedException; use MongoDB\Operation\Count; use MongoDB\Tests\CommandObserver; @@ -808,32 +807,6 @@ public function testListSearchIndexesInheritTypeMap(): void $this->assertIsArray($indexes[0]); } - public function testListSearchIndexesNotSupportedException(): void - { - if (self::isAtlas()) { - self::markTestSkipped('Atlas Search is supported on Atlas'); - } - - $collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName()); - - $this->expectException(SearchNotSupportedException::class); - - $collection->listSearchIndexes(); - } - - public function testCreateSearchIndexNotSupportedException(): void - { - if (self::isAtlas()) { - self::markTestSkipped('Atlas Search is supported on Atlas'); - } - - $collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName()); - - $this->expectException(SearchNotSupportedException::class); - - $collection->createSearchIndex(['mappings' => ['dynamic' => false]], ['name' => 'test-search-index']); - } - /** * Create data fixtures. */ diff --git a/tests/ExamplesTest.php b/tests/ExamplesTest.php index 78ade169c..642798a97 100644 --- a/tests/ExamplesTest.php +++ b/tests/ExamplesTest.php @@ -8,7 +8,6 @@ use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses; use function bin2hex; -use function getenv; use function putenv; use function random_bytes; use function sprintf; @@ -230,8 +229,7 @@ public static function provideExamples(): Generator #[Group('atlas')] public function testAtlasSearch(): void { - $uri = getenv('MONGODB_URI') ?? ''; - if (! self::isAtlas($uri)) { + if (! self::isAtlas()) { $this->markTestSkipped('Atlas Search examples are only supported on MongoDB Atlas'); } diff --git a/tests/Exception/AtlasSearchNotSupportedExceptionTest.php b/tests/Exception/AtlasSearchNotSupportedExceptionTest.php new file mode 100644 index 000000000..5d8d99498 --- /dev/null +++ b/tests/Exception/AtlasSearchNotSupportedExceptionTest.php @@ -0,0 +1,62 @@ +manager, $this->getDatabaseName(), $this->getCollectionName()); + + $this->expectException(AtlasSearchNotSupportedException::class); + + $collection->listSearchIndexes(); + } + + public function testCreateSearchIndexNotSupportedException(): void + { + if (self::isAtlas()) { + self::markTestSkipped('Atlas Search is supported on Atlas'); + } + + $collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName()); + + $this->expectException(AtlasSearchNotSupportedException::class); + + $collection->createSearchIndex(['mappings' => ['dynamic' => false]], ['name' => 'test-search-index']); + } + + public function testOtherStageNotFound(): void + { + $collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName()); + + try { + $collection->aggregate([ + ['$searchStageNotExisting' => ['text' => ['query' => 'test', 'path' => 'field']]], + ]); + self::fail('Expected ServerException was not thrown'); + } catch (ServerException $exception) { + self::assertNotInstanceOf(AtlasSearchNotSupportedException::class, $exception, $exception); + } + } + + public function testOtherCommandNotFound(): void + { + try { + $this->manager->executeCommand($this->getDatabaseName(), new Command(['nonExistingCommand' => 1])); + self::fail('Expected ServerException was not thrown'); + } catch (ServerException $exception) { + self::assertFalse(AtlasSearchNotSupportedException::isAtlasSearchNotSupportedError($exception)); + } + } +} diff --git a/tests/FunctionalTestCase.php b/tests/FunctionalTestCase.php index 156d80149..ea71accec 100644 --- a/tests/FunctionalTestCase.php +++ b/tests/FunctionalTestCase.php @@ -50,8 +50,6 @@ abstract class FunctionalTestCase extends TestCase { - private const ATLAS_TLD = '/\.(mongodb\.net|mongodb-dev\.net)/'; - protected Manager $manager; private array $configuredFailPoints = []; @@ -520,7 +518,7 @@ protected function isEnterprise(): bool public static function isAtlas(?string $uri = null): bool { - return preg_match(self::ATLAS_TLD, $uri ?? static::getUri()); + return (bool) getenv('ATLAS_SUPPORTED'); } /** @see https://www.mongodb.com/docs/manual/core/queryable-encryption/reference/shared-library/ */ From ccd5be164d0294c0627d1e831c10be55ecc993e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Wed, 17 Dec 2025 16:02:14 +0100 Subject: [PATCH 3/4] Rework search index support detection by running a $search pipeline --- phpunit.xml.dist | 1 - tests/ExamplesTest.php | 2 +- ...hp => SearchNotSupportedExceptionTest.php} | 29 ++++++++++--------- tests/FunctionalTestCase.php | 19 ++++++------ tests/UnifiedSpecTests/UnifiedSpecTest.php | 4 +-- tests/UnifiedSpecTests/UnifiedTestRunner.php | 7 ++++- 6 files changed, 33 insertions(+), 29 deletions(-) rename tests/Exception/{AtlasSearchNotSupportedExceptionTest.php => SearchNotSupportedExceptionTest.php} (69%) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index be9b7cd48..b3b46625b 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -16,7 +16,6 @@ - diff --git a/tests/ExamplesTest.php b/tests/ExamplesTest.php index 642798a97..0b0a5b883 100644 --- a/tests/ExamplesTest.php +++ b/tests/ExamplesTest.php @@ -229,7 +229,7 @@ public static function provideExamples(): Generator #[Group('atlas')] public function testAtlasSearch(): void { - if (! self::isAtlas()) { + if (! $this->isAtlas()) { $this->markTestSkipped('Atlas Search examples are only supported on MongoDB Atlas'); } diff --git a/tests/Exception/AtlasSearchNotSupportedExceptionTest.php b/tests/Exception/SearchNotSupportedExceptionTest.php similarity index 69% rename from tests/Exception/AtlasSearchNotSupportedExceptionTest.php rename to tests/Exception/SearchNotSupportedExceptionTest.php index 5d8d99498..7d8731dc9 100644 --- a/tests/Exception/AtlasSearchNotSupportedExceptionTest.php +++ b/tests/Exception/SearchNotSupportedExceptionTest.php @@ -7,33 +7,34 @@ use MongoDB\Driver\Exception\ServerException; use MongoDB\Exception\AtlasSearchNotSupportedException; use MongoDB\Tests\Collection\FunctionalTestCase; +use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; class AtlasSearchNotSupportedExceptionTest extends FunctionalTestCase { + #[DoesNotPerformAssertions] public function testListSearchIndexesNotSupportedException(): void { - if (self::isAtlas()) { - self::markTestSkipped('Atlas Search is supported on Atlas'); - } - $collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName()); - $this->expectException(AtlasSearchNotSupportedException::class); - - $collection->listSearchIndexes(); + try { + $collection->listSearchIndexes(); + } catch (AtlasSearchNotSupportedException) { + // If an exception is thrown because Atlas Search is not supported, + // then the test is successful because it has the correct exception class. + } } + #[DoesNotPerformAssertions] public function testCreateSearchIndexNotSupportedException(): void { - if (self::isAtlas()) { - self::markTestSkipped('Atlas Search is supported on Atlas'); - } - $collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName()); - $this->expectException(AtlasSearchNotSupportedException::class); - - $collection->createSearchIndex(['mappings' => ['dynamic' => false]], ['name' => 'test-search-index']); + try { + $collection->createSearchIndex(['mappings' => ['dynamic' => false]], ['name' => 'test-search-index']); + } catch (AtlasSearchNotSupportedException) { + // If an exception is thrown because Atlas Search is not supported, + // then the test is successful because it has the correct exception class. + } } public function testOtherStageNotFound(): void diff --git a/tests/FunctionalTestCase.php b/tests/FunctionalTestCase.php index ea71accec..69a1d39c3 100644 --- a/tests/FunctionalTestCase.php +++ b/tests/FunctionalTestCase.php @@ -434,11 +434,17 @@ protected function skipIfServerVersion(string $operator, string $version, ?strin protected function skipIfAtlasSearchIndexIsNotSupported(): void { - if (! self::isAtlas()) { - self::markTestSkipped('Search Indexes are only supported on MongoDB Atlas 7.0+'); + try { + $this->manager->executeReadCommand($this->getDatabaseName(), new Command([ + 'aggregate' => __METHOD__, + 'pipeline' => [ + ['$search' => ['text' => ['query' => 'test', 'path' => 'field']]], + ], + 'cursor' => new stdClass(), + ])); + } catch (CommandException $exception) { + self::markTestSkipped($exception->getMessage()); } - - $this->skipIfServerVersion('<', '7.0', 'Search Indexes are only supported on MongoDB Atlas 7.0+'); } protected function skipIfChangeStreamIsNotSupported(): void @@ -516,11 +522,6 @@ protected function isEnterprise(): bool throw new UnexpectedValueException('Could not determine server modules'); } - public static function isAtlas(?string $uri = null): bool - { - return (bool) getenv('ATLAS_SUPPORTED'); - } - /** @see https://www.mongodb.com/docs/manual/core/queryable-encryption/reference/shared-library/ */ public static function isCryptSharedLibAvailable(): bool { diff --git a/tests/UnifiedSpecTests/UnifiedSpecTest.php b/tests/UnifiedSpecTests/UnifiedSpecTest.php index 70d90530e..a581af3c4 100644 --- a/tests/UnifiedSpecTests/UnifiedSpecTest.php +++ b/tests/UnifiedSpecTests/UnifiedSpecTest.php @@ -366,9 +366,7 @@ public static function provideFailingTests(): Generator #[DataProvider('provideIndexManagementTests')] public function testIndexManagement(UnifiedTestCase $test): void { - if (self::isAtlas()) { - self::markTestSkipped('Search Indexes tests must run on a non-Atlas cluster'); - } + $this->skipIfAtlasSearchIndexIsNotSupported(); if (! self::isEnterprise()) { self::markTestSkipped('Specific Atlas error messages are only available on Enterprise server'); diff --git a/tests/UnifiedSpecTests/UnifiedTestRunner.php b/tests/UnifiedSpecTests/UnifiedTestRunner.php index 3069636b7..9e6fde6dc 100644 --- a/tests/UnifiedSpecTests/UnifiedTestRunner.php +++ b/tests/UnifiedSpecTests/UnifiedTestRunner.php @@ -89,7 +89,7 @@ public function __construct(private string $internalClientUri) * * Atlas Data Lake also does not support killAllSessions. */ - if (FunctionalTestCase::isAtlas($internalClientUri) || $this->isAtlasDataLake()) { + if ($this->isAtlas($internalClientUri) || $this->isAtlasDataLake()) { $this->allowKillAllSessions = false; } @@ -307,6 +307,11 @@ private function getTopology(): string }; } + private function isAtlas(string $internalClientUri): bool + { + return preg_match('/\.(mongodb\.net|mongodb-dev\.net)/', $internalClientUri); + } + private function isAtlasDataLake(): bool { $database = $this->internalClient->selectDatabase('admin'); From cc97844f868ea4ef19d4208d035c33a0c1979958 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Wed, 17 Dec 2025 16:20:52 +0100 Subject: [PATCH 4/4] Remove Atlas from the exception name, as this is no longer specific to Atlas, thanks to Community Search --- ...Exception.php => SearchNotSupportedException.php} | 4 ++-- src/Operation/Aggregate.php | 6 +++--- src/Operation/CreateSearchIndexes.php | 6 +++--- tests/Collection/CollectionFunctionalTest.php | 2 +- tests/ExamplesTest.php | 4 +--- tests/Exception/SearchNotSupportedExceptionTest.php | 12 ++++++------ tests/FunctionalTestCase.php | 2 +- tests/SpecTests/SearchIndexSpecTest.php | 2 +- tests/UnifiedSpecTests/UnifiedSpecTest.php | 2 +- 9 files changed, 19 insertions(+), 21 deletions(-) rename src/Exception/{AtlasSearchNotSupportedException.php => SearchNotSupportedException.php} (91%) diff --git a/src/Exception/AtlasSearchNotSupportedException.php b/src/Exception/SearchNotSupportedException.php similarity index 91% rename from src/Exception/AtlasSearchNotSupportedException.php rename to src/Exception/SearchNotSupportedException.php index b65b1620b..3cddfadc9 100644 --- a/src/Exception/AtlasSearchNotSupportedException.php +++ b/src/Exception/SearchNotSupportedException.php @@ -5,7 +5,7 @@ use MongoDB\Driver\Exception\ServerException; use Throwable; -final class AtlasSearchNotSupportedException extends ServerException +final class SearchNotSupportedException extends ServerException { /** @internal */ public static function create(ServerException $e): self @@ -16,7 +16,7 @@ public static function create(ServerException $e): self } /** @internal */ - public static function isAtlasSearchNotSupportedError(Throwable $e): bool + public static function isSearchNotSupportedError(Throwable $e): bool { if (! $e instanceof ServerException) { return false; diff --git a/src/Operation/Aggregate.php b/src/Operation/Aggregate.php index 6827bb07f..3a3084671 100644 --- a/src/Operation/Aggregate.php +++ b/src/Operation/Aggregate.php @@ -27,7 +27,7 @@ use MongoDB\Driver\Server; use MongoDB\Driver\Session; use MongoDB\Driver\WriteConcern; -use MongoDB\Exception\AtlasSearchNotSupportedException; +use MongoDB\Exception\SearchNotSupportedException; use MongoDB\Exception\InvalidArgumentException; use MongoDB\Exception\UnexpectedValueException; use MongoDB\Exception\UnsupportedException; @@ -238,8 +238,8 @@ public function execute(Server $server): CursorInterface try { $cursor = $this->executeCommand($server, $command); } catch (ServerException $exception) { - if (AtlasSearchNotSupportedException::isAtlasSearchNotSupportedError($exception)) { - throw AtlasSearchNotSupportedException::create($exception); + if (SearchNotSupportedException::isSearchNotSupportedError($exception)) { + throw SearchNotSupportedException::create($exception); } throw $exception; diff --git a/src/Operation/CreateSearchIndexes.php b/src/Operation/CreateSearchIndexes.php index 2738b7291..2c8bae0a7 100644 --- a/src/Operation/CreateSearchIndexes.php +++ b/src/Operation/CreateSearchIndexes.php @@ -21,7 +21,7 @@ use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException; use MongoDB\Driver\Exception\ServerException; use MongoDB\Driver\Server; -use MongoDB\Exception\AtlasSearchNotSupportedException; +use MongoDB\Exception\SearchNotSupportedException; use MongoDB\Exception\InvalidArgumentException; use MongoDB\Exception\UnsupportedException; use MongoDB\Model\SearchIndexInput; @@ -88,8 +88,8 @@ public function execute(Server $server): array try { $cursor = $server->executeCommand($this->databaseName, new Command($cmd)); } catch (ServerException $exception) { - if (AtlasSearchNotSupportedException::isAtlasSearchNotSupportedError($exception)) { - throw AtlasSearchNotSupportedException::create($exception); + if (SearchNotSupportedException::isSearchNotSupportedError($exception)) { + throw SearchNotSupportedException::create($exception); } throw $exception; diff --git a/tests/Collection/CollectionFunctionalTest.php b/tests/Collection/CollectionFunctionalTest.php index 95659381c..e5eaa775b 100644 --- a/tests/Collection/CollectionFunctionalTest.php +++ b/tests/Collection/CollectionFunctionalTest.php @@ -783,7 +783,7 @@ public function testMethodInTransactionWithReadConcernOption($method): void public function testListSearchIndexesInheritTypeMap(): void { - $this->skipIfAtlasSearchIndexIsNotSupported(); + $this->skipIfSearchIndexIsNotSupported(); $collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName(), ['typeMap' => ['root' => 'array']]); diff --git a/tests/ExamplesTest.php b/tests/ExamplesTest.php index 0b0a5b883..679e85a25 100644 --- a/tests/ExamplesTest.php +++ b/tests/ExamplesTest.php @@ -229,9 +229,7 @@ public static function provideExamples(): Generator #[Group('atlas')] public function testAtlasSearch(): void { - if (! $this->isAtlas()) { - $this->markTestSkipped('Atlas Search examples are only supported on MongoDB Atlas'); - } + $this->skipIfSearchIndexIsNotSupported(); $this->skipIfServerVersion('<', '7.0', 'Atlas Search examples require MongoDB 7.0 or later'); diff --git a/tests/Exception/SearchNotSupportedExceptionTest.php b/tests/Exception/SearchNotSupportedExceptionTest.php index 7d8731dc9..28d0763ed 100644 --- a/tests/Exception/SearchNotSupportedExceptionTest.php +++ b/tests/Exception/SearchNotSupportedExceptionTest.php @@ -5,11 +5,11 @@ use MongoDB\Collection; use MongoDB\Driver\Command; use MongoDB\Driver\Exception\ServerException; -use MongoDB\Exception\AtlasSearchNotSupportedException; +use MongoDB\Exception\SearchNotSupportedException; use MongoDB\Tests\Collection\FunctionalTestCase; use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; -class AtlasSearchNotSupportedExceptionTest extends FunctionalTestCase +class SearchNotSupportedExceptionTest extends FunctionalTestCase { #[DoesNotPerformAssertions] public function testListSearchIndexesNotSupportedException(): void @@ -18,7 +18,7 @@ public function testListSearchIndexesNotSupportedException(): void try { $collection->listSearchIndexes(); - } catch (AtlasSearchNotSupportedException) { + } catch (SearchNotSupportedException) { // If an exception is thrown because Atlas Search is not supported, // then the test is successful because it has the correct exception class. } @@ -31,7 +31,7 @@ public function testCreateSearchIndexNotSupportedException(): void try { $collection->createSearchIndex(['mappings' => ['dynamic' => false]], ['name' => 'test-search-index']); - } catch (AtlasSearchNotSupportedException) { + } catch (SearchNotSupportedException) { // If an exception is thrown because Atlas Search is not supported, // then the test is successful because it has the correct exception class. } @@ -47,7 +47,7 @@ public function testOtherStageNotFound(): void ]); self::fail('Expected ServerException was not thrown'); } catch (ServerException $exception) { - self::assertNotInstanceOf(AtlasSearchNotSupportedException::class, $exception, $exception); + self::assertNotInstanceOf(SearchNotSupportedException::class, $exception, $exception); } } @@ -57,7 +57,7 @@ public function testOtherCommandNotFound(): void $this->manager->executeCommand($this->getDatabaseName(), new Command(['nonExistingCommand' => 1])); self::fail('Expected ServerException was not thrown'); } catch (ServerException $exception) { - self::assertFalse(AtlasSearchNotSupportedException::isAtlasSearchNotSupportedError($exception)); + self::assertFalse(SearchNotSupportedException::isSearchNotSupportedError($exception)); } } } diff --git a/tests/FunctionalTestCase.php b/tests/FunctionalTestCase.php index 69a1d39c3..1de2bebb4 100644 --- a/tests/FunctionalTestCase.php +++ b/tests/FunctionalTestCase.php @@ -432,7 +432,7 @@ protected function skipIfServerVersion(string $operator, string $version, ?strin } } - protected function skipIfAtlasSearchIndexIsNotSupported(): void + protected function skipIfSearchIndexIsNotSupported(): void { try { $this->manager->executeReadCommand($this->getDatabaseName(), new Command([ diff --git a/tests/SpecTests/SearchIndexSpecTest.php b/tests/SpecTests/SearchIndexSpecTest.php index 6f8d3b144..6d825a129 100644 --- a/tests/SpecTests/SearchIndexSpecTest.php +++ b/tests/SpecTests/SearchIndexSpecTest.php @@ -31,7 +31,7 @@ public function setUp(): void { parent::setUp(); - $this->skipIfAtlasSearchIndexIsNotSupported(); + $this->skipIfSearchIndexIsNotSupported(); } /** diff --git a/tests/UnifiedSpecTests/UnifiedSpecTest.php b/tests/UnifiedSpecTests/UnifiedSpecTest.php index a581af3c4..3d40d4e87 100644 --- a/tests/UnifiedSpecTests/UnifiedSpecTest.php +++ b/tests/UnifiedSpecTests/UnifiedSpecTest.php @@ -366,7 +366,7 @@ public static function provideFailingTests(): Generator #[DataProvider('provideIndexManagementTests')] public function testIndexManagement(UnifiedTestCase $test): void { - $this->skipIfAtlasSearchIndexIsNotSupported(); + $this->skipIfSearchIndexIsNotSupported(); if (! self::isEnterprise()) { self::markTestSkipped('Specific Atlas error messages are only available on Enterprise server');