SchemaTest.php 5.94 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 96 97 98 99 100 101
            $collection->unique('uniquekey');
            $collection->dropIndex('uniquekey');
        });

        $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 109 110 111 112
            $collection->unique('uniquekey');
            $collection->dropIndex(['uniquekey']);
        });

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

    public function testBackground()
    {
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 120 121 122
            $collection->background('backgroundkey');
        });

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

    public function testSparse()
    {
Jens Segers's avatar
Jens Segers committed
123
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
124 125 126 127 128 129 130 131 132
            $collection->sparse('sparsekey');
        });

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

    public function testExpire()
    {
Jens Segers's avatar
Jens Segers committed
133
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
134 135 136 137 138 139 140 141 142
            $collection->expire('expirekey', 60);
        });

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

    public function testSoftDeletes()
    {
Jens Segers's avatar
Jens Segers committed
143
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
144 145 146
            $collection->softDeletes();
        });

Jens Segers's avatar
Jens Segers committed
147
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
148 149 150 151 152 153 154 155 156
            $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
157
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171
            $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']);
    }

    public function testDummies()
    {
Jens Segers's avatar
Jens Segers committed
172
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
173 174 175 176 177
            $collection->boolean('activated')->default(0);
            $collection->integer('user_id')->unsigned();
        });
    }

178 179 180
    public function testSparseUnique()
    {
        Schema::collection('newcollection', function ($collection) {
181
            $collection->sparse_and_unique('sparseuniquekey');
182 183 184 185 186 187 188
        });

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

Jens Segers's avatar
Jens Segers committed
189 190 191 192
    protected function getIndex($collection, $name)
    {
        $collection = DB::getCollection($collection);

Jens Segers's avatar
Jens Segers committed
193 194 195 196
        foreach ($collection->listIndexes() as $index) {
            if (isset($index['key'][$name])) {
                return $index;
            }
Jens Segers's avatar
Jens Segers committed
197 198 199 200
        }

        return false;
    }
201
}