DatabaseMigrationResetCommandTest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Illuminate\Tests\Database;
  3. use Closure;
  4. use Illuminate\Database\Console\Migrations\ResetCommand;
  5. use Illuminate\Database\Migrations\Migrator;
  6. use Illuminate\Foundation\Application;
  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 DatabaseMigrationResetCommandTest extends TestCase
  12. {
  13. protected function tearDown(): void
  14. {
  15. m::close();
  16. }
  17. public function testResetCommandCallsMigratorWithProperArguments()
  18. {
  19. $command = new ResetCommand($migrator = m::mock(Migrator::class));
  20. $app = new ApplicationDatabaseResetStub(['path.database' => __DIR__]);
  21. $app->useDatabasePath(__DIR__);
  22. $command->setLaravel($app);
  23. $migrator->shouldReceive('paths')->once()->andReturn([]);
  24. $migrator->shouldReceive('usingConnection')->once()->with(null, m::type(Closure::class))->andReturnUsing(function ($connection, $callback) {
  25. $callback();
  26. });
  27. $migrator->shouldReceive('repositoryExists')->once()->andReturn(true);
  28. $migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
  29. $migrator->shouldReceive('reset')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], false);
  30. $this->runCommand($command);
  31. }
  32. public function testResetCommandCanBePretended()
  33. {
  34. $command = new ResetCommand($migrator = m::mock(Migrator::class));
  35. $app = new ApplicationDatabaseResetStub(['path.database' => __DIR__]);
  36. $app->useDatabasePath(__DIR__);
  37. $command->setLaravel($app);
  38. $migrator->shouldReceive('paths')->once()->andReturn([]);
  39. $migrator->shouldReceive('usingConnection')->once()->with('foo', m::type(Closure::class))->andReturnUsing(function ($connection, $callback) {
  40. $callback();
  41. });
  42. $migrator->shouldReceive('repositoryExists')->once()->andReturn(true);
  43. $migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
  44. $migrator->shouldReceive('reset')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], true);
  45. $this->runCommand($command, ['--pretend' => true, '--database' => 'foo']);
  46. }
  47. protected function runCommand($command, $input = [])
  48. {
  49. return $command->run(new ArrayInput($input), new NullOutput);
  50. }
  51. }
  52. class ApplicationDatabaseResetStub extends Application
  53. {
  54. public function __construct(array $data = [])
  55. {
  56. foreach ($data as $abstract => $instance) {
  57. $this->instance($abstract, $instance);
  58. }
  59. }
  60. public function environment(...$environments)
  61. {
  62. return 'development';
  63. }
  64. }