11
11
use MongoDB \Collection ;
12
12
use MongoDB \Database ;
13
13
use MongoDB \Laravel \Schema \Blueprint ;
14
+ use MongoDB \Model \IndexInfo ;
14
15
15
16
use function assert ;
16
17
use function collect ;
17
18
use function count ;
19
+ use function sprintf ;
18
20
19
21
class SchemaTest extends TestCase
20
22
{
@@ -81,21 +83,21 @@ public function testIndex(): void
81
83
$ collection ->index ('mykey1 ' );
82
84
});
83
85
84
- $ index = $ this ->getIndex ('newcollection ' , 'mykey1 ' );
86
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'mykey1_1 ' );
85
87
$ this ->assertEquals (1 , $ index ['key ' ]['mykey1 ' ]);
86
88
87
89
Schema::table ('newcollection ' , function ($ collection ) {
88
90
$ collection ->index (['mykey2 ' ]);
89
91
});
90
92
91
- $ index = $ this ->getIndex ('newcollection ' , 'mykey2 ' );
93
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'mykey2_1 ' );
92
94
$ this ->assertEquals (1 , $ index ['key ' ]['mykey2 ' ]);
93
95
94
96
Schema::table ('newcollection ' , function ($ collection ) {
95
97
$ collection ->string ('mykey3 ' )->index ();
96
98
});
97
99
98
- $ index = $ this ->getIndex ('newcollection ' , 'mykey3 ' );
100
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'mykey3_1 ' );
99
101
$ this ->assertEquals (1 , $ index ['key ' ]['mykey3 ' ]);
100
102
}
101
103
@@ -105,7 +107,7 @@ public function testPrimary(): void
105
107
$ collection ->string ('mykey ' , 100 )->primary ();
106
108
});
107
109
108
- $ index = $ this ->getIndex ('newcollection ' , 'mykey ' );
110
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'mykey_1 ' );
109
111
$ this ->assertEquals (1 , $ index ['unique ' ]);
110
112
}
111
113
@@ -115,7 +117,7 @@ public function testUnique(): void
115
117
$ collection ->unique ('uniquekey ' );
116
118
});
117
119
118
- $ index = $ this ->getIndex ('newcollection ' , 'uniquekey ' );
120
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'uniquekey_1 ' );
119
121
$ this ->assertEquals (1 , $ index ['unique ' ]);
120
122
}
121
123
@@ -126,58 +128,52 @@ public function testDropIndex(): void
126
128
$ collection ->dropIndex ('uniquekey_1 ' );
127
129
});
128
130
129
- $ index = $ this ->getIndex ('newcollection ' , 'uniquekey ' );
130
- $ this ->assertEquals (null , $ index );
131
+ $ this ->assertIndexNotExists ('newcollection ' , 'uniquekey_1 ' );
131
132
132
133
Schema::table ('newcollection ' , function ($ collection ) {
133
134
$ collection ->unique ('uniquekey ' );
134
135
$ collection ->dropIndex (['uniquekey ' ]);
135
136
});
136
137
137
- $ index = $ this ->getIndex ('newcollection ' , 'uniquekey ' );
138
- $ this ->assertEquals (null , $ index );
138
+ $ this ->assertIndexNotExists ('newcollection ' , 'uniquekey_1 ' );
139
139
140
140
Schema::table ('newcollection ' , function ($ collection ) {
141
141
$ collection ->index (['field_a ' , 'field_b ' ]);
142
142
});
143
143
144
- $ index = $ this ->getIndex ('newcollection ' , 'field_a_1_field_b_1 ' );
145
- $ this ->assertNotNull ($ index );
144
+ $ this ->assertIndexExists ('newcollection ' , 'field_a_1_field_b_1 ' );
146
145
147
146
Schema::table ('newcollection ' , function ($ collection ) {
148
147
$ collection ->dropIndex (['field_a ' , 'field_b ' ]);
149
148
});
150
149
151
- $ index = $ this ->getIndex ('newcollection ' , 'field_a_1_field_b_1 ' );
152
- $ this ->assertFalse ($ index );
150
+ $ this ->assertIndexNotExists ('newcollection ' , 'field_a_1_field_b_1 ' );
153
151
152
+ $ indexName = 'field_a_-1_field_b_1 ' ;
154
153
Schema::table ('newcollection ' , function ($ collection ) {
155
154
$ collection ->index (['field_a ' => -1 , 'field_b ' => 1 ]);
156
155
});
157
156
158
- $ index = $ this ->getIndex ('newcollection ' , 'field_a_-1_field_b_1 ' );
159
- $ this ->assertNotNull ($ index );
157
+ $ this ->assertIndexExists ('newcollection ' , $ indexName );
160
158
161
159
Schema::table ('newcollection ' , function ($ collection ) {
162
160
$ collection ->dropIndex (['field_a ' => -1 , 'field_b ' => 1 ]);
163
161
});
164
162
165
- $ index = $ this ->getIndex ('newcollection ' , 'field_a_-1_field_b_1 ' );
166
- $ this ->assertFalse ($ index );
163
+ $ this ->assertIndexNotExists ('newcollection ' , $ indexName );
167
164
168
- Schema::table ('newcollection ' , function ($ collection ) {
169
- $ collection ->index (['field_a ' , 'field_b ' ], 'custom_index_name ' );
165
+ $ indexName = 'custom_index_name ' ;
166
+ Schema::table ('newcollection ' , function ($ collection ) use ($ indexName ) {
167
+ $ collection ->index (['field_a ' , 'field_b ' ], $ indexName );
170
168
});
171
169
172
- $ index = $ this ->getIndex ('newcollection ' , 'custom_index_name ' );
173
- $ this ->assertNotNull ($ index );
170
+ $ this ->assertIndexExists ('newcollection ' , $ indexName );
174
171
175
- Schema::table ('newcollection ' , function ($ collection ) {
176
- $ collection ->dropIndex (' custom_index_name ' );
172
+ Schema::table ('newcollection ' , function ($ collection ) use ( $ indexName ) {
173
+ $ collection ->dropIndex ($ indexName );
177
174
});
178
175
179
- $ index = $ this ->getIndex ('newcollection ' , 'custom_index_name ' );
180
- $ this ->assertFalse ($ index );
176
+ $ this ->assertIndexNotExists ('newcollection ' , $ indexName );
181
177
}
182
178
183
179
public function testDropIndexIfExists (): void
@@ -187,66 +183,58 @@ public function testDropIndexIfExists(): void
187
183
$ collection ->dropIndexIfExists ('uniquekey_1 ' );
188
184
});
189
185
190
- $ index = $ this ->getIndex ('newcollection ' , 'uniquekey ' );
191
- $ this ->assertEquals (null , $ index );
186
+ $ this ->assertIndexNotExists ('newcollection ' , 'uniquekey ' );
192
187
193
188
Schema::table ('newcollection ' , function (Blueprint $ collection ) {
194
189
$ collection ->unique ('uniquekey ' );
195
190
$ collection ->dropIndexIfExists (['uniquekey ' ]);
196
191
});
197
192
198
- $ index = $ this ->getIndex ('newcollection ' , 'uniquekey ' );
199
- $ this ->assertEquals (null , $ index );
193
+ $ this ->assertIndexNotExists ('newcollection ' , 'uniquekey ' );
200
194
201
195
Schema::table ('newcollection ' , function (Blueprint $ collection ) {
202
196
$ collection ->index (['field_a ' , 'field_b ' ]);
203
197
});
204
198
205
- $ index = $ this ->getIndex ('newcollection ' , 'field_a_1_field_b_1 ' );
206
- $ this ->assertNotNull ($ index );
199
+ $ this ->assertIndexExists ('newcollection ' , 'field_a_1_field_b_1 ' );
207
200
208
201
Schema::table ('newcollection ' , function (Blueprint $ collection ) {
209
202
$ collection ->dropIndexIfExists (['field_a ' , 'field_b ' ]);
210
203
});
211
204
212
- $ index = $ this ->getIndex ('newcollection ' , 'field_a_1_field_b_1 ' );
213
- $ this ->assertFalse ($ index );
205
+ $ this ->assertIndexNotExists ('newcollection ' , 'field_a_1_field_b_1 ' );
214
206
215
207
Schema::table ('newcollection ' , function (Blueprint $ collection ) {
216
208
$ collection ->index (['field_a ' , 'field_b ' ], 'custom_index_name ' );
217
209
});
218
210
219
- $ index = $ this ->getIndex ('newcollection ' , 'custom_index_name ' );
220
- $ this ->assertNotNull ($ index );
211
+ $ this ->assertIndexExists ('newcollection ' , 'custom_index_name ' );
221
212
222
213
Schema::table ('newcollection ' , function (Blueprint $ collection ) {
223
214
$ collection ->dropIndexIfExists ('custom_index_name ' );
224
215
});
225
216
226
- $ index = $ this ->getIndex ('newcollection ' , 'custom_index_name ' );
227
- $ this ->assertFalse ($ index );
217
+ $ this ->assertIndexNotExists ('newcollection ' , 'custom_index_name ' );
228
218
}
229
219
230
220
public function testHasIndex (): void
231
221
{
232
- $ instance = $ this ;
233
-
234
- Schema::table ('newcollection ' , function (Blueprint $ collection ) use ($ instance ) {
222
+ Schema::table ('newcollection ' , function (Blueprint $ collection ) {
235
223
$ collection ->index ('myhaskey1 ' );
236
- $ instance ->assertTrue ($ collection ->hasIndex ('myhaskey1_1 ' ));
237
- $ instance ->assertFalse ($ collection ->hasIndex ('myhaskey1 ' ));
224
+ $ this ->assertTrue ($ collection ->hasIndex ('myhaskey1_1 ' ));
225
+ $ this ->assertFalse ($ collection ->hasIndex ('myhaskey1 ' ));
238
226
});
239
227
240
- Schema::table ('newcollection ' , function (Blueprint $ collection ) use ( $ instance ) {
228
+ Schema::table ('newcollection ' , function (Blueprint $ collection ) {
241
229
$ collection ->index ('myhaskey2 ' );
242
- $ instance ->assertTrue ($ collection ->hasIndex (['myhaskey2 ' ]));
243
- $ instance ->assertFalse ($ collection ->hasIndex (['myhaskey2_1 ' ]));
230
+ $ this ->assertTrue ($ collection ->hasIndex (['myhaskey2 ' ]));
231
+ $ this ->assertFalse ($ collection ->hasIndex (['myhaskey2_1 ' ]));
244
232
});
245
233
246
- Schema::table ('newcollection ' , function (Blueprint $ collection ) use ( $ instance ) {
234
+ Schema::table ('newcollection ' , function (Blueprint $ collection ) {
247
235
$ collection ->index (['field_a ' , 'field_b ' ]);
248
- $ instance ->assertTrue ($ collection ->hasIndex (['field_a_1_field_b ' ]));
249
- $ instance ->assertFalse ($ collection ->hasIndex (['field_a_1_field_b_1 ' ]));
236
+ $ this ->assertTrue ($ collection ->hasIndex (['field_a_1_field_b ' ]));
237
+ $ this ->assertFalse ($ collection ->hasIndex (['field_a_1_field_b_1 ' ]));
250
238
});
251
239
}
252
240
@@ -256,7 +244,7 @@ public function testSparse(): void
256
244
$ collection ->sparse ('sparsekey ' );
257
245
});
258
246
259
- $ index = $ this ->getIndex ('newcollection ' , 'sparsekey ' );
247
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'sparsekey_1 ' );
260
248
$ this ->assertEquals (1 , $ index ['sparse ' ]);
261
249
}
262
250
@@ -266,7 +254,7 @@ public function testExpire(): void
266
254
$ collection ->expire ('expirekey ' , 60 );
267
255
});
268
256
269
- $ index = $ this ->getIndex ('newcollection ' , 'expirekey ' );
257
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'expirekey_1 ' );
270
258
$ this ->assertEquals (60 , $ index ['expireAfterSeconds ' ]);
271
259
}
272
260
@@ -280,7 +268,7 @@ public function testSoftDeletes(): void
280
268
$ collection ->string ('email ' )->nullable ()->index ();
281
269
});
282
270
283
- $ index = $ this ->getIndex ('newcollection ' , 'email ' );
271
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'email_1 ' );
284
272
$ this ->assertEquals (1 , $ index ['key ' ]['email ' ]);
285
273
}
286
274
@@ -292,10 +280,10 @@ public function testFluent(): void
292
280
$ collection ->timestamp ('created_at ' );
293
281
});
294
282
295
- $ index = $ this ->getIndex ('newcollection ' , 'email ' );
283
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'email_1 ' );
296
284
$ this ->assertEquals (1 , $ index ['key ' ]['email ' ]);
297
285
298
- $ index = $ this ->getIndex ('newcollection ' , 'token ' );
286
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'token_1 ' );
299
287
$ this ->assertEquals (1 , $ index ['key ' ]['token ' ]);
300
288
}
301
289
@@ -307,13 +295,13 @@ public function testGeospatial(): void
307
295
$ collection ->geospatial ('continent ' , '2dsphere ' );
308
296
});
309
297
310
- $ index = $ this ->getIndex ('newcollection ' , 'point ' );
298
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'point_2d ' );
311
299
$ this ->assertEquals ('2d ' , $ index ['key ' ]['point ' ]);
312
300
313
- $ index = $ this ->getIndex ('newcollection ' , 'area ' );
301
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'area_2d ' );
314
302
$ this ->assertEquals ('2d ' , $ index ['key ' ]['area ' ]);
315
303
316
- $ index = $ this ->getIndex ('newcollection ' , 'continent ' );
304
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'continent_2dsphere ' );
317
305
$ this ->assertEquals ('2dsphere ' , $ index ['key ' ]['continent ' ]);
318
306
}
319
307
@@ -332,7 +320,7 @@ public function testSparseUnique(): void
332
320
$ collection ->sparse_and_unique ('sparseuniquekey ' );
333
321
});
334
322
335
- $ index = $ this ->getIndex ('newcollection ' , 'sparseuniquekey ' );
323
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'sparseuniquekey_1 ' );
336
324
$ this ->assertEquals (1 , $ index ['sparse ' ]);
337
325
$ this ->assertEquals (1 , $ index ['unique ' ]);
338
326
}
@@ -573,23 +561,39 @@ public function testVectorSearchIndex()
573
561
self ::assertSame ('vector ' , $ index ['latestDefinition ' ]['fields ' ][0 ]['type ' ]);
574
562
}
575
563
576
- protected function getIndex (string $ collection , string $ name )
564
+ protected function assertIndexExists (string $ collection , string $ name ): IndexInfo
565
+ {
566
+ $ index = $ this ->getIndex ($ collection , $ name );
567
+
568
+ self ::assertNotNull ($ index , sprintf ('Index "%s.%s" does not exist. ' , $ collection , $ name ));
569
+
570
+ return $ index ;
571
+ }
572
+
573
+ protected function assertIndexNotExists (string $ collection , string $ name ): void
577
574
{
578
- $ collection = DB ::getCollection ($ collection );
575
+ $ index = $ this ->getIndex ($ collection , $ name );
576
+
577
+ self ::assertNull ($ index , sprintf ('Index "%s.%s" exists. ' , $ collection , $ name ));
578
+ }
579
+
580
+ protected function getIndex (string $ collection , string $ name ): ?IndexInfo
581
+ {
582
+ $ collection = $ this ->getConnection ('mongodb ' )->getCollection ($ collection );
579
583
assert ($ collection instanceof Collection);
580
584
581
585
foreach ($ collection ->listIndexes () as $ index ) {
582
- if (isset ( $ index[ ' key ' ][ $ name]) ) {
586
+ if ($ index-> getName () === $ name ) {
583
587
return $ index ;
584
588
}
585
589
}
586
590
587
- return false ;
591
+ return null ;
588
592
}
589
593
590
594
protected function getSearchIndex (string $ collection , string $ name ): ?array
591
595
{
592
- $ collection = DB :: getCollection ($ collection );
596
+ $ collection = $ this -> getConnection ( ' mongodb ' )-> getCollection ($ collection );
593
597
assert ($ collection instanceof Collection);
594
598
595
599
foreach ($ collection ->listSearchIndexes (['name ' => $ name , 'typeMap ' => ['root ' => 'array ' , 'array ' => 'array ' , 'document ' => 'array ' ]]) as $ index ) {
0 commit comments