Skip to content

Commit 251bb0a

Browse files
authored
Add possibility to pass empty variation value to keep param empty (#25)
* #23 create fake variation value when not given
1 parent 3ac5d91 commit 251bb0a

File tree

4 files changed

+7
-4
lines changed

4 files changed

+7
-4
lines changed

src/Component/ComponentItemFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private function createVariationParameters(array $parameters, array $variation):
129129
if (\is_array($type)) {
130130
$paramValue = $this->createVariationParameters($type, $variation[$name] ?? []);
131131
} else {
132-
$paramValue = $this->faker->getFakeData([$name => $type], $variation[$name] ?? []);
132+
$paramValue = $this->faker->getFakeData([$name => $type], $variation[$name] ?? null);
133133
}
134134
$params += $paramValue;
135135
}

src/Component/Data/Faker.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function __construct(
1717
) {
1818
}
1919

20-
public function getFakeData(array $params, mixed $variation = []): array
20+
public function getFakeData(array $params, mixed $variation = null): array
2121
{
2222
return $this->createFakeData($params, $variation);
2323
}
@@ -34,7 +34,7 @@ private function createFakeData(array $params, mixed $variation): array
3434
}
3535

3636
foreach ($this->generators as $generator) {
37-
if (\array_key_exists($name, $result) || !$generator->supports($type)) {
37+
if (\array_key_exists($name, $result)) {
3838
continue;
3939
}
4040
if ($generator->supports($type, $variation)) {

src/Component/Data/Generator/ScalarGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __construct()
2121
public function supports(string $type, mixed $context = null): bool
2222
{
2323
// context normally contains the param values for a specific variation, so we generate random values only for non-set params
24-
return empty($context) && \in_array(strtolower($type), [
24+
return null === $context && \in_array(strtolower($type), [
2525
Type::BUILTIN_TYPE_BOOL,
2626
Type::BUILTIN_TYPE_FLOAT,
2727
Type::BUILTIN_TYPE_INT,

tests/Functional/Service/ComponentItemFactoryTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,12 @@ public function testCreateForParamWithOptionalVariationValue(): void
219219
'parameters' => [
220220
'stringParam' => 'String',
221221
'secondParam' => 'String',
222+
'optionalEmpty' => 'String',
222223
],
223224
'variations' => [
224225
'variation1' => [
225226
'stringParam' => 'Some cool hipster text',
227+
'optionalEmpty' => '',
226228
],
227229
],
228230
];
@@ -237,6 +239,7 @@ public function testCreateForParamWithOptionalVariationValue(): void
237239
self::assertArrayHasKey('variation1', $variations);
238240
self::assertArrayHasKey('secondParam', $variations['variation1']);
239241
self::assertIsString($variations['variation1']['secondParam']);
242+
self::assertNull($variations['variation1']['optionalEmpty']);
240243
}
241244

242245
public static function getInvalidComponentConfigurationTestCases(): iterable

0 commit comments

Comments
 (0)