|
7 | 7 | use Exception; |
8 | 8 | use PHPUnit\Framework\TestCase; |
9 | 9 | use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
| 10 | +use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; |
10 | 11 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
11 | 12 | use Symfony\Component\DependencyInjection\Definition; |
12 | 13 | use Symfony\Component\DependencyInjection\Exception\LogicException; |
|
17 | 18 | use Yokai\Batch\Bridge\Symfony\Framework\UserInterface\Templating\SonataAdminTemplating; |
18 | 19 | use Yokai\Batch\Bridge\Symfony\Framework\UserInterface\Templating\TemplatingInterface; |
19 | 20 | use Yokai\Batch\Bridge\Symfony\Messenger\DispatchMessageJobLauncher; |
| 21 | +use Yokai\Batch\Factory\JobExecutionParametersBuilder\ChainJobExecutionParametersBuilder; |
| 22 | +use Yokai\Batch\Factory\JobExecutionParametersBuilder\PerJobJobExecutionParametersBuilder; |
| 23 | +use Yokai\Batch\Factory\JobExecutionParametersBuilder\StaticJobExecutionParametersBuilder; |
| 24 | +use Yokai\Batch\Factory\JobExecutionParametersBuilderInterface; |
20 | 25 | use Yokai\Batch\Launcher\JobLauncherInterface; |
21 | 26 | use Yokai\Batch\Launcher\SimpleJobLauncher; |
22 | 27 | use Yokai\Batch\Storage\JobExecutionStorageInterface; |
@@ -387,4 +392,111 @@ public function errors(): \Generator |
387 | 392 | new ServiceNotFoundException('app.unknown'), |
388 | 393 | ]; |
389 | 394 | } |
| 395 | + |
| 396 | + /** |
| 397 | + * @dataProvider parameters |
| 398 | + */ |
| 399 | + public function testParameters(array $config, array|null $global, array|null $perJob): void |
| 400 | + { |
| 401 | + $container = $this->createContainer($config); |
| 402 | + |
| 403 | + $globalService = $this->getDefinition($container, 'yokai_batch.job_execution_parameters_builder.global'); |
| 404 | + if ($global !== null) { |
| 405 | + self::assertNotNull($globalService); |
| 406 | + self::assertSame(StaticJobExecutionParametersBuilder::class, $globalService->getClass()); |
| 407 | + self::assertTrue($globalService->hasTag('yokai_batch.job_execution_parameters_builder')); |
| 408 | + self::assertSame($global, $globalService->getArgument('$parameters')); |
| 409 | + } else { |
| 410 | + self::assertNull($globalService); |
| 411 | + } |
| 412 | + $perJobService = $this->getDefinition($container, 'yokai_batch.job_execution_parameters_builder.per_job'); |
| 413 | + if ($perJob !== null) { |
| 414 | + self::assertNotNull($perJobService); |
| 415 | + self::assertSame(PerJobJobExecutionParametersBuilder::class, $perJobService->getClass()); |
| 416 | + self::assertTrue($perJobService->hasTag('yokai_batch.job_execution_parameters_builder')); |
| 417 | + self::assertSame($perJob, $perJobService->getArgument('$perJobParameters')); |
| 418 | + } else { |
| 419 | + self::assertNull($perJobService); |
| 420 | + } |
| 421 | + $defaultService = $this->getDefinition($container, JobExecutionParametersBuilderInterface::class); |
| 422 | + self::assertNotNull($defaultService); |
| 423 | + self::assertSame(ChainJobExecutionParametersBuilder::class, $defaultService->getClass()); |
| 424 | + $defaultServiceBuilders = $defaultService->getArgument(0); |
| 425 | + self::assertTrue($defaultServiceBuilders instanceof TaggedIteratorArgument); |
| 426 | + /** @var TaggedIteratorArgument $defaultServiceBuilders */ |
| 427 | + self::assertSame('yokai_batch.job_execution_parameters_builder', $defaultServiceBuilders->getTag()); |
| 428 | + } |
| 429 | + |
| 430 | + public function parameters(): \Generator |
| 431 | + { |
| 432 | + yield 'Global parameters' => [ |
| 433 | + ['parameters' => ['global' => ['global' => true]]], |
| 434 | + ['global' => true], |
| 435 | + null, |
| 436 | + ]; |
| 437 | + yield 'Per job parameters' => [ |
| 438 | + ['parameters' => ['per_job' => ['job.foo' => ['foo' => true], 'job.bar' => ['bar' => true]]]], |
| 439 | + null, |
| 440 | + ['job.foo' => ['foo' => true], 'job.bar' => ['bar' => true]], |
| 441 | + ]; |
| 442 | + yield 'Global AND per job parameters' => [ |
| 443 | + ['parameters' => [ |
| 444 | + 'global' => ['global' => true], |
| 445 | + 'per_job' => ['job.foo' => ['foo' => true], 'job.bar' => ['bar' => true]], |
| 446 | + ]], |
| 447 | + ['global' => true], |
| 448 | + ['job.foo' => ['foo' => true], 'job.bar' => ['bar' => true]], |
| 449 | + ]; |
| 450 | + } |
| 451 | + |
| 452 | + /** |
| 453 | + * @dataProvider invalidParameters |
| 454 | + */ |
| 455 | + public function testInvalidParameters(array $config, \Exception $error): void |
| 456 | + { |
| 457 | + $this->expectExceptionObject($error); |
| 458 | + $this->createContainer($config); |
| 459 | + } |
| 460 | + |
| 461 | + public function invalidParameters(): \Generator |
| 462 | + { |
| 463 | + yield 'Per job parameters value must be an array' => [ |
| 464 | + ['parameters' => ['per_job' => ['job.foo' => 'string']]], |
| 465 | + new InvalidConfigurationException( |
| 466 | + 'Invalid configuration for path "yokai_batch.parameters.per_job.job.foo": Should be an array<string, mixed>.' |
| 467 | + ), |
| 468 | + ]; |
| 469 | + yield 'Per job parameters value must be a string indexed array' => [ |
| 470 | + ['parameters' => ['per_job' => ['job.foo' => [1, 2, 3]]]], |
| 471 | + new InvalidConfigurationException( |
| 472 | + 'Invalid configuration for path "yokai_batch.parameters.per_job.job.foo": Should be an array<string, mixed>.' |
| 473 | + ), |
| 474 | + ]; |
| 475 | + } |
| 476 | + |
| 477 | + private function createContainer(array $config, \Closure|null $configure = null): ContainerBuilder |
| 478 | + { |
| 479 | + $container = new ContainerBuilder(); |
| 480 | + if ($configure !== null) { |
| 481 | + $configure($container); |
| 482 | + } |
| 483 | + $container->registerExtension(new YokaiBatchExtension()); |
| 484 | + $container->loadFromExtension('yokai_batch', $config); |
| 485 | + |
| 486 | + $container->getCompilerPassConfig()->setOptimizationPasses([]); |
| 487 | + $container->getCompilerPassConfig()->setRemovingPasses([]); |
| 488 | + $container->getCompilerPassConfig()->setAfterRemovingPasses([]); |
| 489 | + $container->compile(); |
| 490 | + |
| 491 | + return $container; |
| 492 | + } |
| 493 | + |
| 494 | + private function getDefinition(ContainerBuilder $container, string $id): Definition|null |
| 495 | + { |
| 496 | + try { |
| 497 | + return $container->findDefinition($id); |
| 498 | + } catch (ServiceNotFoundException) { |
| 499 | + return null; |
| 500 | + } |
| 501 | + } |
390 | 502 | } |
0 commit comments