Skip to content

Commit 34e44b7

Browse files
committed
Add helpers for index exists/not-exists
1 parent 58b94c1 commit 34e44b7

File tree

1 file changed

+54
-62
lines changed

1 file changed

+54
-62
lines changed

tests/SchemaTest.php

Lines changed: 54 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use function assert;
1717
use function collect;
1818
use function count;
19+
use function sprintf;
1920

2021
class SchemaTest extends TestCase
2122
{
@@ -82,21 +83,21 @@ public function testIndex(): void
8283
$collection->index('mykey1');
8384
});
8485

85-
$index = $this->getIndex('newcollection', 'mykey1_1');
86+
$index = $this->assertIndexExists('newcollection', 'mykey1_1');
8687
$this->assertEquals(1, $index['key']['mykey1']);
8788

8889
Schema::table('newcollection', function ($collection) {
8990
$collection->index(['mykey2']);
9091
});
9192

92-
$index = $this->getIndex('newcollection', 'mykey2_1');
93+
$index = $this->assertIndexExists('newcollection', 'mykey2_1');
9394
$this->assertEquals(1, $index['key']['mykey2']);
9495

9596
Schema::table('newcollection', function ($collection) {
9697
$collection->string('mykey3')->index();
9798
});
9899

99-
$index = $this->getIndex('newcollection', 'mykey3_1');
100+
$index = $this->assertIndexExists('newcollection', 'mykey3_1');
100101
$this->assertEquals(1, $index['key']['mykey3']);
101102
}
102103

@@ -106,7 +107,7 @@ public function testPrimary(): void
106107
$collection->string('mykey', 100)->primary();
107108
});
108109

109-
$index = $this->getIndex('newcollection', 'mykey_1');
110+
$index = $this->assertIndexExists('newcollection', 'mykey_1');
110111
$this->assertEquals(1, $index['unique']);
111112
}
112113

@@ -116,7 +117,7 @@ public function testUnique(): void
116117
$collection->unique('uniquekey');
117118
});
118119

119-
$index = $this->getIndex('newcollection', 'uniquekey_1');
120+
$index = $this->assertIndexExists('newcollection', 'uniquekey_1');
120121
$this->assertEquals(1, $index['unique']);
121122
}
122123

@@ -127,60 +128,52 @@ public function testDropIndex(): void
127128
$collection->dropIndex('uniquekey_1');
128129
});
129130

130-
$index = $this->getIndex('newcollection', 'uniquekey_1');
131-
$this->assertEquals(null, $index);
131+
$this->assertIndexNotExists('newcollection', 'uniquekey_1');
132132

133133
Schema::table('newcollection', function ($collection) {
134134
$collection->unique('uniquekey');
135135
$collection->dropIndex(['uniquekey']);
136136
});
137137

138-
$index = $this->getIndex('newcollection', 'uniquekey_1');
139-
$this->assertEquals(null, $index);
138+
$this->assertIndexNotExists('newcollection', 'uniquekey_1');
140139

141140
Schema::table('newcollection', function ($collection) {
142141
$collection->index(['field_a', 'field_b']);
143142
});
144143

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');
147145

148146
Schema::table('newcollection', function ($collection) {
149147
$collection->dropIndex(['field_a', 'field_b']);
150148
});
151149

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');
154151

155152
$indexName = 'field_a_-1_field_b_1';
156153
Schema::table('newcollection', function ($collection) {
157154
$collection->index(['field_a' => -1, 'field_b' => 1]);
158155
});
159156

160-
$index = $this->getIndex('newcollection', $indexName);
161-
$this->assertNotNull($index);
157+
$this->assertIndexExists('newcollection', $indexName);
162158

163159
Schema::table('newcollection', function ($collection) {
164160
$collection->dropIndex(['field_a' => -1, 'field_b' => 1]);
165161
});
166162

167-
$index = $this->getIndex('newcollection', $indexName);
168-
$this->assertNull($index);
163+
$this->assertIndexNotExists('newcollection', $indexName);
169164

170165
$indexName = 'custom_index_name';
171166
Schema::table('newcollection', function ($collection) use ($indexName) {
172167
$collection->index(['field_a', 'field_b'], $indexName);
173168
});
174169

175-
$index = $this->getIndex('newcollection', $indexName);
176-
$this->assertNotNull($index);
170+
$this->assertIndexExists('newcollection', $indexName);
177171

178172
Schema::table('newcollection', function ($collection) use ($indexName) {
179173
$collection->dropIndex($indexName);
180174
});
181175

182-
$index = $this->getIndex('newcollection', $indexName);
183-
$this->assertNull($index);
176+
$this->assertIndexNotExists('newcollection', $indexName);
184177
}
185178

186179
public function testDropIndexIfExists(): void
@@ -190,66 +183,58 @@ public function testDropIndexIfExists(): void
190183
$collection->dropIndexIfExists('uniquekey_1');
191184
});
192185

193-
$index = $this->getIndex('newcollection', 'uniquekey');
194-
$this->assertEquals(null, $index);
186+
$this->assertIndexNotExists('newcollection', 'uniquekey');
195187

196188
Schema::table('newcollection', function (Blueprint $collection) {
197189
$collection->unique('uniquekey');
198190
$collection->dropIndexIfExists(['uniquekey']);
199191
});
200192

201-
$index = $this->getIndex('newcollection', 'uniquekey');
202-
$this->assertEquals(null, $index);
193+
$this->assertIndexNotExists('newcollection', 'uniquekey');
203194

204195
Schema::table('newcollection', function (Blueprint $collection) {
205196
$collection->index(['field_a', 'field_b']);
206197
});
207198

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');
210200

211201
Schema::table('newcollection', function (Blueprint $collection) {
212202
$collection->dropIndexIfExists(['field_a', 'field_b']);
213203
});
214204

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');
217206

218207
Schema::table('newcollection', function (Blueprint $collection) {
219208
$collection->index(['field_a', 'field_b'], 'custom_index_name');
220209
});
221210

222-
$index = $this->getIndex('newcollection', 'custom_index_name');
223-
$this->assertNotNull($index);
211+
$this->assertIndexExists('newcollection', 'custom_index_name');
224212

225213
Schema::table('newcollection', function (Blueprint $collection) {
226214
$collection->dropIndexIfExists('custom_index_name');
227215
});
228216

229-
$index = $this->getIndex('newcollection', 'custom_index_name');
230-
$this->assertNull($index);
217+
$this->assertIndexNotExists('newcollection', 'custom_index_name');
231218
}
232219

233220
public function testHasIndex(): void
234221
{
235-
$instance = $this;
236-
237-
Schema::table('newcollection', function (Blueprint $collection) use ($instance) {
222+
Schema::table('newcollection', function (Blueprint $collection) {
238223
$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'));
241226
});
242227

243-
Schema::table('newcollection', function (Blueprint $collection) use ($instance) {
228+
Schema::table('newcollection', function (Blueprint $collection) {
244229
$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']));
247232
});
248233

249-
Schema::table('newcollection', function (Blueprint $collection) use ($instance) {
234+
Schema::table('newcollection', function (Blueprint $collection) {
250235
$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']));
253238
});
254239
}
255240

@@ -259,8 +244,7 @@ public function testSparse(): void
259244
$collection->sparse('sparsekey');
260245
});
261246

262-
$index = $this->getIndex('newcollection', 'sparsekey_1');
263-
$this->assertNotNull($index);
247+
$index = $this->assertIndexExists('newcollection', 'sparsekey_1');
264248
$this->assertEquals(1, $index['sparse']);
265249
}
266250

@@ -270,8 +254,7 @@ public function testExpire(): void
270254
$collection->expire('expirekey', 60);
271255
});
272256

273-
$index = $this->getIndex('newcollection', 'expirekey_1');
274-
$this->assertNotNull($index);
257+
$index = $this->assertIndexExists('newcollection', 'expirekey_1');
275258
$this->assertEquals(60, $index['expireAfterSeconds']);
276259
}
277260

@@ -285,8 +268,7 @@ public function testSoftDeletes(): void
285268
$collection->string('email')->nullable()->index();
286269
});
287270

288-
$index = $this->getIndex('newcollection', 'email_1');
289-
$this->assertNotNull($index);
271+
$index = $this->assertIndexExists('newcollection', 'email_1');
290272
$this->assertEquals(1, $index['key']['email']);
291273
}
292274

@@ -298,12 +280,10 @@ public function testFluent(): void
298280
$collection->timestamp('created_at');
299281
});
300282

301-
$index = $this->getIndex('newcollection', 'email_1');
302-
$this->assertNotNull($index);
283+
$index = $this->assertIndexExists('newcollection', 'email_1');
303284
$this->assertEquals(1, $index['key']['email']);
304285

305-
$index = $this->getIndex('newcollection', 'token_1');
306-
$this->assertNotNull($index);
286+
$index = $this->assertIndexExists('newcollection', 'token_1');
307287
$this->assertEquals(1, $index['key']['token']);
308288
}
309289

@@ -315,16 +295,13 @@ public function testGeospatial(): void
315295
$collection->geospatial('continent', '2dsphere');
316296
});
317297

318-
$index = $this->getIndex('newcollection', 'point_2d');
319-
$this->assertNotNull($index);
298+
$index = $this->assertIndexExists('newcollection', 'point_2d');
320299
$this->assertEquals('2d', $index['key']['point']);
321300

322-
$index = $this->getIndex('newcollection', 'area_2d');
323-
$this->assertNotNull($index);
301+
$index = $this->assertIndexExists('newcollection', 'area_2d');
324302
$this->assertEquals('2d', $index['key']['area']);
325303

326-
$index = $this->getIndex('newcollection', 'continent_2dsphere');
327-
$this->assertNotNull($index);
304+
$index = $this->assertIndexExists('newcollection', 'continent_2dsphere');
328305
$this->assertEquals('2dsphere', $index['key']['continent']);
329306
}
330307

@@ -343,8 +320,7 @@ public function testSparseUnique(): void
343320
$collection->sparse_and_unique('sparseuniquekey');
344321
});
345322

346-
$index = $this->getIndex('newcollection', 'sparseuniquekey_1');
347-
$this->assertNotNull($index);
323+
$index = $this->assertIndexExists('newcollection', 'sparseuniquekey_1');
348324
$this->assertEquals(1, $index['sparse']);
349325
$this->assertEquals(1, $index['unique']);
350326
}
@@ -585,6 +561,22 @@ public function testVectorSearchIndex()
585561
self::assertSame('vector', $index['latestDefinition']['fields'][0]['type']);
586562
}
587563

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+
588580
protected function getIndex(string $collection, string $name): ?IndexInfo
589581
{
590582
$collection = $this->getConnection('mongodb')->getCollection($collection);

0 commit comments

Comments
 (0)