Skip to content

Commit b4df27d

Browse files
authored
Create default variation based on parameters when none is configured
It is now possible to have a component configuration without explicit variation definition. When no variations are given, a default one is created, based on the components parameters.
1 parent 894ac3d commit b4df27d

3 files changed

Lines changed: 122 additions & 5 deletions

File tree

src/Component/ComponentItemFactory.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,44 @@ private function createItem(array $data): ComponentItem
5252
->setDescription($data['description'] ?? '')
5353
->setTags($data['tags'] ?? [])
5454
->setParameters($data['parameters'] ?? [])
55-
->setVariations($data['variations'] ?? []);
55+
->setVariations($data['variations'] ?? [
56+
'default' => $this->createVariationParameters($data['parameters'] ?? [])
57+
]);
5658

5759
return $item;
5860
}
61+
62+
public function createVariationParameters(array $parameters): array
63+
{
64+
$params = [];
65+
foreach ($parameters as $name => $type) {
66+
if (is_array($type)) {
67+
$paramValue = $this->createVariationParameters($type);
68+
} else {
69+
$paramValue = $this->createParamValue($type);
70+
}
71+
$params[$name] = $paramValue;
72+
}
73+
74+
return $params;
75+
}
76+
77+
private function createParamValue(string $type): bool|int|float|string|null
78+
{
79+
switch (strtolower($type)) {
80+
default:
81+
return null;
82+
case 'string':
83+
return 'Hello World';
84+
case 'int':
85+
case 'integer':
86+
return random_int(0, 100000);
87+
case 'bool':
88+
case 'boolean':
89+
return [true, false][rand(0,1)];
90+
case 'float':
91+
case 'double':
92+
return (float) rand(1, 1000) / 100;
93+
}
94+
}
5995
}

templates/component/_item.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@
5858
</div>
5959
<div class="twig-doc-variation-description">
6060
<ul>
61-
{% for key, value in variation %}
61+
{% for name, value in variation %}
6262
<li>
63-
<b>{{ key }}:</b>
63+
<b>{{ name }}:</b>
6464
{% include '@TwigDoc/component/_parameter.html.twig' with {parameter: value} %}
6565
</li>
6666
{% endfor %}

tests/Functional/Service/ComponentItemFactoryTest.php

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22

33
namespace Qossmic\TwigDocBundle\Tests\Functional\Service;
44

5-
use PHPUnit\Framework\Attributes\CoversNothing;
5+
use PHPUnit\Framework\Attributes\CoversClass;
66
use PHPUnit\Framework\Attributes\DataProvider;
7+
use PHPUnit\Framework\Attributes\UsesClass;
8+
use Qossmic\TwigDocBundle\Component\ComponentItem;
79
use Qossmic\TwigDocBundle\Component\ComponentItemFactory;
810
use Qossmic\TwigDocBundle\Exception\InvalidComponentConfigurationException;
11+
use Qossmic\TwigDocBundle\Service\CategoryService;
912
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1013
use TypeError;
1114

12-
#[CoversNothing]
15+
#[CoversClass(ComponentItemFactory::class)]
16+
#[UsesClass(CategoryService::class)]
1317
class ComponentItemFactoryTest extends KernelTestCase
1418
{
1519
#[DataProvider('getInvalidComponentConfigurationTestCases')]
@@ -25,6 +29,83 @@ public function testInvalidComponentConfiguration(array $componentData, string $
2529
$service->create($componentData);
2630
}
2731

32+
public function testComponentWithoutParameters(): void
33+
{
34+
$data = [
35+
'name' => 'TestComponent',
36+
'title' => 'Test title',
37+
'description' => 'description',
38+
'category' => 'MainCategory',
39+
];
40+
41+
/** @var ComponentItemFactory $factory */
42+
$factory = self::getContainer()->get('twig_doc.service.component_factory');
43+
44+
$component = $factory->create($data);
45+
46+
self::assertInstanceOf(ComponentItem::class, $component);
47+
}
48+
49+
public function testFactoryCreatesDefaultVariationWhenMissingInConfig(): void
50+
{
51+
$data = [
52+
'name' => 'TestComponent',
53+
'title' => 'Test title',
54+
'description' => 'description',
55+
'category' => 'MainCategory',
56+
];
57+
58+
/** @var ComponentItemFactory $factory */
59+
$factory = self::getContainer()->get('twig_doc.service.component_factory');
60+
61+
$component = $factory->create($data);
62+
63+
self::assertArrayHasKey('default', $component->getVariations());
64+
}
65+
66+
public function testFactoryCreatesDefaultVariationWithParameterTypes(): void
67+
{
68+
$data = [
69+
'name' => 'TestComponent',
70+
'title' => 'Test title',
71+
'description' => 'description',
72+
'category' => 'MainCategory',
73+
'parameters' => [
74+
'string' => 'String',
75+
'float' => 'Float',
76+
'double' => 'Double',
77+
'int' => 'Int',
78+
'integer' => 'Integer',
79+
'bool' => 'Bool',
80+
'boolean' => 'Boolean',
81+
'unknown' => 'CustomType',
82+
'complex' => [
83+
'title' => 'String',
84+
'amount' => 'Float',
85+
]
86+
]
87+
];
88+
89+
/** @var ComponentItemFactory $factory */
90+
$factory = self::getContainer()->get('twig_doc.service.component_factory');
91+
92+
$component = $factory->create($data);
93+
94+
self::assertInstanceOf(ComponentItem::class, $component);
95+
self::assertIsBool($component->getVariations()['default']['bool']);
96+
self::assertIsBool($component->getVariations()['default']['boolean']);
97+
self::assertIsString($component->getVariations()['default']['string']);
98+
self::assertIsInt($component->getVariations()['default']['int']);
99+
self::assertIsInt($component->getVariations()['default']['integer']);
100+
self::assertIsFloat($component->getVariations()['default']['float']);
101+
self::assertIsFloat($component->getVariations()['default']['double']);
102+
self::assertNull($component->getVariations()['default']['unknown']);
103+
104+
self::assertIsArray($component->getVariations()['default']['complex']);
105+
self::assertIsString($component->getVariations()['default']['complex']['title']);
106+
self::assertIsFloat($component->getVariations()['default']['complex']['amount']);
107+
}
108+
28109
public static function getInvalidComponentConfigurationTestCases(): iterable
29110
{
30111
yield [

0 commit comments

Comments
 (0)