Skip to content

Commit

Permalink
Create default variation based on parameters when none is configured
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
davidhoelzel authored Mar 8, 2024
1 parent 894ac3d commit b4df27d
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 5 deletions.
38 changes: 37 additions & 1 deletion src/Component/ComponentItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,44 @@ private function createItem(array $data): ComponentItem
->setDescription($data['description'] ?? '')
->setTags($data['tags'] ?? [])
->setParameters($data['parameters'] ?? [])
->setVariations($data['variations'] ?? []);
->setVariations($data['variations'] ?? [
'default' => $this->createVariationParameters($data['parameters'] ?? [])
]);

return $item;
}

public function createVariationParameters(array $parameters): array
{
$params = [];
foreach ($parameters as $name => $type) {
if (is_array($type)) {
$paramValue = $this->createVariationParameters($type);
} else {
$paramValue = $this->createParamValue($type);
}
$params[$name] = $paramValue;
}

return $params;
}

private function createParamValue(string $type): bool|int|float|string|null
{
switch (strtolower($type)) {
default:
return null;
case 'string':
return 'Hello World';
case 'int':
case 'integer':
return random_int(0, 100000);
case 'bool':
case 'boolean':
return [true, false][rand(0,1)];
case 'float':
case 'double':
return (float) rand(1, 1000) / 100;
}
}
}
4 changes: 2 additions & 2 deletions templates/component/_item.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@
</div>
<div class="twig-doc-variation-description">
<ul>
{% for key, value in variation %}
{% for name, value in variation %}
<li>
<b>{{ key }}:</b>
<b>{{ name }}:</b>
{% include '@TwigDoc/component/_parameter.html.twig' with {parameter: value} %}
</li>
{% endfor %}
Expand Down
85 changes: 83 additions & 2 deletions tests/Functional/Service/ComponentItemFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

namespace Qossmic\TwigDocBundle\Tests\Functional\Service;

use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\UsesClass;
use Qossmic\TwigDocBundle\Component\ComponentItem;
use Qossmic\TwigDocBundle\Component\ComponentItemFactory;
use Qossmic\TwigDocBundle\Exception\InvalidComponentConfigurationException;
use Qossmic\TwigDocBundle\Service\CategoryService;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use TypeError;

#[CoversNothing]
#[CoversClass(ComponentItemFactory::class)]
#[UsesClass(CategoryService::class)]
class ComponentItemFactoryTest extends KernelTestCase
{
#[DataProvider('getInvalidComponentConfigurationTestCases')]
Expand All @@ -25,6 +29,83 @@ public function testInvalidComponentConfiguration(array $componentData, string $
$service->create($componentData);
}

public function testComponentWithoutParameters(): void
{
$data = [
'name' => 'TestComponent',
'title' => 'Test title',
'description' => 'description',
'category' => 'MainCategory',
];

/** @var ComponentItemFactory $factory */
$factory = self::getContainer()->get('twig_doc.service.component_factory');

$component = $factory->create($data);

self::assertInstanceOf(ComponentItem::class, $component);
}

public function testFactoryCreatesDefaultVariationWhenMissingInConfig(): void
{
$data = [
'name' => 'TestComponent',
'title' => 'Test title',
'description' => 'description',
'category' => 'MainCategory',
];

/** @var ComponentItemFactory $factory */
$factory = self::getContainer()->get('twig_doc.service.component_factory');

$component = $factory->create($data);

self::assertArrayHasKey('default', $component->getVariations());
}

public function testFactoryCreatesDefaultVariationWithParameterTypes(): void
{
$data = [
'name' => 'TestComponent',
'title' => 'Test title',
'description' => 'description',
'category' => 'MainCategory',
'parameters' => [
'string' => 'String',
'float' => 'Float',
'double' => 'Double',
'int' => 'Int',
'integer' => 'Integer',
'bool' => 'Bool',
'boolean' => 'Boolean',
'unknown' => 'CustomType',
'complex' => [
'title' => 'String',
'amount' => 'Float',
]
]
];

/** @var ComponentItemFactory $factory */
$factory = self::getContainer()->get('twig_doc.service.component_factory');

$component = $factory->create($data);

self::assertInstanceOf(ComponentItem::class, $component);
self::assertIsBool($component->getVariations()['default']['bool']);
self::assertIsBool($component->getVariations()['default']['boolean']);
self::assertIsString($component->getVariations()['default']['string']);
self::assertIsInt($component->getVariations()['default']['int']);
self::assertIsInt($component->getVariations()['default']['integer']);
self::assertIsFloat($component->getVariations()['default']['float']);
self::assertIsFloat($component->getVariations()['default']['double']);
self::assertNull($component->getVariations()['default']['unknown']);

self::assertIsArray($component->getVariations()['default']['complex']);
self::assertIsString($component->getVariations()['default']['complex']['title']);
self::assertIsFloat($component->getVariations()['default']['complex']['amount']);
}

public static function getInvalidComponentConfigurationTestCases(): iterable
{
yield [
Expand Down

0 comments on commit b4df27d

Please sign in to comment.