|
5 | 5 | namespace InteractionDesignFoundation\GeoIP\Tests; |
6 | 6 |
|
7 | 7 | use Illuminate\Cache\CacheManager; |
8 | | -use Mockery; |
| 8 | +use InteractionDesignFoundation\GeoIP\Cache; |
| 9 | +use InteractionDesignFoundation\GeoIP\Location; |
| 10 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 11 | +use PHPUnit\Framework\Attributes\Test; |
9 | 12 |
|
10 | | -/** |
11 | | - * @covers \InteractionDesignFoundation\GeoIP\Cache |
12 | | - */ |
| 13 | +#[CoversClass(Cache::class)] |
13 | 14 | class CacheTest extends TestCase |
14 | 15 | { |
15 | | - /** @test */ |
| 16 | + #[Test] |
16 | 17 | public function should_return_valid_location(): void |
17 | 18 | { |
18 | | - $data = [ |
| 19 | + $cache = new Cache(app(CacheManager::class), [], 30); |
| 20 | + $originalLocation = new Location([ |
19 | 21 | 'ip' => '81.2.69.142', |
20 | 22 | 'iso_code' => 'US', |
21 | 23 | 'lat' => 41.31, |
22 | 24 | 'lon' => -72.92, |
23 | | - ]; |
24 | | - |
25 | | - $cacheMock = Mockery::mock(CacheManager::class) |
26 | | - ->shouldAllowMockingProtectedMethods(); |
27 | | - |
28 | | - $cacheMock->shouldReceive('supportsTags')->andReturn(false); |
29 | | - $cacheMock->shouldReceive('get')->with($data['ip'])->andReturn($data); |
30 | | - |
31 | | - $geo_ip = $this->makeGeoIP([], $cacheMock); |
32 | | - $geo_ip->getCache()->setPrefix(''); |
33 | | - |
34 | | - $location = $geo_ip->getCache()->get($data['ip']); |
35 | | - |
36 | | - $this->assertInstanceOf(\InteractionDesignFoundation\GeoIP\Location::class, $location); |
37 | | - $this->assertSame($location->ip, $data['ip']); |
38 | | - $this->assertSame($location->default, false); |
39 | | - } |
40 | | - |
41 | | - /** @test */ |
42 | | - public function should_return_invalid_location(): void |
43 | | - { |
44 | | - $cacheMock = Mockery::mock(CacheManager::class) |
45 | | - ->shouldAllowMockingProtectedMethods(); |
46 | | - |
47 | | - $cacheMock->shouldReceive('supportsTags')->andReturn(false); |
48 | | - |
49 | | - $geo_ip = $this->makeGeoIP([], $cacheMock); |
50 | | - $geo_ip->getCache()->setPrefix(''); |
| 25 | + ]); |
51 | 26 |
|
52 | | - $cacheMock->shouldReceive('get')->with('81.2.69.142')->andReturn(null); |
| 27 | + $cache->set($originalLocation['ip'], $originalLocation); |
| 28 | + $uncachedLocation = $cache->get($originalLocation['ip']); |
53 | 29 |
|
54 | | - $this->assertSame($geo_ip->getCache()->get('81.2.69.142'), null); |
| 30 | + $this->assertInstanceOf(Location::class, $uncachedLocation); |
| 31 | + $this->assertSame($uncachedLocation->ip, $originalLocation->ip); |
| 32 | + $this->assertSame($uncachedLocation->default, false); |
55 | 33 | } |
56 | 34 |
|
57 | | - /** @test */ |
58 | | - public function should_set_location(): void |
| 35 | + #[Test] |
| 36 | + public function it_flushes_empty_cache(): void |
59 | 37 | { |
60 | | - $location = new \InteractionDesignFoundation\GeoIP\Location([ |
61 | | - 'ip' => '81.2.69.142', |
62 | | - 'iso_code' => 'US', |
63 | | - 'lat' => 41.31, |
64 | | - 'lon' => -72.92, |
65 | | - ]); |
66 | | - |
67 | | - $cacheMock = Mockery::mock(CacheManager::class) |
68 | | - ->shouldAllowMockingProtectedMethods(); |
| 38 | + $cache = new Cache(app(CacheManager::class), [], 30); |
69 | 39 |
|
70 | | - $cacheMock->shouldReceive('supportsTags')->andReturn(false); |
| 40 | + $flushResult = $cache->flush(); |
71 | 41 |
|
72 | | - $geo_ip = $this->makeGeoIP([], $cacheMock); |
73 | | - $geo_ip->getCache()->setPrefix(''); |
74 | | - |
75 | | - $cacheMock->shouldReceive('put')->withArgs(['81.2.69.142', $location->toArray(), $geo_ip->config('cache_expires')])->andReturn(null); |
76 | | - |
77 | | - $cacheMock->shouldReceive('tags') |
78 | | - ->with($geo_ip->config('cache_tags')) |
79 | | - ->andReturnSelf(); |
80 | | - |
81 | | - $this->assertSame($geo_ip->getCache()->set('81.2.69.142', $location), null); |
| 42 | + $this->assertTrue($flushResult); |
82 | 43 | } |
83 | 44 |
|
84 | | - /** @test */ |
85 | | - public function should_flush_locations(): void |
| 45 | + #[Test] |
| 46 | + public function it_flushes_non_empty_cache(): void |
86 | 47 | { |
87 | | - $cacheMock = Mockery::mock(CacheManager::class) |
88 | | - ->shouldAllowMockingProtectedMethods(); |
89 | | - $cacheMock->shouldReceive('supportsTags')->andReturn(false); |
90 | | - |
91 | | - $geo_ip = $this->makeGeoIP([], $cacheMock); |
92 | | - |
93 | | - $cacheMock->shouldReceive('flush')->andReturn(true); |
| 48 | + $cache = new Cache(app(CacheManager::class), [], 30); |
| 49 | + $cache->set('42', new Location()); |
94 | 50 |
|
95 | | - $cacheMock->shouldReceive('tags')->with($geo_ip->config('cache_tags'))->andReturnSelf(); |
| 51 | + $flushResult = $cache->flush(); |
96 | 52 |
|
97 | | - $this->assertSame($geo_ip->getCache()->flush(), true); |
| 53 | + $this->assertTrue($flushResult); |
98 | 54 | } |
99 | 55 | } |
0 commit comments