Skip to content

Commit

Permalink
Add possibility to pass empty variation value to keep param empty (#25)
Browse files Browse the repository at this point in the history
* #23 create fake variation value when not given
  • Loading branch information
davidhoelzel authored Apr 22, 2024
1 parent 3ac5d91 commit 251bb0a
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Component/ComponentItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Component/Data/Faker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Component/Data/Generator/ScalarGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions tests/Functional/Service/ComponentItemFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,12 @@ public function testCreateForParamWithOptionalVariationValue(): void
'parameters' => [
'stringParam' => 'String',
'secondParam' => 'String',
'optionalEmpty' => 'String',
],
'variations' => [
'variation1' => [
'stringParam' => 'Some cool hipster text',
'optionalEmpty' => '',
],
],
];
Expand All @@ -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
Expand Down

0 comments on commit 251bb0a

Please sign in to comment.