File: DatabaseSchemaBuilderTest.php

package info (click to toggle)
php-laravel-framework 11.44.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 22,184 kB
  • sloc: php: 265,914; sh: 167; javascript: 51; makefile: 46
file content (140 lines) | stat: -rw-r--r-- 4,780 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
137
138
139
140
<?php

namespace Illuminate\Tests\Integration\Database\Sqlite;

use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Orchestra\Testbench\Attributes\RequiresDatabase;
use Orchestra\Testbench\TestCase;

#[RequiresDatabase('sqlite')]
class DatabaseSchemaBuilderTest extends TestCase
{
    protected function defineEnvironment($app)
    {
        $app['config']->set([
            'database.connections.sqlite-with-prefix' => [
                'driver' => 'sqlite',
                'database' => ':memory:',
                'prefix' => 'example_',
                'prefix_indexes' => false,
            ],
            'database.connections.sqlite-with-indexed-prefix' => [
                'driver' => 'sqlite',
                'database' => ':memory:',
                'prefix' => 'example_',
                'prefix_indexes' => true,
            ],
        ]);
    }

    public function testDropAllTablesWorksWithForeignKeys()
    {
        Schema::create('table1', function (Blueprint $table) {
            $table->integer('id');
            $table->string('name');
        });

        Schema::create('table2', function (Blueprint $table) {
            $table->integer('id');
            $table->string('user_id');
            $table->foreign('user_id')->references('id')->on('table1');
        });

        $this->assertTrue(Schema::hasTable('table1'));
        $this->assertTrue(Schema::hasTable('table2'));

        Schema::dropAllTables();

        $this->assertFalse(Schema::hasTable('table1'));
        $this->assertFalse(Schema::hasTable('table2'));
    }

    public function testHasColumnAndIndexWithPrefixIndexDisabled()
    {
        $connection = DB::connection('sqlite-with-prefix');

        Schema::connection('sqlite-with-prefix')->create('table1', function (Blueprint $table) {
            $table->integer('id');
            $table->string('name')->index();
        });

        $indexes = array_column($connection->getSchemaBuilder()->getIndexes('table1'), 'name');

        $this->assertContains('table1_name_index', $indexes, 'name');
    }

    public function testHasColumnAndIndexWithPrefixIndexEnabled()
    {
        $connection = DB::connection('sqlite-with-indexed-prefix');

        Schema::connection('sqlite-with-indexed-prefix')->create('table1', function (Blueprint $table) {
            $table->integer('id');
            $table->string('name')->index();
        });

        $indexes = array_column($connection->getSchemaBuilder()->getIndexes('table1'), 'name');

        $this->assertContains('example_table1_name_index', $indexes);
    }

    public function testAlterTableAddForeignKeyWithPrefix()
    {
        $schema = Schema::connection('sqlite-with-prefix');

        $schema->create('table1', function (Blueprint $table) {
            $table->id();
        });

        $schema->create('table2', function (Blueprint $table) {
            $table->id();
            $table->foreignId('author_id')->constrained('table1');
        });

        $schema->table('table2', function (Blueprint $table) {
            $table->foreignId('moderator_id')->constrained('table1');
        });

        $foreignKeys = collect($schema->getForeignKeys('table2'));

        $this->assertTrue($foreignKeys->contains(
            fn ($fk) => $fk['foreign_table'] === 'example_table1' &&
                $fk['foreign_columns'] === ['id'] &&
                $fk['columns'] === ['author_id'])
        );

        $this->assertTrue($foreignKeys->contains(
            fn ($fk) => $fk['foreign_table'] === 'example_table1' &&
                $fk['foreign_columns'] === ['id'] &&
                $fk['columns'] === ['moderator_id'])
        );
    }

    public function testAlterTableAddForeignKeyWithExpressionDefault()
    {
        Schema::create('items', function (Blueprint $table) {
            $table->id();
            $table->json('flags')->default(new Expression('(JSON_ARRAY())'));
        });

        Schema::table('items', function (Blueprint $table) {
            $table->foreignId('item_id')->nullable()->constrained('items');
        });

        $this->assertTrue(collect(Schema::getForeignKeys('items'))->contains(
            fn ($fk) => $fk['foreign_table'] === 'items' &&
                $fk['foreign_columns'] === ['id'] &&
                $fk['columns'] === ['item_id']
        ));

        $columns = Schema::getColumns('items');

        $this->assertTrue(collect($columns)->contains(
            fn ($column) => $column['name'] === 'flags' && $column['default'] === 'JSON_ARRAY()'
        ));

        $this->assertTrue(collect($columns)->contains(fn ($column) => $column['name'] === 'item_id' && $column['nullable']));
    }
}