3
3
namespace MongoDB \Tests ;
4
4
5
5
use Generator ;
6
- use MongoDB \Client ;
7
6
7
+ use function bin2hex ;
8
8
use function getenv ;
9
+ use function putenv ;
10
+ use function random_bytes ;
11
+ use function sprintf ;
9
12
10
13
/** @runTestsInSeparateProcesses */
11
14
final class ExamplesTest extends FunctionalTestCase
@@ -183,7 +186,7 @@ public static function provideExamples(): Generator
183
186
}
184
187
185
188
/**
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+
187
190
* Tips for insiders: if using a cloud-dev server, append ".mongodb.net" to the MONGODB_URI.
188
191
*
189
192
* @group atlas
@@ -197,22 +200,37 @@ public function testAtlasSearch(): void
197
200
198
201
$ this ->skipIfServerVersion ('< ' , '7.0 ' , 'Atlas Search examples require MongoDB 7.0 or later ' );
199
202
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'
206
219
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
209
227
210
- require __DIR__ . '/../examples/atlas-search.php ' ;
228
+ Enjoy MongoDB Atlas Search!
229
+
230
+
231
+ OUTPUT;
211
232
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 );
216
234
}
217
235
218
236
public function testChangeStream (): void
0 commit comments