DatabaseMigrationMigrateCommandTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace Illuminate\Tests\Database;
  3. use Illuminate\Contracts\Events\Dispatcher;
  4. use Illuminate\Database\Console\Migrations\MigrateCommand;
  5. use Illuminate\Database\Events\SchemaLoaded;
  6. use Illuminate\Database\Migrations\Migrator;
  7. use Illuminate\Foundation\Application;
  8. use Mockery as m;
  9. use PHPUnit\Framework\TestCase;
  10. use stdClass;
  11. use Symfony\Component\Console\Input\ArrayInput;
  12. use Symfony\Component\Console\Output\NullOutput;
  13. class DatabaseMigrationMigrateCommandTest extends TestCase
  14. {
  15. protected function tearDown(): void
  16. {
  17. m::close();
  18. }
  19. public function testBasicMigrationsCallMigratorWithProperArguments()
  20. {
  21. $command = new MigrateCommand($migrator = m::mock(Migrator::class), $dispatcher = m::mock(Dispatcher::class));
  22. $app = new ApplicationDatabaseMigrationStub(['path.database' => __DIR__]);
  23. $app->useDatabasePath(__DIR__);
  24. $command->setLaravel($app);
  25. $migrator->shouldReceive('paths')->once()->andReturn([]);
  26. $migrator->shouldReceive('hasRunAnyMigrations')->andReturn(true);
  27. $migrator->shouldReceive('usingConnection')->once()->andReturnUsing(function ($name, $callback) {
  28. return $callback();
  29. });
  30. $migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
  31. $migrator->shouldReceive('run')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => false, 'step' => false]);
  32. $migrator->shouldReceive('getNotes')->andReturn([]);
  33. $migrator->shouldReceive('repositoryExists')->once()->andReturn(true);
  34. $this->runCommand($command);
  35. }
  36. public function testMigrationsCanBeRunWithStoredSchema()
  37. {
  38. $command = new MigrateCommand($migrator = m::mock(Migrator::class), $dispatcher = m::mock(Dispatcher::class));
  39. $app = new ApplicationDatabaseMigrationStub(['path.database' => __DIR__]);
  40. $app->useDatabasePath(__DIR__);
  41. $command->setLaravel($app);
  42. $migrator->shouldReceive('paths')->once()->andReturn([]);
  43. $migrator->shouldReceive('hasRunAnyMigrations')->andReturn(false);
  44. $migrator->shouldReceive('resolveConnection')->andReturn($connection = m::mock(stdClass::class));
  45. $connection->shouldReceive('getName')->andReturn('mysql');
  46. $migrator->shouldReceive('usingConnection')->once()->andReturnUsing(function ($name, $callback) {
  47. return $callback();
  48. });
  49. $migrator->shouldReceive('deleteRepository')->once();
  50. $connection->shouldReceive('getSchemaState')->andReturn($schemaState = m::mock(stdClass::class));
  51. $schemaState->shouldReceive('handleOutputUsing')->andReturnSelf();
  52. $schemaState->shouldReceive('load')->once()->with(__DIR__.'/stubs/schema.sql');
  53. $dispatcher->shouldReceive('dispatch')->once()->with(m::type(SchemaLoaded::class));
  54. $migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
  55. $migrator->shouldReceive('run')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => false, 'step' => false]);
  56. $migrator->shouldReceive('getNotes')->andReturn([]);
  57. $migrator->shouldReceive('repositoryExists')->once()->andReturn(true);
  58. $this->runCommand($command, ['--schema-path' => __DIR__.'/stubs/schema.sql']);
  59. }
  60. public function testMigrationRepositoryCreatedWhenNecessary()
  61. {
  62. $params = [$migrator = m::mock(Migrator::class), $dispatcher = m::mock(Dispatcher::class)];
  63. $command = $this->getMockBuilder(MigrateCommand::class)->onlyMethods(['call'])->setConstructorArgs($params)->getMock();
  64. $app = new ApplicationDatabaseMigrationStub(['path.database' => __DIR__]);
  65. $app->useDatabasePath(__DIR__);
  66. $command->setLaravel($app);
  67. $migrator->shouldReceive('paths')->once()->andReturn([]);
  68. $migrator->shouldReceive('hasRunAnyMigrations')->andReturn(true);
  69. $migrator->shouldReceive('usingConnection')->once()->andReturnUsing(function ($name, $callback) {
  70. return $callback();
  71. });
  72. $migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
  73. $migrator->shouldReceive('run')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => false, 'step' => false]);
  74. $migrator->shouldReceive('repositoryExists')->once()->andReturn(false);
  75. $command->expects($this->once())->method('call')->with($this->equalTo('migrate:install'), $this->equalTo([]));
  76. $this->runCommand($command);
  77. }
  78. public function testTheCommandMayBePretended()
  79. {
  80. $command = new MigrateCommand($migrator = m::mock(Migrator::class), $dispatcher = m::mock(Dispatcher::class));
  81. $app = new ApplicationDatabaseMigrationStub(['path.database' => __DIR__]);
  82. $app->useDatabasePath(__DIR__);
  83. $command->setLaravel($app);
  84. $migrator->shouldReceive('paths')->once()->andReturn([]);
  85. $migrator->shouldReceive('hasRunAnyMigrations')->andReturn(true);
  86. $migrator->shouldReceive('usingConnection')->once()->andReturnUsing(function ($name, $callback) {
  87. return $callback();
  88. });
  89. $migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
  90. $migrator->shouldReceive('run')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => true, 'step' => false]);
  91. $migrator->shouldReceive('repositoryExists')->once()->andReturn(true);
  92. $this->runCommand($command, ['--pretend' => true]);
  93. }
  94. public function testTheDatabaseMayBeSet()
  95. {
  96. $command = new MigrateCommand($migrator = m::mock(Migrator::class), $dispatcher = m::mock(Dispatcher::class));
  97. $app = new ApplicationDatabaseMigrationStub(['path.database' => __DIR__]);
  98. $app->useDatabasePath(__DIR__);
  99. $command->setLaravel($app);
  100. $migrator->shouldReceive('paths')->once()->andReturn([]);
  101. $migrator->shouldReceive('hasRunAnyMigrations')->andReturn(true);
  102. $migrator->shouldReceive('usingConnection')->once()->andReturnUsing(function ($name, $callback) {
  103. return $callback();
  104. });
  105. $migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
  106. $migrator->shouldReceive('run')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => false, 'step' => false]);
  107. $migrator->shouldReceive('repositoryExists')->once()->andReturn(true);
  108. $this->runCommand($command, ['--database' => 'foo']);
  109. }
  110. public function testStepMayBeSet()
  111. {
  112. $command = new MigrateCommand($migrator = m::mock(Migrator::class), $dispatcher = m::mock(Dispatcher::class));
  113. $app = new ApplicationDatabaseMigrationStub(['path.database' => __DIR__]);
  114. $app->useDatabasePath(__DIR__);
  115. $command->setLaravel($app);
  116. $migrator->shouldReceive('paths')->once()->andReturn([]);
  117. $migrator->shouldReceive('hasRunAnyMigrations')->andReturn(true);
  118. $migrator->shouldReceive('usingConnection')->once()->andReturnUsing(function ($name, $callback) {
  119. return $callback();
  120. });
  121. $migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
  122. $migrator->shouldReceive('run')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => false, 'step' => true]);
  123. $migrator->shouldReceive('repositoryExists')->once()->andReturn(true);
  124. $this->runCommand($command, ['--step' => true]);
  125. }
  126. protected function runCommand($command, $input = [])
  127. {
  128. return $command->run(new ArrayInput($input), new NullOutput);
  129. }
  130. }
  131. class ApplicationDatabaseMigrationStub extends Application
  132. {
  133. public function __construct(array $data = [])
  134. {
  135. foreach ($data as $abstract => $instance) {
  136. $this->instance($abstract, $instance);
  137. }
  138. }
  139. public function environment(...$environments)
  140. {
  141. return 'development';
  142. }
  143. }