123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace Illuminate\Tests\Database;
- use Illuminate\Database\Migrations\MigrationCreator;
- use Illuminate\Filesystem\Filesystem;
- use InvalidArgumentException;
- use Mockery as m;
- use PHPUnit\Framework\TestCase;
- class DatabaseMigrationCreatorTest extends TestCase
- {
- protected function tearDown(): void
- {
- m::close();
- }
- public function testBasicCreateMethodStoresMigrationFile()
- {
- $creator = $this->getCreator();
- $creator->expects($this->any())->method('getDatePrefix')->willReturn('foo');
- $creator->getFilesystem()->shouldReceive('exists')->once()->with('stubs/migration.stub')->andReturn(false);
- $creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.stub')->andReturn('DummyClass');
- $creator->getFilesystem()->shouldReceive('ensureDirectoryExists')->once()->with('foo');
- $creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'CreateBar');
- $creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/*.php')->andReturn(['foo/foo_create_bar.php']);
- $creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/foo_create_bar.php');
- $creator->create('create_bar', 'foo');
- }
- public function testBasicCreateMethodCallsPostCreateHooks()
- {
- $table = 'baz';
- $creator = $this->getCreator();
- unset($_SERVER['__migration.creator']);
- $creator->afterCreate(function ($table) {
- $_SERVER['__migration.creator'] = $table;
- });
- $creator->expects($this->any())->method('getDatePrefix')->willReturn('foo');
- $creator->getFilesystem()->shouldReceive('exists')->once()->with('stubs/migration.update.stub')->andReturn(false);
- $creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.update.stub')->andReturn('DummyClass DummyTable');
- $creator->getFilesystem()->shouldReceive('ensureDirectoryExists')->once()->with('foo');
- $creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'CreateBar baz');
- $creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/*.php')->andReturn(['foo/foo_create_bar.php']);
- $creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/foo_create_bar.php');
- $creator->create('create_bar', 'foo', $table);
- $this->assertEquals($_SERVER['__migration.creator'], $table);
- unset($_SERVER['__migration.creator']);
- }
- public function testTableUpdateMigrationStoresMigrationFile()
- {
- $creator = $this->getCreator();
- $creator->expects($this->any())->method('getDatePrefix')->willReturn('foo');
- $creator->getFilesystem()->shouldReceive('exists')->once()->with('stubs/migration.update.stub')->andReturn(false);
- $creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.update.stub')->andReturn('DummyClass DummyTable');
- $creator->getFilesystem()->shouldReceive('ensureDirectoryExists')->once()->with('foo');
- $creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'CreateBar baz');
- $creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/*.php')->andReturn(['foo/foo_create_bar.php']);
- $creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/foo_create_bar.php');
- $creator->create('create_bar', 'foo', 'baz');
- }
- public function testTableCreationMigrationStoresMigrationFile()
- {
- $creator = $this->getCreator();
- $creator->expects($this->any())->method('getDatePrefix')->willReturn('foo');
- $creator->getFilesystem()->shouldReceive('exists')->once()->with('stubs/migration.create.stub')->andReturn(false);
- $creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.create.stub')->andReturn('DummyClass DummyTable');
- $creator->getFilesystem()->shouldReceive('ensureDirectoryExists')->once()->with('foo');
- $creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'CreateBar baz');
- $creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/*.php')->andReturn(['foo/foo_create_bar.php']);
- $creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/foo_create_bar.php');
- $creator->create('create_bar', 'foo', 'baz', true);
- }
- public function testTableUpdateMigrationWontCreateDuplicateClass()
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('A MigrationCreatorFakeMigration class already exists.');
- $creator = $this->getCreator();
- $creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/*.php')->andReturn(['foo/foo_create_bar.php']);
- $creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/foo_create_bar.php');
- $creator->create('migration_creator_fake_migration', 'foo');
- }
- protected function getCreator()
- {
- $files = m::mock(Filesystem::class);
- $customStubs = 'stubs';
- return $this->getMockBuilder(MigrationCreator::class)
- ->onlyMethods(['getDatePrefix'])
- ->setConstructorArgs([$files, $customStubs])
- ->getMock();
- }
- }
|