-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add commands to list one time tasks
- Loading branch information
Showing
9 changed files
with
244 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Shopware\Deployment\Command; | ||
|
||
use Shopware\Deployment\Services\OneTimeTasks; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\Table; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
#[AsCommand( | ||
name: 'one-time-task:list', | ||
description: 'List all executed one-time tasks', | ||
)] | ||
class OneTimeTaskListCommand extends Command | ||
{ | ||
public function __construct(private readonly OneTimeTasks $oneTimeTasks) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$list = $this->oneTimeTasks->getExecutedTasks(); | ||
|
||
$table = new Table($output); | ||
$table->setHeaders(['ID', 'Executed at']); | ||
|
||
foreach ($list as $id => $data) { | ||
$table->addRow([$id, $data['created_at']]); | ||
} | ||
|
||
$table->render(); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Shopware\Deployment\Command; | ||
|
||
use Shopware\Deployment\Services\OneTimeTasks; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
#[AsCommand( | ||
name: 'one-time-task:mark', | ||
description: 'Mark a one-time task as run without executing it', | ||
)] | ||
class OneTimeTaskMarkCommand extends Command | ||
{ | ||
public function __construct(private readonly OneTimeTasks $oneTimeTasks) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->addArgument('id', InputArgument::REQUIRED, 'The ID of the one-time task'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
|
||
try { | ||
$this->oneTimeTasks->markAsRun($input->getArgument('id')); | ||
} catch (\Throwable) { | ||
$io->error('Could not mark one-time task as run, as it has been marked as run before.'); | ||
|
||
return Command::FAILURE; | ||
} | ||
|
||
$io->success('One-time task marked as run'); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Shopware\Deployment\Command; | ||
|
||
use Shopware\Deployment\Services\OneTimeTasks; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
#[AsCommand( | ||
name: 'one-time-task:unmark', | ||
description: 'Unmark a one-time task as run without executing it', | ||
)] | ||
class OneTimeTaskUnmarkCommand extends Command | ||
{ | ||
public function __construct(private readonly OneTimeTasks $oneTimeTasks) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->addArgument('id', InputArgument::REQUIRED, 'The ID of the one-time task'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
|
||
$this->oneTimeTasks->remove($input->getArgument('id')); | ||
|
||
$io->success('One-time task mark has been removed, this script will be executed on next deployment.'); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Shopware\Deployment\Tests\Command; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
use Shopware\Deployment\Command\OneTimeTaskListCommand; | ||
use Shopware\Deployment\Services\OneTimeTasks; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
#[CoversClass(OneTimeTaskListCommand::class)] | ||
class OneTimeTaskListCommandTest extends TestCase | ||
{ | ||
public function testList(): void | ||
{ | ||
$taskService = $this->createMock(OneTimeTasks::class); | ||
$taskService->method('getExecutedTasks')->willReturn([ | ||
'test' => ['created_at' => '2021-01-01 00:00:00'], | ||
]); | ||
|
||
$cmd = new OneTimeTaskListCommand($taskService); | ||
$tester = new CommandTester($cmd); | ||
$tester->execute([]); | ||
|
||
$tester->assertCommandIsSuccessful(); | ||
static::assertStringContainsString('test', $tester->getDisplay()); | ||
static::assertStringContainsString('2021-01-01 00:00:00', $tester->getDisplay()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Shopware\Deployment\Tests\Command; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
use Shopware\Deployment\Command\OneTimeTaskMarkCommand; | ||
use Shopware\Deployment\Services\OneTimeTasks; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
#[CoversClass(OneTimeTaskMarkCommand::class)] | ||
class OneTimeTaskMarkCommandTest extends TestCase | ||
{ | ||
public function testMark(): void | ||
{ | ||
$taskService = $this->createMock(OneTimeTasks::class); | ||
$taskService | ||
->expects(static::once()) | ||
->method('markAsRun') | ||
->with('test'); | ||
|
||
$cmd = new OneTimeTaskMarkCommand($taskService); | ||
$tester = new CommandTester($cmd); | ||
$tester->execute(['id' => 'test']); | ||
|
||
$tester->assertCommandIsSuccessful(); | ||
} | ||
|
||
public function testMarkAgain(): void | ||
{ | ||
$taskService = $this->createMock(OneTimeTasks::class); | ||
$taskService | ||
->expects(static::once()) | ||
->method('markAsRun') | ||
->willThrowException(new \Exception('Task already marked as run')); | ||
|
||
$cmd = new OneTimeTaskMarkCommand($taskService); | ||
|
||
$tester = new CommandTester($cmd); | ||
$tester->execute(['id' => 'test']); | ||
|
||
static::assertSame(Command::FAILURE, $tester->getStatusCode()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Shopware\Deployment\Tests\Command; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
use Shopware\Deployment\Command\OneTimeTaskUnmarkCommand; | ||
use Shopware\Deployment\Services\OneTimeTasks; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
#[CoversClass(OneTimeTaskUnmarkCommand::class)] | ||
class OneTimeTaskUnmarkCommandTest extends TestCase | ||
{ | ||
public function testUnmark(): void | ||
{ | ||
$taskService = $this->createMock(OneTimeTasks::class); | ||
$taskService | ||
->expects(static::once()) | ||
->method('remove') | ||
->with('test'); | ||
|
||
$cmd = new OneTimeTaskUnmarkCommand($taskService); | ||
$tester = new CommandTester($cmd); | ||
$tester->execute(['id' => 'test']); | ||
|
||
$tester->assertCommandIsSuccessful(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters