DatabaseMigrationRefreshCommandTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Illuminate\Tests\Database;
  3. use Illuminate\Contracts\Events\Dispatcher;
  4. use Illuminate\Database\Console\Migrations\MigrateCommand;
  5. use Illuminate\Database\Console\Migrations\RefreshCommand;
  6. use Illuminate\Database\Console\Migrations\ResetCommand;
  7. use Illuminate\Database\Console\Migrations\RollbackCommand;
  8. use Illuminate\Database\Events\DatabaseRefreshed;
  9. use Illuminate\Foundation\Application;
  10. use Mockery as m;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Application as ConsoleApplication;
  13. use Symfony\Component\Console\Input\ArrayInput;
  14. use Symfony\Component\Console\Output\NullOutput;
  15. class DatabaseMigrationRefreshCommandTest extends TestCase
  16. {
  17. protected function tearDown(): void
  18. {
  19. m::close();
  20. }
  21. public function testRefreshCommandCallsCommandsWithProperArguments()
  22. {
  23. $command = new RefreshCommand;
  24. $app = new ApplicationDatabaseRefreshStub(['path.database' => __DIR__]);
  25. $dispatcher = $app->instance(Dispatcher::class, $events = m::mock());
  26. $console = m::mock(ConsoleApplication::class)->makePartial();
  27. $console->__construct();
  28. $command->setLaravel($app);
  29. $command->setApplication($console);
  30. $resetCommand = m::mock(ResetCommand::class);
  31. $migrateCommand = m::mock(MigrateCommand::class);
  32. $console->shouldReceive('find')->with('migrate:reset')->andReturn($resetCommand);
  33. $console->shouldReceive('find')->with('migrate')->andReturn($migrateCommand);
  34. $dispatcher->shouldReceive('dispatch')->once()->with(m::type(DatabaseRefreshed::class));
  35. $quote = DIRECTORY_SEPARATOR === '\\' ? '"' : "'";
  36. $resetCommand->shouldReceive('run')->with(new InputMatcher("--force=1 {$quote}migrate:reset{$quote}"), m::any());
  37. $migrateCommand->shouldReceive('run')->with(new InputMatcher('--force=1 migrate'), m::any());
  38. $this->runCommand($command);
  39. }
  40. public function testRefreshCommandCallsCommandsWithStep()
  41. {
  42. $command = new RefreshCommand;
  43. $app = new ApplicationDatabaseRefreshStub(['path.database' => __DIR__]);
  44. $dispatcher = $app->instance(Dispatcher::class, $events = m::mock());
  45. $console = m::mock(ConsoleApplication::class)->makePartial();
  46. $console->__construct();
  47. $command->setLaravel($app);
  48. $command->setApplication($console);
  49. $rollbackCommand = m::mock(RollbackCommand::class);
  50. $migrateCommand = m::mock(MigrateCommand::class);
  51. $console->shouldReceive('find')->with('migrate:rollback')->andReturn($rollbackCommand);
  52. $console->shouldReceive('find')->with('migrate')->andReturn($migrateCommand);
  53. $dispatcher->shouldReceive('dispatch')->once()->with(m::type(DatabaseRefreshed::class));
  54. $quote = DIRECTORY_SEPARATOR === '\\' ? '"' : "'";
  55. $rollbackCommand->shouldReceive('run')->with(new InputMatcher("--step=2 --force=1 {$quote}migrate:rollback{$quote}"), m::any());
  56. $migrateCommand->shouldReceive('run')->with(new InputMatcher('--force=1 migrate'), m::any());
  57. $this->runCommand($command, ['--step' => 2]);
  58. }
  59. protected function runCommand($command, $input = [])
  60. {
  61. return $command->run(new ArrayInput($input), new NullOutput);
  62. }
  63. }
  64. class InputMatcher extends m\Matcher\MatcherAbstract
  65. {
  66. /**
  67. * @param \Symfony\Component\Console\Input\ArrayInput $actual
  68. * @return bool
  69. */
  70. public function match(&$actual)
  71. {
  72. return (string) $actual == $this->_expected;
  73. }
  74. public function __toString()
  75. {
  76. return '';
  77. }
  78. }
  79. class ApplicationDatabaseRefreshStub extends Application
  80. {
  81. public function __construct(array $data = [])
  82. {
  83. foreach ($data as $abstract => $instance) {
  84. $this->instance($abstract, $instance);
  85. }
  86. }
  87. public function environment(...$environments)
  88. {
  89. return 'development';
  90. }
  91. }