From cf1c154588184b08e85c8ea69b86c83b544ff2be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20Eugon=C3=A9?= Date: Wed, 8 Jan 2025 10:33:20 +0100 Subject: [PATCH] Add a component to build JobParameters defaults at contruction time (#123) * Add a component to build JobParameters defaults at contruction time * wip * Fixed symfony configuration structure * Add tests for Job configuration * Fix checkstyle --- .../tests/RunCommandJobLauncherTest.php | 3 +- .../tests/RunJobCommandTest.php | 3 +- .../src/DependencyInjection/Configuration.php | 47 ++++++++ .../YokaiBatchExtension.php | 23 ++++ .../src/Resources/services/global/alias.xml | 3 + .../src/Resources/services/global/core.xml | 6 + .../YokaiBatchExtensionTest.php | 112 ++++++++++++++++++ .../tests/DispatchMessageJobLauncherTest.php | 12 +- .../tests/LaunchJobMessageHandlerTest.php | 6 +- src/batch/src/Factory/JobExecutionFactory.php | 2 + .../ChainJobExecutionParametersBuilder.php | 37 ++++++ .../NullJobExecutionParametersBuilder.php | 18 +++ .../PerJobJobExecutionParametersBuilder.php | 26 ++++ .../StaticJobExecutionParametersBuilder.php | 26 ++++ ...JobExecutionParametersBuilderInterface.php | 21 ++++ .../tests/Factory/JobExecutionFactoryTest.php | 6 +- ...ChainJobExecutionParametersBuilderTest.php | 36 ++++++ .../NullJobExecutionParametersBuilderTest.php | 24 ++++ ...erJobJobExecutionParametersBuilderTest.php | 31 +++++ ...taticJobExecutionParametersBuilderTest.php | 24 ++++ .../tests/Job/JobExecutionAccessorTest.php | 10 +- .../tests/Launcher/SimpleJobLauncherTest.php | 10 +- tests/integration/JobTestCase.php | 3 +- 23 files changed, 474 insertions(+), 15 deletions(-) create mode 100644 src/batch/src/Factory/JobExecutionParametersBuilder/ChainJobExecutionParametersBuilder.php create mode 100644 src/batch/src/Factory/JobExecutionParametersBuilder/NullJobExecutionParametersBuilder.php create mode 100644 src/batch/src/Factory/JobExecutionParametersBuilder/PerJobJobExecutionParametersBuilder.php create mode 100644 src/batch/src/Factory/JobExecutionParametersBuilder/StaticJobExecutionParametersBuilder.php create mode 100644 src/batch/src/Factory/JobExecutionParametersBuilderInterface.php create mode 100644 src/batch/tests/Factory/JobExecutionParametersBuilder/ChainJobExecutionParametersBuilderTest.php create mode 100644 src/batch/tests/Factory/JobExecutionParametersBuilder/NullJobExecutionParametersBuilderTest.php create mode 100644 src/batch/tests/Factory/JobExecutionParametersBuilder/PerJobJobExecutionParametersBuilderTest.php create mode 100644 src/batch/tests/Factory/JobExecutionParametersBuilder/StaticJobExecutionParametersBuilderTest.php diff --git a/src/batch-symfony-console/tests/RunCommandJobLauncherTest.php b/src/batch-symfony-console/tests/RunCommandJobLauncherTest.php index c34cb9d0..679e3b00 100644 --- a/src/batch-symfony-console/tests/RunCommandJobLauncherTest.php +++ b/src/batch-symfony-console/tests/RunCommandJobLauncherTest.php @@ -11,6 +11,7 @@ use Yokai\Batch\Bridge\Symfony\Console\CommandRunner; use Yokai\Batch\Bridge\Symfony\Console\RunCommandJobLauncher; use Yokai\Batch\Factory\JobExecutionFactory; +use Yokai\Batch\Factory\JobExecutionParametersBuilder\NullJobExecutionParametersBuilder; use Yokai\Batch\Factory\UniqidJobExecutionIdGenerator; use Yokai\Batch\Test\Storage\InMemoryJobExecutionStorage; @@ -29,7 +30,7 @@ public function testLaunch(): void ->shouldBeCalledTimes(1); $launcher = new RunCommandJobLauncher( - new JobExecutionFactory(new UniqidJobExecutionIdGenerator()), + new JobExecutionFactory(new UniqidJobExecutionIdGenerator(), new NullJobExecutionParametersBuilder()), $commandRunner->reveal(), $storage = new InMemoryJobExecutionStorage(), 'test.log' diff --git a/src/batch-symfony-console/tests/RunJobCommandTest.php b/src/batch-symfony-console/tests/RunJobCommandTest.php index 1e178327..7de49f4f 100644 --- a/src/batch-symfony-console/tests/RunJobCommandTest.php +++ b/src/batch-symfony-console/tests/RunJobCommandTest.php @@ -14,6 +14,7 @@ use Yokai\Batch\Bridge\Symfony\Console\RunJobCommand; use Yokai\Batch\Exception\UnexpectedValueException; use Yokai\Batch\Factory\JobExecutionFactory; +use Yokai\Batch\Factory\JobExecutionParametersBuilder\NullJobExecutionParametersBuilder; use Yokai\Batch\Factory\UniqidJobExecutionIdGenerator; use Yokai\Batch\Job\JobExecutionAccessor; use Yokai\Batch\Job\JobExecutor; @@ -38,7 +39,7 @@ protected function setUp(): void $this->job = $this->prophesize(JobInterface::class); $this->accessor = new JobExecutionAccessor( - new JobExecutionFactory(new UniqidJobExecutionIdGenerator()), + new JobExecutionFactory(new UniqidJobExecutionIdGenerator(), new NullJobExecutionParametersBuilder()), new InMemoryJobExecutionStorage(), ); $this->executor = new JobExecutor( diff --git a/src/batch-symfony-framework/src/DependencyInjection/Configuration.php b/src/batch-symfony-framework/src/DependencyInjection/Configuration.php index da4a56f0..e2c36113 100644 --- a/src/batch-symfony-framework/src/DependencyInjection/Configuration.php +++ b/src/batch-symfony-framework/src/DependencyInjection/Configuration.php @@ -14,6 +14,7 @@ * @phpstan-type Config array{ * storage: StorageConfig, * launcher: LauncherConfig, + * parameters: ParametersConfig, * ui: UserInterfaceConfig, * } * @phpstan-type StorageConfig array{ @@ -31,6 +32,10 @@ * default: string|null, * launchers: array, * } + * @phpstan-type ParametersConfig array{ + * global: array, + * per_job: array>, + * } * @phpstan-type UserInterfaceConfig array{ * enabled: bool, * security: array{ @@ -59,6 +64,7 @@ public function getConfigTreeBuilder(): TreeBuilder ->children() ->append($this->storage()) ->append($this->launcher()) + ->append($this->parameters()) ->append($this->ui()) ->end() ; @@ -131,6 +137,47 @@ private function launcher(): ArrayNodeDefinition return $node; } + private function parameters(): ArrayNodeDefinition + { + /** @var ArrayNodeDefinition $node */ + $node = (new TreeBuilder('parameters'))->getRootNode(); + + $isStringAssociativeArray = function (mixed $value): bool { + if (!\is_array($value)) { + return false; + } + + foreach ($value as $key => $unused) { + if (!\is_string($key)) { + return false; + } + } + + return true; + }; + + $node + ->addDefaultsIfNotSet() + ->children() + ->arrayNode('global') + ->useAttributeAsKey('name') + ->variablePrototype() + ->end() + ->end() + ->arrayNode('per_job') + ->useAttributeAsKey('name') + ->variablePrototype() + ->validate() + ->ifTrue(fn(mixed $value) => !$isStringAssociativeArray($value)) + ->thenInvalid('Should be an array.') + ->end() + ->end() + ->end() + ; + + return $node; + } + private function ui(): ArrayNodeDefinition { /** @var ArrayNodeDefinition $node */ diff --git a/src/batch-symfony-framework/src/DependencyInjection/YokaiBatchExtension.php b/src/batch-symfony-framework/src/DependencyInjection/YokaiBatchExtension.php index b5d087cc..41a7edbc 100644 --- a/src/batch-symfony-framework/src/DependencyInjection/YokaiBatchExtension.php +++ b/src/batch-symfony-framework/src/DependencyInjection/YokaiBatchExtension.php @@ -22,6 +22,8 @@ use Yokai\Batch\Bridge\Symfony\Framework\UserInterface\Templating\ConfigurableTemplating; use Yokai\Batch\Bridge\Symfony\Framework\UserInterface\Templating\SonataAdminTemplating; use Yokai\Batch\Bridge\Symfony\Framework\UserInterface\Templating\TemplatingInterface; +use Yokai\Batch\Factory\JobExecutionParametersBuilder\PerJobJobExecutionParametersBuilder; +use Yokai\Batch\Factory\JobExecutionParametersBuilder\StaticJobExecutionParametersBuilder; use Yokai\Batch\Launcher\JobLauncherInterface; use Yokai\Batch\Storage\FilesystemJobExecutionStorage; use Yokai\Batch\Storage\JobExecutionStorageInterface; @@ -34,6 +36,7 @@ * @phpstan-import-type Config from Configuration * @phpstan-import-type StorageConfig from Configuration * @phpstan-import-type LauncherConfig from Configuration + * @phpstan-import-type ParametersConfig from Configuration * @phpstan-import-type UserInterfaceConfig from Configuration */ final class YokaiBatchExtension extends Extension @@ -64,6 +67,7 @@ public function load(array $configs, ContainerBuilder $container): void $this->configureStorage($container, $config['storage']); $this->configureLauncher($container, $config['launcher']); + $this->configureParameters($container, $config['parameters']); $this->configureUserInterface($container, $loader, $config['ui']); $container->registerAliasForArgument('yokai_batch.logger', LoggerInterface::class, 'yokaiBatchLogger'); @@ -189,6 +193,25 @@ private function configureLauncher(ContainerBuilder $container, array $config): ); } + /** + * @param ParametersConfig $config + */ + private function configureParameters(ContainerBuilder $container, array $config): void + { + if ($config['global'] !== []) { + $container->register('yokai_batch.job_execution_parameters_builder.global') + ->setClass(StaticJobExecutionParametersBuilder::class) + ->setArgument('$parameters', $config['global']) + ->addTag('yokai_batch.job_execution_parameters_builder'); + } + if ($config['per_job'] !== []) { + $container->register('yokai_batch.job_execution_parameters_builder.per_job') + ->setClass(PerJobJobExecutionParametersBuilder::class) + ->setArgument('$perJobParameters', $config['per_job']) + ->addTag('yokai_batch.job_execution_parameters_builder'); + } + } + /** * @param UserInterfaceConfig $config */ diff --git a/src/batch-symfony-framework/src/Resources/services/global/alias.xml b/src/batch-symfony-framework/src/Resources/services/global/alias.xml index 66671cc0..6d89983a 100644 --- a/src/batch-symfony-framework/src/Resources/services/global/alias.xml +++ b/src/batch-symfony-framework/src/Resources/services/global/alias.xml @@ -21,5 +21,8 @@ + + diff --git a/src/batch-symfony-framework/src/Resources/services/global/core.xml b/src/batch-symfony-framework/src/Resources/services/global/core.xml index b9b2ac40..bf35089b 100644 --- a/src/batch-symfony-framework/src/Resources/services/global/core.xml +++ b/src/batch-symfony-framework/src/Resources/services/global/core.xml @@ -10,6 +10,7 @@ + + + + + diff --git a/src/batch-symfony-framework/tests/DependencyInjection/YokaiBatchExtensionTest.php b/src/batch-symfony-framework/tests/DependencyInjection/YokaiBatchExtensionTest.php index 15ace87c..57d85ed5 100644 --- a/src/batch-symfony-framework/tests/DependencyInjection/YokaiBatchExtensionTest.php +++ b/src/batch-symfony-framework/tests/DependencyInjection/YokaiBatchExtensionTest.php @@ -7,6 +7,7 @@ use Exception; use PHPUnit\Framework\TestCase; use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; +use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Exception\LogicException; @@ -17,6 +18,10 @@ use Yokai\Batch\Bridge\Symfony\Framework\UserInterface\Templating\SonataAdminTemplating; use Yokai\Batch\Bridge\Symfony\Framework\UserInterface\Templating\TemplatingInterface; use Yokai\Batch\Bridge\Symfony\Messenger\DispatchMessageJobLauncher; +use Yokai\Batch\Factory\JobExecutionParametersBuilder\ChainJobExecutionParametersBuilder; +use Yokai\Batch\Factory\JobExecutionParametersBuilder\PerJobJobExecutionParametersBuilder; +use Yokai\Batch\Factory\JobExecutionParametersBuilder\StaticJobExecutionParametersBuilder; +use Yokai\Batch\Factory\JobExecutionParametersBuilderInterface; use Yokai\Batch\Launcher\JobLauncherInterface; use Yokai\Batch\Launcher\SimpleJobLauncher; use Yokai\Batch\Storage\JobExecutionStorageInterface; @@ -387,4 +392,111 @@ public function errors(): \Generator new ServiceNotFoundException('app.unknown'), ]; } + + /** + * @dataProvider parameters + */ + public function testParameters(array $config, array|null $global, array|null $perJob): void + { + $container = $this->createContainer($config); + + $globalService = $this->getDefinition($container, 'yokai_batch.job_execution_parameters_builder.global'); + if ($global !== null) { + self::assertNotNull($globalService); + self::assertSame(StaticJobExecutionParametersBuilder::class, $globalService->getClass()); + self::assertTrue($globalService->hasTag('yokai_batch.job_execution_parameters_builder')); + self::assertSame($global, $globalService->getArgument('$parameters')); + } else { + self::assertNull($globalService); + } + $perJobService = $this->getDefinition($container, 'yokai_batch.job_execution_parameters_builder.per_job'); + if ($perJob !== null) { + self::assertNotNull($perJobService); + self::assertSame(PerJobJobExecutionParametersBuilder::class, $perJobService->getClass()); + self::assertTrue($perJobService->hasTag('yokai_batch.job_execution_parameters_builder')); + self::assertSame($perJob, $perJobService->getArgument('$perJobParameters')); + } else { + self::assertNull($perJobService); + } + $defaultService = $this->getDefinition($container, JobExecutionParametersBuilderInterface::class); + self::assertNotNull($defaultService); + self::assertSame(ChainJobExecutionParametersBuilder::class, $defaultService->getClass()); + $defaultServiceBuilders = $defaultService->getArgument(0); + self::assertTrue($defaultServiceBuilders instanceof TaggedIteratorArgument); + /** @var TaggedIteratorArgument $defaultServiceBuilders */ + self::assertSame('yokai_batch.job_execution_parameters_builder', $defaultServiceBuilders->getTag()); + } + + public function parameters(): \Generator + { + yield 'Global parameters' => [ + ['parameters' => ['global' => ['global' => true]]], + ['global' => true], + null, + ]; + yield 'Per job parameters' => [ + ['parameters' => ['per_job' => ['job.foo' => ['foo' => true], 'job.bar' => ['bar' => true]]]], + null, + ['job.foo' => ['foo' => true], 'job.bar' => ['bar' => true]], + ]; + yield 'Global AND per job parameters' => [ + ['parameters' => [ + 'global' => ['global' => true], + 'per_job' => ['job.foo' => ['foo' => true], 'job.bar' => ['bar' => true]], + ]], + ['global' => true], + ['job.foo' => ['foo' => true], 'job.bar' => ['bar' => true]], + ]; + } + + /** + * @dataProvider invalidParameters + */ + public function testInvalidParameters(array $config, \Exception $error): void + { + $this->expectExceptionObject($error); + $this->createContainer($config); + } + + public function invalidParameters(): \Generator + { + yield 'Per job parameters value must be an array' => [ + ['parameters' => ['per_job' => ['job.foo' => 'string']]], + new InvalidConfigurationException( + 'Invalid configuration for path "yokai_batch.parameters.per_job.job.foo": Should be an array.' + ), + ]; + yield 'Per job parameters value must be a string indexed array' => [ + ['parameters' => ['per_job' => ['job.foo' => [1, 2, 3]]]], + new InvalidConfigurationException( + 'Invalid configuration for path "yokai_batch.parameters.per_job.job.foo": Should be an array.' + ), + ]; + } + + private function createContainer(array $config, \Closure|null $configure = null): ContainerBuilder + { + $container = new ContainerBuilder(); + if ($configure !== null) { + $configure($container); + } + $container->registerExtension(new YokaiBatchExtension()); + $container->loadFromExtension('yokai_batch', $config); + + $container->getCompilerPassConfig()->setOptimizationPasses([]); + $container->getCompilerPassConfig()->setRemovingPasses([]); + $container->getCompilerPassConfig()->setAfterRemovingPasses([]); + $container->compile(); + + return $container; + } + + private function getDefinition(ContainerBuilder $container, string $id): Definition|null + { + try { + return $container->findDefinition($id); + } catch (ServiceNotFoundException) { + return null; + } + } } diff --git a/src/batch-symfony-messenger/tests/DispatchMessageJobLauncherTest.php b/src/batch-symfony-messenger/tests/DispatchMessageJobLauncherTest.php index 3c9569c8..d6b9b2bd 100644 --- a/src/batch-symfony-messenger/tests/DispatchMessageJobLauncherTest.php +++ b/src/batch-symfony-messenger/tests/DispatchMessageJobLauncherTest.php @@ -10,6 +10,7 @@ use Yokai\Batch\Bridge\Symfony\Messenger\DispatchMessageJobLauncher; use Yokai\Batch\Bridge\Symfony\Messenger\LaunchJobMessage; use Yokai\Batch\Factory\JobExecutionFactory; +use Yokai\Batch\Factory\JobExecutionParametersBuilder\NullJobExecutionParametersBuilder; use Yokai\Batch\Factory\UniqidJobExecutionIdGenerator; use Yokai\Batch\Test\Factory\SequenceJobExecutionIdGenerator; use Yokai\Batch\Test\Storage\InMemoryJobExecutionStorage; @@ -21,7 +22,7 @@ final class DispatchMessageJobLauncherTest extends TestCase public function testLaunch(): void { $jobLauncher = new DispatchMessageJobLauncher( - new JobExecutionFactory(new UniqidJobExecutionIdGenerator()), + new JobExecutionFactory(new UniqidJobExecutionIdGenerator(), new NullJobExecutionParametersBuilder()), $storage = new InMemoryJobExecutionStorage(), $messageBus = new BufferingMessageBus() ); @@ -41,9 +42,12 @@ public function testLaunch(): void public function testLaunchWithNoId(): void { $jobLauncher = new DispatchMessageJobLauncher( - new JobExecutionFactory(new SequenceJobExecutionIdGenerator(['123456789'])), + new JobExecutionFactory( + new SequenceJobExecutionIdGenerator(['123456789']), + new NullJobExecutionParametersBuilder(), + ), $storage = new InMemoryJobExecutionStorage(), - $messageBus = new BufferingMessageBus() + $messageBus = new BufferingMessageBus(), ); $jobExecutionFromLauncher = $jobLauncher->launch('testing'); @@ -60,7 +64,7 @@ public function testLaunchWithNoId(): void public function testLaunchAndMessengerFail(): void { $jobLauncher = new DispatchMessageJobLauncher( - new JobExecutionFactory(new UniqidJobExecutionIdGenerator()), + new JobExecutionFactory(new UniqidJobExecutionIdGenerator(), new NullJobExecutionParametersBuilder()), $storage = new InMemoryJobExecutionStorage(), new FailingMessageBus(new TransportException('This is a test')) ); diff --git a/src/batch-symfony-messenger/tests/LaunchJobMessageHandlerTest.php b/src/batch-symfony-messenger/tests/LaunchJobMessageHandlerTest.php index 7da6449e..d9d591c4 100644 --- a/src/batch-symfony-messenger/tests/LaunchJobMessageHandlerTest.php +++ b/src/batch-symfony-messenger/tests/LaunchJobMessageHandlerTest.php @@ -9,6 +9,7 @@ use Yokai\Batch\Bridge\Symfony\Messenger\LaunchJobMessage; use Yokai\Batch\Bridge\Symfony\Messenger\LaunchJobMessageHandler; use Yokai\Batch\Factory\JobExecutionFactory; +use Yokai\Batch\Factory\JobExecutionParametersBuilder\NullJobExecutionParametersBuilder; use Yokai\Batch\Job\JobExecutionAccessor; use Yokai\Batch\Job\JobExecutor; use Yokai\Batch\Job\JobInterface; @@ -35,7 +36,10 @@ public function execute(JobExecution $jobExecution): void $jobExecutionStorage = new InMemoryJobExecutionStorage(); $handler = new LaunchJobMessageHandler( new JobExecutionAccessor( - new JobExecutionFactory(new SequenceJobExecutionIdGenerator(['123456'])), + new JobExecutionFactory( + new SequenceJobExecutionIdGenerator(['123456']), + new NullJobExecutionParametersBuilder(), + ), $jobExecutionStorage, ), new JobExecutor( diff --git a/src/batch/src/Factory/JobExecutionFactory.php b/src/batch/src/Factory/JobExecutionFactory.php index a63b1041..9d1d75ff 100644 --- a/src/batch/src/Factory/JobExecutionFactory.php +++ b/src/batch/src/Factory/JobExecutionFactory.php @@ -14,6 +14,7 @@ final class JobExecutionFactory { public function __construct( private JobExecutionIdGeneratorInterface $idGenerator, + private JobExecutionParametersBuilderInterface $parametersBuilder, ) { } @@ -24,6 +25,7 @@ public function __construct( */ public function create(string $name, array $configuration = []): JobExecution { + $configuration = $configuration + $this->parametersBuilder->build($name); /** @var string $id */ $id = $configuration['_id'] ??= $this->idGenerator->generate(); diff --git a/src/batch/src/Factory/JobExecutionParametersBuilder/ChainJobExecutionParametersBuilder.php b/src/batch/src/Factory/JobExecutionParametersBuilder/ChainJobExecutionParametersBuilder.php new file mode 100644 index 00000000..8c222dbe --- /dev/null +++ b/src/batch/src/Factory/JobExecutionParametersBuilder/ChainJobExecutionParametersBuilder.php @@ -0,0 +1,37 @@ + + */ + private readonly iterable $builders, + ) { + } + + public function build(string $name): array + { + $values = []; + foreach ($this->builders as $builder) { + $values[] = $builder->build($name); + } + + if ($values === []) { + return []; + } + + return \array_merge(...$values); + } +} diff --git a/src/batch/src/Factory/JobExecutionParametersBuilder/NullJobExecutionParametersBuilder.php b/src/batch/src/Factory/JobExecutionParametersBuilder/NullJobExecutionParametersBuilder.php new file mode 100644 index 00000000..afd50968 --- /dev/null +++ b/src/batch/src/Factory/JobExecutionParametersBuilder/NullJobExecutionParametersBuilder.php @@ -0,0 +1,18 @@ +> + */ + private readonly array $perJobParameters, + ) { + } + + public function build(string $name): array + { + return $this->perJobParameters[$name] ?? []; + } +} diff --git a/src/batch/src/Factory/JobExecutionParametersBuilder/StaticJobExecutionParametersBuilder.php b/src/batch/src/Factory/JobExecutionParametersBuilder/StaticJobExecutionParametersBuilder.php new file mode 100644 index 00000000..472910ac --- /dev/null +++ b/src/batch/src/Factory/JobExecutionParametersBuilder/StaticJobExecutionParametersBuilder.php @@ -0,0 +1,26 @@ + + */ + private readonly array $parameters, + ) { + } + + public function build(string $name): array + { + return $this->parameters; + } +} diff --git a/src/batch/src/Factory/JobExecutionParametersBuilderInterface.php b/src/batch/src/Factory/JobExecutionParametersBuilderInterface.php new file mode 100644 index 00000000..33933336 --- /dev/null +++ b/src/batch/src/Factory/JobExecutionParametersBuilderInterface.php @@ -0,0 +1,21 @@ + + */ + public function build(string $name): array; +} diff --git a/src/batch/tests/Factory/JobExecutionFactoryTest.php b/src/batch/tests/Factory/JobExecutionFactoryTest.php index 894fd87a..75b2e9cd 100644 --- a/src/batch/tests/Factory/JobExecutionFactoryTest.php +++ b/src/batch/tests/Factory/JobExecutionFactoryTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use Yokai\Batch\Factory\JobExecutionFactory; +use Yokai\Batch\Factory\JobExecutionParametersBuilder\NullJobExecutionParametersBuilder; use Yokai\Batch\Factory\UniqidJobExecutionIdGenerator; use Yokai\Batch\JobExecution; @@ -13,7 +14,10 @@ class JobExecutionFactoryTest extends TestCase { public function testCreate(): void { - $executionFactory = new JobExecutionFactory(new UniqidJobExecutionIdGenerator()); + $executionFactory = new JobExecutionFactory( + new UniqidJobExecutionIdGenerator(), + new NullJobExecutionParametersBuilder() + ); $executionWithoutConfig = $executionFactory->create('export'); self::assertSame('export', $executionWithoutConfig->getJobName()); diff --git a/src/batch/tests/Factory/JobExecutionParametersBuilder/ChainJobExecutionParametersBuilderTest.php b/src/batch/tests/Factory/JobExecutionParametersBuilder/ChainJobExecutionParametersBuilderTest.php new file mode 100644 index 00000000..efc57826 --- /dev/null +++ b/src/batch/tests/Factory/JobExecutionParametersBuilder/ChainJobExecutionParametersBuilderTest.php @@ -0,0 +1,36 @@ + ['foo' => true], + 'job.bar' => ['bar' => true], + ]), + new StaticJobExecutionParametersBuilder(['default' => true]), + ]); + self::assertSame( + ['foo' => true, 'default' => true], + $builder->build('job.foo'), + ); + self::assertSame( + ['bar' => true, 'default' => true], + $builder->build('job.bar'), + ); + self::assertSame( + ['default' => true], + $builder->build('job.baz'), + ); + } +} diff --git a/src/batch/tests/Factory/JobExecutionParametersBuilder/NullJobExecutionParametersBuilderTest.php b/src/batch/tests/Factory/JobExecutionParametersBuilder/NullJobExecutionParametersBuilderTest.php new file mode 100644 index 00000000..864b1710 --- /dev/null +++ b/src/batch/tests/Factory/JobExecutionParametersBuilder/NullJobExecutionParametersBuilderTest.php @@ -0,0 +1,24 @@ +build('job.foo'), + ); + self::assertSame( + [], + $builder->build('job.baz'), + ); + } +} diff --git a/src/batch/tests/Factory/JobExecutionParametersBuilder/PerJobJobExecutionParametersBuilderTest.php b/src/batch/tests/Factory/JobExecutionParametersBuilder/PerJobJobExecutionParametersBuilderTest.php new file mode 100644 index 00000000..234df185 --- /dev/null +++ b/src/batch/tests/Factory/JobExecutionParametersBuilder/PerJobJobExecutionParametersBuilderTest.php @@ -0,0 +1,31 @@ + ['string' => 'foo', 'number' => 1, 'bool' => false], + 'job.bar' => ['string' => 'bar', 'number' => 2, 'bool' => true], + ]); + self::assertSame( + ['string' => 'foo', 'number' => 1, 'bool' => false], + $builder->build('job.foo'), + ); + self::assertSame( + ['string' => 'bar', 'number' => 2, 'bool' => true], + $builder->build('job.bar'), + ); + self::assertSame( + [], + $builder->build('job.baz'), + ); + } +} diff --git a/src/batch/tests/Factory/JobExecutionParametersBuilder/StaticJobExecutionParametersBuilderTest.php b/src/batch/tests/Factory/JobExecutionParametersBuilder/StaticJobExecutionParametersBuilderTest.php new file mode 100644 index 00000000..d22e3779 --- /dev/null +++ b/src/batch/tests/Factory/JobExecutionParametersBuilder/StaticJobExecutionParametersBuilderTest.php @@ -0,0 +1,24 @@ + 'foo', 'number' => 1, 'bool' => false]); + self::assertSame( + ['string' => 'foo', 'number' => 1, 'bool' => false], + $builder->build('job.foo'), + ); + self::assertSame( + ['string' => 'foo', 'number' => 1, 'bool' => false], + $builder->build('job.baz'), + ); + } +} diff --git a/src/batch/tests/Job/JobExecutionAccessorTest.php b/src/batch/tests/Job/JobExecutionAccessorTest.php index fa0e36e6..f316df25 100644 --- a/src/batch/tests/Job/JobExecutionAccessorTest.php +++ b/src/batch/tests/Job/JobExecutionAccessorTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use Yokai\Batch\Factory\JobExecutionFactory; +use Yokai\Batch\Factory\JobExecutionParametersBuilder\NullJobExecutionParametersBuilder; use Yokai\Batch\Job\JobExecutionAccessor; use Yokai\Batch\JobExecution; use Yokai\Batch\Test\Factory\SequenceJobExecutionIdGenerator; @@ -16,10 +17,13 @@ class JobExecutionAccessorTest extends TestCase public function test(): void { $accessor = new JobExecutionAccessor( - new JobExecutionFactory(new SequenceJobExecutionIdGenerator(['123', '456'])), + new JobExecutionFactory( + new SequenceJobExecutionIdGenerator(['123', '456']), + new NullJobExecutionParametersBuilder(), + ), $storage = new InMemoryJobExecutionStorage( - $existing = JobExecution::createRoot('abc', 'test') - ) + $existing = JobExecution::createRoot('abc', 'test'), + ), ); self::assertSame($existing, $accessor->get('test', ['_id' => 'abc'])); diff --git a/src/batch/tests/Launcher/SimpleJobLauncherTest.php b/src/batch/tests/Launcher/SimpleJobLauncherTest.php index dcf5ec57..95215a0c 100644 --- a/src/batch/tests/Launcher/SimpleJobLauncherTest.php +++ b/src/batch/tests/Launcher/SimpleJobLauncherTest.php @@ -8,6 +8,7 @@ use Prophecy\PhpUnit\ProphecyTrait; use Yokai\Batch\BatchStatus; use Yokai\Batch\Factory\JobExecutionFactory; +use Yokai\Batch\Factory\JobExecutionParametersBuilder\NullJobExecutionParametersBuilder; use Yokai\Batch\Job\JobExecutionAccessor; use Yokai\Batch\Job\JobExecutor; use Yokai\Batch\Job\JobInterface; @@ -26,14 +27,17 @@ public function test(): void $launcher = new SimpleJobLauncher( new JobExecutionAccessor( - new JobExecutionFactory(new SequenceJobExecutionIdGenerator(['123'])), + new JobExecutionFactory( + new SequenceJobExecutionIdGenerator(['123']), + new NullJobExecutionParametersBuilder(), + ), $jobExecutionStorage = new InMemoryJobExecutionStorage(), ), new JobExecutor( JobRegistry::fromJobArray(['phpunit' => $job->reveal()]), $jobExecutionStorage, - null - ) + null, + ), ); $execution = $launcher->launch('phpunit'); diff --git a/tests/integration/JobTestCase.php b/tests/integration/JobTestCase.php index 07f51253..d5a75412 100644 --- a/tests/integration/JobTestCase.php +++ b/tests/integration/JobTestCase.php @@ -7,6 +7,7 @@ use PHPUnit\Framework\TestCase; use Yokai\Batch\BatchStatus; use Yokai\Batch\Factory\JobExecutionFactory; +use Yokai\Batch\Factory\JobExecutionParametersBuilder\NullJobExecutionParametersBuilder; use Yokai\Batch\Factory\UniqidJobExecutionIdGenerator; use Yokai\Batch\Failure; use Yokai\Batch\Job\JobExecutionAccessor; @@ -51,7 +52,7 @@ public function testExecuteJob(JobExecutionStorageInterface $jobExecutionStorage $launcher = new SimpleJobLauncher( new JobExecutionAccessor( - new JobExecutionFactory(new UniqidJobExecutionIdGenerator()), + new JobExecutionFactory(new UniqidJobExecutionIdGenerator(), new NullJobExecutionParametersBuilder()), $jobExecutionStorage ), self::createJobExecutor($jobExecutionStorage, [$jobName => $job])