1111use MongoDB \Collection ;
1212use MongoDB \Database ;
1313use MongoDB \Laravel \Schema \Blueprint ;
14+ use MongoDB \Model \IndexInfo ;
1415
1516use function assert ;
1617use function collect ;
1718use function count ;
19+ use function str_starts_with ;
1820
1921class SchemaTest extends TestCase
2022{
@@ -149,7 +151,7 @@ public function testDropIndex(): void
149151 });
150152
151153 $ index = $ this ->getIndex ('newcollection ' , 'field_a_1_field_b_1 ' );
152- $ this ->assertFalse ($ index );
154+ $ this ->assertNull ($ index );
153155
154156 Schema::table ('newcollection ' , function ($ collection ) {
155157 $ collection ->index (['field_a ' => -1 , 'field_b ' => 1 ]);
@@ -162,8 +164,8 @@ public function testDropIndex(): void
162164 $ collection ->dropIndex (['field_a ' => -1 , 'field_b ' => 1 ]);
163165 });
164166
165- $ index = $ this ->getIndex ('newcollection ' , 'field_a_-1_field_b_1 ' );
166- $ this ->assertFalse ($ index );
167+ $ index = $ this ->getIndex ('newcollection ' , 'field_a_1_field_b_1 ' );
168+ $ this ->assertNull ($ index );
167169
168170 Schema::table ('newcollection ' , function ($ collection ) {
169171 $ collection ->index (['field_a ' , 'field_b ' ], 'custom_index_name ' );
@@ -177,7 +179,7 @@ public function testDropIndex(): void
177179 });
178180
179181 $ index = $ this ->getIndex ('newcollection ' , 'custom_index_name ' );
180- $ this ->assertFalse ($ index );
182+ $ this ->assertNull ($ index );
181183 }
182184
183185 public function testDropIndexIfExists (): void
@@ -210,7 +212,7 @@ public function testDropIndexIfExists(): void
210212 });
211213
212214 $ index = $ this ->getIndex ('newcollection ' , 'field_a_1_field_b_1 ' );
213- $ this ->assertFalse ($ index );
215+ $ this ->assertNull ($ index );
214216
215217 Schema::table ('newcollection ' , function (Blueprint $ collection ) {
216218 $ collection ->index (['field_a ' , 'field_b ' ], 'custom_index_name ' );
@@ -224,7 +226,7 @@ public function testDropIndexIfExists(): void
224226 });
225227
226228 $ index = $ this ->getIndex ('newcollection ' , 'custom_index_name ' );
227- $ this ->assertFalse ($ index );
229+ $ this ->assertNull ($ index );
228230 }
229231
230232 public function testHasIndex (): void
@@ -257,6 +259,7 @@ public function testSparse(): void
257259 });
258260
259261 $ index = $ this ->getIndex ('newcollection ' , 'sparsekey ' );
262+ $ this ->assertNotNull ($ index );
260263 $ this ->assertEquals (1 , $ index ['sparse ' ]);
261264 }
262265
@@ -573,23 +576,24 @@ public function testVectorSearchIndex()
573576 self ::assertSame ('vector ' , $ index ['latestDefinition ' ]['fields ' ][0 ]['type ' ]);
574577 }
575578
576- protected function getIndex (string $ collection , string $ name )
579+ /** MongoDB generates index names by concatenating the key field names and an incrementing integer. */
580+ protected function getIndex (string $ collection , string $ name ): ?IndexInfo
577581 {
578- $ collection = DB :: getCollection ($ collection );
582+ $ collection = $ this -> getConnection ( ' mongodb ' )-> getCollection ($ collection );
579583 assert ($ collection instanceof Collection);
580584
581585 foreach ($ collection ->listIndexes () as $ index ) {
582- if (isset ($ index[ ' key ' ][ $ name] )) {
586+ if (str_starts_with ($ index-> getName (), $ name )) {
583587 return $ index ;
584588 }
585589 }
586590
587- return false ;
591+ return null ;
588592 }
589593
590594 protected function getSearchIndex (string $ collection , string $ name ): ?array
591595 {
592- $ collection = DB :: getCollection ($ collection );
596+ $ collection = $ this -> getConnection ( ' mongodb ' )-> getCollection ($ collection );
593597 assert ($ collection instanceof Collection);
594598
595599 foreach ($ collection ->listSearchIndexes (['name ' => $ name , 'typeMap ' => ['root ' => 'array ' , 'array ' => 'array ' , 'document ' => 'array ' ]]) as $ index ) {
0 commit comments