DatabaseMigrationMakeCommandTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace Illuminate\Tests\Database;
  3. use Illuminate\Database\Console\Migrations\MigrateMakeCommand;
  4. use Illuminate\Database\Migrations\MigrationCreator;
  5. use Illuminate\Foundation\Application;
  6. use Illuminate\Support\Composer;
  7. use Mockery as m;
  8. use PHPUnit\Framework\TestCase;
  9. use Symfony\Component\Console\Input\ArrayInput;
  10. use Symfony\Component\Console\Output\NullOutput;
  11. class DatabaseMigrationMakeCommandTest extends TestCase
  12. {
  13. protected function tearDown(): void
  14. {
  15. m::close();
  16. }
  17. public function testBasicCreateDumpsAutoload()
  18. {
  19. $command = new MigrateMakeCommand(
  20. $creator = m::mock(MigrationCreator::class),
  21. $composer = m::mock(Composer::class)
  22. );
  23. $app = new Application;
  24. $app->useDatabasePath(__DIR__);
  25. $command->setLaravel($app);
  26. $creator->shouldReceive('create')->once()
  27. ->with('create_foo', __DIR__.DIRECTORY_SEPARATOR.'migrations', 'foo', true)
  28. ->andReturn(__DIR__.'/migrations/2021_04_23_110457_create_foo.php');
  29. $composer->shouldReceive('dumpAutoloads')->once();
  30. $this->runCommand($command, ['name' => 'create_foo']);
  31. }
  32. public function testBasicCreateGivesCreatorProperArguments()
  33. {
  34. $command = new MigrateMakeCommand(
  35. $creator = m::mock(MigrationCreator::class),
  36. m::mock(Composer::class)->shouldIgnoreMissing()
  37. );
  38. $app = new Application;
  39. $app->useDatabasePath(__DIR__);
  40. $command->setLaravel($app);
  41. $creator->shouldReceive('create')->once()
  42. ->with('create_foo', __DIR__.DIRECTORY_SEPARATOR.'migrations', 'foo', true)
  43. ->andReturn(__DIR__.'/migrations/2021_04_23_110457_create_foo.php');
  44. $this->runCommand($command, ['name' => 'create_foo']);
  45. }
  46. public function testBasicCreateGivesCreatorProperArgumentsWhenNameIsStudlyCase()
  47. {
  48. $command = new MigrateMakeCommand(
  49. $creator = m::mock(MigrationCreator::class),
  50. m::mock(Composer::class)->shouldIgnoreMissing()
  51. );
  52. $app = new Application;
  53. $app->useDatabasePath(__DIR__);
  54. $command->setLaravel($app);
  55. $creator->shouldReceive('create')->once()
  56. ->with('create_foo', __DIR__.DIRECTORY_SEPARATOR.'migrations', 'foo', true)
  57. ->andReturn(__DIR__.'/migrations/2021_04_23_110457_create_foo.php');
  58. $this->runCommand($command, ['name' => 'CreateFoo']);
  59. }
  60. public function testBasicCreateGivesCreatorProperArgumentsWhenTableIsSet()
  61. {
  62. $command = new MigrateMakeCommand(
  63. $creator = m::mock(MigrationCreator::class),
  64. m::mock(Composer::class)->shouldIgnoreMissing()
  65. );
  66. $app = new Application;
  67. $app->useDatabasePath(__DIR__);
  68. $command->setLaravel($app);
  69. $creator->shouldReceive('create')->once()
  70. ->with('create_foo', __DIR__.DIRECTORY_SEPARATOR.'migrations', 'users', true)
  71. ->andReturn(__DIR__.'/migrations/2021_04_23_110457_create_foo.php');
  72. $this->runCommand($command, ['name' => 'create_foo', '--create' => 'users']);
  73. }
  74. public function testBasicCreateGivesCreatorProperArgumentsWhenCreateTablePatternIsFound()
  75. {
  76. $command = new MigrateMakeCommand(
  77. $creator = m::mock(MigrationCreator::class),
  78. m::mock(Composer::class)->shouldIgnoreMissing()
  79. );
  80. $app = new Application;
  81. $app->useDatabasePath(__DIR__);
  82. $command->setLaravel($app);
  83. $creator->shouldReceive('create')->once()
  84. ->with('create_users_table', __DIR__.DIRECTORY_SEPARATOR.'migrations', 'users', true)
  85. ->andReturn(__DIR__.'/migrations/2021_04_23_110457_create_users_table.php');
  86. $this->runCommand($command, ['name' => 'create_users_table']);
  87. }
  88. public function testCanSpecifyPathToCreateMigrationsIn()
  89. {
  90. $command = new MigrateMakeCommand(
  91. $creator = m::mock(MigrationCreator::class),
  92. m::mock(Composer::class)->shouldIgnoreMissing()
  93. );
  94. $app = new Application;
  95. $command->setLaravel($app);
  96. $app->setBasePath('/home/laravel');
  97. $creator->shouldReceive('create')->once()
  98. ->with('create_foo', '/home/laravel/vendor/laravel-package/migrations', 'users', true)
  99. ->andReturn('/home/laravel/vendor/laravel-package/migrations/2021_04_23_110457_create_foo.php');
  100. $this->runCommand($command, ['name' => 'create_foo', '--path' => 'vendor/laravel-package/migrations', '--create' => 'users']);
  101. }
  102. protected function runCommand($command, $input = [])
  103. {
  104. return $command->run(new ArrayInput($input), new NullOutput);
  105. }
  106. }