SchemaTest.php 10.8 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 37 38
    {
        Schema::create('newcollection_two', null, ['capped' => true, 'size' => 1024]);
        $this->assertTrue(Schema::hasCollection('newcollection_two'));
        $this->assertTrue(Schema::hasTable('newcollection_two'));
    }

Simon Schaufelberger's avatar
Simon Schaufelberger committed
39
    public function testDrop(): void
Jens Segers's avatar
Jens Segers committed
40 41 42 43 44 45
    {
        Schema::create('newcollection');
        Schema::drop('newcollection');
        $this->assertFalse(Schema::hasCollection('newcollection'));
    }

Simon Schaufelberger's avatar
Simon Schaufelberger committed
46
    public function testBluePrint(): void
Jens Segers's avatar
Jens Segers committed
47 48 49
    {
        $instance = $this;

Jens Segers's avatar
Jens Segers committed
50
        Schema::collection('newcollection', function ($collection) use ($instance) {
Jens Segers's avatar
Jens Segers committed
51
            $instance->assertInstanceOf(Blueprint::class, $collection);
Jens Segers's avatar
Jens Segers committed
52 53
        });

Jens Segers's avatar
Jens Segers committed
54
        Schema::table('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 58
        });
    }

Simon Schaufelberger's avatar
Simon Schaufelberger committed
59
    public function testIndex(): void
Jens Segers's avatar
Jens Segers committed
60
    {
Jens Segers's avatar
Jens Segers committed
61
        Schema::collection('newcollection', function ($collection) {
62
            $collection->index('mykey1');
Jens Segers's avatar
Jens Segers committed
63 64
        });

65 66 67
        $index = $this->getIndex('newcollection', 'mykey1');
        $this->assertEquals(1, $index['key']['mykey1']);

Jens Segers's avatar
Jens Segers committed
68
        Schema::collection('newcollection', function ($collection) {
69 70 71 72 73 74
            $collection->index(['mykey2']);
        });

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

Jens Segers's avatar
Jens Segers committed
75
        Schema::collection('newcollection', function ($collection) {
76 77
            $collection->string('mykey3')->index();
        });
Jens Segers's avatar
Jens Segers committed
78

79 80 81 82
        $index = $this->getIndex('newcollection', 'mykey3');
        $this->assertEquals(1, $index['key']['mykey3']);
    }

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

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

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

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

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

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

Jens Segers's avatar
Jens Segers committed
113
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
114 115 116 117 118 119
            $collection->unique('uniquekey');
            $collection->dropIndex(['uniquekey']);
        });

        $index = $this->getIndex('newcollection', 'uniquekey');
        $this->assertEquals(null, $index);
Mike Nichols's avatar
Mike Nichols committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

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

        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
148 149
    }

150
    public function testDropIndexIfExists(): void
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 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
    {
        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);
    }

197
    public function testHasIndex(): void
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    {
        $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
220
    public function testBackground(): void
Jens Segers's avatar
Jens Segers committed
221
    {
Jens Segers's avatar
Jens Segers committed
222
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
223 224 225 226 227 228 229
            $collection->background('backgroundkey');
        });

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

Simon Schaufelberger's avatar
Simon Schaufelberger committed
230
    public function testSparse(): void
Jens Segers's avatar
Jens Segers committed
231
    {
Jens Segers's avatar
Jens Segers committed
232
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
233 234 235 236 237 238 239
            $collection->sparse('sparsekey');
        });

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

Simon Schaufelberger's avatar
Simon Schaufelberger committed
240
    public function testExpire(): void
Jens Segers's avatar
Jens Segers committed
241
    {
Jens Segers's avatar
Jens Segers committed
242
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
243 244 245 246 247 248 249
            $collection->expire('expirekey', 60);
        });

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

Simon Schaufelberger's avatar
Simon Schaufelberger committed
250
    public function testSoftDeletes(): void
Jens Segers's avatar
Jens Segers committed
251
    {
Jens Segers's avatar
Jens Segers committed
252
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
253 254 255
            $collection->softDeletes();
        });

Jens Segers's avatar
Jens Segers committed
256
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
257 258 259 260 261 262 263
            $collection->string('email')->nullable()->index();
        });

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

Simon Schaufelberger's avatar
Simon Schaufelberger committed
264
    public function testFluent(): void
Jens Segers's avatar
Jens Segers committed
265
    {
Jens Segers's avatar
Jens Segers committed
266
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
267 268 269 270 271 272 273 274 275 276 277 278
            $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
279
    public function testGeospatial(): void
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    {
        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
297
    public function testDummies(): void
Jens Segers's avatar
Jens Segers committed
298
    {
Jens Segers's avatar
Jens Segers committed
299
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
300 301 302
            $collection->boolean('activated')->default(0);
            $collection->integer('user_id')->unsigned();
        });
303
        $this->expectNotToPerformAssertions();
Jens Segers's avatar
Jens Segers committed
304 305
    }

Simon Schaufelberger's avatar
Simon Schaufelberger committed
306
    public function testSparseUnique(): void
307 308
    {
        Schema::collection('newcollection', function ($collection) {
309
            $collection->sparse_and_unique('sparseuniquekey');
310 311 312 313 314 315 316
        });

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

Simon Schaufelberger's avatar
Simon Schaufelberger committed
317
    protected function getIndex(string $collection, string $name)
Jens Segers's avatar
Jens Segers committed
318 319 320
    {
        $collection = DB::getCollection($collection);

Jens Segers's avatar
Jens Segers committed
321 322 323 324
        foreach ($collection->listIndexes() as $index) {
            if (isset($index['key'][$name])) {
                return $index;
            }
Jens Segers's avatar
Jens Segers committed
325 326 327 328
        }

        return false;
    }
329
}