Skip to content

Commit 14c3251

Browse files
committed
Use error message when error code is not specific to atlas search on old server versions
1 parent cadfba0 commit 14c3251

File tree

9 files changed

+114
-69
lines changed

9 files changed

+114
-69
lines changed

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<ini name="zend.assertions" value="1"/>
1717
<env name="MONGODB_URI" value="mongodb://127.0.0.1:27017/?serverSelectionTimeoutMS=100"/>
1818
<env name="MONGODB_DATABASE" value="phplib_test"/>
19+
<env name="ATLAS_SUPPORTED" value="0" />
1920
</php>
2021

2122
<testsuites>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace MongoDB\Exception;
4+
5+
use MongoDB\Driver\Exception\ServerException;
6+
use Throwable;
7+
8+
use function in_array;
9+
10+
final class AtlasSearchNotSupportedException extends ServerException
11+
{
12+
/** @internal */
13+
public static function create(ServerException $e): self
14+
{
15+
$message = $e->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';
16+
17+
return new self($message, $e->getCode(), $e);
18+
}
19+
20+
/** @internal */
21+
public static function isAtlasSearchNotSupportedError(Throwable $e): bool
22+
{
23+
if (! $e instanceof ServerException) {
24+
return false;
25+
}
26+
27+
return match($e->getCode()) {
28+
// MongoDB 8: Using Atlas Search Database Commands and the $listSearchIndexes aggregation stage requires additional configuration.
29+
31082 => true,
30+
// MongoDB 7: $listSearchIndexes stage is only allowed on MongoDB Atlas
31+
6047401 => true,
32+
// MongoDB 7-ent: Search index commands are only supported with Atlas.
33+
115 => true,
34+
// MongoDB 4 to 6, 7-community
35+
59 => str_contains('no such command: \'createSearchIndexes\'', $e->getMessage()),
36+
// MongoDB 4 to 6
37+
40324 => str_contains('Unrecognized pipeline stage name: \'$listSearchIndexes\'', $e->getMessage()),
38+
// Not an Atlas Search error
39+
default => false,
40+
};
41+
}
42+
}

src/Exception/SearchNotSupportedException.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/Operation/Aggregate.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
use MongoDB\Driver\Session;
2929
use MongoDB\Driver\WriteConcern;
3030
use MongoDB\Exception\InvalidArgumentException;
31-
use MongoDB\Exception\SearchNotSupportedException;
31+
use MongoDB\Exception\AtlasSearchNotSupportedException;
3232
use MongoDB\Exception\UnexpectedValueException;
3333
use MongoDB\Exception\UnsupportedException;
3434
use MongoDB\Model\CodecCursor;
@@ -238,8 +238,8 @@ public function execute(Server $server): CursorInterface
238238
try {
239239
$cursor = $this->executeCommand($server, $command);
240240
} catch (ServerException $exception) {
241-
if (SearchNotSupportedException::isSearchNotSupportedError($exception)) {
242-
throw SearchNotSupportedException::create($exception);
241+
if (AtlasSearchNotSupportedException::isAtlasSearchNotSupportedError($exception)) {
242+
throw AtlasSearchNotSupportedException::create($exception);
243243
}
244244

245245
throw $exception;

src/Operation/CreateSearchIndexes.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use MongoDB\Driver\Exception\ServerException;
2323
use MongoDB\Driver\Server;
2424
use MongoDB\Exception\InvalidArgumentException;
25-
use MongoDB\Exception\SearchNotSupportedException;
25+
use MongoDB\Exception\AtlasSearchNotSupportedException;
2626
use MongoDB\Exception\UnsupportedException;
2727
use MongoDB\Model\SearchIndexInput;
2828

@@ -88,8 +88,8 @@ public function execute(Server $server): array
8888
try {
8989
$cursor = $server->executeCommand($this->databaseName, new Command($cmd));
9090
} catch (ServerException $exception) {
91-
if (SearchNotSupportedException::isSearchNotSupportedError($exception)) {
92-
throw SearchNotSupportedException::create($exception);
91+
if (AtlasSearchNotSupportedException::isAtlasSearchNotSupportedError($exception)) {
92+
throw AtlasSearchNotSupportedException::create($exception);
9393
}
9494

9595
throw $exception;

tests/Collection/CollectionFunctionalTest.php

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use MongoDB\Driver\ReadPreference;
1414
use MongoDB\Driver\WriteConcern;
1515
use MongoDB\Exception\InvalidArgumentException;
16-
use MongoDB\Exception\SearchNotSupportedException;
16+
use MongoDB\Exception\AtlasSearchNotSupportedException;
1717
use MongoDB\Exception\UnsupportedException;
1818
use MongoDB\Operation\Count;
1919
use MongoDB\Tests\CommandObserver;
@@ -808,32 +808,6 @@ public function testListSearchIndexesInheritTypeMap(): void
808808
$this->assertIsArray($indexes[0]);
809809
}
810810

811-
public function testListSearchIndexesNotSupportedException(): void
812-
{
813-
if (self::isAtlas()) {
814-
self::markTestSkipped('Atlas Search is supported on Atlas');
815-
}
816-
817-
$collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
818-
819-
$this->expectException(SearchNotSupportedException::class);
820-
821-
$collection->listSearchIndexes();
822-
}
823-
824-
public function testCreateSearchIndexNotSupportedException(): void
825-
{
826-
if (self::isAtlas()) {
827-
self::markTestSkipped('Atlas Search is supported on Atlas');
828-
}
829-
830-
$collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
831-
832-
$this->expectException(SearchNotSupportedException::class);
833-
834-
$collection->createSearchIndex(['mappings' => ['dynamic' => false]], ['name' => 'test-search-index']);
835-
}
836-
837811
/**
838812
* Create data fixtures.
839813
*/

tests/ExamplesTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,7 @@ public static function provideExamples(): Generator
230230
#[Group('atlas')]
231231
public function testAtlasSearch(): void
232232
{
233-
$uri = getenv('MONGODB_URI') ?? '';
234-
if (! self::isAtlas($uri)) {
233+
if (! self::isAtlas()) {
235234
$this->markTestSkipped('Atlas Search examples are only supported on MongoDB Atlas');
236235
}
237236

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Exception;
4+
5+
use MongoDB\Collection;
6+
use MongoDB\Driver\Command;
7+
use MongoDB\Driver\Exception\ServerException;
8+
use MongoDB\Exception\AtlasSearchNotSupportedException;
9+
use MongoDB\Tests\Collection\FunctionalTestCase;
10+
11+
class AtlasSearchNotSupportedExceptionTest extends FunctionalTestCase
12+
{
13+
public function testListSearchIndexesNotSupportedException(): void
14+
{
15+
if (self::isAtlas()) {
16+
self::markTestSkipped('Atlas Search is supported on Atlas');
17+
}
18+
19+
$collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
20+
21+
$this->expectException(AtlasSearchNotSupportedException::class);
22+
23+
$collection->listSearchIndexes();
24+
}
25+
26+
public function testCreateSearchIndexNotSupportedException(): void
27+
{
28+
if (self::isAtlas()) {
29+
self::markTestSkipped('Atlas Search is supported on Atlas');
30+
}
31+
32+
$collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
33+
34+
$this->expectException(AtlasSearchNotSupportedException::class);
35+
36+
$collection->createSearchIndex(['mappings' => ['dynamic' => false]], ['name' => 'test-search-index']);
37+
}
38+
39+
public function testOtherStageNotFound(): void
40+
{
41+
$collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
42+
43+
try {
44+
$collection->aggregate([
45+
['$searchStageNotExisting' => ['text' => ['query' => 'test', 'path' => 'field']]],
46+
]);
47+
self::fail('Expected ServerException was not thrown');
48+
} catch (ServerException $exception) {
49+
self::assertNotInstanceOf(AtlasSearchNotSupportedException::class, $exception, $exception);
50+
}
51+
}
52+
53+
public function testOtherCommandNotFound(): void
54+
{
55+
try {
56+
$this->manager->executeCommand($this->getDatabaseName(), new Command(['nonExistingCommand' => 1]));
57+
self::fail('Expected ServerException was not thrown');
58+
} catch (ServerException $exception) {
59+
self::assertFalse(AtlasSearchNotSupportedException::isAtlasSearchNotSupportedError($exception));
60+
}
61+
}
62+
}

tests/FunctionalTestCase.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@
5050

5151
abstract class FunctionalTestCase extends TestCase
5252
{
53-
private const ATLAS_TLD = '/\.(mongodb\.net|mongodb-dev\.net)/';
54-
5553
protected Manager $manager;
5654

5755
private array $configuredFailPoints = [];
@@ -520,7 +518,7 @@ protected function isEnterprise(): bool
520518

521519
public static function isAtlas(?string $uri = null): bool
522520
{
523-
return preg_match(self::ATLAS_TLD, $uri ?? static::getUri());
521+
return (bool) getenv('ATLAS_SUPPORTED');
524522
}
525523

526524
/** @see https://www.mongodb.com/docs/manual/core/queryable-encryption/reference/shared-library/ */

0 commit comments

Comments
 (0)