Skip to content

Commit 91aa69e

Browse files
authored
Force casting cache result to boolean (#73)
1 parent fd1d520 commit 91aa69e

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Support/GatewayCache.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace YlsIdeas\FeatureFlags\Support;
44

55
use Illuminate\Contracts\Cache\Repository;
6+
use Psr\SimpleCache\InvalidArgumentException;
67
use YlsIdeas\FeatureFlags\Contracts\Cacheable;
78

89
/**
@@ -21,9 +22,12 @@ public function hits(string $feature): bool
2122
return $this->repository->has($this->generateKey($feature));
2223
}
2324

25+
/**
26+
* @throws InvalidArgumentException
27+
*/
2428
public function result(string $feature): bool
2529
{
26-
return $this->repository->get($this->generateKey($feature));
30+
return (bool) $this->repository->get($this->generateKey($feature));
2731
}
2832

2933
public function store(string $feature, ?bool $result): void

tests/Support/GatewayCacheTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@ public function test_it_hits_the_cache_correct(): void
3333
$this->assertTrue($cache->hits('my-feature'));
3434
}
3535

36+
public function test_it_retrieves_the_cache_correctly(): void
37+
{
38+
$repository = Mockery::mock(Repository::class);
39+
$cachable = Mockery::mock(Cacheable::class);
40+
41+
$cachable->shouldReceive('generateKey')
42+
->with('my-feature')
43+
->once()
44+
->andReturn('key');
45+
46+
$repository->shouldReceive('get')
47+
->with('test:key')
48+
->once()
49+
->andReturn(null);
50+
51+
$cache = new GatewayCache($repository, 'test', $cachable);
52+
53+
$this->assertFalse($cache->result('my-feature'));
54+
}
55+
3656
public function test_it_stores_with_a_ttl_correctly(): void
3757
{
3858
$repository = Mockery::mock(Repository::class);

0 commit comments

Comments
 (0)