Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit aae9170

Browse files
authored
Fix tests on Schema index helpers (#3236)
Add helpers for index exists/not-exists
1 parent 6cb3838 commit aae9170

File tree

1 file changed

+66
-62
lines changed

1 file changed

+66
-62
lines changed

‎tests/SchemaTest.php‎

Lines changed: 66 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
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;
1718
use function count;
19+
use function sprintf;
1820

1921
class SchemaTest extends TestCase
2022
{
@@ -81,21 +83,21 @@ public function testIndex(): void
8183
$collection->index('mykey1');
8284
});
8385

84-
$index = $this->getIndex('newcollection', 'mykey1');
86+
$index = $this->assertIndexExists('newcollection', 'mykey1_1');
8587
$this->assertEquals(1, $index['key']['mykey1']);
8688

8789
Schema::table('newcollection', function ($collection) {
8890
$collection->index(['mykey2']);
8991
});
9092

91-
$index = $this->getIndex('newcollection', 'mykey2');
93+
$index = $this->assertIndexExists('newcollection', 'mykey2_1');
9294
$this->assertEquals(1, $index['key']['mykey2']);
9395

9496
Schema::table('newcollection', function ($collection) {
9597
$collection->string('mykey3')->index();
9698
});
9799

98-
$index = $this->getIndex('newcollection', 'mykey3');
100+
$index = $this->assertIndexExists('newcollection', 'mykey3_1');
99101
$this->assertEquals(1, $index['key']['mykey3']);
100102
}
101103

@@ -105,7 +107,7 @@ public function testPrimary(): void
105107
$collection->string('mykey', 100)->primary();
106108
});
107109

108-
$index = $this->getIndex('newcollection', 'mykey');
110+
$index = $this->assertIndexExists('newcollection', 'mykey_1');
109111
$this->assertEquals(1, $index['unique']);
110112
}
111113

@@ -115,7 +117,7 @@ public function testUnique(): void
115117
$collection->unique('uniquekey');
116118
});
117119

118-
$index = $this->getIndex('newcollection', 'uniquekey');
120+
$index = $this->assertIndexExists('newcollection', 'uniquekey_1');
119121
$this->assertEquals(1, $index['unique']);
120122
}
121123

@@ -126,58 +128,52 @@ public function testDropIndex(): void
126128
$collection->dropIndex('uniquekey_1');
127129
});
128130

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

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

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

140140
Schema::table('newcollection', function ($collection) {
141141
$collection->index(['field_a', 'field_b']);
142142
});
143143

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

147146
Schema::table('newcollection', function ($collection) {
148147
$collection->dropIndex(['field_a', 'field_b']);
149148
});
150149

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

152+
$indexName = 'field_a_-1_field_b_1';
154153
Schema::table('newcollection', function ($collection) {
155154
$collection->index(['field_a' => -1, 'field_b' => 1]);
156155
});
157156

158-
$index = $this->getIndex('newcollection', 'field_a_-1_field_b_1');
159-
$this->assertNotNull($index);
157+
$this->assertIndexExists('newcollection', $indexName);
160158

161159
Schema::table('newcollection', function ($collection) {
162160
$collection->dropIndex(['field_a' => -1, 'field_b' => 1]);
163161
});
164162

165-
$index = $this->getIndex('newcollection', 'field_a_-1_field_b_1');
166-
$this->assertFalse($index);
163+
$this->assertIndexNotExists('newcollection', $indexName);
167164

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);
170168
});
171169

172-
$index = $this->getIndex('newcollection', 'custom_index_name');
173-
$this->assertNotNull($index);
170+
$this->assertIndexExists('newcollection', $indexName);
174171

175-
Schema::table('newcollection', function ($collection) {
176-
$collection->dropIndex('custom_index_name');
172+
Schema::table('newcollection', function ($collection) use ($indexName) {
173+
$collection->dropIndex($indexName);
177174
});
178175

179-
$index = $this->getIndex('newcollection', 'custom_index_name');
180-
$this->assertFalse($index);
176+
$this->assertIndexNotExists('newcollection', $indexName);
181177
}
182178

183179
public function testDropIndexIfExists(): void
@@ -187,66 +183,58 @@ public function testDropIndexIfExists(): void
187183
$collection->dropIndexIfExists('uniquekey_1');
188184
});
189185

190-
$index = $this->getIndex('newcollection', 'uniquekey');
191-
$this->assertEquals(null, $index);
186+
$this->assertIndexNotExists('newcollection', 'uniquekey');
192187

193188
Schema::table('newcollection', function (Blueprint $collection) {
194189
$collection->unique('uniquekey');
195190
$collection->dropIndexIfExists(['uniquekey']);
196191
});
197192

198-
$index = $this->getIndex('newcollection', 'uniquekey');
199-
$this->assertEquals(null, $index);
193+
$this->assertIndexNotExists('newcollection', 'uniquekey');
200194

201195
Schema::table('newcollection', function (Blueprint $collection) {
202196
$collection->index(['field_a', 'field_b']);
203197
});
204198

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

208201
Schema::table('newcollection', function (Blueprint $collection) {
209202
$collection->dropIndexIfExists(['field_a', 'field_b']);
210203
});
211204

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

215207
Schema::table('newcollection', function (Blueprint $collection) {
216208
$collection->index(['field_a', 'field_b'], 'custom_index_name');
217209
});
218210

219-
$index = $this->getIndex('newcollection', 'custom_index_name');
220-
$this->assertNotNull($index);
211+
$this->assertIndexExists('newcollection', 'custom_index_name');
221212

222213
Schema::table('newcollection', function (Blueprint $collection) {
223214
$collection->dropIndexIfExists('custom_index_name');
224215
});
225216

226-
$index = $this->getIndex('newcollection', 'custom_index_name');
227-
$this->assertFalse($index);
217+
$this->assertIndexNotExists('newcollection', 'custom_index_name');
228218
}
229219

230220
public function testHasIndex(): void
231221
{
232-
$instance = $this;
233-
234-
Schema::table('newcollection', function (Blueprint $collection) use ($instance) {
222+
Schema::table('newcollection', function (Blueprint $collection) {
235223
$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'));
238226
});
239227

240-
Schema::table('newcollection', function (Blueprint $collection) use ($instance) {
228+
Schema::table('newcollection', function (Blueprint $collection) {
241229
$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']));
244232
});
245233

246-
Schema::table('newcollection', function (Blueprint $collection) use ($instance) {
234+
Schema::table('newcollection', function (Blueprint $collection) {
247235
$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']));
250238
});
251239
}
252240

@@ -256,7 +244,7 @@ public function testSparse(): void
256244
$collection->sparse('sparsekey');
257245
});
258246

259-
$index = $this->getIndex('newcollection', 'sparsekey');
247+
$index = $this->assertIndexExists('newcollection', 'sparsekey_1');
260248
$this->assertEquals(1, $index['sparse']);
261249
}
262250

@@ -266,7 +254,7 @@ public function testExpire(): void
266254
$collection->expire('expirekey', 60);
267255
});
268256

269-
$index = $this->getIndex('newcollection', 'expirekey');
257+
$index = $this->assertIndexExists('newcollection', 'expirekey_1');
270258
$this->assertEquals(60, $index['expireAfterSeconds']);
271259
}
272260

@@ -280,7 +268,7 @@ public function testSoftDeletes(): void
280268
$collection->string('email')->nullable()->index();
281269
});
282270

283-
$index = $this->getIndex('newcollection', 'email');
271+
$index = $this->assertIndexExists('newcollection', 'email_1');
284272
$this->assertEquals(1, $index['key']['email']);
285273
}
286274

@@ -292,10 +280,10 @@ public function testFluent(): void
292280
$collection->timestamp('created_at');
293281
});
294282

295-
$index = $this->getIndex('newcollection', 'email');
283+
$index = $this->assertIndexExists('newcollection', 'email_1');
296284
$this->assertEquals(1, $index['key']['email']);
297285

298-
$index = $this->getIndex('newcollection', 'token');
286+
$index = $this->assertIndexExists('newcollection', 'token_1');
299287
$this->assertEquals(1, $index['key']['token']);
300288
}
301289

@@ -307,13 +295,13 @@ public function testGeospatial(): void
307295
$collection->geospatial('continent', '2dsphere');
308296
});
309297

310-
$index = $this->getIndex('newcollection', 'point');
298+
$index = $this->assertIndexExists('newcollection', 'point_2d');
311299
$this->assertEquals('2d', $index['key']['point']);
312300

313-
$index = $this->getIndex('newcollection', 'area');
301+
$index = $this->assertIndexExists('newcollection', 'area_2d');
314302
$this->assertEquals('2d', $index['key']['area']);
315303

316-
$index = $this->getIndex('newcollection', 'continent');
304+
$index = $this->assertIndexExists('newcollection', 'continent_2dsphere');
317305
$this->assertEquals('2dsphere', $index['key']['continent']);
318306
}
319307

@@ -332,7 +320,7 @@ public function testSparseUnique(): void
332320
$collection->sparse_and_unique('sparseuniquekey');
333321
});
334322

335-
$index = $this->getIndex('newcollection', 'sparseuniquekey');
323+
$index = $this->assertIndexExists('newcollection', 'sparseuniquekey_1');
336324
$this->assertEquals(1, $index['sparse']);
337325
$this->assertEquals(1, $index['unique']);
338326
}
@@ -573,23 +561,39 @@ public function testVectorSearchIndex()
573561
self::assertSame('vector', $index['latestDefinition']['fields'][0]['type']);
574562
}
575563

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
577574
{
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);
579583
assert($collection instanceof Collection);
580584

581585
foreach ($collection->listIndexes() as $index) {
582-
if (isset($index['key'][$name])) {
586+
if ($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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /