From 251bb0aa0eb3a584446a019a95d4a27cfd2fd36d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20H=C3=B6lzel?= <56548897+davidhoelzel@users.noreply.github.com> Date: Mon, 22 Apr 2024 09:55:54 +0200 Subject: [PATCH] Add possibility to pass empty variation value to keep param empty (#25) * #23 create fake variation value when not given --- src/Component/ComponentItemFactory.php | 2 +- src/Component/Data/Faker.php | 4 ++-- src/Component/Data/Generator/ScalarGenerator.php | 2 +- tests/Functional/Service/ComponentItemFactoryTest.php | 3 +++ 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Component/ComponentItemFactory.php b/src/Component/ComponentItemFactory.php index dd2afd5..d5b1030 100644 --- a/src/Component/ComponentItemFactory.php +++ b/src/Component/ComponentItemFactory.php @@ -129,7 +129,7 @@ private function createVariationParameters(array $parameters, array $variation): if (\is_array($type)) { $paramValue = $this->createVariationParameters($type, $variation[$name] ?? []); } else { - $paramValue = $this->faker->getFakeData([$name => $type], $variation[$name] ?? []); + $paramValue = $this->faker->getFakeData([$name => $type], $variation[$name] ?? null); } $params += $paramValue; } diff --git a/src/Component/Data/Faker.php b/src/Component/Data/Faker.php index b951d1e..482198a 100644 --- a/src/Component/Data/Faker.php +++ b/src/Component/Data/Faker.php @@ -17,7 +17,7 @@ public function __construct( ) { } - public function getFakeData(array $params, mixed $variation = []): array + public function getFakeData(array $params, mixed $variation = null): array { return $this->createFakeData($params, $variation); } @@ -34,7 +34,7 @@ private function createFakeData(array $params, mixed $variation): array } foreach ($this->generators as $generator) { - if (\array_key_exists($name, $result) || !$generator->supports($type)) { + if (\array_key_exists($name, $result)) { continue; } if ($generator->supports($type, $variation)) { diff --git a/src/Component/Data/Generator/ScalarGenerator.php b/src/Component/Data/Generator/ScalarGenerator.php index 64f68c6..6c187e3 100644 --- a/src/Component/Data/Generator/ScalarGenerator.php +++ b/src/Component/Data/Generator/ScalarGenerator.php @@ -21,7 +21,7 @@ public function __construct() public function supports(string $type, mixed $context = null): bool { // context normally contains the param values for a specific variation, so we generate random values only for non-set params - return empty($context) && \in_array(strtolower($type), [ + return null === $context && \in_array(strtolower($type), [ Type::BUILTIN_TYPE_BOOL, Type::BUILTIN_TYPE_FLOAT, Type::BUILTIN_TYPE_INT, diff --git a/tests/Functional/Service/ComponentItemFactoryTest.php b/tests/Functional/Service/ComponentItemFactoryTest.php index 5653864..e6b8a4e 100644 --- a/tests/Functional/Service/ComponentItemFactoryTest.php +++ b/tests/Functional/Service/ComponentItemFactoryTest.php @@ -219,10 +219,12 @@ public function testCreateForParamWithOptionalVariationValue(): void 'parameters' => [ 'stringParam' => 'String', 'secondParam' => 'String', + 'optionalEmpty' => 'String', ], 'variations' => [ 'variation1' => [ 'stringParam' => 'Some cool hipster text', + 'optionalEmpty' => '', ], ], ]; @@ -237,6 +239,7 @@ public function testCreateForParamWithOptionalVariationValue(): void self::assertArrayHasKey('variation1', $variations); self::assertArrayHasKey('secondParam', $variations['variation1']); self::assertIsString($variations['variation1']['secondParam']); + self::assertNull($variations['variation1']['optionalEmpty']); } public static function getInvalidComponentConfigurationTestCases(): iterable