|
1 | 1 | <?php
|
2 | 2 |
|
| 3 | +use Jenssegers\Mongodb\Schema\Blueprint; |
| 4 | + |
3 | 5 | class SchemaTest extends TestCase
|
4 | 6 | {
|
5 | 7 | public function tearDown(): void
|
@@ -144,6 +146,76 @@ public function testDropIndex()
|
144 | 146 | $this->assertFalse($index);
|
145 | 147 | }
|
146 | 148 |
|
| 149 | + public function testDropIndexIfExists() |
| 150 | + { |
| 151 | + Schema::collection('newcollection', function (Blueprint $collection) { |
| 152 | + $collection->unique('uniquekey'); |
| 153 | + $collection->dropIndexIfExists('uniquekey_1'); |
| 154 | + }); |
| 155 | + |
| 156 | + $index = $this->getIndex('newcollection', 'uniquekey'); |
| 157 | + $this->assertEquals(null, $index); |
| 158 | + |
| 159 | + Schema::collection('newcollection', function (Blueprint $collection) { |
| 160 | + $collection->unique('uniquekey'); |
| 161 | + $collection->dropIndexIfExists(['uniquekey']); |
| 162 | + }); |
| 163 | + |
| 164 | + $index = $this->getIndex('newcollection', 'uniquekey'); |
| 165 | + $this->assertEquals(null, $index); |
| 166 | + |
| 167 | + Schema::collection('newcollection', function (Blueprint $collection) { |
| 168 | + $collection->index(['field_a', 'field_b']); |
| 169 | + }); |
| 170 | + |
| 171 | + $index = $this->getIndex('newcollection', 'field_a_1_field_b_1'); |
| 172 | + $this->assertNotNull($index); |
| 173 | + |
| 174 | + Schema::collection('newcollection', function (Blueprint $collection) { |
| 175 | + $collection->dropIndexIfExists(['field_a', 'field_b']); |
| 176 | + }); |
| 177 | + |
| 178 | + $index = $this->getIndex('newcollection', 'field_a_1_field_b_1'); |
| 179 | + $this->assertFalse($index); |
| 180 | + |
| 181 | + Schema::collection('newcollection', function (Blueprint $collection) { |
| 182 | + $collection->index(['field_a', 'field_b'], 'custom_index_name'); |
| 183 | + }); |
| 184 | + |
| 185 | + $index = $this->getIndex('newcollection', 'custom_index_name'); |
| 186 | + $this->assertNotNull($index); |
| 187 | + |
| 188 | + Schema::collection('newcollection', function (Blueprint $collection) { |
| 189 | + $collection->dropIndexIfExists('custom_index_name'); |
| 190 | + }); |
| 191 | + |
| 192 | + $index = $this->getIndex('newcollection', 'custom_index_name'); |
| 193 | + $this->assertFalse($index); |
| 194 | + } |
| 195 | + |
| 196 | + public function testHasIndex() |
| 197 | + { |
| 198 | + $instance = $this; |
| 199 | + |
| 200 | + Schema::collection('newcollection', function (Blueprint $collection) use ($instance) { |
| 201 | + $collection->index('myhaskey1'); |
| 202 | + $instance->assertTrue($collection->hasIndex('myhaskey1_1')); |
| 203 | + $instance->assertFalse($collection->hasIndex('myhaskey1')); |
| 204 | + }); |
| 205 | + |
| 206 | + Schema::collection('newcollection', function (Blueprint $collection) use ($instance) { |
| 207 | + $collection->index('myhaskey2'); |
| 208 | + $instance->assertTrue($collection->hasIndex(['myhaskey2'])); |
| 209 | + $instance->assertFalse($collection->hasIndex(['myhaskey2_1'])); |
| 210 | + }); |
| 211 | + |
| 212 | + Schema::collection('newcollection', function (Blueprint $collection) use ($instance) { |
| 213 | + $collection->index(['field_a', 'field_b']); |
| 214 | + $instance->assertTrue($collection->hasIndex(['field_a_1_field_b'])); |
| 215 | + $instance->assertFalse($collection->hasIndex(['field_a_1_field_b_1'])); |
| 216 | + }); |
| 217 | + } |
| 218 | + |
147 | 219 | public function testBackground()
|
148 | 220 | {
|
149 | 221 | Schema::collection('newcollection', function ($collection) {
|
|
0 commit comments