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

Jens Segers's avatar
Jens Segers committed
3 4
class SchemaTest extends TestCase
{
5

Jens Segers's avatar
Jens Segers committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
    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
22
        Schema::create('newcollection', function ($collection) use ($instance) {
Jens Segers's avatar
Jens Segers committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
            $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
40
        Schema::collection('newcollection', function ($collection) use ($instance) {
Jens Segers's avatar
Jens Segers committed
41 42 43
            $instance->assertInstanceOf('Jenssegers\Mongodb\Schema\Blueprint', $collection);
        });

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

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

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

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

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

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

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

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

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

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

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

    public function testDropIndex()
    {
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');
            $collection->dropIndex('uniquekey');
        });

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

Jens Segers's avatar
Jens Segers committed
103
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
104 105 106 107 108 109 110 111 112 113
            $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
114
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
115 116 117 118 119 120 121 122 123
            $collection->background('backgroundkey');
        });

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

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

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

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

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

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

Jens Segers's avatar
Jens Segers committed
148
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
149 150 151 152 153 154 155 156 157
            $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
158
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172
            $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
173
        Schema::collection('newcollection', function ($collection) {
Jens Segers's avatar
Jens Segers committed
174 175 176 177 178 179 180 181 182
            $collection->boolean('activated')->default(0);
            $collection->integer('user_id')->unsigned();
        });
    }

    protected function getIndex($collection, $name)
    {
        $collection = DB::getCollection($collection);

Jens Segers's avatar
Jens Segers committed
183 184 185 186
        foreach ($collection->listIndexes() as $index) {
            if (isset($index['key'][$name])) {
                return $index;
            }
Jens Segers's avatar
Jens Segers committed
187 188 189 190
        }

        return false;
    }
191
}