|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the CleverAge/ProcessBundle package. |
| 7 | + * |
| 8 | + * Copyright (c) Clever-Age |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CleverAge\ProcessBundle\Tests; |
| 15 | + |
| 16 | +use CleverAge\ProcessBundle\Configuration\ProcessConfiguration; |
| 17 | +use CleverAge\ProcessBundle\Configuration\TaskConfiguration; |
| 18 | +use CleverAge\ProcessBundle\Context\ContextualOptionResolver; |
| 19 | +use CleverAge\ProcessBundle\Model\AbstractConfigurableTask; |
| 20 | +use CleverAge\ProcessBundle\Model\ProcessHistory; |
| 21 | +use CleverAge\ProcessBundle\Model\ProcessState; |
| 22 | +use CleverAge\ProcessBundle\Task\ObjectUpdaterTask; |
| 23 | +use CleverAge\ProcessBundle\Task\Serialization\DeserializerTask; |
| 24 | +use CleverAge\ProcessBundle\Task\Serialization\NormalizerTask; |
| 25 | +use CleverAge\ProcessBundle\Task\Serialization\SerializerTask; |
| 26 | +use CleverAge\ProcessBundle\Task\SimpleBatchTask; |
| 27 | +use CleverAge\ProcessBundle\Transformer\Array\ArrayFilterTransformer; |
| 28 | +use CleverAge\ProcessBundle\Transformer\ConditionTrait; |
| 29 | +use CleverAge\ProcessBundle\Transformer\ConfigurableTransformerInterface; |
| 30 | +use CleverAge\ProcessBundle\Transformer\String\HashTransformer; |
| 31 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 32 | +use PHPUnit\Framework\TestCase; |
| 33 | +use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; |
| 34 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 35 | +use Symfony\Component\PropertyAccess\PropertyAccess; |
| 36 | +use Symfony\Component\PropertyAccess\PropertyPath; |
| 37 | +use Symfony\Component\Serializer\Serializer; |
| 38 | + |
| 39 | +/** |
| 40 | + * Options are validated when resolved, instead of failing later with an unclear error. |
| 41 | + */ |
| 42 | +#[\PHPUnit\Framework\Attributes\CoversClass(HashTransformer::class)] |
| 43 | +#[\PHPUnit\Framework\Attributes\CoversClass(ArrayFilterTransformer::class)] |
| 44 | +#[\PHPUnit\Framework\Attributes\CoversTrait(ConditionTrait::class)] |
| 45 | +#[\PHPUnit\Framework\Attributes\CoversClass(SimpleBatchTask::class)] |
| 46 | +#[\PHPUnit\Framework\Attributes\CoversClass(NormalizerTask::class)] |
| 47 | +#[\PHPUnit\Framework\Attributes\CoversClass(SerializerTask::class)] |
| 48 | +#[\PHPUnit\Framework\Attributes\CoversClass(DeserializerTask::class)] |
| 49 | +#[\PHPUnit\Framework\Attributes\CoversClass(ObjectUpdaterTask::class)] |
| 50 | +#[\PHPUnit\Framework\Attributes\UsesClass(AbstractConfigurableTask::class)] |
| 51 | +#[\PHPUnit\Framework\Attributes\UsesClass(ProcessConfiguration::class)] |
| 52 | +#[\PHPUnit\Framework\Attributes\UsesClass(TaskConfiguration::class)] |
| 53 | +#[\PHPUnit\Framework\Attributes\UsesClass(ContextualOptionResolver::class)] |
| 54 | +#[\PHPUnit\Framework\Attributes\UsesClass(ProcessHistory::class)] |
| 55 | +#[\PHPUnit\Framework\Attributes\UsesClass(ProcessState::class)] |
| 56 | +class OptionAllowedTypesTest extends TestCase |
| 57 | +{ |
| 58 | + /** |
| 59 | + * @return iterable<string, array{string, array<string, mixed>}> |
| 60 | + */ |
| 61 | + public static function invalidTransformerOptionsProvider(): iterable |
| 62 | + { |
| 63 | + yield 'hash raw_output' => [HashTransformer::class, ['algo' => 'md5', 'raw_output' => 'yes']]; |
| 64 | + yield 'condition empty' => [ArrayFilterTransformer::class, ['condition' => ['empty' => 'name']]]; |
| 65 | + yield 'condition not_empty' => [ArrayFilterTransformer::class, ['condition' => ['not_empty' => 'name']]]; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param class-string<ConfigurableTransformerInterface> $class |
| 70 | + * @param array<string, mixed> $options |
| 71 | + */ |
| 72 | + #[DataProvider('invalidTransformerOptionsProvider')] |
| 73 | + public function testInvalidTransformerOptionIsRejected(string $class, array $options): void |
| 74 | + { |
| 75 | + $this->expectException(InvalidOptionsException::class); |
| 76 | + |
| 77 | + $this->resolveTransformerOptions($class, $options); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @return iterable<string, array{string, array<string, mixed>}> |
| 82 | + */ |
| 83 | + public static function validTransformerOptionsProvider(): iterable |
| 84 | + { |
| 85 | + yield 'hash raw_output' => [HashTransformer::class, ['algo' => 'md5', 'raw_output' => true]]; |
| 86 | + yield 'condition empty' => [ArrayFilterTransformer::class, ['condition' => ['empty' => ['name' => null]]]]; |
| 87 | + yield 'condition not_empty' => [ArrayFilterTransformer::class, ['condition' => ['not_empty' => ['name' => null]]]]; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param class-string<ConfigurableTransformerInterface> $class |
| 92 | + * @param array<string, mixed> $options |
| 93 | + */ |
| 94 | + #[DataProvider('validTransformerOptionsProvider')] |
| 95 | + public function testValidTransformerOptionIsAccepted(string $class, array $options): void |
| 96 | + { |
| 97 | + self::assertNotEmpty($this->resolveTransformerOptions($class, $options)); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @return iterable<string, array{string, array<string, mixed>}> |
| 102 | + */ |
| 103 | + public static function invalidTaskOptionsProvider(): iterable |
| 104 | + { |
| 105 | + yield 'simple batch batch_count' => [SimpleBatchTask::class, ['batch_count' => '10']]; |
| 106 | + yield 'normalizer context' => [NormalizerTask::class, ['format' => 'json', 'context' => 'groups']]; |
| 107 | + yield 'serializer context' => [SerializerTask::class, ['format' => 'json', 'context' => 'groups']]; |
| 108 | + yield 'deserializer context' => [DeserializerTask::class, ['type' => 'array', 'format' => 'json', 'context' => 'groups']]; |
| 109 | + yield 'object updater property_path' => [ObjectUpdaterTask::class, ['property_path' => ['name']]]; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @param class-string<AbstractConfigurableTask> $class |
| 114 | + * @param array<string, mixed> $options |
| 115 | + */ |
| 116 | + #[DataProvider('invalidTaskOptionsProvider')] |
| 117 | + public function testInvalidTaskOptionIsRejected(string $class, array $options): void |
| 118 | + { |
| 119 | + $this->expectException(InvalidOptionsException::class); |
| 120 | + |
| 121 | + $this->initializeTask($class, $options); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @return iterable<string, array{string, array<string, mixed>}> |
| 126 | + */ |
| 127 | + public static function validTaskOptionsProvider(): iterable |
| 128 | + { |
| 129 | + yield 'simple batch batch_count' => [SimpleBatchTask::class, ['batch_count' => 10]]; |
| 130 | + yield 'simple batch null batch_count' => [SimpleBatchTask::class, ['batch_count' => null]]; |
| 131 | + yield 'normalizer context' => [NormalizerTask::class, ['format' => 'json', 'context' => ['groups' => ['a']]]]; |
| 132 | + yield 'serializer context' => [SerializerTask::class, ['format' => 'json', 'context' => []]]; |
| 133 | + yield 'deserializer context' => [DeserializerTask::class, ['type' => 'array', 'format' => 'json', 'context' => []]]; |
| 134 | + yield 'object updater string property_path' => [ObjectUpdaterTask::class, ['property_path' => 'name']]; |
| 135 | + yield 'object updater PropertyPath property_path' => [ObjectUpdaterTask::class, ['property_path' => new PropertyPath('name')]]; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * @param class-string<AbstractConfigurableTask> $class |
| 140 | + * @param array<string, mixed> $options |
| 141 | + */ |
| 142 | + #[DataProvider('validTaskOptionsProvider')] |
| 143 | + public function testValidTaskOptionIsAccepted(string $class, array $options): void |
| 144 | + { |
| 145 | + $this->expectNotToPerformAssertions(); |
| 146 | + |
| 147 | + $this->initializeTask($class, $options); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * @param class-string<ConfigurableTransformerInterface> $class |
| 152 | + * @param array<string, mixed> $options |
| 153 | + * |
| 154 | + * @return array<string, mixed> |
| 155 | + */ |
| 156 | + private function resolveTransformerOptions(string $class, array $options): array |
| 157 | + { |
| 158 | + $transformer = ArrayFilterTransformer::class === $class |
| 159 | + ? new ArrayFilterTransformer(PropertyAccess::createPropertyAccessor()) |
| 160 | + : new $class(); |
| 161 | + $resolver = new OptionsResolver(); |
| 162 | + $transformer->configureOptions($resolver); |
| 163 | + |
| 164 | + return $resolver->resolve($options); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * @param class-string<AbstractConfigurableTask> $class |
| 169 | + * @param array<string, mixed> $options |
| 170 | + */ |
| 171 | + private function initializeTask(string $class, array $options): void |
| 172 | + { |
| 173 | + $task = match ($class) { |
| 174 | + NormalizerTask::class, SerializerTask::class, DeserializerTask::class => new $class(new Serializer()), |
| 175 | + ObjectUpdaterTask::class => new ObjectUpdaterTask(PropertyAccess::createPropertyAccessor()), |
| 176 | + default => new $class(), |
| 177 | + }; |
| 178 | + |
| 179 | + $processConfiguration = new ProcessConfiguration('test', []); |
| 180 | + $state = new ProcessState($processConfiguration, new ProcessHistory($processConfiguration)); |
| 181 | + $state->setContextualOptionResolver(new ContextualOptionResolver()); |
| 182 | + $state->setContext([]); |
| 183 | + $state->setTaskConfiguration(new TaskConfiguration('task', $class, $options)); |
| 184 | + $task->initialize($state); |
| 185 | + } |
| 186 | +} |
0 commit comments