-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a way for JobExecution storage to be setup before used (#107)
* Add a way for JobExecution storage to be setup before used * Fixed code style * Test DoctrineDBALJobExecutionStorage::createSchema deprecated method * Fixed wrong usage of magic constants * Removed deprecation test * Proper test without deprecation
- Loading branch information
1 parent
91d1785
commit 9fe827c
Showing
8 changed files
with
222 additions
and
15 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
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,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yokai\Batch\Bridge\Symfony\Console; | ||
|
||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Yokai\Batch\Storage\JobExecutionStorageInterface; | ||
use Yokai\Batch\Storage\SetupableJobExecutionStorageInterface; | ||
|
||
/** | ||
* Prepare the required infrastructure for the job execution storage. | ||
*/ | ||
#[AsCommand(name: 'yokai:batch:setup-storage', description: 'Prepare the required infrastructure for the storage')] | ||
final class SetupStorageCommand extends Command | ||
{ | ||
public function __construct( | ||
private JobExecutionStorageInterface $storage, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setHelp( | ||
<<<EOF | ||
The <info>%command.name%</info> command setups the job execution storage: | ||
<info>php %command.full_name%</info> | ||
EOF | ||
) | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
|
||
if ($this->storage instanceof SetupableJobExecutionStorageInterface) { | ||
$this->storage->setup(); | ||
$io->success('The storage was set up successfully.'); | ||
} else { | ||
$io->note('The storage does not support setup.'); | ||
} | ||
|
||
return self::SUCCESS; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/batch-symfony-console/tests/SetupStorageCommandTest.php
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,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yokai\Batch\Tests\Bridge\Symfony\Console; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
use Yokai\Batch\Bridge\Symfony\Console\SetupStorageCommand; | ||
use Yokai\Batch\Exception\JobExecutionNotFoundException; | ||
use Yokai\Batch\JobExecution; | ||
use Yokai\Batch\Storage\JobExecutionStorageInterface; | ||
use Yokai\Batch\Storage\SetupableJobExecutionStorageInterface; | ||
|
||
final class SetupStorageCommandTest extends TestCase | ||
{ | ||
public function testSetupRequired(): void | ||
{ | ||
$this->execute( | ||
$storage = new class() implements | ||
JobExecutionStorageInterface, | ||
SetupableJobExecutionStorageInterface { | ||
public bool $wasSetup = false; | ||
|
||
public function setup(): void | ||
{ | ||
$this->wasSetup = true; | ||
} | ||
|
||
public function store(JobExecution $execution): void | ||
{ | ||
} | ||
|
||
public function remove(JobExecution $execution): void | ||
{ | ||
} | ||
|
||
public function retrieve(string $jobName, string $executionId): JobExecution | ||
{ | ||
throw new JobExecutionNotFoundException($jobName, $executionId); | ||
} | ||
}, | ||
'[OK] The storage was set up successfully.', | ||
); | ||
self::assertTrue($storage->wasSetup); | ||
} | ||
|
||
public function testSetupNotRequired(): void | ||
{ | ||
$this->execute( | ||
new class() implements JobExecutionStorageInterface { | ||
public function store(JobExecution $execution): void | ||
{ | ||
} | ||
|
||
public function remove(JobExecution $execution): void | ||
{ | ||
} | ||
|
||
public function retrieve(string $jobName, string $executionId): JobExecution | ||
{ | ||
throw new JobExecutionNotFoundException($jobName, $executionId); | ||
} | ||
}, | ||
'! [NOTE] The storage does not support setup.', | ||
); | ||
} | ||
|
||
private function execute(JobExecutionStorageInterface $storage, string $expected): void | ||
{ | ||
$tester = new CommandTester(new SetupStorageCommand($storage)); | ||
$tester->execute([]); | ||
$tester->assertCommandIsSuccessful(); | ||
self::assertSame($expected, \trim($tester->getDisplay(true))); | ||
} | ||
} |
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
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yokai\Batch\Tests\Bridge\Symfony\Framework; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
|
||
final class CliTest extends KernelTestCase | ||
{ | ||
public function testRegisteredCommands(): void | ||
{ | ||
$names = \array_keys( | ||
(new Application(self::bootKernel()))->all('yokai'), | ||
); | ||
\sort($names); | ||
self::assertSame( | ||
[ | ||
'yokai:batch:run', | ||
'yokai:batch:setup-storage', | ||
], | ||
$names, | ||
); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/batch/src/Storage/SetupableJobExecutionStorageInterface.php
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yokai\Batch\Storage; | ||
|
||
/** | ||
* A job execution having this interface tells the developers it should be setuped before being used. | ||
*/ | ||
interface SetupableJobExecutionStorageInterface | ||
{ | ||
/** | ||
* Setup the storage. | ||
*/ | ||
public function setup(): void; | ||
} |