Skip to content

Commit 31e6efe

Browse files
committed
Fix tests on Schema index
1 parent 6cb3838 commit 31e6efe

File tree

1 file changed

+47
-34
lines changed

1 file changed

+47
-34
lines changed

tests/SchemaTest.php

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use MongoDB\Collection;
1212
use MongoDB\Database;
1313
use MongoDB\Laravel\Schema\Blueprint;
14+
use MongoDB\Model\IndexInfo;
1415

1516
use function assert;
1617
use function collect;
@@ -81,21 +82,21 @@ public function testIndex(): void
8182
$collection->index('mykey1');
8283
});
8384

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

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

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

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

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

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

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

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

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

@@ -126,15 +127,15 @@ public function testDropIndex(): void
126127
$collection->dropIndex('uniquekey_1');
127128
});
128129

129-
$index = $this->getIndex('newcollection', 'uniquekey');
130+
$index = $this->getIndex('newcollection', 'uniquekey_1');
130131
$this->assertEquals(null, $index);
131132

132133
Schema::table('newcollection', function ($collection) {
133134
$collection->unique('uniquekey');
134135
$collection->dropIndex(['uniquekey']);
135136
});
136137

137-
$index = $this->getIndex('newcollection', 'uniquekey');
138+
$index = $this->getIndex('newcollection', 'uniquekey_1');
138139
$this->assertEquals(null, $index);
139140

140141
Schema::table('newcollection', function ($collection) {
@@ -149,35 +150,37 @@ public function testDropIndex(): void
149150
});
150151

151152
$index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
152-
$this->assertFalse($index);
153+
$this->assertNull($index);
153154

155+
$indexName = 'field_a_-1_field_b_1';
154156
Schema::table('newcollection', function ($collection) {
155157
$collection->index(['field_a' => -1, 'field_b' => 1]);
156158
});
157159

158-
$index = $this->getIndex('newcollection', 'field_a_-1_field_b_1');
160+
$index = $this->getIndex('newcollection', $indexName);
159161
$this->assertNotNull($index);
160162

161163
Schema::table('newcollection', function ($collection) {
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', $indexName);
168+
$this->assertNull($index);
167169

168-
Schema::table('newcollection', function ($collection) {
169-
$collection->index(['field_a', 'field_b'], 'custom_index_name');
170+
$indexName = 'custom_index_name';
171+
Schema::table('newcollection', function ($collection) use ($indexName) {
172+
$collection->index(['field_a', 'field_b'], $indexName);
170173
});
171174

172-
$index = $this->getIndex('newcollection', 'custom_index_name');
175+
$index = $this->getIndex('newcollection', $indexName);
173176
$this->assertNotNull($index);
174177

175-
Schema::table('newcollection', function ($collection) {
176-
$collection->dropIndex('custom_index_name');
178+
Schema::table('newcollection', function ($collection) use ($indexName) {
179+
$collection->dropIndex($indexName);
177180
});
178181

179-
$index = $this->getIndex('newcollection', 'custom_index_name');
180-
$this->assertFalse($index);
182+
$index = $this->getIndex('newcollection', $indexName);
183+
$this->assertNull($index);
181184
}
182185

183186
public function testDropIndexIfExists(): void
@@ -210,7 +213,7 @@ public function testDropIndexIfExists(): void
210213
});
211214

212215
$index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
213-
$this->assertFalse($index);
216+
$this->assertNull($index);
214217

215218
Schema::table('newcollection', function (Blueprint $collection) {
216219
$collection->index(['field_a', 'field_b'], 'custom_index_name');
@@ -224,7 +227,7 @@ public function testDropIndexIfExists(): void
224227
});
225228

226229
$index = $this->getIndex('newcollection', 'custom_index_name');
227-
$this->assertFalse($index);
230+
$this->assertNull($index);
228231
}
229232

230233
public function testHasIndex(): void
@@ -256,7 +259,8 @@ public function testSparse(): void
256259
$collection->sparse('sparsekey');
257260
});
258261

259-
$index = $this->getIndex('newcollection', 'sparsekey');
262+
$index = $this->getIndex('newcollection', 'sparsekey_1');
263+
$this->assertNotNull($index);
260264
$this->assertEquals(1, $index['sparse']);
261265
}
262266

@@ -266,7 +270,8 @@ public function testExpire(): void
266270
$collection->expire('expirekey', 60);
267271
});
268272

269-
$index = $this->getIndex('newcollection', 'expirekey');
273+
$index = $this->getIndex('newcollection', 'expirekey_1');
274+
$this->assertNotNull($index);
270275
$this->assertEquals(60, $index['expireAfterSeconds']);
271276
}
272277

@@ -280,7 +285,8 @@ public function testSoftDeletes(): void
280285
$collection->string('email')->nullable()->index();
281286
});
282287

283-
$index = $this->getIndex('newcollection', 'email');
288+
$index = $this->getIndex('newcollection', 'email_1');
289+
$this->assertNotNull($index);
284290
$this->assertEquals(1, $index['key']['email']);
285291
}
286292

@@ -292,10 +298,12 @@ public function testFluent(): void
292298
$collection->timestamp('created_at');
293299
});
294300

295-
$index = $this->getIndex('newcollection', 'email');
301+
$index = $this->getIndex('newcollection', 'email_1');
302+
$this->assertNotNull($index);
296303
$this->assertEquals(1, $index['key']['email']);
297304

298-
$index = $this->getIndex('newcollection', 'token');
305+
$index = $this->getIndex('newcollection', 'token_1');
306+
$this->assertNotNull($index);
299307
$this->assertEquals(1, $index['key']['token']);
300308
}
301309

@@ -307,13 +315,16 @@ public function testGeospatial(): void
307315
$collection->geospatial('continent', '2dsphere');
308316
});
309317

310-
$index = $this->getIndex('newcollection', 'point');
318+
$index = $this->getIndex('newcollection', 'point_2d');
319+
$this->assertNotNull($index);
311320
$this->assertEquals('2d', $index['key']['point']);
312321

313-
$index = $this->getIndex('newcollection', 'area');
322+
$index = $this->getIndex('newcollection', 'area_2d');
323+
$this->assertNotNull($index);
314324
$this->assertEquals('2d', $index['key']['area']);
315325

316-
$index = $this->getIndex('newcollection', 'continent');
326+
$index = $this->getIndex('newcollection', 'continent_2dsphere');
327+
$this->assertNotNull($index);
317328
$this->assertEquals('2dsphere', $index['key']['continent']);
318329
}
319330

@@ -332,7 +343,8 @@ public function testSparseUnique(): void
332343
$collection->sparse_and_unique('sparseuniquekey');
333344
});
334345

335-
$index = $this->getIndex('newcollection', 'sparseuniquekey');
346+
$index = $this->getIndex('newcollection', 'sparseuniquekey_1');
347+
$this->assertNotNull($index);
336348
$this->assertEquals(1, $index['sparse']);
337349
$this->assertEquals(1, $index['unique']);
338350
}
@@ -573,23 +585,24 @@ public function testVectorSearchIndex()
573585
self::assertSame('vector', $index['latestDefinition']['fields'][0]['type']);
574586
}
575587

576-
protected function getIndex(string $collection, string $name)
588+
/** MongoDB generates index names by concatenating the key field names and an incrementing integer. */
589+
protected function getIndex(string $collection, string $name): ?IndexInfo
577590
{
578-
$collection = DB::getCollection($collection);
591+
$collection = $this->getConnection('mongodb')->getCollection($collection);
579592
assert($collection instanceof Collection);
580593

581594
foreach ($collection->listIndexes() as $index) {
582-
if (isset($index['key'][$name])) {
595+
if ($index->getName() === $name) {
583596
return $index;
584597
}
585598
}
586599

587-
return false;
600+
return null;
588601
}
589602

590603
protected function getSearchIndex(string $collection, string $name): ?array
591604
{
592-
$collection = DB::getCollection($collection);
605+
$collection = $this->getConnection('mongodb')->getCollection($collection);
593606
assert($collection instanceof Collection);
594607

595608
foreach ($collection->listSearchIndexes(['name' => $name, 'typeMap' => ['root' => 'array', 'array' => 'array', 'document' => 'array']]) as $index) {

0 commit comments

Comments
 (0)