Skip to content

Commit b5eccd3

Browse files
authored
Merge pull request #25 from InteractionDesignFoundation/no-exception-if-tags-not-supported
Do not try to use tags and Cache driver doesn't support them
2 parents 2e92cde + 418f257 commit b5eccd3

File tree

3 files changed

+13
-19
lines changed

3 files changed

+13
-19
lines changed

src/Cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Cache
3737
*/
3838
public function __construct(CacheManager $cache, $tags, $expires = 30)
3939
{
40-
$this->cache = $tags ? $cache->tags($tags) : $cache;
40+
$this->cache = ($tags === [] || !$cache->supportsTags()) ? $cache : $cache->tags($tags);
4141
$this->expires = $expires;
4242
}
4343

tests/CacheTest.php

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ public function should_return_valid_location()
2525
$cacheMock = Mockery::mock(CacheManager::class)
2626
->shouldAllowMockingProtectedMethods();
2727

28-
$cacheMock->shouldReceive('get')
29-
->with($data['ip'])
30-
->andReturn($data);
28+
$cacheMock->shouldReceive('supportsTags')->andReturn(false);
29+
$cacheMock->shouldReceive('get')->with($data['ip'])->andReturn($data);
3130

3231
$geo_ip = $this->makeGeoIP([], $cacheMock);
3332
$geo_ip->getCache()->setPrefix('');
@@ -45,16 +44,12 @@ public function should_return_invalid_location()
4544
$cacheMock = Mockery::mock(CacheManager::class)
4645
->shouldAllowMockingProtectedMethods();
4746

47+
$cacheMock->shouldReceive('supportsTags')->andReturn(false);
48+
4849
$geo_ip = $this->makeGeoIP([], $cacheMock);
4950
$geo_ip->getCache()->setPrefix('');
5051

51-
$cacheMock->shouldReceive('get')
52-
->with('81.2.69.142')
53-
->andReturn(null);
54-
55-
$cacheMock->shouldReceive('tags')
56-
->with($geo_ip->config('cache_tags'))
57-
->andReturnSelf();
52+
$cacheMock->shouldReceive('get')->with('81.2.69.142')->andReturn(null);
5853

5954
$this->assertSame($geo_ip->getCache()->get('81.2.69.142'), null);
6055
}
@@ -72,12 +67,12 @@ public function should_set_location()
7267
$cacheMock = Mockery::mock(CacheManager::class)
7368
->shouldAllowMockingProtectedMethods();
7469

70+
$cacheMock->shouldReceive('supportsTags')->andReturn(false);
71+
7572
$geo_ip = $this->makeGeoIP([], $cacheMock);
7673
$geo_ip->getCache()->setPrefix('');
7774

78-
$cacheMock->shouldReceive('put')
79-
->withArgs(['81.2.69.142', $location->toArray(), $geo_ip->config('cache_expires')])
80-
->andReturn(null);
75+
$cacheMock->shouldReceive('put')->withArgs(['81.2.69.142', $location->toArray(), $geo_ip->config('cache_expires')])->andReturn(null);
8176

8277
$cacheMock->shouldReceive('tags')
8378
->with($geo_ip->config('cache_tags'))
@@ -94,12 +89,10 @@ public function should_flush_locations()
9489

9590
$geo_ip = $this->makeGeoIP([], $cacheMock);
9691

97-
$cacheMock->shouldReceive('flush')
98-
->andReturn(true);
92+
$cacheMock->shouldReceive('flush')->andReturn(true);
9993

100-
$cacheMock->shouldReceive('tags')
101-
->with($geo_ip->config('cache_tags'))
102-
->andReturnSelf();
94+
$cacheMock->shouldReceive('tags')->with($geo_ip->config('cache_tags'))->andReturnSelf();
95+
$cacheMock->shouldReceive('tags')->with($geo_ip->config('cache_tags'))->andReturnSelf();
10396

10497
$this->assertSame($geo_ip->getCache()->flush(), true);
10598
}

tests/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ protected function tearDown(): void
2828
protected function makeGeoIP(array $config = [], $cacheMock = null)
2929
{
3030
$cacheMock = $cacheMock ?: Mockery::mock(CacheManager::class);
31+
$cacheMock->shouldReceive('supportsTags')->andReturn(false);
3132

3233
$config = array_merge($this->getConfig(), $config);
3334

0 commit comments

Comments
 (0)