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

#27 fixed missing sub-key in template data for nested arrays #28

Merged
merged 2 commits into from
Apr 24, 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 @@ -127,7 +127,7 @@ private function createVariationParameters(array $parameters, array $variation):

foreach ($parameters as $name => $type) {
if (\is_array($type)) {
$paramValue = $this->createVariationParameters($type, $variation[$name] ?? []);
$paramValue[$name] = $this->createVariationParameters($type, $variation[$name] ?? []);
} else {
$paramValue = $this->faker->getFakeData([$name => $type], $variation[$name] ?? null);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Component/Data/Faker.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private function createFakeData(array $params, mixed $variation): array

if (!\array_key_exists($name, $result)) {
// set from variation
$result[$name] = $variation;
$result[$name] = $variation[$name] ?? $variation;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Component/Data/Generator/NullGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class NullGenerator implements GeneratorInterface
{
public function supports(string $type, mixed $context = null): bool
{
return empty($context);
return null === $context || '' === $context;
}

public function generate(string $type, mixed $context = null): null
Expand Down
37 changes: 37 additions & 0 deletions tests/Functional/Service/ComponentItemFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,43 @@ public function testCreateForParamWithOptionalVariationValue(): void
self::assertNull($variations['variation1']['optionalEmpty']);
}

public function testCreateForArrayParameter(): void
{
$data = [
'name' => 'TestComponent',
'title' => 'Test title',
'description' => 'description',
'category' => 'MainCategory',
'path' => 'path/to/component',
'renderPath' => 'path/to/component',
'parameters' => [
'arrayParam' => [
'param1' => 'String',
'param2' => 'Boolean',
],
],
'variations' => [
'variation1' => [
'arrayParam' => [
'param1' => 'Some cool hipster text',
'param2' => false,
],
],
],
];

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

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

self::assertIsArray($variations);
self::assertArrayHasKey('variation1', $variations);
self::assertEquals('Some cool hipster text', $variations['variation1']['arrayParam']['param1']);
self::assertFalse($variations['variation1']['arrayParam']['param2']);
}

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