SchemaTest.php 11.5 KB
Newer Older
1
<?php
Simon Schaufelberger's avatar
Simon Schaufelberger committed
2
declare(strict_types=1);
3

Jens Segers's avatar
Jens Segers committed
4 5
use Jenssegers\Mongodb\Schema\Blueprint;

Jens Segers's avatar
Jens Segers committed
6 7
class SchemaTest extends TestCase
{
Stas's avatar
Stas committed
8
    public function tearDown(): void
Jens Segers's avatar
Jens Segers committed
9 10
    {
        Schema::drop('newcollection');
11
        Schema::drop('newcollection_two');
Jens Segers's avatar
Jens Segers committed
12 13
    }

Simon Schaufelberger's avatar
Simon Schaufelberger committed
14
    public function testCreate(): void
Jens Segers's avatar
Jens Segers committed
15 16 17 18 19 20
    {
        Schema::create('newcollection');
        $this->assertTrue(Schema::hasCollection('newcollection'));
        $this->assertTrue(Schema::hasTable('newcollection'));
    }

Simon Schaufelberger's avatar
Simon Schaufelberger committed
21
    public function testCreateWithCallback(): void
Jens Segers's avatar
Jens Segers committed
22 23 24
    {
        $instance = $this;

Jens Segers's avatar
Jens Segers committed
25
        Schema::create('newcollection', function ($collection) use ($instance) {
Jens Segers's avatar
Jens Segers committed
26
            $instance->assertInstanceOf(Blueprint::class, $collection);
Jens Segers's avatar
Jens Segers committed
27 28 29 30 31
        });

        $this->assertTrue(Schema::hasCollection('newcollection'));
    }

Simon Schaufelberger's avatar
Simon Schaufelberger committed
32
    public function testCreateWithOptions(): void
33 34 35 36
    {
        Schema::create('newcollection_two', null, ['capped' => true, 'size' => 1024]);
        $this->assertTrue(Schema::hasCollection('newcollection_two'));
        $this->assertTrue(Schema::hasTable('newcollection_two'));
Ditty's avatar
Ditty committed
37

38
        $collection = Schema::getCollection('newcollection_two');
Ditty's avatar
Ditty committed
39 40
        $this->assertTrue($collection['options']['capped']);
        $this->assertEquals(1024, $collection['options']['size']);
41 42
    }

Simon Schaufelberger's avatar
Simon Schaufelberger committed
43
    public function testDrop(): void
Jens Segers's avatar
Jens Segers committed
44 45 46 47 48 49
    {
        Schema::create('newcollection');
        Schema::drop('newcollection');
        $this->assertFalse(Schema::hasCollection('newcollection'));
    }

Simon Schaufelberger's avatar
Simon Schaufelberger committed
50
    public function testBluePrint(): void
Jens Segers's avatar
Jens Segers committed
51 52 53
    {
        $instance = $this;

Jens Segers's avatar
Jens Segers committed
54
        Schema::collection('newcollection', function ($collection) use ($instance) {
Jens Segers's avatar
Jens Segers committed
55
            $instance->assertInstanceOf(Blueprint::class, $collection);
Jens Segers's avatar
Jens Segers committed
56 57
        });

Jens Segers's avatar
Jens Segers committed
58
        Schema::table('newcollection', function ($collection) use ($instance) {
Jens Segers's avatar
Jens Segers committed
59
            $instance->assertInstanceOf(Blueprint::class, $collection);
Jens Segers's avatar
Jens Segers committed
60 61 62
        });
    }

Simon Schaufelberger's avatar
Simon Schaufelberger committed
63
    public function testIndex(): void
Jens Segers's avatar
Jens Segers committed
64
    {
Jens Segers's avatar
Jens Segers committed
65
        Schema::collection('newcollection', function ($collection) {
66
            $collection->index('mykey1');
Jens Segers's avatar
Jens Segers committed
67 68
        });

69 70 71
        $index = $this->getIndex('newcollection', 'mykey1');
        $this->assertEquals(1, $index['key']['mykey1']);

Jens Segers's avatar
Jens Segers committed
72
        Schema::collection('newcollection', function ($collection) {
73 74 75 76 77 78
            $collection->index(['mykey2']);
        });

        $index = $this->getIndex('newcollection', 'mykey2');
        $this->assertEquals(1, $index['key']['mykey2']);

Jens Segers's avatar
Jens Segers committed
79
        Schema::collection('newcollection', function ($collection) {
80 81
            $collection->string('mykey3')->index();
        });
Jens Segers's avatar
Jens Segers committed
82

83 84 85 86
        $index = $this->getIndex('newcollection', 'mykey3');
        $this->assertEquals(1, $index['key']['mykey3']);
    }

Simon Schaufelberger's avatar
Simon Schaufelberger committed
87
    public function testPrimary(): void
88
    {
Jens Segers's avatar
Jens Segers committed
89
        Schema::collection('newcollection', function ($collection) {
90
            $collection->string('mykey', 100)->primary();
Jens Segers's avatar
Jens Segers committed
91 92 93
        });

        $index = $this->getIndex('newcollection', 'mykey');
94
        $this->assertEquals(1, $index['unique']);
Jens Segers's avatar
Jens Segers committed
95 96
    }

Simon Schaufelberger's avatar
Simon Schaufelberger committed
97
    public function testUnique(): void
Jens Segers's avatar
Jens Segers committed
98
    {
Jens Segers's avatar
Jens Segers committed
99
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
100 101 102 103 104 105 106
            $collection->unique('uniquekey');
        });

        $index = $this->getIndex('newcollection', 'uniquekey');
        $this->assertEquals(1, $index['unique']);
    }

Simon Schaufelberger's avatar
Simon Schaufelberger committed
107
    public function testDropIndex(): void
Jens Segers's avatar
Jens Segers committed
108
    {
Jens Segers's avatar
Jens Segers committed
109
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
110
            $collection->unique('uniquekey');
Mike Nichols's avatar
Mike Nichols committed
111
            $collection->dropIndex('uniquekey_1');
Jens Segers's avatar
Jens Segers committed
112 113 114 115 116
        });

        $index = $this->getIndex('newcollection', 'uniquekey');
        $this->assertEquals(null, $index);

Jens Segers's avatar
Jens Segers committed
117
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
118 119 120 121 122 123
            $collection->unique('uniquekey');
            $collection->dropIndex(['uniquekey']);
        });

        $index = $this->getIndex('newcollection', 'uniquekey');
        $this->assertEquals(null, $index);
Mike Nichols's avatar
Mike Nichols committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

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

        $index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
        $this->assertNotNull($index);

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

        $index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
        $this->assertFalse($index);

139 140 141 142 143 144 145 146 147 148 149 150 151 152
        Schema::collection('newcollection', function ($collection) {
            $collection->index(['field_a' => -1, 'field_b' => 1]);
        });

        $index = $this->getIndex('newcollection', 'field_a_-1_field_b_1');
        $this->assertNotNull($index);

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

        $index = $this->getIndex('newcollection', 'field_a_-1_field_b_1');
        $this->assertFalse($index);

Mike Nichols's avatar
Mike Nichols committed
153 154 155 156 157 158 159 160 161 162 163 164 165
        Schema::collection('newcollection', function ($collection) {
            $collection->index(['field_a', 'field_b'], 'custom_index_name');
        });

        $index = $this->getIndex('newcollection', 'custom_index_name');
        $this->assertNotNull($index);

        Schema::collection('newcollection', function ($collection) {
            $collection->dropIndex('custom_index_name');
        });

        $index = $this->getIndex('newcollection', 'custom_index_name');
        $this->assertFalse($index);
Jens Segers's avatar
Jens Segers committed
166 167
    }

168
    public function testDropIndexIfExists(): void
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    {
        Schema::collection('newcollection', function (Blueprint $collection) {
            $collection->unique('uniquekey');
            $collection->dropIndexIfExists('uniquekey_1');
        });

        $index = $this->getIndex('newcollection', 'uniquekey');
        $this->assertEquals(null, $index);

        Schema::collection('newcollection', function (Blueprint $collection) {
            $collection->unique('uniquekey');
            $collection->dropIndexIfExists(['uniquekey']);
        });

        $index = $this->getIndex('newcollection', 'uniquekey');
        $this->assertEquals(null, $index);

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

        $index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
        $this->assertNotNull($index);

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

        $index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
        $this->assertFalse($index);

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

        $index = $this->getIndex('newcollection', 'custom_index_name');
        $this->assertNotNull($index);

        Schema::collection('newcollection', function (Blueprint $collection) {
            $collection->dropIndexIfExists('custom_index_name');
        });

        $index = $this->getIndex('newcollection', 'custom_index_name');
        $this->assertFalse($index);
    }

215
    public function testHasIndex(): void
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    {
        $instance = $this;

        Schema::collection('newcollection', function (Blueprint $collection) use ($instance) {
            $collection->index('myhaskey1');
            $instance->assertTrue($collection->hasIndex('myhaskey1_1'));
            $instance->assertFalse($collection->hasIndex('myhaskey1'));
        });

        Schema::collection('newcollection', function (Blueprint $collection) use ($instance) {
            $collection->index('myhaskey2');
            $instance->assertTrue($collection->hasIndex(['myhaskey2']));
            $instance->assertFalse($collection->hasIndex(['myhaskey2_1']));
        });

        Schema::collection('newcollection', function (Blueprint $collection) use ($instance) {
            $collection->index(['field_a', 'field_b']);
            $instance->assertTrue($collection->hasIndex(['field_a_1_field_b']));
            $instance->assertFalse($collection->hasIndex(['field_a_1_field_b_1']));
        });
    }

Simon Schaufelberger's avatar
Simon Schaufelberger committed
238
    public function testBackground(): void
Jens Segers's avatar
Jens Segers committed
239
    {
Jens Segers's avatar
Jens Segers committed
240
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
241 242 243 244 245 246 247
            $collection->background('backgroundkey');
        });

        $index = $this->getIndex('newcollection', 'backgroundkey');
        $this->assertEquals(1, $index['background']);
    }

Simon Schaufelberger's avatar
Simon Schaufelberger committed
248
    public function testSparse(): void
Jens Segers's avatar
Jens Segers committed
249
    {
Jens Segers's avatar
Jens Segers committed
250
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
251 252 253 254 255 256 257
            $collection->sparse('sparsekey');
        });

        $index = $this->getIndex('newcollection', 'sparsekey');
        $this->assertEquals(1, $index['sparse']);
    }

Simon Schaufelberger's avatar
Simon Schaufelberger committed
258
    public function testExpire(): void
Jens Segers's avatar
Jens Segers committed
259
    {
Jens Segers's avatar
Jens Segers committed
260
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
261 262 263 264 265 266 267
            $collection->expire('expirekey', 60);
        });

        $index = $this->getIndex('newcollection', 'expirekey');
        $this->assertEquals(60, $index['expireAfterSeconds']);
    }

Simon Schaufelberger's avatar
Simon Schaufelberger committed
268
    public function testSoftDeletes(): void
Jens Segers's avatar
Jens Segers committed
269
    {
Jens Segers's avatar
Jens Segers committed
270
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
271 272 273
            $collection->softDeletes();
        });

Jens Segers's avatar
Jens Segers committed
274
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
275 276 277 278 279 280 281
            $collection->string('email')->nullable()->index();
        });

        $index = $this->getIndex('newcollection', 'email');
        $this->assertEquals(1, $index['key']['email']);
    }

Simon Schaufelberger's avatar
Simon Schaufelberger committed
282
    public function testFluent(): void
Jens Segers's avatar
Jens Segers committed
283
    {
Jens Segers's avatar
Jens Segers committed
284
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
285 286 287 288 289 290 291 292 293 294 295 296
            $collection->string('email')->index();
            $collection->string('token')->index();
            $collection->timestamp('created_at');
        });

        $index = $this->getIndex('newcollection', 'email');
        $this->assertEquals(1, $index['key']['email']);

        $index = $this->getIndex('newcollection', 'token');
        $this->assertEquals(1, $index['key']['token']);
    }

Simon Schaufelberger's avatar
Simon Schaufelberger committed
297
    public function testGeospatial(): void
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
    {
        Schema::collection('newcollection', function ($collection) {
            $collection->geospatial('point');
            $collection->geospatial('area', '2d');
            $collection->geospatial('continent', '2dsphere');
        });

        $index = $this->getIndex('newcollection', 'point');
        $this->assertEquals('2d', $index['key']['point']);

        $index = $this->getIndex('newcollection', 'area');
        $this->assertEquals('2d', $index['key']['area']);

        $index = $this->getIndex('newcollection', 'continent');
        $this->assertEquals('2dsphere', $index['key']['continent']);
    }

Simon Schaufelberger's avatar
Simon Schaufelberger committed
315
    public function testDummies(): void
Jens Segers's avatar
Jens Segers committed
316
    {
Jens Segers's avatar
Jens Segers committed
317
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
318 319 320
            $collection->boolean('activated')->default(0);
            $collection->integer('user_id')->unsigned();
        });
321
        $this->expectNotToPerformAssertions();
Jens Segers's avatar
Jens Segers committed
322 323
    }

Simon Schaufelberger's avatar
Simon Schaufelberger committed
324
    public function testSparseUnique(): void
325 326
    {
        Schema::collection('newcollection', function ($collection) {
327
            $collection->sparse_and_unique('sparseuniquekey');
328 329 330 331 332 333 334
        });

        $index = $this->getIndex('newcollection', 'sparseuniquekey');
        $this->assertEquals(1, $index['sparse']);
        $this->assertEquals(1, $index['unique']);
    }

Simon Schaufelberger's avatar
Simon Schaufelberger committed
335
    protected function getIndex(string $collection, string $name)
Jens Segers's avatar
Jens Segers committed
336 337 338
    {
        $collection = DB::getCollection($collection);

Jens Segers's avatar
Jens Segers committed
339 340 341 342
        foreach ($collection->listIndexes() as $index) {
            if (isset($index['key'][$name])) {
                return $index;
            }
Jens Segers's avatar
Jens Segers committed
343 344 345 346
        }

        return false;
    }
347
}