16
16
use function assert ;
17
17
use function collect ;
18
18
use function count ;
19
+ use function sprintf ;
19
20
20
21
class SchemaTest extends TestCase
21
22
{
@@ -82,21 +83,21 @@ public function testIndex(): void
82
83
$ collection ->index ('mykey1 ' );
83
84
});
84
85
85
- $ index = $ this ->getIndex ('newcollection ' , 'mykey1_1 ' );
86
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'mykey1_1 ' );
86
87
$ this ->assertEquals (1 , $ index ['key ' ]['mykey1 ' ]);
87
88
88
89
Schema::table ('newcollection ' , function ($ collection ) {
89
90
$ collection ->index (['mykey2 ' ]);
90
91
});
91
92
92
- $ index = $ this ->getIndex ('newcollection ' , 'mykey2_1 ' );
93
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'mykey2_1 ' );
93
94
$ this ->assertEquals (1 , $ index ['key ' ]['mykey2 ' ]);
94
95
95
96
Schema::table ('newcollection ' , function ($ collection ) {
96
97
$ collection ->string ('mykey3 ' )->index ();
97
98
});
98
99
99
- $ index = $ this ->getIndex ('newcollection ' , 'mykey3_1 ' );
100
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'mykey3_1 ' );
100
101
$ this ->assertEquals (1 , $ index ['key ' ]['mykey3 ' ]);
101
102
}
102
103
@@ -106,7 +107,7 @@ public function testPrimary(): void
106
107
$ collection ->string ('mykey ' , 100 )->primary ();
107
108
});
108
109
109
- $ index = $ this ->getIndex ('newcollection ' , 'mykey_1 ' );
110
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'mykey_1 ' );
110
111
$ this ->assertEquals (1 , $ index ['unique ' ]);
111
112
}
112
113
@@ -116,7 +117,7 @@ public function testUnique(): void
116
117
$ collection ->unique ('uniquekey ' );
117
118
});
118
119
119
- $ index = $ this ->getIndex ('newcollection ' , 'uniquekey_1 ' );
120
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'uniquekey_1 ' );
120
121
$ this ->assertEquals (1 , $ index ['unique ' ]);
121
122
}
122
123
@@ -127,60 +128,52 @@ public function testDropIndex(): void
127
128
$ collection ->dropIndex ('uniquekey_1 ' );
128
129
});
129
130
130
- $ index = $ this ->getIndex ('newcollection ' , 'uniquekey_1 ' );
131
- $ this ->assertEquals (null , $ index );
131
+ $ this ->assertIndexNotExists ('newcollection ' , 'uniquekey_1 ' );
132
132
133
133
Schema::table ('newcollection ' , function ($ collection ) {
134
134
$ collection ->unique ('uniquekey ' );
135
135
$ collection ->dropIndex (['uniquekey ' ]);
136
136
});
137
137
138
- $ index = $ this ->getIndex ('newcollection ' , 'uniquekey_1 ' );
139
- $ this ->assertEquals (null , $ index );
138
+ $ this ->assertIndexNotExists ('newcollection ' , 'uniquekey_1 ' );
140
139
141
140
Schema::table ('newcollection ' , function ($ collection ) {
142
141
$ collection ->index (['field_a ' , 'field_b ' ]);
143
142
});
144
143
145
- $ index = $ this ->getIndex ('newcollection ' , 'field_a_1_field_b_1 ' );
146
- $ this ->assertNotNull ($ index );
144
+ $ this ->assertIndexExists ('newcollection ' , 'field_a_1_field_b_1 ' );
147
145
148
146
Schema::table ('newcollection ' , function ($ collection ) {
149
147
$ collection ->dropIndex (['field_a ' , 'field_b ' ]);
150
148
});
151
149
152
- $ index = $ this ->getIndex ('newcollection ' , 'field_a_1_field_b_1 ' );
153
- $ this ->assertNull ($ index );
150
+ $ this ->assertIndexNotExists ('newcollection ' , 'field_a_1_field_b_1 ' );
154
151
155
152
$ indexName = 'field_a_-1_field_b_1 ' ;
156
153
Schema::table ('newcollection ' , function ($ collection ) {
157
154
$ collection ->index (['field_a ' => -1 , 'field_b ' => 1 ]);
158
155
});
159
156
160
- $ index = $ this ->getIndex ('newcollection ' , $ indexName );
161
- $ this ->assertNotNull ($ index );
157
+ $ this ->assertIndexExists ('newcollection ' , $ indexName );
162
158
163
159
Schema::table ('newcollection ' , function ($ collection ) {
164
160
$ collection ->dropIndex (['field_a ' => -1 , 'field_b ' => 1 ]);
165
161
});
166
162
167
- $ index = $ this ->getIndex ('newcollection ' , $ indexName );
168
- $ this ->assertNull ($ index );
163
+ $ this ->assertIndexNotExists ('newcollection ' , $ indexName );
169
164
170
165
$ indexName = 'custom_index_name ' ;
171
166
Schema::table ('newcollection ' , function ($ collection ) use ($ indexName ) {
172
167
$ collection ->index (['field_a ' , 'field_b ' ], $ indexName );
173
168
});
174
169
175
- $ index = $ this ->getIndex ('newcollection ' , $ indexName );
176
- $ this ->assertNotNull ($ index );
170
+ $ this ->assertIndexExists ('newcollection ' , $ indexName );
177
171
178
172
Schema::table ('newcollection ' , function ($ collection ) use ($ indexName ) {
179
173
$ collection ->dropIndex ($ indexName );
180
174
});
181
175
182
- $ index = $ this ->getIndex ('newcollection ' , $ indexName );
183
- $ this ->assertNull ($ index );
176
+ $ this ->assertIndexNotExists ('newcollection ' , $ indexName );
184
177
}
185
178
186
179
public function testDropIndexIfExists (): void
@@ -190,66 +183,58 @@ public function testDropIndexIfExists(): void
190
183
$ collection ->dropIndexIfExists ('uniquekey_1 ' );
191
184
});
192
185
193
- $ index = $ this ->getIndex ('newcollection ' , 'uniquekey ' );
194
- $ this ->assertEquals (null , $ index );
186
+ $ this ->assertIndexNotExists ('newcollection ' , 'uniquekey ' );
195
187
196
188
Schema::table ('newcollection ' , function (Blueprint $ collection ) {
197
189
$ collection ->unique ('uniquekey ' );
198
190
$ collection ->dropIndexIfExists (['uniquekey ' ]);
199
191
});
200
192
201
- $ index = $ this ->getIndex ('newcollection ' , 'uniquekey ' );
202
- $ this ->assertEquals (null , $ index );
193
+ $ this ->assertIndexNotExists ('newcollection ' , 'uniquekey ' );
203
194
204
195
Schema::table ('newcollection ' , function (Blueprint $ collection ) {
205
196
$ collection ->index (['field_a ' , 'field_b ' ]);
206
197
});
207
198
208
- $ index = $ this ->getIndex ('newcollection ' , 'field_a_1_field_b_1 ' );
209
- $ this ->assertNotNull ($ index );
199
+ $ this ->assertIndexExists ('newcollection ' , 'field_a_1_field_b_1 ' );
210
200
211
201
Schema::table ('newcollection ' , function (Blueprint $ collection ) {
212
202
$ collection ->dropIndexIfExists (['field_a ' , 'field_b ' ]);
213
203
});
214
204
215
- $ index = $ this ->getIndex ('newcollection ' , 'field_a_1_field_b_1 ' );
216
- $ this ->assertNull ($ index );
205
+ $ this ->assertIndexNotExists ('newcollection ' , 'field_a_1_field_b_1 ' );
217
206
218
207
Schema::table ('newcollection ' , function (Blueprint $ collection ) {
219
208
$ collection ->index (['field_a ' , 'field_b ' ], 'custom_index_name ' );
220
209
});
221
210
222
- $ index = $ this ->getIndex ('newcollection ' , 'custom_index_name ' );
223
- $ this ->assertNotNull ($ index );
211
+ $ this ->assertIndexExists ('newcollection ' , 'custom_index_name ' );
224
212
225
213
Schema::table ('newcollection ' , function (Blueprint $ collection ) {
226
214
$ collection ->dropIndexIfExists ('custom_index_name ' );
227
215
});
228
216
229
- $ index = $ this ->getIndex ('newcollection ' , 'custom_index_name ' );
230
- $ this ->assertNull ($ index );
217
+ $ this ->assertIndexNotExists ('newcollection ' , 'custom_index_name ' );
231
218
}
232
219
233
220
public function testHasIndex (): void
234
221
{
235
- $ instance = $ this ;
236
-
237
- Schema::table ('newcollection ' , function (Blueprint $ collection ) use ($ instance ) {
222
+ Schema::table ('newcollection ' , function (Blueprint $ collection ) {
238
223
$ collection ->index ('myhaskey1 ' );
239
- $ instance ->assertTrue ($ collection ->hasIndex ('myhaskey1_1 ' ));
240
- $ instance ->assertFalse ($ collection ->hasIndex ('myhaskey1 ' ));
224
+ $ this ->assertTrue ($ collection ->hasIndex ('myhaskey1_1 ' ));
225
+ $ this ->assertFalse ($ collection ->hasIndex ('myhaskey1 ' ));
241
226
});
242
227
243
- Schema::table ('newcollection ' , function (Blueprint $ collection ) use ( $ instance ) {
228
+ Schema::table ('newcollection ' , function (Blueprint $ collection ) {
244
229
$ collection ->index ('myhaskey2 ' );
245
- $ instance ->assertTrue ($ collection ->hasIndex (['myhaskey2 ' ]));
246
- $ instance ->assertFalse ($ collection ->hasIndex (['myhaskey2_1 ' ]));
230
+ $ this ->assertTrue ($ collection ->hasIndex (['myhaskey2 ' ]));
231
+ $ this ->assertFalse ($ collection ->hasIndex (['myhaskey2_1 ' ]));
247
232
});
248
233
249
- Schema::table ('newcollection ' , function (Blueprint $ collection ) use ( $ instance ) {
234
+ Schema::table ('newcollection ' , function (Blueprint $ collection ) {
250
235
$ collection ->index (['field_a ' , 'field_b ' ]);
251
- $ instance ->assertTrue ($ collection ->hasIndex (['field_a_1_field_b ' ]));
252
- $ 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 ' ]));
253
238
});
254
239
}
255
240
@@ -259,8 +244,7 @@ public function testSparse(): void
259
244
$ collection ->sparse ('sparsekey ' );
260
245
});
261
246
262
- $ index = $ this ->getIndex ('newcollection ' , 'sparsekey_1 ' );
263
- $ this ->assertNotNull ($ index );
247
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'sparsekey_1 ' );
264
248
$ this ->assertEquals (1 , $ index ['sparse ' ]);
265
249
}
266
250
@@ -270,8 +254,7 @@ public function testExpire(): void
270
254
$ collection ->expire ('expirekey ' , 60 );
271
255
});
272
256
273
- $ index = $ this ->getIndex ('newcollection ' , 'expirekey_1 ' );
274
- $ this ->assertNotNull ($ index );
257
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'expirekey_1 ' );
275
258
$ this ->assertEquals (60 , $ index ['expireAfterSeconds ' ]);
276
259
}
277
260
@@ -285,8 +268,7 @@ public function testSoftDeletes(): void
285
268
$ collection ->string ('email ' )->nullable ()->index ();
286
269
});
287
270
288
- $ index = $ this ->getIndex ('newcollection ' , 'email_1 ' );
289
- $ this ->assertNotNull ($ index );
271
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'email_1 ' );
290
272
$ this ->assertEquals (1 , $ index ['key ' ]['email ' ]);
291
273
}
292
274
@@ -298,12 +280,10 @@ public function testFluent(): void
298
280
$ collection ->timestamp ('created_at ' );
299
281
});
300
282
301
- $ index = $ this ->getIndex ('newcollection ' , 'email_1 ' );
302
- $ this ->assertNotNull ($ index );
283
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'email_1 ' );
303
284
$ this ->assertEquals (1 , $ index ['key ' ]['email ' ]);
304
285
305
- $ index = $ this ->getIndex ('newcollection ' , 'token_1 ' );
306
- $ this ->assertNotNull ($ index );
286
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'token_1 ' );
307
287
$ this ->assertEquals (1 , $ index ['key ' ]['token ' ]);
308
288
}
309
289
@@ -315,16 +295,13 @@ public function testGeospatial(): void
315
295
$ collection ->geospatial ('continent ' , '2dsphere ' );
316
296
});
317
297
318
- $ index = $ this ->getIndex ('newcollection ' , 'point_2d ' );
319
- $ this ->assertNotNull ($ index );
298
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'point_2d ' );
320
299
$ this ->assertEquals ('2d ' , $ index ['key ' ]['point ' ]);
321
300
322
- $ index = $ this ->getIndex ('newcollection ' , 'area_2d ' );
323
- $ this ->assertNotNull ($ index );
301
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'area_2d ' );
324
302
$ this ->assertEquals ('2d ' , $ index ['key ' ]['area ' ]);
325
303
326
- $ index = $ this ->getIndex ('newcollection ' , 'continent_2dsphere ' );
327
- $ this ->assertNotNull ($ index );
304
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'continent_2dsphere ' );
328
305
$ this ->assertEquals ('2dsphere ' , $ index ['key ' ]['continent ' ]);
329
306
}
330
307
@@ -343,8 +320,7 @@ public function testSparseUnique(): void
343
320
$ collection ->sparse_and_unique ('sparseuniquekey ' );
344
321
});
345
322
346
- $ index = $ this ->getIndex ('newcollection ' , 'sparseuniquekey_1 ' );
347
- $ this ->assertNotNull ($ index );
323
+ $ index = $ this ->assertIndexExists ('newcollection ' , 'sparseuniquekey_1 ' );
348
324
$ this ->assertEquals (1 , $ index ['sparse ' ]);
349
325
$ this ->assertEquals (1 , $ index ['unique ' ]);
350
326
}
@@ -585,6 +561,22 @@ public function testVectorSearchIndex()
585
561
self ::assertSame ('vector ' , $ index ['latestDefinition ' ]['fields ' ][0 ]['type ' ]);
586
562
}
587
563
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
574
+ {
575
+ $ index = $ this ->getIndex ($ collection , $ name );
576
+
577
+ self ::assertNull ($ index , sprintf ('Index "%s.%s" exists. ' , $ collection , $ name ));
578
+ }
579
+
588
580
protected function getIndex (string $ collection , string $ name ): ?IndexInfo
589
581
{
590
582
$ collection = $ this ->getConnection ('mongodb ' )->getCollection ($ collection );
0 commit comments