Skip to content

Commit b4ca509

Browse files
committed
Fix failing test
1 parent b66b0bb commit b4ca509

File tree

7 files changed

+96
-213
lines changed

7 files changed

+96
-213
lines changed

src/Cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct(CacheManager $cache, $tags, $expires = 30)
3838
*
3939
* @param string $name
4040
*
41-
* @return array
41+
* @return Location|null
4242
*/
4343
public function get($name)
4444
{

src/GeoIP.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ public function getLocation($ip = null)
115115

116116
// Should cache location
117117
if ($this->shouldCache($ip, $this->location)) {
118-
dd('Cache It');
119118
$this->getCache()->set($ip, $this->location);
120119
}
121120

@@ -135,7 +134,7 @@ private function find($ip = null)
135134
// Check cache for location
136135
if ($this->config('cache', 'none') !== 'none' && $location = $this->getCache()->get($ip)) {
137136
$location->cached = true;
138-
dd('Cached!');
137+
139138
return $location;
140139
}
141140

tests/Cache/LaravelTest.php

Lines changed: 0 additions & 108 deletions
This file was deleted.

tests/Cache/SessionTest.php

Lines changed: 0 additions & 90 deletions
This file was deleted.

tests/CacheTest.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Torann\GeoIP\Tests;
4+
5+
use Mockery;
6+
7+
class CacheTest extends TestCase
8+
{
9+
/**
10+
* @test
11+
*/
12+
public function shouldReturnValidLocation()
13+
{
14+
$data = [
15+
'ip' => '81.2.69.142',
16+
'iso_code' => 'US',
17+
'lat' => 41.31,
18+
'lon' => -72.92,
19+
];
20+
21+
$cacheMock = Mockery::mock('Illuminate\Cache\CacheManager');
22+
$cacheMock->shouldReceive('get')->with($data['ip'])->andReturn($data);
23+
24+
$geo_ip = $this->makeGeoIP([], $cacheMock);
25+
26+
$location = $geo_ip->getCache()->get($data['ip']);
27+
28+
$this->assertInstanceOf(\Torann\GeoIP\Location::class, $location);
29+
$this->assertEquals($location->ip, $data['ip']);
30+
$this->assertEquals($location->default, false);
31+
}
32+
33+
/**
34+
* @test
35+
*/
36+
public function shouldReturnInvalidLocation()
37+
{
38+
$cacheMock = Mockery::mock('Illuminate\Cache\CacheManager');
39+
40+
$geo_ip = $this->makeGeoIP([], $cacheMock);
41+
42+
$cacheMock->shouldReceive('get')->with('81.2.69.142')->andReturn(null);
43+
$cacheMock->shouldReceive('tags')->with($geo_ip->config('cache_tags'))->andReturnSelf();
44+
45+
$this->assertEquals($geo_ip->getCache()->get('81.2.69.142'), null);
46+
}
47+
48+
/**
49+
* @test
50+
*/
51+
public function shouldSetLocation()
52+
{
53+
$location = new \Torann\GeoIP\Location([
54+
'ip' => '81.2.69.142',
55+
'iso_code' => 'US',
56+
'lat' => 41.31,
57+
'lon' => -72.92,
58+
]);
59+
60+
$cacheMock = Mockery::mock('Illuminate\Cache\CacheManager');
61+
62+
$geo_ip = $this->makeGeoIP([], $cacheMock);
63+
64+
$cacheMock->shouldReceive('put')->withArgs(['81.2.69.142', $location->toArray(), $geo_ip->config('cache_expires')])->andReturn(null);
65+
$cacheMock->shouldReceive('tags')->with($geo_ip->config('cache_tags'))->andReturnSelf();
66+
67+
$this->assertEquals($geo_ip->getCache()->set('81.2.69.142', $location), null);
68+
}
69+
70+
/**
71+
* @test
72+
*/
73+
public function shouldFlushLocations()
74+
{
75+
$cacheMock = Mockery::mock('Illuminate\Cache\CacheManager');
76+
77+
$geo_ip = $this->makeGeoIP([], $cacheMock);
78+
79+
$cacheMock->shouldReceive('flush')->andReturn(true);
80+
$cacheMock->shouldReceive('tags')->with($geo_ip->config('cache_tags'))->andReturnSelf();
81+
82+
$this->assertEquals($geo_ip->getCache()->flush(), true);
83+
}
84+
}

tests/GeoIPTest.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Torann\GeoIP\Tests;
44

5+
use Mockery;
6+
57
class GeoIPTest extends TestCase
68
{
79
/**
@@ -37,16 +39,8 @@ public function testGetService()
3739
*/
3840
public function testGetCache()
3941
{
40-
$geo_ip = $this->makeGeoIP([
41-
'cache' => 'session',
42-
]);
43-
44-
// Get config values
45-
$config = $this->getConfig()['cache_drivers']['session'];
46-
unset($config['class']);
47-
48-
self::$functions->shouldReceive('app')->with('Torann\GeoIP\Cache\Session', [$config])->andReturn(true);
42+
$geo_ip = $this->makeGeoIP();
4943

50-
$this->assertEquals($geo_ip->getCache(), true);
44+
$this->assertInstanceOf(\Torann\GeoIP\Cache::class, $geo_ip->getCache());
5145
}
5246
}

tests/TestCase.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ public function tearDown()
1919
Mockery::close();
2020
}
2121

22-
protected function makeGeoIP(array $config = [])
22+
protected function makeGeoIP(array $config = [], $cacheMock = null)
2323
{
24+
$cacheMock = $cacheMock ?: Mockery::mock('Illuminate\Cache\CacheManager');
25+
2426
$config = array_merge($this->getConfig(), $config);
2527

26-
return new \Torann\GeoIP\GeoIP($config);
28+
$cacheMock->shouldReceive('tags')->with(['torann-geoip-location'])->andReturnSelf();
29+
30+
return new \Torann\GeoIP\GeoIP($config, $cacheMock);
2731
}
2832

2933
protected function getConfig()

0 commit comments

Comments
 (0)