Skip to content

Commit cf8ed79

Browse files
authoredDec 6, 2023
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
1 parent e1c54ad commit cf8ed79

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
 

‎src/SetupStorageCommand.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yokai\Batch\Bridge\Symfony\Console;
6+
7+
use Symfony\Component\Console\Attribute\AsCommand;
8+
use Symfony\Component\Console\Command\Command;
9+
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
use Symfony\Component\Console\Style\SymfonyStyle;
12+
use Yokai\Batch\Storage\JobExecutionStorageInterface;
13+
use Yokai\Batch\Storage\SetupableJobExecutionStorageInterface;
14+
15+
/**
16+
* Prepare the required infrastructure for the job execution storage.
17+
*/
18+
#[AsCommand(name: 'yokai:batch:setup-storage', description: 'Prepare the required infrastructure for the storage')]
19+
final class SetupStorageCommand extends Command
20+
{
21+
public function __construct(
22+
private JobExecutionStorageInterface $storage,
23+
) {
24+
parent::__construct();
25+
}
26+
27+
protected function configure(): void
28+
{
29+
$this
30+
->setHelp(
31+
<<<EOF
32+
The <info>%command.name%</info> command setups the job execution storage:
33+
34+
<info>php %command.full_name%</info>
35+
EOF
36+
)
37+
;
38+
}
39+
40+
protected function execute(InputInterface $input, OutputInterface $output): int
41+
{
42+
$io = new SymfonyStyle($input, $output);
43+
44+
if ($this->storage instanceof SetupableJobExecutionStorageInterface) {
45+
$this->storage->setup();
46+
$io->success('The storage was set up successfully.');
47+
} else {
48+
$io->note('The storage does not support setup.');
49+
}
50+
51+
return self::SUCCESS;
52+
}
53+
}

‎tests/SetupStorageCommandTest.php

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yokai\Batch\Tests\Bridge\Symfony\Console;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Symfony\Component\Console\Tester\CommandTester;
9+
use Yokai\Batch\Bridge\Symfony\Console\SetupStorageCommand;
10+
use Yokai\Batch\Exception\JobExecutionNotFoundException;
11+
use Yokai\Batch\JobExecution;
12+
use Yokai\Batch\Storage\JobExecutionStorageInterface;
13+
use Yokai\Batch\Storage\SetupableJobExecutionStorageInterface;
14+
15+
final class SetupStorageCommandTest extends TestCase
16+
{
17+
public function testSetupRequired(): void
18+
{
19+
$this->execute(
20+
$storage = new class() implements
21+
JobExecutionStorageInterface,
22+
SetupableJobExecutionStorageInterface {
23+
public bool $wasSetup = false;
24+
25+
public function setup(): void
26+
{
27+
$this->wasSetup = true;
28+
}
29+
30+
public function store(JobExecution $execution): void
31+
{
32+
}
33+
34+
public function remove(JobExecution $execution): void
35+
{
36+
}
37+
38+
public function retrieve(string $jobName, string $executionId): JobExecution
39+
{
40+
throw new JobExecutionNotFoundException($jobName, $executionId);
41+
}
42+
},
43+
'[OK] The storage was set up successfully.',
44+
);
45+
self::assertTrue($storage->wasSetup);
46+
}
47+
48+
public function testSetupNotRequired(): void
49+
{
50+
$this->execute(
51+
new class() implements JobExecutionStorageInterface {
52+
public function store(JobExecution $execution): void
53+
{
54+
}
55+
56+
public function remove(JobExecution $execution): void
57+
{
58+
}
59+
60+
public function retrieve(string $jobName, string $executionId): JobExecution
61+
{
62+
throw new JobExecutionNotFoundException($jobName, $executionId);
63+
}
64+
},
65+
'! [NOTE] The storage does not support setup.',
66+
);
67+
}
68+
69+
private function execute(JobExecutionStorageInterface $storage, string $expected): void
70+
{
71+
$tester = new CommandTester(new SetupStorageCommand($storage));
72+
$tester->execute([]);
73+
$tester->assertCommandIsSuccessful();
74+
self::assertSame($expected, \trim($tester->getDisplay(true)));
75+
}
76+
}

0 commit comments

Comments
 (0)