Skip to content

Commit c48931d

Browse files
committed
increase test coverage
1 parent 3fd8515 commit c48931d

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

src/Casts/FeatureStateCast.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
class FeatureStateCast implements CastsAttributes
1010
{
11-
public function get($model, string $key, $value, array $attributes)
11+
public function get($model, string $key, $value, array $attributes): FeatureState
1212
{
1313
return FeatureState::from($attributes['state']);
1414
}
1515

16-
public function set($model, string $key, $value, array $attributes)
16+
public function set($model, string $key, $value, array $attributes): array
1717
{
1818
if (! $value instanceof FeatureState) {
1919
throw new InvalidArgumentException('The given value is not an instance of FeatureState.');

src/Models/Feature.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace Codinglabs\FeatureFlags\Models;
44

5+
use Illuminate\Database\Eloquent\Model;
56
use Codinglabs\FeatureFlags\Casts\FeatureStateCast;
67
use Illuminate\Database\Eloquent\Factories\HasFactory;
78

8-
class Feature extends \Illuminate\Database\Eloquent\Model
9+
class Feature extends Model
910
{
1011
use HasFactory;
1112

tests/FeatureFlagTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919
FeatureFlag::reset();
2020
});
2121

22+
it('throws an exception if casting to a feature state that does not exist', function () {
23+
$this->expectException(\InvalidArgumentException::class);
24+
25+
Feature::factory()->create([
26+
'name' => 'some-feature',
27+
'state' => 'foo',
28+
]);
29+
});
30+
2231
it('throws an exception if calling isOn on a feature that does not exist', function () {
2332
$this->expectException(MissingFeatureException::class);
2433

@@ -141,6 +150,60 @@
141150
->and(FeatureFlag::getState('some-on-feature'))->toBe(FeatureState::on());
142151
});
143152

153+
it('can turn on a feature', function () {
154+
Event::fake();
155+
156+
Feature::factory()->create([
157+
'name' => 'some-feature',
158+
'state' => FeatureState::off()
159+
]);
160+
161+
cache()->store('array')->set('testing.some-feature', 'off');
162+
163+
FeatureFlag::turnOn('some-feature');
164+
165+
Event::assertDispatched(FeatureUpdatedEvent::class);
166+
expect(FeatureFlag::isOn('some-feature'))->toBeTrue()
167+
->and(FeatureFlag::isOff('some-feature'))->toBeFalse()
168+
->and(cache()->store('array')->get('testing.some-feature'))->toBe(FeatureState::on()->value);
169+
});
170+
171+
it('can turn off a feature', function () {
172+
Event::fake();
173+
174+
Feature::factory()->create([
175+
'name' => 'some-feature',
176+
'state' => FeatureState::on()
177+
]);
178+
179+
cache()->store('array')->set('testing.some-feature', 'on');
180+
181+
FeatureFlag::turnOff('some-feature');
182+
183+
Event::assertDispatched(FeatureUpdatedEvent::class);
184+
expect(FeatureFlag::isOn('some-feature'))->toBeFalse()
185+
->and(FeatureFlag::isOff('some-feature'))->toBeTrue()
186+
->and(cache()->store('array')->get('testing.some-feature'))->toBe(FeatureState::off()->value);
187+
});
188+
189+
it('can make a feature dynamic', function () {
190+
Event::fake();
191+
192+
Feature::factory()->create([
193+
'name' => 'some-feature',
194+
'state' => FeatureState::on()
195+
]);
196+
197+
cache()->store('array')->set('testing.some-feature', 'on');
198+
199+
FeatureFlag::makeDynamic('some-feature');
200+
201+
Event::assertDispatched(FeatureUpdatedEvent::class);
202+
expect(FeatureFlag::isOn('some-feature'))->toBeFalse()
203+
->and(FeatureFlag::isOff('some-feature'))->toBeTrue()
204+
->and(cache()->store('array')->get('testing.some-feature'))->toBe(FeatureState::dynamic()->value);
205+
});
206+
144207
it('can update a features state', function () {
145208
Event::fake();
146209

0 commit comments

Comments
 (0)