DatabaseMigrationCreatorTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Illuminate\Tests\Database;
  3. use Illuminate\Database\Migrations\MigrationCreator;
  4. use Illuminate\Filesystem\Filesystem;
  5. use InvalidArgumentException;
  6. use Mockery as m;
  7. use PHPUnit\Framework\TestCase;
  8. class DatabaseMigrationCreatorTest extends TestCase
  9. {
  10. protected function tearDown(): void
  11. {
  12. m::close();
  13. }
  14. public function testBasicCreateMethodStoresMigrationFile()
  15. {
  16. $creator = $this->getCreator();
  17. $creator->expects($this->any())->method('getDatePrefix')->willReturn('foo');
  18. $creator->getFilesystem()->shouldReceive('exists')->once()->with('stubs/migration.stub')->andReturn(false);
  19. $creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.stub')->andReturn('DummyClass');
  20. $creator->getFilesystem()->shouldReceive('ensureDirectoryExists')->once()->with('foo');
  21. $creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'CreateBar');
  22. $creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/*.php')->andReturn(['foo/foo_create_bar.php']);
  23. $creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/foo_create_bar.php');
  24. $creator->create('create_bar', 'foo');
  25. }
  26. public function testBasicCreateMethodCallsPostCreateHooks()
  27. {
  28. $table = 'baz';
  29. $creator = $this->getCreator();
  30. unset($_SERVER['__migration.creator']);
  31. $creator->afterCreate(function ($table) {
  32. $_SERVER['__migration.creator'] = $table;
  33. });
  34. $creator->expects($this->any())->method('getDatePrefix')->willReturn('foo');
  35. $creator->getFilesystem()->shouldReceive('exists')->once()->with('stubs/migration.update.stub')->andReturn(false);
  36. $creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.update.stub')->andReturn('DummyClass DummyTable');
  37. $creator->getFilesystem()->shouldReceive('ensureDirectoryExists')->once()->with('foo');
  38. $creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'CreateBar baz');
  39. $creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/*.php')->andReturn(['foo/foo_create_bar.php']);
  40. $creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/foo_create_bar.php');
  41. $creator->create('create_bar', 'foo', $table);
  42. $this->assertEquals($_SERVER['__migration.creator'], $table);
  43. unset($_SERVER['__migration.creator']);
  44. }
  45. public function testTableUpdateMigrationStoresMigrationFile()
  46. {
  47. $creator = $this->getCreator();
  48. $creator->expects($this->any())->method('getDatePrefix')->willReturn('foo');
  49. $creator->getFilesystem()->shouldReceive('exists')->once()->with('stubs/migration.update.stub')->andReturn(false);
  50. $creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.update.stub')->andReturn('DummyClass DummyTable');
  51. $creator->getFilesystem()->shouldReceive('ensureDirectoryExists')->once()->with('foo');
  52. $creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'CreateBar baz');
  53. $creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/*.php')->andReturn(['foo/foo_create_bar.php']);
  54. $creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/foo_create_bar.php');
  55. $creator->create('create_bar', 'foo', 'baz');
  56. }
  57. public function testTableCreationMigrationStoresMigrationFile()
  58. {
  59. $creator = $this->getCreator();
  60. $creator->expects($this->any())->method('getDatePrefix')->willReturn('foo');
  61. $creator->getFilesystem()->shouldReceive('exists')->once()->with('stubs/migration.create.stub')->andReturn(false);
  62. $creator->getFilesystem()->shouldReceive('get')->once()->with($creator->stubPath().'/migration.create.stub')->andReturn('DummyClass DummyTable');
  63. $creator->getFilesystem()->shouldReceive('ensureDirectoryExists')->once()->with('foo');
  64. $creator->getFilesystem()->shouldReceive('put')->once()->with('foo/foo_create_bar.php', 'CreateBar baz');
  65. $creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/*.php')->andReturn(['foo/foo_create_bar.php']);
  66. $creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/foo_create_bar.php');
  67. $creator->create('create_bar', 'foo', 'baz', true);
  68. }
  69. public function testTableUpdateMigrationWontCreateDuplicateClass()
  70. {
  71. $this->expectException(InvalidArgumentException::class);
  72. $this->expectExceptionMessage('A MigrationCreatorFakeMigration class already exists.');
  73. $creator = $this->getCreator();
  74. $creator->getFilesystem()->shouldReceive('glob')->once()->with('foo/*.php')->andReturn(['foo/foo_create_bar.php']);
  75. $creator->getFilesystem()->shouldReceive('requireOnce')->once()->with('foo/foo_create_bar.php');
  76. $creator->create('migration_creator_fake_migration', 'foo');
  77. }
  78. protected function getCreator()
  79. {
  80. $files = m::mock(Filesystem::class);
  81. $customStubs = 'stubs';
  82. return $this->getMockBuilder(MigrationCreator::class)
  83. ->onlyMethods(['getDatePrefix'])
  84. ->setConstructorArgs([$files, $customStubs])
  85. ->getMock();
  86. }
  87. }