SchemaTest.php 7.58 KB
Newer Older
1 2
<?php

Jens Segers's avatar
Jens Segers committed
3 4
class SchemaTest extends TestCase
{
Jens Segers's avatar
Jens Segers committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
    public function tearDown()
    {
        Schema::drop('newcollection');
    }

    public function testCreate()
    {
        Schema::create('newcollection');
        $this->assertTrue(Schema::hasCollection('newcollection'));
        $this->assertTrue(Schema::hasTable('newcollection'));
    }

    public function testCreateWithCallback()
    {
        $instance = $this;

Jens Segers's avatar
Jens Segers committed
21
        Schema::create('newcollection', function ($collection) use ($instance) {
Jens Segers's avatar
Jens Segers committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
            $instance->assertInstanceOf('Jenssegers\Mongodb\Schema\Blueprint', $collection);
        });

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

    public function testDrop()
    {
        Schema::create('newcollection');
        Schema::drop('newcollection');
        $this->assertFalse(Schema::hasCollection('newcollection'));
    }

    public function testBluePrint()
    {
        $instance = $this;

Jens Segers's avatar
Jens Segers committed
39
        Schema::collection('newcollection', function ($collection) use ($instance) {
Jens Segers's avatar
Jens Segers committed
40 41 42
            $instance->assertInstanceOf('Jenssegers\Mongodb\Schema\Blueprint', $collection);
        });

Jens Segers's avatar
Jens Segers committed
43
        Schema::table('newcollection', function ($collection) use ($instance) {
Jens Segers's avatar
Jens Segers committed
44 45 46 47 48 49
            $instance->assertInstanceOf('Jenssegers\Mongodb\Schema\Blueprint', $collection);
        });
    }

    public function testIndex()
    {
Jens Segers's avatar
Jens Segers committed
50
        Schema::collection('newcollection', function ($collection) {
51
            $collection->index('mykey1');
Jens Segers's avatar
Jens Segers committed
52 53
        });

54 55 56
        $index = $this->getIndex('newcollection', 'mykey1');
        $this->assertEquals(1, $index['key']['mykey1']);

Jens Segers's avatar
Jens Segers committed
57
        Schema::collection('newcollection', function ($collection) {
58 59 60 61 62 63
            $collection->index(['mykey2']);
        });

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

Jens Segers's avatar
Jens Segers committed
64
        Schema::collection('newcollection', function ($collection) {
65 66
            $collection->string('mykey3')->index();
        });
Jens Segers's avatar
Jens Segers committed
67

68 69 70 71 72 73
        $index = $this->getIndex('newcollection', 'mykey3');
        $this->assertEquals(1, $index['key']['mykey3']);
    }

    public function testPrimary()
    {
Jens Segers's avatar
Jens Segers committed
74
        Schema::collection('newcollection', function ($collection) {
75
            $collection->string('mykey', 100)->primary();
Jens Segers's avatar
Jens Segers committed
76 77 78
        });

        $index = $this->getIndex('newcollection', 'mykey');
79
        $this->assertEquals(1, $index['unique']);
Jens Segers's avatar
Jens Segers committed
80 81 82 83
    }

    public function testUnique()
    {
Jens Segers's avatar
Jens Segers committed
84
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
85 86 87 88 89 90 91 92 93
            $collection->unique('uniquekey');
        });

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

    public function testDropIndex()
    {
Jens Segers's avatar
Jens Segers committed
94
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
95
            $collection->unique('uniquekey');
Mike Nichols's avatar
Mike Nichols committed
96
            $collection->dropIndex('uniquekey_1');
Jens Segers's avatar
Jens Segers committed
97 98 99 100 101
        });

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

Jens Segers's avatar
Jens Segers committed
102
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
103 104 105 106 107 108
            $collection->unique('uniquekey');
            $collection->dropIndex(['uniquekey']);
        });

        $index = $this->getIndex('newcollection', 'uniquekey');
        $this->assertEquals(null, $index);
Mike Nichols's avatar
Mike Nichols committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

        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
137 138 139 140
    }

    public function testBackground()
    {
Jens Segers's avatar
Jens Segers committed
141
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
142 143 144 145 146 147 148 149 150
            $collection->background('backgroundkey');
        });

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

    public function testSparse()
    {
Jens Segers's avatar
Jens Segers committed
151
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
152 153 154 155 156 157 158 159 160
            $collection->sparse('sparsekey');
        });

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

    public function testExpire()
    {
Jens Segers's avatar
Jens Segers committed
161
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
162 163 164 165 166 167 168 169 170
            $collection->expire('expirekey', 60);
        });

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

    public function testSoftDeletes()
    {
Jens Segers's avatar
Jens Segers committed
171
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
172 173 174
            $collection->softDeletes();
        });

Jens Segers's avatar
Jens Segers committed
175
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
176 177 178 179 180 181 182 183 184
            $collection->string('email')->nullable()->index();
        });

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

    public function testFluent()
    {
Jens Segers's avatar
Jens Segers committed
185
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
186 187 188 189 190 191 192 193 194 195 196 197
            $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']);
    }

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    public function testGeospatial()
    {
        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']);
    }

Jens Segers's avatar
Jens Segers committed
216 217
    public function testDummies()
    {
Jens Segers's avatar
Jens Segers committed
218
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
219 220 221 222 223
            $collection->boolean('activated')->default(0);
            $collection->integer('user_id')->unsigned();
        });
    }

224 225 226
    public function testSparseUnique()
    {
        Schema::collection('newcollection', function ($collection) {
227
            $collection->sparse_and_unique('sparseuniquekey');
228 229 230 231 232 233 234
        });

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

Jens Segers's avatar
Jens Segers committed
235 236 237 238
    protected function getIndex($collection, $name)
    {
        $collection = DB::getCollection($collection);

Jens Segers's avatar
Jens Segers committed
239 240 241 242
        foreach ($collection->listIndexes() as $index) {
            if (isset($index['key'][$name])) {
                return $index;
            }
Jens Segers's avatar
Jens Segers committed
243 244 245 246
        }

        return false;
    }
247
}