Skip to content

Commit aca2ca4

Browse files
[10.x] Add testing documentation (#8561)
* Add testing docs to Pennant * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent def6fd1 commit aca2ca4

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

pennant.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- [Bulk Updates](#bulk-updates)
2222
- [Purging Features](#purging-features)
2323
- [Events](#events)
24+
- [Testing](#testing)
2425

2526
<a name="introduction"></a>
2627
## Introduction
@@ -701,3 +702,48 @@ class EventServiceProvider extends ServiceProvider
701702
### `Laravel\Pennant\Events\DynamicallyRegisteringFeatureClass`
702703

703704
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

Comments
 (0)