Skip to content

Commit ccd5be1

Browse files
committed
Rework search index support detection by running a $search pipeline
1 parent f0aac73 commit ccd5be1

File tree

6 files changed

+33
-29
lines changed

6 files changed

+33
-29
lines changed

phpunit.xml.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
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" />
2019
</php>
2120

2221
<testsuites>

tests/ExamplesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public static function provideExamples(): Generator
229229
#[Group('atlas')]
230230
public function testAtlasSearch(): void
231231
{
232-
if (! self::isAtlas()) {
232+
if (! $this->isAtlas()) {
233233
$this->markTestSkipped('Atlas Search examples are only supported on MongoDB Atlas');
234234
}
235235

tests/Exception/AtlasSearchNotSupportedExceptionTest.php renamed to tests/Exception/SearchNotSupportedExceptionTest.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,34 @@
77
use MongoDB\Driver\Exception\ServerException;
88
use MongoDB\Exception\AtlasSearchNotSupportedException;
99
use MongoDB\Tests\Collection\FunctionalTestCase;
10+
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
1011

1112
class AtlasSearchNotSupportedExceptionTest extends FunctionalTestCase
1213
{
14+
#[DoesNotPerformAssertions]
1315
public function testListSearchIndexesNotSupportedException(): void
1416
{
15-
if (self::isAtlas()) {
16-
self::markTestSkipped('Atlas Search is supported on Atlas');
17-
}
18-
1917
$collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
2018

21-
$this->expectException(AtlasSearchNotSupportedException::class);
22-
23-
$collection->listSearchIndexes();
19+
try {
20+
$collection->listSearchIndexes();
21+
} catch (AtlasSearchNotSupportedException) {
22+
// If an exception is thrown because Atlas Search is not supported,
23+
// then the test is successful because it has the correct exception class.
24+
}
2425
}
2526

27+
#[DoesNotPerformAssertions]
2628
public function testCreateSearchIndexNotSupportedException(): void
2729
{
28-
if (self::isAtlas()) {
29-
self::markTestSkipped('Atlas Search is supported on Atlas');
30-
}
31-
3230
$collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
3331

34-
$this->expectException(AtlasSearchNotSupportedException::class);
35-
36-
$collection->createSearchIndex(['mappings' => ['dynamic' => false]], ['name' => 'test-search-index']);
32+
try {
33+
$collection->createSearchIndex(['mappings' => ['dynamic' => false]], ['name' => 'test-search-index']);
34+
} catch (AtlasSearchNotSupportedException) {
35+
// If an exception is thrown because Atlas Search is not supported,
36+
// then the test is successful because it has the correct exception class.
37+
}
3738
}
3839

3940
public function testOtherStageNotFound(): void

tests/FunctionalTestCase.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -434,11 +434,17 @@ protected function skipIfServerVersion(string $operator, string $version, ?strin
434434

435435
protected function skipIfAtlasSearchIndexIsNotSupported(): void
436436
{
437-
if (! self::isAtlas()) {
438-
self::markTestSkipped('Search Indexes are only supported on MongoDB Atlas 7.0+');
437+
try {
438+
$this->manager->executeReadCommand($this->getDatabaseName(), new Command([
439+
'aggregate' => __METHOD__,
440+
'pipeline' => [
441+
['$search' => ['text' => ['query' => 'test', 'path' => 'field']]],
442+
],
443+
'cursor' => new stdClass(),
444+
]));
445+
} catch (CommandException $exception) {
446+
self::markTestSkipped($exception->getMessage());
439447
}
440-
441-
$this->skipIfServerVersion('<', '7.0', 'Search Indexes are only supported on MongoDB Atlas 7.0+');
442448
}
443449

444450
protected function skipIfChangeStreamIsNotSupported(): void
@@ -516,11 +522,6 @@ protected function isEnterprise(): bool
516522
throw new UnexpectedValueException('Could not determine server modules');
517523
}
518524

519-
public static function isAtlas(?string $uri = null): bool
520-
{
521-
return (bool) getenv('ATLAS_SUPPORTED');
522-
}
523-
524525
/** @see https://www.mongodb.com/docs/manual/core/queryable-encryption/reference/shared-library/ */
525526
public static function isCryptSharedLibAvailable(): bool
526527
{

tests/UnifiedSpecTests/UnifiedSpecTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,7 @@ public static function provideFailingTests(): Generator
366366
#[DataProvider('provideIndexManagementTests')]
367367
public function testIndexManagement(UnifiedTestCase $test): void
368368
{
369-
if (self::isAtlas()) {
370-
self::markTestSkipped('Search Indexes tests must run on a non-Atlas cluster');
371-
}
369+
$this->skipIfAtlasSearchIndexIsNotSupported();
372370

373371
if (! self::isEnterprise()) {
374372
self::markTestSkipped('Specific Atlas error messages are only available on Enterprise server');

tests/UnifiedSpecTests/UnifiedTestRunner.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function __construct(private string $internalClientUri)
8989
*
9090
* Atlas Data Lake also does not support killAllSessions.
9191
*/
92-
if (FunctionalTestCase::isAtlas($internalClientUri) || $this->isAtlasDataLake()) {
92+
if ($this->isAtlas($internalClientUri) || $this->isAtlasDataLake()) {
9393
$this->allowKillAllSessions = false;
9494
}
9595

@@ -307,6 +307,11 @@ private function getTopology(): string
307307
};
308308
}
309309

310+
private function isAtlas(string $internalClientUri): bool
311+
{
312+
return preg_match('/\.(mongodb\.net|mongodb-dev\.net)/', $internalClientUri);
313+
}
314+
310315
private function isAtlasDataLake(): bool
311316
{
312317
$database = $this->internalClient->selectDatabase('admin');

0 commit comments

Comments
 (0)