diff --git a/README.md b/README.md index 2e8d5a8..a399631 100644 --- a/README.md +++ b/README.md @@ -93,8 +93,16 @@ The following view content will only be rendered if the feature flag is truthy: ``` ```blade -@configcat('new_registration_flow') +@unlessconfigcat('new_registration_flow') + Old registration form +@endconfigcat +``` + +```blade +@configcat('new_registration_flow_1') Sign up +@elseconfigcat('new_registration_flow_2') + Get started @else Register @endconfigcat diff --git a/src/ConfigCatServiceProvider.php b/src/ConfigCatServiceProvider.php index 46b9564..531712a 100644 --- a/src/ConfigCatServiceProvider.php +++ b/src/ConfigCatServiceProvider.php @@ -91,7 +91,19 @@ protected function bladeDirectives() Blade::directive('configcat', function (string $featureKey, $user = null) { $expression = $user ? "{$featureKey}, {$user}" : "{$featureKey}"; - return ""; + return ""; + }); + + Blade::directive('elseconfigcat', function (string $feature, $user = null) { + $expression = $user ? "{$feature}, {$user}" : "{$feature}"; + + return ""; + }); + + Blade::directive('unlessconfigcat', function (string $feature, $user = null) { + $expression = $user ? "{$feature}, {$user}" : "{$feature}"; + + return ""; }); Blade::directive('endconfigcat', function () { diff --git a/tests/Feature/BladeDirectivesTest.php b/tests/Feature/BladeDirectivesTest.php new file mode 100644 index 0000000..af98fd8 --- /dev/null +++ b/tests/Feature/BladeDirectivesTest.php @@ -0,0 +1,112 @@ + true, + 'disabled_feature' => false, + ]); + + Route::get('/foo', function () { + return view('feature'); + }); + + $this->get('/foo')->assertDontSee('I am hidden'); + } + + public function test_it_will_consider_an_unknown_feature_flag_to_be_disabled() + { + ConfigCat::fake([ + 'enabled_feature' => true, + 'disabled_feature' => false, + ]); + + Route::get('/foo', function () { + return view('feature'); + }); + + $this->get('/foo')->assertSee('You can see me'); + } + + public function test_it_will_consider_a_feature_flag_as_a_number_setting_to_be_disabled() + { + ConfigCat::fake([ + 'enabled_feature' => 123, + 'disabled_feature' => false, + ]); + + Route::get('/foo', function () { + return view('feature'); + }); + + $this->get('/foo')->assertDontSee('I should be visible'); + $this->get('/foo')->assertSee('I should not be visible'); + } + + public function test_it_will_consider_a_feature_flag_as_a_text_setting_to_be_disabled() + { + ConfigCat::fake([ + 'enabled_feature' => 'foobar', + 'disabled_feature' => false, + ]); + + Route::get('/foo', function () { + return view('feature'); + }); + + $this->get('/foo')->assertDontSee('I should be visible'); + $this->get('/foo')->assertSee('I should not be visible'); + } + + public function test_it_supports_the_unlessconfigcat_directive() + { + ConfigCat::fake([ + 'enabled_feature' => true, + 'disabled_feature' => false, + ]); + + Route::get('/foo', function () { + return view('feature'); + }); + + $this->get('/foo')->assertSee('I am not hidden'); + } + + public function test_it_supports_the_else_directive() + { + ConfigCat::fake([ + 'enabled_feature' => false, + 'disabled_feature' => false, + ]); + + Route::get('/foo', function () { + return view('feature'); + }); + + $this->get('/foo')->assertDontSee('I should be visible'); + $this->get('/foo')->assertSee('I should not be visible'); + } + + public function test_it_supports_the_elseconfigcat_directive() + { + ConfigCat::fake([ + 'enabled_feature' => true, + 'disabled_feature' => false, + ]); + + Route::get('/foo', function () { + return view('feature'); + }); + + $this->get('/foo')->assertDontSee('You cannot see me'); + $this->get('/foo')->assertSee('You can see me'); + } +} diff --git a/tests/Feature/ConfigCatTest.php b/tests/Feature/Facades/ConfigCatTest.php similarity index 50% rename from tests/Feature/ConfigCatTest.php rename to tests/Feature/Facades/ConfigCatTest.php index 081464c..978d5dc 100644 --- a/tests/Feature/ConfigCatTest.php +++ b/tests/Feature/Facades/ConfigCatTest.php @@ -1,66 +1,15 @@ true, - 'some_disabled_feature' => false, - ]); - - $this->assertTrue(configcat('some_enabled_feature')); - $this->assertFalse(configcat('some_disabled_feature')); - } - - public function test_global_helper_returns_false_when_a_feature_flag_does_not_exist() - { - ConfigCat::fake(['some_feature' => true]); - - $this->assertFalse(configcat('some_unknown_feature')); - } - - public function test_global_helper_can_retrieve_a_text_setting() - { - ConfigCat::fake(['some_feature_as_a_string' => 'foo']); - - $this->assertEquals('foo', configcat('some_feature_as_a_string')); - } - - public function test_global_helper_can_retrieve_a_number_setting() - { - ConfigCat::fake(['some_feature_as_a_string' => 123]); - - $this->assertEquals(123, configcat('some_feature_as_a_string')); - } - - public function test_global_helper_relies_on_the_facade() - { - ConfigCat::shouldReceive('get')->once()->with('some_feature'); - - configcat('some_feature'); - } - - public function test_global_helper_can_be_used_with_a_given_user() - { - $user = new \Illuminate\Foundation\Auth\User(); - $user->id = 123; - $user->email = 'foo@bar.com'; - - ConfigCat::shouldReceive('get')->once()->with('some_feature', $user); - - configcat('some_feature', $user); - } - public function test_the_facade_can_override_feature_flags() { config(['configcat.overrides.enabled' => true]); @@ -80,35 +29,6 @@ public function test_the_facade_can_override_feature_flags() ); } - public function test_the_blade_directive_will_render_something_only_when_the_corresponding_feature_flag_is_enabled() - { - ConfigCat::fake([ - 'enabled_feature' => true, - 'disabled_feature' => false, - ]); - - Route::get('/foo', function () { - return view('feature'); - }); - - $this->get('/foo')->assertSee('I should be visible'); - $this->get('/foo')->assertDontSee('I am hidden'); - } - - public function test_the_blade_directive_supports_the_else_directive() - { - ConfigCat::fake([ - 'enabled_feature' => false, - ]); - - Route::get('/foo', function () { - return view('feature'); - }); - - $this->get('/foo')->assertDontSee('I should be visible'); - $this->get('/foo')->assertSee('I should not be visible'); - } - public function test_config_cat_client_is_called_when_resolving_feature_flags() { $this->mock(ClientInterface::class, function (MockInterface $mock) { diff --git a/tests/Feature/HelperTest.php b/tests/Feature/HelperTest.php new file mode 100644 index 0000000..8415f44 --- /dev/null +++ b/tests/Feature/HelperTest.php @@ -0,0 +1,59 @@ + true, + 'some_disabled_feature' => false, + ]); + + $this->assertTrue(configcat('some_enabled_feature')); + $this->assertFalse(configcat('some_disabled_feature')); + } + + public function test_global_helper_returns_false_when_a_feature_flag_does_not_exist() + { + ConfigCat::fake(['some_feature' => true]); + + $this->assertFalse(configcat('some_unknown_feature')); + } + + public function test_global_helper_can_retrieve_a_text_setting() + { + ConfigCat::fake(['some_feature_as_a_string' => 'foo']); + + $this->assertEquals('foo', configcat('some_feature_as_a_string')); + } + + public function test_global_helper_can_retrieve_a_number_setting() + { + ConfigCat::fake(['some_feature_as_a_string' => 123]); + + $this->assertEquals(123, configcat('some_feature_as_a_string')); + } + + public function test_global_helper_relies_on_the_facade() + { + ConfigCat::shouldReceive('get')->once()->with('some_feature'); + + configcat('some_feature'); + } + + public function test_global_helper_can_be_used_with_a_given_user() + { + $user = new \Illuminate\Foundation\Auth\User(); + $user->id = 123; + $user->email = 'foo@bar.com'; + + ConfigCat::shouldReceive('get')->once()->with('some_feature', $user); + + configcat('some_feature', $user); + } +} diff --git a/tests/resources/views/feature.blade.php b/tests/resources/views/feature.blade.php index 0145afe..cedf05b 100644 --- a/tests/resources/views/feature.blade.php +++ b/tests/resources/views/feature.blade.php @@ -1,13 +1,23 @@ + @configcat('disabled_feature') + I am hidden + @endconfigcat + + @unlessconfigcat('disabled_feature') + I am not hidden + @endconfigcat + @configcat('enabled_feature') - I should be visible + I should be visible @else - I should not be visible + I should not be visible @endconfigcat - @configcat('disabled_feature') - I am hidden + @configcat('unknown_feature') + You cannot see me + @elseconfigcat('enabled_feature') + You can see me @endconfigcat