From 320fe213412f7b8cf8cd61f68317c812120f5739 Mon Sep 17 00:00:00 2001 From: david Date: Fri, 19 Apr 2024 11:06:16 +0200 Subject: [PATCH 1/3] #23 create fake variation value when not given --- src/Component/ComponentItemFactory.php | 2 +- .../Service/ComponentItemFactoryTest.php | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/Component/ComponentItemFactory.php b/src/Component/ComponentItemFactory.php index b3e5c96..dd2afd5 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] ?? []); } $params += $paramValue; } diff --git a/tests/Functional/Service/ComponentItemFactoryTest.php b/tests/Functional/Service/ComponentItemFactoryTest.php index 8ff435d..1bdb19f 100644 --- a/tests/Functional/Service/ComponentItemFactoryTest.php +++ b/tests/Functional/Service/ComponentItemFactoryTest.php @@ -207,6 +207,38 @@ public function testCreateForObjectParameter(): void static::assertEquals('Mitsubishi', $car->getManufacturer()->getName()); } + public function testCreateForParamWithOptionalVariationValue() + { + $data = [ + 'name' => 'component', + 'title' => 'title', + 'description' => 'description', + 'category' => 'MainCategory', + 'path' => 'path', + 'renderPath' => 'renderPath', + 'parameters' => [ + 'stringParam' => 'String', + 'secondParam' => 'String', + ], + 'variations' => [ + 'variation1' => [ + 'stringParam' => 'Some cool hipster text', + ], + ], + ]; + + /** @var ComponentItemFactory $factory */ + $factory = self::getContainer()->get('twig_doc.service.component_factory'); + + $item = $factory->create($data); + $variations = $item->getVariations(); + + self::assertIsArray($variations); + self::assertArrayHasKey('variation1', $variations); + self::assertArrayHasKey('secondParam', $variations['variation1']); + self::assertIsString($variations['variation1']['secondParam']); + } + public static function getInvalidComponentConfigurationTestCases(): iterable { yield [ From e67887cdad4d66bd0d4188fe9838d527cf968bb4 Mon Sep 17 00:00:00 2001 From: david Date: Fri, 19 Apr 2024 11:08:33 +0200 Subject: [PATCH 2/3] #23 cs-fixer --- tests/Functional/Service/ComponentItemFactoryTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Functional/Service/ComponentItemFactoryTest.php b/tests/Functional/Service/ComponentItemFactoryTest.php index 1bdb19f..5653864 100644 --- a/tests/Functional/Service/ComponentItemFactoryTest.php +++ b/tests/Functional/Service/ComponentItemFactoryTest.php @@ -207,7 +207,7 @@ public function testCreateForObjectParameter(): void static::assertEquals('Mitsubishi', $car->getManufacturer()->getName()); } - public function testCreateForParamWithOptionalVariationValue() + public function testCreateForParamWithOptionalVariationValue(): void { $data = [ 'name' => 'component', From 56f2af245922fb1ee15dbf21e469da00fe718c97 Mon Sep 17 00:00:00 2001 From: david Date: Fri, 19 Apr 2024 12:56:08 +0200 Subject: [PATCH 3/3] #23 optional parameters are now converted to null when empty; when null, random values are generated as before --- 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