|
21 | 21 | - [Bulk Updates](#bulk-updates)
|
22 | 22 | - [Purging Features](#purging-features)
|
23 | 23 | - [Events](#events)
|
| 24 | +- [Testing](#testing) |
24 | 25 |
|
25 | 26 | <a name="introduction"></a>
|
26 | 27 | ## Introduction
|
@@ -701,3 +702,48 @@ class EventServiceProvider extends ServiceProvider
|
701 | 702 | ### `Laravel\Pennant\Events\DynamicallyRegisteringFeatureClass`
|
702 | 703 |
|
703 | 704 | This event is dispatched when a class based feature is being dynamically checked for the first time during a request.
|
| 705 | + |
| 706 | +<a name="testing"></a> |
| 707 | +## Testing |
| 708 | + |
| 709 | +When testing code that interacts with feature flags, the easiest way to control the feature flag's returned value in your tests is to simply re-define the feature. For example, imagine you have the following feature defined in one of your application's service provider: |
| 710 | + |
| 711 | +```php |
| 712 | +use Illuminate\Support\Arr; |
| 713 | +use Laravel\Pennant\Feature; |
| 714 | + |
| 715 | +Feature::define('purchase-button', fn () => Arr::random([ |
| 716 | + 'blue-sapphire', |
| 717 | + 'seafoam-green', |
| 718 | + 'tart-orange', |
| 719 | +])); |
| 720 | +``` |
| 721 | + |
| 722 | +To modify the feature's returned value in your tests, you may re-define the feature at the beginning of the test. The following test will always pass, even though the `Arr::random()` implementation is still present in the service provider: |
| 723 | + |
| 724 | +```php |
| 725 | +use Laravel\Pennant\Feature; |
| 726 | + |
| 727 | +public function test_it_can_control_feature_values() |
| 728 | +{ |
| 729 | + Feature::define('purchase-button', 'seafoam-green'); |
| 730 | + |
| 731 | + $this->assertSame('seafoam-green', Feature::value('purchase-button')); |
| 732 | +} |
| 733 | +``` |
| 734 | + |
| 735 | +The same approach may be used for class based features: |
| 736 | + |
| 737 | +```php |
| 738 | +use App\Features\NewApi; |
| 739 | +use Laravel\Pennant\Feature; |
| 740 | + |
| 741 | +public function test_it_can_control_feature_values() |
| 742 | +{ |
| 743 | + Feature::define(NewApi::class, true); |
| 744 | + |
| 745 | + $this->assertTrue(Feature::value(NewApi::class)); |
| 746 | +} |
| 747 | +``` |
| 748 | + |
| 749 | +If your feature is returning a `Lottery` instance, there are a handful of useful [testing helpers available](/docs/{{version}}/helpers#testing-lotteries). |
0 commit comments