Skip to content

Commit

Permalink
Better Blade directives (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
clemblanco authored Feb 21, 2023
1 parent d71a8a2 commit 73869ed
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 87 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion src/ConfigCatServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,19 @@ protected function bladeDirectives()
Blade::directive('configcat', function (string $featureKey, $user = null) {
$expression = $user ? "{$featureKey}, {$user}" : "{$featureKey}";

return "<?php if (configcat({$expression})): ?>";
return "<?php if (configcat({$expression}) === true): ?>";
});

Blade::directive('elseconfigcat', function (string $feature, $user = null) {
$expression = $user ? "{$feature}, {$user}" : "{$feature}";

return "<?php elseif (configcat({$expression}) === true): ?>";
});

Blade::directive('unlessconfigcat', function (string $feature, $user = null) {
$expression = $user ? "{$feature}, {$user}" : "{$feature}";

return "<?php if (configcat({$expression}) === false): ?>";
});

Blade::directive('endconfigcat', function () {
Expand Down
112 changes: 112 additions & 0 deletions tests/Feature/BladeDirectivesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace PodPoint\ConfigCat\Tests\Feature;

use Illuminate\Support\Facades\Route;
use PodPoint\ConfigCat\Facades\ConfigCat;
use PodPoint\ConfigCat\Tests\TestCase;

class BladeDirectivesTest extends TestCase
{
public function test_it_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')->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');
}
}
Original file line number Diff line number Diff line change
@@ -1,66 +1,15 @@
<?php

namespace PodPoint\ConfigCat\Tests\Feature;
namespace PodPoint\ConfigCat\Tests\Feature\Facades;

use ConfigCat\ClientInterface;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Route;
use Mockery\MockInterface;
use PodPoint\ConfigCat\Facades\ConfigCat;
use PodPoint\ConfigCat\Tests\TestCase;

class ConfigCatTest extends TestCase
{
public function test_global_helper_can_be_used_to_check_if_a_feature_flag_is_enabled_or_disabled()
{
ConfigCat::fake([
'some_enabled_feature' => 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 = '[email protected]';

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]);
Expand All @@ -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) {
Expand Down
59 changes: 59 additions & 0 deletions tests/Feature/HelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace PodPoint\ConfigCat\Tests\Feature;

use PodPoint\ConfigCat\Facades\ConfigCat;
use PodPoint\ConfigCat\Tests\TestCase;

class HelperTest extends TestCase
{
public function test_global_helper_can_be_used_to_check_if_a_feature_flag_is_enabled_or_disabled()
{
ConfigCat::fake([
'some_enabled_feature' => 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 = '[email protected]';

ConfigCat::shouldReceive('get')->once()->with('some_feature', $user);

configcat('some_feature', $user);
}
}
18 changes: 14 additions & 4 deletions tests/resources/views/feature.blade.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
<html>
<body>
@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
</body>
</html>

0 comments on commit 73869ed

Please sign in to comment.