1212use MongoDB \Laravel \Schema \Builder ;
1313use MongoDB \Laravel \Tests \Models \Book ;
1414use PHPUnit \Framework \Attributes \Group ;
15+ use ReflectionClass ;
1516
1617use function array_map ;
1718use function assert ;
@@ -29,6 +30,10 @@ class AtlasSearchTest extends TestCase
2930
3031 public function setUp (): void
3132 {
33+ // Use a unique prefix per test to avoid collisions when the search index is
34+ // deleted asynchronously while we try to create it in setup of the next test
35+ $ _SERVER ['DB_PREFIX ' ] = (new ReflectionClass ($ this ))->getShortName () . '_ ' . $ this ->name () . '_ ' ;
36+
3237 parent ::setUp ();
3338
3439 Book::insert ($ this ->addVector ([
@@ -54,7 +59,7 @@ public function setUp(): void
5459 ['title ' => 'Pattern Recognition and Machine Learning ' ],
5560 ]));
5661
57- $ collection = $ this ->getConnection ('mongodb ' )->getCollection (' books ' );
62+ $ collection = $ this ->getConnection ('mongodb ' )->getCollection ($ this -> getBookCollectionName () );
5863 assert ($ collection instanceof MongoDBCollection);
5964
6065 try {
@@ -92,25 +97,24 @@ public function setUp(): void
9297 // Wait for the index to be ready
9398 do {
9499 $ ready = true ;
95- usleep (10_000 );
100+ usleep (100 );
96101 foreach ($ collection ->listSearchIndexes () as $ index ) {
97- if ($ index ['status ' ] !== 'READY ' ) {
98- $ ready = false ;
99- }
102+ $ ready = $ ready && $ index ['queryable ' ];
100103 }
101104 } while (! $ ready );
102105 }
103106
104107 public function tearDown (): void
105108 {
106- $ this ->getConnection ('mongodb ' )->getCollection ('books ' )->drop ();
109+ $ this ->getConnection ('mongodb ' )->getCollection ($ this ->getBookCollectionName ())->drop ();
110+ unset($ _SERVER ['DB_PREFIX ' ]);
107111
108112 parent ::tearDown ();
109113 }
110114
111115 public function testGetIndexes ()
112116 {
113- $ indexes = Schema::getIndexes (' books ' );
117+ $ indexes = Schema::getIndexes ($ this -> getBookCollectionName () );
114118
115119 self ::assertIsArray ($ indexes );
116120 self ::assertCount (4 , $ indexes );
@@ -171,7 +175,7 @@ public function testEloquentBuilderSearch()
171175
172176 public function testDatabaseBuilderSearch ()
173177 {
174- $ results = $ this ->getConnection ('mongodb ' )->table (' books ' )
178+ $ results = $ this ->getConnection ('mongodb ' )->table ($ this -> getBookCollectionName () )
175179 ->search (Search::text ('title ' , 'systems ' ), sort: ['title ' => 1 ]);
176180
177181 self ::assertInstanceOf (LaravelCollection::class, $ results );
@@ -199,7 +203,7 @@ public function testEloquentBuilderAutocomplete()
199203
200204 public function testDatabaseBuilderAutocomplete ()
201205 {
202- $ results = $ this ->getConnection ('mongodb ' )->table (' books ' )
206+ $ results = $ this ->getConnection ('mongodb ' )->table ($ this -> getBookCollectionName () )
203207 ->autocomplete ('title ' , 'system ' );
204208
205209 self ::assertInstanceOf (LaravelCollection::class, $ results );
@@ -213,7 +217,7 @@ public function testDatabaseBuilderAutocomplete()
213217
214218 public function testDatabaseBuilderVectorSearch ()
215219 {
216- $ results = $ this ->getConnection ('mongodb ' )->table (' books ' )
220+ $ results = $ this ->getConnection ('mongodb ' )->table ($ this -> getBookCollectionName () )
217221 ->vectorSearch (
218222 index: 'vector ' ,
219223 path: 'vector4 ' ,
@@ -253,6 +257,15 @@ public function testEloquentBuilderVectorSearch()
253257 );
254258 }
255259
260+ private function getBookCollectionName (): string
261+ {
262+ $ name = (new Book ())->getTable ();
263+
264+ self ::assertNotEquals ('books ' , $ name , 'DB_PREFIX is not set correctly ' );
265+
266+ return $ name ;
267+ }
268+
256269 /** Generate random vectors using fixed seed to make tests deterministic */
257270 private function addVector (array $ items ): array
258271 {
0 commit comments