-
Notifications
You must be signed in to change notification settings - Fork 2
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
Clear unregistered features #59
base: master
Are you sure you want to change the base?
Changes from 1 commit
e71ee55
7cae30e
7bf4a3c
20f2789
4196df2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,4 +97,28 @@ public function configureFeature(string $feature, string $strategy, string $meth | |
'parameters' => $parameters, | ||
]); | ||
} | ||
|
||
public function listUnregisteredFeatures(): array | ||
{ | ||
$configuredFeatures = array_unique(array_reduce( | ||
$this->strategies, | ||
fn (array $features, TogglingStrategy $strategy) => \array_merge($features, $strategy->listFeatures()), | ||
[] | ||
)); | ||
|
||
$registeredFeatures = array_keys($this->registry->getDefinitions()); | ||
|
||
$unregisteredFeatures = array_values(array_diff($configuredFeatures, $registeredFeatures)); | ||
|
||
sort($unregisteredFeatures); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if this should be part of this API or in the clients of this API: it feels like sorting things should be dealt with at display time... |
||
|
||
return $unregisteredFeatures; | ||
} | ||
|
||
public function clearFeatureConfiguration(string $feature): void | ||
{ | ||
foreach ($this->strategies as $strategy) { | ||
$strategy->clearFeatureConfiguration($feature); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To avoid data loss, I think we should add an assertion to make sure the feature is not registered before removing its configuration. |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,6 +144,49 @@ public function testStrategiesCanBeCombinedWithBooleanOperators(): void | |
static::assertTrue($router->hasFeature('target', 'feature')); | ||
} | ||
|
||
public function testListUnregisteredFeatures(): void | ||
{ | ||
$router = $this->configureToggleRouter( | ||
new FeatureDefinition('feature', 'awesome feature', 'onoff or whitelist or percentage'), | ||
$this->configureAllStrategies() | ||
); | ||
|
||
$router->configureFeature('feature', 'whitelist', 'allow', 'target'); | ||
$router->configureFeature('unregistered', 'whitelist', 'allow', 'target'); | ||
$router->configureFeature('unregistered', 'percentage', 'slide', '50'); | ||
|
||
static::assertSame(['unregistered'], $router->listUnregisteredFeatures()); | ||
} | ||
|
||
public function testConfigurationCanBeClearedForRegisteredFeatures(): void | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we sure we want this behaviour? Like I said in my previous comment, users could lose configuration for registered features. |
||
{ | ||
$router = $this->configureToggleRouter( | ||
new FeatureDefinition('feature', 'awesome feature', 'onoff or whitelist or percentage'), | ||
$this->configureAllStrategies() | ||
); | ||
|
||
$router->configureFeature('feature', 'whitelist', 'allow', 'target'); | ||
|
||
static::assertTrue($router->hasFeature('target', 'feature')); | ||
|
||
$router->clearFeatureConfiguration('feature'); | ||
|
||
static::assertFalse($router->hasFeature('target', 'feature')); | ||
} | ||
|
||
public function testConfigurationCanBeClearedForUnregisteredFeatures(): void | ||
{ | ||
$router = $this->configureToggleRouter(null, $this->configureAllStrategies()); | ||
|
||
$router->configureFeature('feature', 'whitelist', 'allow', 'target'); | ||
|
||
static::assertSame(['feature'], $router->listUnregisteredFeatures()); | ||
|
||
$router->clearFeatureConfiguration('feature'); | ||
|
||
static::assertEmpty($router->listUnregisteredFeatures()); | ||
} | ||
|
||
/** | ||
* @param array<string, TogglingStrategy> $strategies | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can avoid some
array_merge()
calls by doing the following: