Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 66b67bc

Browse files
committedFeb 24, 2024··
Do not throe exception if Cache driver doesn't support tags
1 parent 2e92cde commit 66b67bc

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed
 

‎src/Cache.php

+1-1
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

+11-18
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
}

0 commit comments

Comments
 (0)
Please sign in to comment.