Skip to content

Use VariableNode on "settings" configuration #370

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

Merged
merged 3 commits into from
Dec 3, 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
18 changes: 13 additions & 5 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Meilisearch\Bundle\Searchable;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;

final class Configuration implements ConfigurationInterface
{
Expand Down Expand Up @@ -62,11 +63,20 @@ public function getConfigTreeBuilder(): TreeBuilder
->info('Property accessor path (like method or property name) used to decide if an entry should be indexed.')
->defaultNull()
->end()
->arrayNode('settings')
->variableNode('settings')
->defaultValue([])
->info('Configure indices settings, see: https://www.meilisearch.com/docs/reference/api/settings')
->beforeNormalization()
->always()
->then(static function (array $value) {
->then(static function ($value) {
if (null === $value) {
return [];
}

if (!\is_array($value)) {
throw new InvalidConfigurationException('Settings must be an array.');
}

$stringSettings = ['distinctAttribute', 'proximityPrecision', 'searchCutoffMs'];

foreach ($stringSettings as $setting) {
Expand All @@ -77,9 +87,7 @@ public function getConfigTreeBuilder(): TreeBuilder

return $value;
})
->end()
->arrayPrototype()
->variablePrototype()->end()
->end()
->end()
->end()
->end()
Expand Down
75 changes: 75 additions & 0 deletions tests/Unit/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,46 @@ public function testValidConfig(array $inputConfig, array $expectedConfig): void
$this->assertProcessedConfigurationEquals($inputConfig, $expectedConfig);
}

/**
* @param mixed $value
*
* @dataProvider dataTestSettingsDynamicCheckerInvalid
*/
public function testSettingsDynamicCheckerInvalid($value): void
{
$this->assertConfigurationIsInvalid([
'meilisearch' => [
'indices' => [
[
'name' => 'items',
'class' => 'App\Entity\Post',
'settings' => $value,
],
],
],
], 'Settings must be an array.');
}

/**
* @param mixed $value
*
* @dataProvider dataTestSettingsDynamicCheckerValid
*/
public function testSettingsDynamicCheckerValid($value): void
{
$this->assertConfigurationIsValid([
'meilisearch' => [
'indices' => [
[
'name' => 'items',
'class' => 'App\Entity\Post',
'settings' => $value,
],
],
],
]);
}

/**
* @return iterable<array{inputConfig: array<mixed>, expectedConfig: array<mixed>}>
*/
Expand Down Expand Up @@ -294,6 +334,41 @@ public static function dataTestConfigurationTree(): iterable
];
}

/**
* @return iterable<array{value: mixed}>
*/
public static function dataTestSettingsDynamicCheckerInvalid(): iterable
{
yield 'string is not acceptable' => [
'value' => 'hello',
];
yield 'int is not acceptable' => [
'value' => 1,
];
yield 'bool is not acceptable' => [
'value' => true,
];
}

/**
* @return iterable<array{value: mixed}>
*/
public static function dataTestSettingsDynamicCheckerValid(): iterable
{
yield 'array is acceptable' => [
'value' => [],
];
yield 'array with arbitrary key is acceptable' => [
'value' => [
'key' => 'value',
'key2' => 'value2',
],
];
yield 'null is acceptable' => [
'value' => null,
];
}

protected function getConfiguration(): Configuration
{
return new Configuration();
Expand Down
Loading