Skip to content

Commit ccb799b

Browse files
committed
Add fixtures to run atlas-search example in CI
1 parent 033fdfb commit ccb799b

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

examples/atlas-search.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,16 @@
3535
// They usually take less than 5 minutes to complete.
3636
define('WAIT_TIMEOUT_SEC', 300);
3737

38+
// The sample dataset is loaded into the "sample_airbnb.listingsAndReviews" collection.
39+
$databaseName = getenv('MONGODB_DATABASE') ?: 'sample_airbnb';
40+
$collectionName = getenv('MONGODB_COLLECTION') ?: 'listingsAndReviews';
41+
3842
$client = new Client($uri);
39-
$collection = $client->selectCollection('sample_airbnb', 'listingsAndReviews');
43+
$collection = $client->selectCollection($databaseName, $collectionName);
4044

4145
$count = $collection->estimatedDocumentCount();
4246
if ($count === 0) {
43-
echo 'This example requires the sample_airbnb database with the listingsAndReviews collection.', "\n";
47+
echo 'This example requires the "', $databaseName, '" database with the "', $collectionName, '" collection.', "\n";
4448
echo 'Load the sample dataset in your MongoDB Atlas cluster before running this example:', "\n";
4549
echo ' https://www.mongodb.com/docs/atlas/sample-data/', "\n";
4650
exit(1);

tests/ExamplesTest.php

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace MongoDB\Tests;
44

55
use Generator;
6-
use MongoDB\Client;
76

7+
use function bin2hex;
88
use function getenv;
9+
use function putenv;
10+
use function random_bytes;
11+
use function sprintf;
912

1013
/** @runTestsInSeparateProcesses */
1114
final class ExamplesTest extends FunctionalTestCase
@@ -183,7 +186,7 @@ public static function provideExamples(): Generator
183186
}
184187

185188
/**
186-
* MongoDB Atlas Search example requires a MongoDB Atlas M10+ cluster with MongoDB 7.0+ and sample data loaded.
189+
* MongoDB Atlas Search example requires a MongoDB Atlas M10+ cluster with MongoDB 7.0+
187190
* Tips for insiders: if using a cloud-dev server, append ".mongodb.net" to the MONGODB_URI.
188191
*
189192
* @group atlas
@@ -197,22 +200,37 @@ public function testAtlasSearch(): void
197200

198201
$this->skipIfServerVersion('<', '7.0', 'Atlas Search examples require MongoDB 7.0 or later');
199202

200-
$client = new Client($uri);
201-
$collection = $client->selectCollection('sample_airbnb', 'listingsAndReviews');
202-
$count = $collection->estimatedDocumentCount();
203-
if ($count === 0) {
204-
$this->markTestSkipped('Atlas Search examples require the sample_airbnb database with the listingsAndReviews collection');
205-
}
203+
// Generate random collection name to avoid conflicts with consecutive runs as the index creation is asynchronous
204+
$collectionName = sprintf('%s.%s', $this->getCollectionName(), bin2hex(random_bytes(5)));
205+
$databaseName = $this->getDatabaseName();
206+
$collection = $this->createCollection($databaseName, $collectionName);
207+
$collection->insertMany([
208+
['name' => 'Ribeira Charming Duplex'],
209+
['name' => 'Ocean View Bondi Beach'],
210+
['name' => 'Luxury ocean view Beach Villa 622'],
211+
['name' => 'Ocean & Beach View Condo WBR H204'],
212+
['name' => 'Bondi Beach Spacious Studio With Ocean View'],
213+
['name' => 'New York City - Upper West Side Apt'],
214+
]);
215+
putenv(sprintf('MONGODB_DATABASE=%s', $databaseName));
216+
putenv(sprintf('MONGODB_COLLECTION=%s', $collectionName));
217+
218+
$expectedOutput = <<<'OUTPUT'
206219
207-
// Clean variables to avoid conflict with example
208-
unset($uri, $client, $collection, $count);
220+
Creating the index.
221+
%s
222+
Performing a text search...
223+
- Ocean View Bondi Beach
224+
- Luxury ocean view Beach Villa 622
225+
- Ocean & Beach View Condo WBR H204
226+
- Bondi Beach Spacious Studio With Ocean View
209227
210-
require __DIR__ . '/../examples/atlas-search.php';
228+
Enjoy MongoDB Atlas Search!
229+
230+
231+
OUTPUT;
211232

212-
$output = $this->getActualOutputForAssertion();
213-
$this->assertStringContainsString("\nCreating the index.\n...", $output);
214-
$this->assertStringContainsString("\nPerforming a text search...\n - ", $output);
215-
$this->assertStringContainsString("\nEnjoy MongoDB Atlas Search!\n", $output);
233+
$this->assertExampleOutput(__DIR__ . '/../examples/atlas-search.php', $expectedOutput);
216234
}
217235

218236
public function testChangeStream(): void

0 commit comments

Comments
 (0)