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

[1.x] Ensure values returns feature name #107

Merged
merged 1 commit into from
Jun 20, 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
11 changes: 11 additions & 0 deletions src/Drivers/Decorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,17 @@ public function purge($features = null): void
}
}

/**
* Retrieve the feature's name.
*
* @param string $feature
* @return string
*/
public function name($feature)
{
return $this->resolveFeature($feature);
}

Comment on lines +421 to +431
Copy link
Member Author

@timacdonald timacdonald Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the resolveFeature function is protected, I have introduced a new method for this so we didn't have to introduce a breaking change by changing the visibility.

Also, name is a much nicer public facing method name.

We use this internally, but it can also be useful in user-land code.

Feature::name(FeatureWithoutNameProperty::class);
// App\Features\FeatureWithoutNameProperty
Feature::name(FeatureWithNameProperty::class);
// 'feature-with-name-property'

/**
* Resolve the feature name and ensure it is defined.
*
Expand Down
4 changes: 2 additions & 2 deletions src/PendingScopedFeatureInteraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function loadMissing($features)
*/
public function value($feature)
{
return $this->values([$feature])[$feature];
return head($this->values([$feature]));
}

/**
Expand All @@ -97,7 +97,7 @@ public function values($features)

return Collection::make($features)
->mapWithKeys(fn ($feature) => [
$feature => $this->driver->get($feature, $this->scope()[0]),
$this->driver->name($feature) => $this->driver->get($feature, $this->scope()[0]),
])
->all();
}
Expand Down
75 changes: 75 additions & 0 deletions tests/Feature/DatabaseDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,63 @@ public function test_it_only_retries_on_conflicts_for_individual_queries()

$this->assertSame(1, $insertAttempts);
}

public function test_it_keys_by_feature_name()
{
Feature::define(FeatureWithName::class);
Feature::define(FeatureWithoutName::class);

$this->assertSame([
'feature-with-name' => 'feature-with-name-value',
'Tests\\Feature\\FeatureWithoutName' => 'feature-without-name-value',
], Feature::all());

$this->assertSame([
'feature-with-name' => 'feature-with-name-value',
'feature-with-name' => 'feature-with-name-value',
'Tests\\Feature\\FeatureWithoutName' => 'feature-without-name-value',
], Feature::values([
'feature-with-name',
FeatureWithName::class,
FeatureWithoutName::class,
]));

$this->assertSame([
'feature-with-name' => ['feature-with-name-value'],
'Tests\\Feature\\FeatureWithoutName' => ['feature-without-name-value'],
], Feature::load([
'feature-with-name',
FeatureWithoutName::class,
]));

$this->assertSame([
'feature-with-name' => ['feature-with-name-value'],
'Tests\\Feature\\FeatureWithoutName' => ['feature-without-name-value'],
], Feature::load([
FeatureWithName::class,
FeatureWithoutName::class,
]));

Feature::flushCache();

$this->assertSame([
'feature-with-name' => ['feature-with-name-value'],
'Tests\\Feature\\FeatureWithoutName' => ['feature-without-name-value'],
], Feature::loadMissing([
'feature-with-name',
FeatureWithoutName::class,
]));

Feature::flushCache();

$this->assertSame([
'feature-with-name' => ['feature-with-name-value'],
'Tests\\Feature\\FeatureWithoutName' => ['feature-without-name-value'],
], Feature::loadMissing([
FeatureWithName::class,
FeatureWithoutName::class,
]));
}
}

class UnregisteredFeature
Expand Down Expand Up @@ -1409,3 +1466,21 @@ public function __invoke()
return 'unregistered-value';
}
}

class FeatureWithoutName
{
public function __invoke()
{
return 'feature-without-name-value';
}
}

class FeatureWithName
{
public $name = 'feature-with-name';

public function __invoke()
{
return 'feature-with-name-value';
}
}