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
|
<?php
namespace Illuminate\Tests\Database;
use Illuminate\Container\Container;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Facade;
use PHPUnit\Framework\TestCase;
class DatabaseSchemaBuilderIntegrationTest extends TestCase
{
protected $db;
/**
* Bootstrap database.
*
* @return void
*/
protected function setUp(): void
{
$this->db = $db = new DB;
$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);
$db->setAsGlobal();
$container = new Container;
$container->instance('db', $db->getDatabaseManager());
Facade::setFacadeApplication($container);
}
protected function tearDown(): void
{
Facade::clearResolvedInstances();
Facade::setFacadeApplication(null);
}
public function testDropAllTablesWorksWithForeignKeys()
{
$this->db->connection()->getSchemaBuilder()->create('table1', function (Blueprint $table) {
$table->integer('id');
$table->string('name');
});
$this->db->connection()->getSchemaBuilder()->create('table2', function (Blueprint $table) {
$table->integer('id');
$table->string('user_id');
$table->foreign('user_id')->references('id')->on('table1');
});
$this->assertTrue($this->db->connection()->getSchemaBuilder()->hasTable('table1'));
$this->assertTrue($this->db->connection()->getSchemaBuilder()->hasTable('table2'));
$this->db->connection()->getSchemaBuilder()->dropAllTables();
$this->assertFalse($this->db->connection()->getSchemaBuilder()->hasTable('table1'));
$this->assertFalse($this->db->connection()->getSchemaBuilder()->hasTable('table2'));
}
public function testHasColumnWithTablePrefix()
{
$this->db->connection()->setTablePrefix('test_');
$this->db->connection()->getSchemaBuilder()->create('table1', function (Blueprint $table) {
$table->integer('id');
$table->string('name');
});
$this->assertTrue($this->db->connection()->getSchemaBuilder()->hasColumn('table1', 'name'));
}
public function testHasColumnAndIndexWithPrefixIndexDisabled()
{
$this->db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => 'example_',
'prefix_indexes' => false,
]);
$this->db->connection()->getSchemaBuilder()->create('table1', function (Blueprint $table) {
$table->integer('id');
$table->string('name')->index();
});
$this->assertTrue($this->db->connection()->getSchemaBuilder()->hasIndex('table1', 'table1_name_index'));
}
public function testHasColumnAndIndexWithPrefixIndexEnabled()
{
$this->db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => 'example_',
'prefix_indexes' => true,
]);
$this->db->connection()->getSchemaBuilder()->create('table1', function (Blueprint $table) {
$table->integer('id');
$table->string('name')->index();
});
$this->assertTrue($this->db->connection()->getSchemaBuilder()->hasIndex('table1', 'example_table1_name_index'));
}
public function testDropColumnWithTablePrefix()
{
$this->db->connection()->setTablePrefix('test_');
$this->schemaBuilder()->create('pandemic_table', function (Blueprint $table) {
$table->integer('id');
$table->string('stay_home');
$table->string('covid19');
$table->string('wear_mask');
});
// drop single columns
$this->assertTrue($this->schemaBuilder()->hasColumn('pandemic_table', 'stay_home'));
$this->schemaBuilder()->dropColumns('pandemic_table', 'stay_home');
$this->assertFalse($this->schemaBuilder()->hasColumn('pandemic_table', 'stay_home'));
// drop multiple columns
$this->assertTrue($this->schemaBuilder()->hasColumn('pandemic_table', 'covid19'));
$this->schemaBuilder()->dropColumns('pandemic_table', ['covid19', 'wear_mask']);
$this->assertFalse($this->schemaBuilder()->hasColumn('pandemic_table', 'wear_mask'));
$this->assertFalse($this->schemaBuilder()->hasColumn('pandemic_table', 'covid19'));
}
private function schemaBuilder()
{
return $this->db->connection()->getSchemaBuilder();
}
}
|