123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- <?php
- namespace Illuminate\Tests\Database;
- use Illuminate\Database\Connection;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Database\Schema\Builder;
- use Illuminate\Database\Schema\Grammars\MySqlGrammar;
- use Illuminate\Database\Schema\Grammars\PostgresGrammar;
- use Illuminate\Database\Schema\Grammars\SQLiteGrammar;
- use Illuminate\Database\Schema\Grammars\SqlServerGrammar;
- use Mockery as m;
- use PHPUnit\Framework\TestCase;
- class DatabaseSchemaBlueprintTest extends TestCase
- {
- protected function tearDown(): void
- {
- m::close();
- Builder::$defaultMorphKeyType = 'int';
- }
- public function testToSqlRunsCommandsFromBlueprint()
- {
- $conn = m::mock(Connection::class);
- $conn->shouldReceive('statement')->once()->with('foo');
- $conn->shouldReceive('statement')->once()->with('bar');
- $grammar = m::mock(MySqlGrammar::class);
- $blueprint = $this->getMockBuilder(Blueprint::class)->onlyMethods(['toSql'])->setConstructorArgs(['users'])->getMock();
- $blueprint->expects($this->once())->method('toSql')->with($this->equalTo($conn), $this->equalTo($grammar))->willReturn(['foo', 'bar']);
- $blueprint->build($conn, $grammar);
- }
- public function testIndexDefaultNames()
- {
- $blueprint = new Blueprint('users');
- $blueprint->unique(['foo', 'bar']);
- $commands = $blueprint->getCommands();
- $this->assertSame('users_foo_bar_unique', $commands[0]->index);
- $blueprint = new Blueprint('users');
- $blueprint->index('foo');
- $commands = $blueprint->getCommands();
- $this->assertSame('users_foo_index', $commands[0]->index);
- $blueprint = new Blueprint('geo');
- $blueprint->spatialIndex('coordinates');
- $commands = $blueprint->getCommands();
- $this->assertSame('geo_coordinates_spatialindex', $commands[0]->index);
- }
- public function testIndexDefaultNamesWhenPrefixSupplied()
- {
- $blueprint = new Blueprint('users', null, 'prefix_');
- $blueprint->unique(['foo', 'bar']);
- $commands = $blueprint->getCommands();
- $this->assertSame('prefix_users_foo_bar_unique', $commands[0]->index);
- $blueprint = new Blueprint('users', null, 'prefix_');
- $blueprint->index('foo');
- $commands = $blueprint->getCommands();
- $this->assertSame('prefix_users_foo_index', $commands[0]->index);
- $blueprint = new Blueprint('geo', null, 'prefix_');
- $blueprint->spatialIndex('coordinates');
- $commands = $blueprint->getCommands();
- $this->assertSame('prefix_geo_coordinates_spatialindex', $commands[0]->index);
- }
- public function testDropIndexDefaultNames()
- {
- $blueprint = new Blueprint('users');
- $blueprint->dropUnique(['foo', 'bar']);
- $commands = $blueprint->getCommands();
- $this->assertSame('users_foo_bar_unique', $commands[0]->index);
- $blueprint = new Blueprint('users');
- $blueprint->dropIndex(['foo']);
- $commands = $blueprint->getCommands();
- $this->assertSame('users_foo_index', $commands[0]->index);
- $blueprint = new Blueprint('geo');
- $blueprint->dropSpatialIndex(['coordinates']);
- $commands = $blueprint->getCommands();
- $this->assertSame('geo_coordinates_spatialindex', $commands[0]->index);
- }
- public function testDropIndexDefaultNamesWhenPrefixSupplied()
- {
- $blueprint = new Blueprint('users', null, 'prefix_');
- $blueprint->dropUnique(['foo', 'bar']);
- $commands = $blueprint->getCommands();
- $this->assertSame('prefix_users_foo_bar_unique', $commands[0]->index);
- $blueprint = new Blueprint('users', null, 'prefix_');
- $blueprint->dropIndex(['foo']);
- $commands = $blueprint->getCommands();
- $this->assertSame('prefix_users_foo_index', $commands[0]->index);
- $blueprint = new Blueprint('geo', null, 'prefix_');
- $blueprint->dropSpatialIndex(['coordinates']);
- $commands = $blueprint->getCommands();
- $this->assertSame('prefix_geo_coordinates_spatialindex', $commands[0]->index);
- }
- public function testDefaultCurrentDateTime()
- {
- $base = new Blueprint('users', function ($table) {
- $table->dateTime('created')->useCurrent();
- });
- $connection = m::mock(Connection::class);
- $blueprint = clone $base;
- $this->assertEquals(['alter table `users` add `created` datetime default CURRENT_TIMESTAMP not null'], $blueprint->toSql($connection, new MySqlGrammar));
- $blueprint = clone $base;
- $this->assertEquals(['alter table "users" add column "created" timestamp(0) without time zone default CURRENT_TIMESTAMP not null'], $blueprint->toSql($connection, new PostgresGrammar));
- $blueprint = clone $base;
- $this->assertEquals(['alter table "users" add column "created" datetime default CURRENT_TIMESTAMP not null'], $blueprint->toSql($connection, new SQLiteGrammar));
- $blueprint = clone $base;
- $this->assertEquals(['alter table "users" add "created" datetime default CURRENT_TIMESTAMP not null'], $blueprint->toSql($connection, new SqlServerGrammar));
- }
- public function testDefaultCurrentTimestamp()
- {
- $base = new Blueprint('users', function ($table) {
- $table->timestamp('created')->useCurrent();
- });
- $connection = m::mock(Connection::class);
- $blueprint = clone $base;
- $this->assertEquals(['alter table `users` add `created` timestamp default CURRENT_TIMESTAMP not null'], $blueprint->toSql($connection, new MySqlGrammar));
- $blueprint = clone $base;
- $this->assertEquals(['alter table "users" add column "created" timestamp(0) without time zone default CURRENT_TIMESTAMP not null'], $blueprint->toSql($connection, new PostgresGrammar));
- $blueprint = clone $base;
- $this->assertEquals(['alter table "users" add column "created" datetime default CURRENT_TIMESTAMP not null'], $blueprint->toSql($connection, new SQLiteGrammar));
- $blueprint = clone $base;
- $this->assertEquals(['alter table "users" add "created" datetime default CURRENT_TIMESTAMP not null'], $blueprint->toSql($connection, new SqlServerGrammar));
- }
- public function testUnsignedDecimalTable()
- {
- $base = new Blueprint('users', function ($table) {
- $table->unsignedDecimal('money', 10, 2)->useCurrent();
- });
- $connection = m::mock(Connection::class);
- $blueprint = clone $base;
- $this->assertEquals(['alter table `users` add `money` decimal(10, 2) unsigned not null'], $blueprint->toSql($connection, new MySqlGrammar));
- }
- public function testRemoveColumn()
- {
- $base = new Blueprint('users', function ($table) {
- $table->string('foo');
- $table->string('remove_this');
- $table->removeColumn('remove_this');
- });
- $connection = m::mock(Connection::class);
- $blueprint = clone $base;
- $this->assertEquals(['alter table `users` add `foo` varchar(255) not null'], $blueprint->toSql($connection, new MySqlGrammar));
- }
- public function testMacroable()
- {
- Blueprint::macro('foo', function () {
- return $this->addCommand('foo');
- });
- MySqlGrammar::macro('compileFoo', function () {
- return 'bar';
- });
- $blueprint = new Blueprint('users', function ($table) {
- $table->foo();
- });
- $connection = m::mock(Connection::class);
- $this->assertEquals(['bar'], $blueprint->toSql($connection, new MySqlGrammar));
- }
- public function testDefaultUsingIdMorph()
- {
- $base = new Blueprint('comments', function ($table) {
- $table->morphs('commentable');
- });
- $connection = m::mock(Connection::class);
- $blueprint = clone $base;
- $this->assertEquals([
- 'alter table `comments` add `commentable_type` varchar(255) not null, add `commentable_id` bigint unsigned not null',
- 'alter table `comments` add index `comments_commentable_type_commentable_id_index`(`commentable_type`, `commentable_id`)',
- ], $blueprint->toSql($connection, new MySqlGrammar));
- }
- public function testDefaultUsingNullableIdMorph()
- {
- $base = new Blueprint('comments', function ($table) {
- $table->nullableMorphs('commentable');
- });
- $connection = m::mock(Connection::class);
- $blueprint = clone $base;
- $this->assertEquals([
- 'alter table `comments` add `commentable_type` varchar(255) null, add `commentable_id` bigint unsigned null',
- 'alter table `comments` add index `comments_commentable_type_commentable_id_index`(`commentable_type`, `commentable_id`)',
- ], $blueprint->toSql($connection, new MySqlGrammar));
- }
- public function testDefaultUsingUuidMorph()
- {
- Builder::defaultMorphKeyType('uuid');
- $base = new Blueprint('comments', function ($table) {
- $table->morphs('commentable');
- });
- $connection = m::mock(Connection::class);
- $blueprint = clone $base;
- $this->assertEquals([
- 'alter table `comments` add `commentable_type` varchar(255) not null, add `commentable_id` char(36) not null',
- 'alter table `comments` add index `comments_commentable_type_commentable_id_index`(`commentable_type`, `commentable_id`)',
- ], $blueprint->toSql($connection, new MySqlGrammar));
- }
- public function testDefaultUsingNullableUuidMorph()
- {
- Builder::defaultMorphKeyType('uuid');
- $base = new Blueprint('comments', function ($table) {
- $table->nullableMorphs('commentable');
- });
- $connection = m::mock(Connection::class);
- $blueprint = clone $base;
- $this->assertEquals([
- 'alter table `comments` add `commentable_type` varchar(255) null, add `commentable_id` char(36) null',
- 'alter table `comments` add index `comments_commentable_type_commentable_id_index`(`commentable_type`, `commentable_id`)',
- ], $blueprint->toSql($connection, new MySqlGrammar));
- }
- public function testGenerateRelationshipColumnWithIncrementalModel()
- {
- $base = new Blueprint('posts', function ($table) {
- $table->foreignIdFor('Illuminate\Foundation\Auth\User');
- });
- $connection = m::mock(Connection::class);
- $blueprint = clone $base;
- $this->assertEquals([
- 'alter table `posts` add `user_id` bigint unsigned not null',
- ], $blueprint->toSql($connection, new MySqlGrammar));
- }
- public function testGenerateRelationshipColumnWithUuidModel()
- {
- require_once __DIR__.'/stubs/EloquentModelUuidStub.php';
- $base = new Blueprint('posts', function ($table) {
- $table->foreignIdFor('EloquentModelUuidStub');
- });
- $connection = m::mock(Connection::class);
- $blueprint = clone $base;
- $this->assertEquals([
- 'alter table `posts` add `eloquent_model_uuid_stub_id` char(36) not null',
- ], $blueprint->toSql($connection, new MySqlGrammar));
- }
- public function testTinyTextColumn()
- {
- $base = new Blueprint('posts', function ($table) {
- $table->tinyText('note');
- });
- $connection = m::mock(Connection::class);
- $blueprint = clone $base;
- $this->assertEquals([
- 'alter table `posts` add `note` tinytext not null',
- ], $blueprint->toSql($connection, new MySqlGrammar));
- $blueprint = clone $base;
- $this->assertEquals([
- 'alter table "posts" add column "note" text not null',
- ], $blueprint->toSql($connection, new SQLiteGrammar));
- $blueprint = clone $base;
- $this->assertEquals([
- 'alter table "posts" add column "note" varchar(255) not null',
- ], $blueprint->toSql($connection, new PostgresGrammar));
- $blueprint = clone $base;
- $this->assertEquals([
- 'alter table "posts" add "note" nvarchar(255) not null',
- ], $blueprint->toSql($connection, new SqlServerGrammar));
- }
- public function testTinyTextNullableColumn()
- {
- $base = new Blueprint('posts', function ($table) {
- $table->tinyText('note')->nullable();
- });
- $connection = m::mock(Connection::class);
- $blueprint = clone $base;
- $this->assertEquals([
- 'alter table `posts` add `note` tinytext null',
- ], $blueprint->toSql($connection, new MySqlGrammar));
- $blueprint = clone $base;
- $this->assertEquals([
- 'alter table "posts" add column "note" text',
- ], $blueprint->toSql($connection, new SQLiteGrammar));
- $blueprint = clone $base;
- $this->assertEquals([
- 'alter table "posts" add column "note" varchar(255) null',
- ], $blueprint->toSql($connection, new PostgresGrammar));
- $blueprint = clone $base;
- $this->assertEquals([
- 'alter table "posts" add "note" nvarchar(255) null',
- ], $blueprint->toSql($connection, new SqlServerGrammar));
- }
- }
|