Skip to content

Commit eccfd1c

Browse files
authored
fake parameters are only created when feature is enabled (#30)
Parameters not provided in a variation will be provided as NULL
1 parent d27c9ff commit eccfd1c

File tree

17 files changed

+188
-54
lines changed

17 files changed

+188
-54
lines changed

docs/BundleConfiguration.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The bundle provides following defaults:
77
```yaml
88
twig_doc:
99
doc_identifier: TWIG_DOC
10+
use_fake_parameter: false
1011
directories:
1112
- '%twig.default_path%/components'
1213
categories:
@@ -51,6 +52,12 @@ MY_DOC_IDENTIFIER#}
5152
<div class="fancy-component"></div>
5253
```
5354

55+
### Fake Parameters
56+
57+
By default, the creation of fake parameters is disabled!
58+
59+
When enabled, the bundle fakes parameters based on parameter-config of the component
60+
5461
### Categories
5562

5663
The bundle groups components into categories and optionally into sub-categories.

docs/ComponentConfiguration.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,46 @@ components:
8585
8686
### Parameter Provision
8787
88-
You must provide the types of your template parameters in the configuration.
88+
You must provide the types of your template parameters in the configuration.
8989
As twig templates are not aware of types, there is no other possibility at the moment.
90+
91+
Unless you enable use_fake_parameter in the bundle configuration, parameter-values for the components
92+
must be configured in each variation for this component!
93+
94+
`use_fake_parameter: false`
95+
96+
When a parameter is not provided in a variation, it will be set to NULL.
97+
98+
Objects will not be provided as objects, only as arrays based on the variation configuration.
99+
100+
e.g:
101+
```yaml
102+
...
103+
parameters:
104+
car: App\Entity\Car
105+
owner: String
106+
variation:
107+
blue:
108+
car:
109+
color: blue
110+
manufacturer:
111+
name: Mitsubishi
112+
```
113+
will result that car is provided as an array when the component is rendered:
114+
```php
115+
[
116+
'color' => 'blue',
117+
'manufacturer' => [
118+
'name' => 'Mitsubishi'
119+
],
120+
'owner' => null
121+
]
122+
```
123+
124+
When you do not call fancy object-methods in your component-templates, this should suffice for most cases.
125+
126+
`use_fake_paramter: true`
127+
90128
As this bundle makes use of [Nelmio/Alice](https://github.com/nelmio/alice) and [FakerPhp](https://fakerphp.github.io), all you need to do is
91129
define the types of your parameters in the component configuration.
92130
The bundle will take care of creating a set of parameters for every component.

phpunit.xml.dist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
4-
bootstrap="tests/bootstrap.php"
54
cacheDirectory=".phpunit.cache"
65
executionOrder="depends,defects"
76
requireCoverageMetadata="true"
8-
beStrictAboutCoverageMetadata="true"
97
beStrictAboutOutputDuringTests="true"
108
failOnRisky="true"
119
failOnWarning="true"

src/Component/ComponentItemFactory.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
public function __construct(
1616
private ValidatorInterface $validator,
1717
private CategoryService $categoryService,
18-
private Faker $faker
18+
private Faker $faker,
19+
private bool $useFakeParams
1920
) {
2021
}
2122

@@ -105,7 +106,7 @@ private function parseVariations(?array $variations, ?array $parameters): array
105106

106107
if (!$variations) {
107108
return [
108-
'default' => $this->faker->getFakeData($parameters),
109+
'default' => $this->createVariationParameters($parameters, []),
109110
];
110111
}
111112

@@ -125,11 +126,34 @@ private function createVariationParameters(array $parameters, array $variation):
125126
{
126127
$params = [];
127128

129+
if ($this->useFakeParams) {
130+
return $this->createFakeParamValues($parameters, $variation);
131+
}
132+
133+
foreach ($parameters as $name => $type) {
134+
if (\is_array($type)) {
135+
$params[$name] = $this->createVariationParameters($type, $variation[$name] ?? []);
136+
} else {
137+
$params[$name] = $variation[$name] ?? null;
138+
}
139+
}
140+
141+
return $params;
142+
}
143+
144+
private function createFakeParamValues(array $parameters, array $variation): array
145+
{
146+
$params = [];
147+
128148
foreach ($parameters as $name => $type) {
129149
if (\is_array($type)) {
130150
$paramValue[$name] = $this->createVariationParameters($type, $variation[$name] ?? []);
131151
} else {
132-
$paramValue = $this->faker->getFakeData([$name => $type], $variation[$name] ?? null);
152+
if (\array_key_exists($name, $variation) && null === $variation[$name]) {
153+
$paramValue = [$name => null];
154+
} else {
155+
$paramValue = $this->faker->getFakeData([$name => $type], $variation[$name] ?? null);
156+
}
133157
}
134158
$params += $paramValue;
135159
}

src/Component/Data/Generator/NullGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class NullGenerator implements GeneratorInterface
1010
{
1111
public function supports(string $type, mixed $context = null): bool
1212
{
13-
return null === $context || '' === $context;
13+
return true;
1414
}
1515

1616
public function generate(string $type, mixed $context = null): null

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function getConfigTreeBuilder(): TreeBuilder
2121
->thenInvalid('The twig_doc documentation identifier must match \w (regex)')
2222
->end()
2323
->end()
24+
->booleanNode('use_fake_parameter')->defaultFalse()->end()
2425
->arrayNode('breakpoints')
2526
->defaultValue([
2627
'small' => 240,

src/DependencyInjection/TwigDocExtension.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,8 @@ public function load(array $configs, ContainerBuilder $container): void
3636

3737
$definition = $container->getDefinition('twig_doc.service.faker');
3838
$definition->setArgument('$generators', tagged_iterator('twig_doc.data_generator'));
39+
40+
$definition = $container->getDefinition('twig_doc.service.component_factory');
41+
$definition->setArgument('$useFakeParams', $config['use_fake_parameter']);
3942
}
4043
}

templates/component/_invalid_component.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
{% if loop.first %}
3636
<ul>
3737
{% endif %}
38-
<li>{{ key }}: {{ value }}</li>
38+
<li>{{ key }}</li>
3939
{% if loop.last %}
4040
</ul>
4141
{% endif %}

tests/Functional/Service/ComponentItemFactoryTest.php

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Qossmic\TwigDocBundle\Exception\InvalidComponentConfigurationException;
1919
use Qossmic\TwigDocBundle\Service\CategoryService;
2020
use Qossmic\TwigDocBundle\Tests\TestApp\Entity\Car;
21+
use Qossmic\TwigDocBundle\Tests\TestApp\Test\ConfigurableContainerTrait;
2122
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
2223

2324
#[CoversClass(ComponentItemFactory::class)]
@@ -29,6 +30,8 @@
2930
#[UsesClass(NullGenerator::class)]
3031
class ComponentItemFactoryTest extends KernelTestCase
3132
{
33+
use ConfigurableContainerTrait;
34+
3235
#[DataProvider('getValidComponents')]
3336
public function testValidComponent(array $componentData): void
3437
{
@@ -115,8 +118,10 @@ public function testFactoryCreatesDefaultVariationWithParameterTypes(): void
115118
],
116119
];
117120

121+
$container = $this->createContainer(['twig_doc' => ['use_fake_parameter' => true]]);
122+
118123
/** @var ComponentItemFactory $factory */
119-
$factory = self::getContainer()->get('twig_doc.service.component_factory');
124+
$factory = $container->get('twig_doc.service.component_factory');
120125

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

@@ -135,6 +140,40 @@ public function testFactoryCreatesDefaultVariationWithParameterTypes(): void
135140
self::assertIsFloat($component->getVariations()['default']['complex']['amount']);
136141
}
137142

143+
public function testCreateObjectParameter(): void
144+
{
145+
$data = [
146+
'name' => 'component',
147+
'title' => 'title',
148+
'description' => 'description',
149+
'category' => 'MainCategory',
150+
'path' => 'path',
151+
'renderPath' => 'renderPath',
152+
'parameters' => [
153+
'car' => Car::class,
154+
],
155+
'variations' => [
156+
'variation1' => [
157+
'car' => [
158+
'color' => 'blue',
159+
],
160+
],
161+
'variation2' => [
162+
'car' => null,
163+
],
164+
],
165+
];
166+
167+
$container = $this->createContainer(['twig_doc' => ['use_fake_parameter' => true]]);
168+
169+
/** @var ComponentItemFactory $factory */
170+
$factory = $container->get('twig_doc.service.component_factory');
171+
172+
$component = $factory->create($data);
173+
174+
self::assertInstanceOf(ComponentItem::class, $component);
175+
}
176+
138177
public function testCreateKeepsParamValueFromVariation(): void
139178
{
140179
$data = [
@@ -200,11 +239,14 @@ public function testCreateForObjectParameter(): void
200239
static::assertIsArray($variations['fuchsia']);
201240
static::assertArrayHasKey('car', $variations['fuchsia']);
202241

203-
$car = $variations['fuchsia']['car'];
242+
$carData = $variations['fuchsia']['car'];
204243

205-
static::assertInstanceOf(Car::class, $car);
206-
static::assertEquals('fuchsia', $car->getColor());
207-
static::assertEquals('Mitsubishi', $car->getManufacturer()->getName());
244+
static::assertEquals([
245+
'color' => 'fuchsia',
246+
'manufacturer' => [
247+
'name' => 'Mitsubishi',
248+
],
249+
], $carData);
208250
}
209251

210252
public function testCreateForParamWithOptionalVariationValue(): void
@@ -224,7 +266,7 @@ public function testCreateForParamWithOptionalVariationValue(): void
224266
'variations' => [
225267
'variation1' => [
226268
'stringParam' => 'Some cool hipster text',
227-
'optionalEmpty' => '',
269+
'optionalEmpty' => null,
228270
],
229271
],
230272
];
@@ -238,7 +280,7 @@ public function testCreateForParamWithOptionalVariationValue(): void
238280
self::assertIsArray($variations);
239281
self::assertArrayHasKey('variation1', $variations);
240282
self::assertArrayHasKey('secondParam', $variations['variation1']);
241-
self::assertIsString($variations['variation1']['secondParam']);
283+
self::assertNull($variations['variation1']['secondParam']);
242284
self::assertNull($variations['variation1']['optionalEmpty']);
243285
}
244286

tests/TestApp/Kernel.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ class Kernel extends SymfonyKernel
1717
{
1818
use MicroKernelTrait;
1919

20-
public function __construct()
20+
public function __construct(string $environment = 'test', bool $debug = false, private readonly array $extraConfigs = [])
2121
{
22-
parent::__construct('test', false);
22+
parent::__construct($environment, $debug);
2323
}
2424

2525
public function registerBundles(): iterable
@@ -42,6 +42,10 @@ protected function configureContainer(ContainerBuilder $container, LoaderInterfa
4242
$loader->load(__DIR__.'/config/services.php');
4343
$loader->load(__DIR__.'/config/packages/*.php', 'glob');
4444
$loader->load(__DIR__.'/config/packages/*.yaml', 'glob');
45+
46+
foreach ($this->extraConfigs as $name => $config) {
47+
$container->prependExtensionConfig($name, $config);
48+
}
4549
}
4650

4751
public function getCacheDir(): string

0 commit comments

Comments
 (0)