Skip to content

Commit

Permalink
Merge pull request #32 from stevebauman/move-to-handle
Browse files Browse the repository at this point in the history
Move command constructor args to `handle()`
  • Loading branch information
babenkoivan authored Oct 1, 2021
2 parents e173e5e + c51040c commit 358e205
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 135 deletions.
37 changes: 8 additions & 29 deletions src/Console/FreshCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,26 @@ class FreshCommand extends Command
* @var string
*/
protected $description = 'Drop all indices and re-run all migrations';

/**
* @var Migrator
*/
private $migrator;
/**
* @var MigrationRepository
*/
private $migrationRepository;
/**
* @var IndexManagerInterface
* @return int
*/
private $indexManager;

public function __construct(
public function handle(
Migrator $migrator,
MigrationRepository $migrationRepository,
IndexManagerInterface $indexManager
) {
parent::__construct();

$this->migrator = $migrator;
$this->migrationRepository = $migrationRepository;
$this->indexManager = $indexManager;
}

/**
* @return int
*/
public function handle()
{
$this->migrator->setOutput($this->output);
$migrator->setOutput($this->output);

if (! $this->confirmToProceed() || ! $this->migrator->isReady()) {
if (! $this->confirmToProceed() || ! $migrator->isReady()) {
return 1;
}

$this->indexManager->drop('*');
$indexManager->drop('*');

$this->migrationRepository->truncate();
$migrationRepository->truncate();

$this->migrator->migrateAll();
$migrator->migrateAll();

return 0;
}
Expand Down
22 changes: 3 additions & 19 deletions src/Console/MakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,23 @@ class MakeCommand extends Command
* @var string
*/
protected $description = 'Create a new migration file';
/**
* @var Filesystem
*/
private $filesystem;
/**
* @var MigrationStorage
*/
private $migrationStorage;

public function __construct(Filesystem $filesystem, MigrationStorage $migrationStorage)
{
parent::__construct();

$this->filesystem = $filesystem;
$this->migrationStorage = $migrationStorage;
}

/**
* @return int
*
* @throws FileNotFoundException
*/
public function handle()
public function handle(Filesystem $filesystem, MigrationStorage $migrationStorage)
{
$name = Str::snake(trim($this->argument('name')));

$fileName = sprintf('%s_%s', (new Carbon())->format('Y_m_d_His'), $name);
$className = Str::studly($name);

$stub = $this->filesystem->get(__DIR__ . '/stubs/migration.blank.stub');
$stub = $filesystem->get(__DIR__ . '/stubs/migration.blank.stub');
$content = str_replace('DummyClass', $className, $stub);

$this->migrationStorage->create($fileName, $content);
$migrationStorage->create($fileName, $content);

$this->output->writeln('<info>Created migration:</info> ' . $fileName);

Expand Down
21 changes: 5 additions & 16 deletions src/Console/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,22 @@ class MigrateCommand extends Command
* @var string
*/
protected $description = 'Run the migrations';
/**
* @var Migrator
*/
private $migrator;

public function __construct(Migrator $migrator)
{
parent::__construct();

$this->migrator = $migrator;
}

/**
* @return int
*/
public function handle()
public function handle(Migrator $migrator)
{
$this->migrator->setOutput($this->output);
$migrator->setOutput($this->output);

if (!$this->confirmToProceed() || !$this->migrator->isReady()) {
if (!$this->confirmToProceed() || !$migrator->isReady()) {
return 1;
}

if ($fileName = $this->argument('fileName')) {
$this->migrator->migrateOne($fileName);
$migrator->migrateOne($fileName);
} else {
$this->migrator->migrateAll();
$migrator->migrateAll();
}

return 0;
Expand Down
21 changes: 5 additions & 16 deletions src/Console/RefreshCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,20 @@ class RefreshCommand extends Command
* @var string
*/
protected $description = 'Reset and re-run all migrations';
/**
* @var Migrator
*/
private $migrator;

public function __construct(Migrator $migrator)
{
parent::__construct();

$this->migrator = $migrator;
}

/**
* @return int
*/
public function handle()
public function handle(Migrator $migrator)
{
$this->migrator->setOutput($this->output);
$migrator->setOutput($this->output);

if (!$this->confirmToProceed() || !$this->migrator->isReady()) {
if (!$this->confirmToProceed() || !$migrator->isReady()) {
return 1;
}

$this->migrator->rollbackAll();
$this->migrator->migrateAll();
$migrator->rollbackAll();
$migrator->migrateAll();

return 0;
}
Expand Down
19 changes: 4 additions & 15 deletions src/Console/ResetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,19 @@ class ResetCommand extends Command
* @var string
*/
protected $description = 'Rollback all migrations';
/**
* @var Migrator
*/
private $migrator;

public function __construct(Migrator $migrator)
{
parent::__construct();

$this->migrator = $migrator;
}

/**
* @return int
*/
public function handle()
public function handle(Migrator $migrator)
{
$this->migrator->setOutput($this->output);
$migrator->setOutput($this->output);

if (!$this->confirmToProceed() || !$this->migrator->isReady()) {
if (!$this->confirmToProceed() || !$migrator->isReady()) {
return 1;
}

$this->migrator->rollbackAll();
$migrator->rollbackAll();

return 0;
}
Expand Down
21 changes: 5 additions & 16 deletions src/Console/RollbackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,22 @@ class RollbackCommand extends Command
* @var string
*/
protected $description = 'Rollback migrations';
/**
* @var Migrator
*/
private $migrator;

public function __construct(Migrator $migrator)
{
parent::__construct();

$this->migrator = $migrator;
}

/**
* @return int
*/
public function handle()
public function handle(Migrator $migrator)
{
$this->migrator->setOutput($this->output);
$migrator->setOutput($this->output);

if (!$this->confirmToProceed() || !$this->migrator->isReady()) {
if (!$this->confirmToProceed() || !$migrator->isReady()) {
return 1;
}

if ($fileName = $this->argument('fileName')) {
$this->migrator->rollbackOne($fileName);
$migrator->rollbackOne($fileName);
} else {
$this->migrator->rollbackLastBatch();
$migrator->rollbackLastBatch();
}

return 0;
Expand Down
19 changes: 4 additions & 15 deletions src/Console/StatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,19 @@ class StatusCommand extends Command
* @var string
*/
protected $description = 'Show the status of each migration';
/**
* @var Migrator
*/
private $migrator;

public function __construct(Migrator $migrator)
{
parent::__construct();

$this->migrator = $migrator;
}

/**
* @return int
*/
public function handle()
public function handle(Migrator $migrator)
{
$this->migrator->setOutput($this->output);
$migrator->setOutput($this->output);

if (!$this->migrator->isReady()) {
if (!$migrator->isReady()) {
return 1;
}

$this->migrator->showStatus();
$migrator->showStatus();

return 0;
}
Expand Down
7 changes: 6 additions & 1 deletion tests/Integration/Console/FreshCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ protected function setUp(): void
parent::setUp();

$this->migrator = $this->createMock(Migrator::class);
$this->app->instance(Migrator::class, $this->migrator);

$this->migrationRepository = $this->createMock(MigrationRepository::class);
$this->app->instance(MigrationRepository::class, $this->migrationRepository);

$this->indexManager = $this->createMock(IndexManagerInterface::class);
$this->app->instance(IndexManagerInterface::class, $this->indexManager);

$this->command = new FreshCommand($this->migrator, $this->migrationRepository, $this->indexManager);
$this->command = new FreshCommand();
$this->command->setLaravel($this->app);
}

Expand Down
5 changes: 2 additions & 3 deletions tests/Integration/Console/MakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use ElasticMigrations\Console\MakeCommand;
use ElasticMigrations\Filesystem\MigrationStorage;
use ElasticMigrations\Tests\Integration\TestCase;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;

Expand All @@ -16,8 +15,8 @@ final class MakeCommandTest extends TestCase
{
public function test_migration_file_can_be_created(): void
{
$fileSystem = resolve(Filesystem::class);
$migrationStorageMock = $this->createMock(MigrationStorage::class);
$this->app->instance(MigrationStorage::class, $migrationStorageMock);

/** @var string $migrationStub */
$migrationStub = file_get_contents(dirname(__DIR__, 3) . '/src/Console/stubs/migration.blank.stub');
Expand All @@ -30,7 +29,7 @@ public function test_migration_file_can_be_created(): void
str_replace('DummyClass', 'TestMigrationCreation', $migrationStub)
);

$command = new MakeCommand($fileSystem, $migrationStorageMock);
$command = new MakeCommand();
$command->setLaravel($this->app);

$input = new ArrayInput(['name' => 'test_migration_creation']);
Expand Down
3 changes: 2 additions & 1 deletion tests/Integration/Console/MigrateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ protected function setUp(): void
parent::setUp();

$this->migrator = $this->createMock(Migrator::class);
$this->app->instance(Migrator::class, $this->migrator);

$this->command = new MigrateCommand($this->migrator);
$this->command = new MigrateCommand();
$this->command->setLaravel($this->app);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Integration/Console/RefreshCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ protected function setUp(): void
parent::setUp();

$this->migrator = $this->createMock(Migrator::class);
$this->app->instance(Migrator::class, $this->migrator);

$this->command = new RefreshCommand($this->migrator);
$this->command = new RefreshCommand();
$this->command->setLaravel($this->app);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Integration/Console/ResetCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ protected function setUp(): void
parent::setUp();

$this->migrator = $this->createMock(Migrator::class);
$this->app->instance(Migrator::class, $this->migrator);

$this->command = new ResetCommand($this->migrator);
$this->command = new ResetCommand();
$this->command->setLaravel($this->app);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Integration/Console/RollbackCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ protected function setUp(): void
parent::setUp();

$this->migrator = $this->createMock(Migrator::class);
$this->app->instance(Migrator::class, $this->migrator);

$this->command = new RollbackCommand($this->migrator);
$this->command = new RollbackCommand();
$this->command->setLaravel($this->app);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Integration/Console/StatusCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ protected function setUp(): void
parent::setUp();

$this->migrator = $this->createMock(Migrator::class);
$this->app->instance(Migrator::class, $this->migrator);

$this->command = new StatusCommand($this->migrator);
$this->command = new StatusCommand();
$this->command->setLaravel($this->app);
}

Expand Down

0 comments on commit 358e205

Please sign in to comment.