Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to pass empty variation value to keep param empty #25

Merged
merged 4 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading