From b225fbb8f984ba80251c90d665ba139b29d2c9fa Mon Sep 17 00:00:00 2001 From: MilesChou Date: Fri, 7 Jul 2023 17:00:41 +0800 Subject: [PATCH] Add test --- app/Console/Commands/OperationActivate.php | 2 ++ app/Console/Commands/OperationDeactivate.php | 2 ++ app/Providers/FeatureFlagsServiceProvider.php | 1 + .../Http/Controllers/User/CatalogPageTest.php | 24 +++++++++++++++++++ .../Http/Controllers/User/ShopTest.php | 23 ++++++++++++++++++ 5 files changed, 52 insertions(+) diff --git a/app/Console/Commands/OperationActivate.php b/app/Console/Commands/OperationActivate.php index 7ef9da57..9d183a5e 100644 --- a/app/Console/Commands/OperationActivate.php +++ b/app/Console/Commands/OperationActivate.php @@ -15,6 +15,8 @@ public function handle(FeatureManager $feature): int { $feature->activate('operation'); + $this->line('Activated'); + return self::SUCCESS; } } diff --git a/app/Console/Commands/OperationDeactivate.php b/app/Console/Commands/OperationDeactivate.php index f7734f20..3beef04e 100644 --- a/app/Console/Commands/OperationDeactivate.php +++ b/app/Console/Commands/OperationDeactivate.php @@ -15,6 +15,8 @@ public function handle(FeatureManager $feature): int { $feature->deactivate('operation'); + $this->line('Deactivated'); + return self::SUCCESS; } } diff --git a/app/Providers/FeatureFlagsServiceProvider.php b/app/Providers/FeatureFlagsServiceProvider.php index af7c0acb..eec4611a 100644 --- a/app/Providers/FeatureFlagsServiceProvider.php +++ b/app/Providers/FeatureFlagsServiceProvider.php @@ -9,5 +9,6 @@ class FeatureFlagsServiceProvider extends ServiceProvider { public function boot(): void { + Feature::define('operation', fn() => true); } } diff --git a/tests/Feature/Http/Controllers/User/CatalogPageTest.php b/tests/Feature/Http/Controllers/User/CatalogPageTest.php index 2f13280f..e4ee341a 100644 --- a/tests/Feature/Http/Controllers/User/CatalogPageTest.php +++ b/tests/Feature/Http/Controllers/User/CatalogPageTest.php @@ -3,6 +3,8 @@ namespace Tests\Feature\Http\Controllers\User; use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Support\Facades\Artisan; +use Laravel\Pennant\Feature; use PHPUnit\Framework\Attributes\Test; use Tests\TestCase; @@ -17,4 +19,26 @@ public function seeBasicItem() ->assertSeeText('Categories') ->assertSeeText('Price'); } + + #[Test] + public function seeCartItemWhenFeatureOn() + { + Artisan::call('db:seed'); + + Feature::activate('operation'); + + $this->get('/catalog') + ->assertSeeText('Add to cart'); + } + + #[Test] + public function dontSeeCartItemWhenFeatureOff() + { + Artisan::call('db:seed'); + + Feature::deactivate('operation'); + + $this->get('/catalog') + ->assertDontSeeText('Add to cart'); + } } diff --git a/tests/Feature/Http/Controllers/User/ShopTest.php b/tests/Feature/Http/Controllers/User/ShopTest.php index 9bf9e773..cc522970 100644 --- a/tests/Feature/Http/Controllers/User/ShopTest.php +++ b/tests/Feature/Http/Controllers/User/ShopTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\Http\Controllers\User; use Illuminate\Foundation\Testing\RefreshDatabase; +use Laravel\Pennant\Feature; use PHPUnit\Framework\Attributes\Test; use Tests\TestCase; @@ -17,4 +18,26 @@ public function seeBasicTitle() ->assertSeeText('Categories') ->assertSeeText('Just For You'); } + + #[Test] + public function seeLoginItemWhenOperationFeatureOn() + { + Feature::activate('operation'); + + $this->get('/catalog') + ->assertSeeText('Track my order') + ->assertSeeText('Login') + ->assertSeeText('Sign up'); + } + + #[Test] + public function dontSeeLoginItemWhenOperationFeatureOff() + { + Feature::deactivate('operation'); + + $this->get('/catalog') + ->assertDontSeeText('Track my order') + ->assertDontSeeText('Login') + ->assertDontSeeText('Sign up'); + } }