DatabaseMigrationRollbackCommandTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Illuminate\Tests\Database;
  3. use Illuminate\Database\Console\Migrations\RollbackCommand;
  4. use Illuminate\Database\Migrations\Migrator;
  5. use Illuminate\Foundation\Application;
  6. use Mockery as m;
  7. use PHPUnit\Framework\TestCase;
  8. use Symfony\Component\Console\Input\ArrayInput;
  9. use Symfony\Component\Console\Output\NullOutput;
  10. class DatabaseMigrationRollbackCommandTest extends TestCase
  11. {
  12. protected function tearDown(): void
  13. {
  14. m::close();
  15. }
  16. public function testRollbackCommandCallsMigratorWithProperArguments()
  17. {
  18. $command = new RollbackCommand($migrator = m::mock(Migrator::class));
  19. $app = new ApplicationDatabaseRollbackStub(['path.database' => __DIR__]);
  20. $app->useDatabasePath(__DIR__);
  21. $command->setLaravel($app);
  22. $migrator->shouldReceive('paths')->once()->andReturn([]);
  23. $migrator->shouldReceive('usingConnection')->once()->andReturnUsing(function ($name, $callback) {
  24. return $callback();
  25. });
  26. $migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
  27. $migrator->shouldReceive('rollback')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => false, 'step' => 0]);
  28. $this->runCommand($command);
  29. }
  30. public function testRollbackCommandCallsMigratorWithStepOption()
  31. {
  32. $command = new RollbackCommand($migrator = m::mock(Migrator::class));
  33. $app = new ApplicationDatabaseRollbackStub(['path.database' => __DIR__]);
  34. $app->useDatabasePath(__DIR__);
  35. $command->setLaravel($app);
  36. $migrator->shouldReceive('paths')->once()->andReturn([]);
  37. $migrator->shouldReceive('usingConnection')->once()->andReturnUsing(function ($name, $callback) {
  38. return $callback();
  39. });
  40. $migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
  41. $migrator->shouldReceive('rollback')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => false, 'step' => 2]);
  42. $this->runCommand($command, ['--step' => 2]);
  43. }
  44. public function testRollbackCommandCanBePretended()
  45. {
  46. $command = new RollbackCommand($migrator = m::mock(Migrator::class));
  47. $app = new ApplicationDatabaseRollbackStub(['path.database' => __DIR__]);
  48. $app->useDatabasePath(__DIR__);
  49. $command->setLaravel($app);
  50. $migrator->shouldReceive('paths')->once()->andReturn([]);
  51. $migrator->shouldReceive('usingConnection')->once()->andReturnUsing(function ($name, $callback) {
  52. return $callback();
  53. });
  54. $migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
  55. $migrator->shouldReceive('rollback')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], true);
  56. $this->runCommand($command, ['--pretend' => true, '--database' => 'foo']);
  57. }
  58. public function testRollbackCommandCanBePretendedWithStepOption()
  59. {
  60. $command = new RollbackCommand($migrator = m::mock(Migrator::class));
  61. $app = new ApplicationDatabaseRollbackStub(['path.database' => __DIR__]);
  62. $app->useDatabasePath(__DIR__);
  63. $command->setLaravel($app);
  64. $migrator->shouldReceive('paths')->once()->andReturn([]);
  65. $migrator->shouldReceive('usingConnection')->once()->andReturnUsing(function ($name, $callback) {
  66. return $callback();
  67. });
  68. $migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
  69. $migrator->shouldReceive('rollback')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => true, 'step' => 2]);
  70. $this->runCommand($command, ['--pretend' => true, '--database' => 'foo', '--step' => 2]);
  71. }
  72. protected function runCommand($command, $input = [])
  73. {
  74. return $command->run(new ArrayInput($input), new NullOutput);
  75. }
  76. }
  77. class ApplicationDatabaseRollbackStub extends Application
  78. {
  79. public function __construct(array $data = [])
  80. {
  81. foreach ($data as $abstract => $instance) {
  82. $this->instance($abstract, $instance);
  83. }
  84. }
  85. public function environment(...$environments)
  86. {
  87. return 'development';
  88. }
  89. }