Skip to content

Commit c4b73d3

Browse files
authored
Merge pull request #32 from InteractionDesignFoundation/clean-dev-dependencies
Cleanup dev dependencies
2 parents e57e5cd + 5ce07f6 commit c4b73d3

File tree

6 files changed

+60
-142
lines changed

6 files changed

+60
-142
lines changed

composer.json

+3-7
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@
2121
"require-dev": {
2222
"friendsofphp/php-cs-fixer": "^3.50",
2323
"geoip2/geoip2": "^3.0",
24-
"mockery/mockery": "^1.6",
24+
"orchestra/testbench": "^8.8 || ^9.0",
2525
"phpstan/phpstan": "^1.10",
2626
"phpunit/phpunit": "^10.5 || ^11.0",
2727
"squizlabs/php_codesniffer": "^3.9",
28-
"vimeo/psalm": "^5.22",
29-
"vlucas/phpdotenv": "^5.4"
28+
"vimeo/psalm": "^5.22"
3029
},
3130
"suggest": {
3231
"geoip2/geoip2": "Required to use the MaxMind database or web service with GeoIP (~2.1).",
@@ -43,10 +42,7 @@
4342
"autoload-dev": {
4443
"psr-4": {
4544
"InteractionDesignFoundation\\GeoIP\\Tests\\": "tests/"
46-
},
47-
"files": [
48-
"tests/TestFunctions.php"
49-
]
45+
}
5046
},
5147
"config": {
5248
"sort-packages": true

psalm-baseline.xml

+19-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="5.22.2@d768d914152dbbf3486c36398802f74e80cfde48">
2+
<files psalm-version="5.23.1@8471a896ccea3526b26d082f4461eeea467f10a4">
33
<file src="src/Cache.php">
44
<MixedInferredReturnType>
55
<code><![CDATA[bool]]></code>
@@ -26,15 +26,16 @@
2626
<NonInvariantDocblockPropertyType>
2727
<code><![CDATA[$description]]></code>
2828
</NonInvariantDocblockPropertyType>
29+
<PossiblyUndefinedMethod>
30+
<code><![CDATA[config]]></code>
31+
<code><![CDATA[getCache]]></code>
32+
</PossiblyUndefinedMethod>
2933
<PropertyNotSetInConstructor>
3034
<code><![CDATA[Clear]]></code>
3135
<code><![CDATA[Clear]]></code>
3236
<code><![CDATA[Clear]]></code>
3337
<code><![CDATA[Clear]]></code>
3438
</PropertyNotSetInConstructor>
35-
<UndefinedFunction>
36-
<code><![CDATA[config('cache.default')]]></code>
37-
</UndefinedFunction>
3839
</file>
3940
<file src="src/Console/Update.php">
4041
<DeprecatedMethod>
@@ -54,6 +55,9 @@
5455
<NonInvariantDocblockPropertyType>
5556
<code><![CDATA[$description]]></code>
5657
</NonInvariantDocblockPropertyType>
58+
<PossiblyUndefinedMethod>
59+
<code><![CDATA[getService]]></code>
60+
</PossiblyUndefinedMethod>
5761
<PropertyNotSetInConstructor>
5862
<code><![CDATA[Update]]></code>
5963
<code><![CDATA[Update]]></code>
@@ -68,9 +72,16 @@
6872
</InvalidParamDefault>
6973
</file>
7074
<file src="src/GeoIP.php">
75+
<DeprecatedConstant>
76+
<code><![CDATA[Logger::ERROR]]></code>
77+
</DeprecatedConstant>
7178
<DeprecatedMethod>
7279
<code><![CDATA[setPrefix]]></code>
7380
</DeprecatedMethod>
81+
<DeprecatedProperty>
82+
<code><![CDATA[$this->remote_ip]]></code>
83+
<code><![CDATA[$this->remote_ip]]></code>
84+
</DeprecatedProperty>
7485
<DocblockTypeContradiction>
7586
<code><![CDATA[$this->currencies === null]]></code>
7687
<code><![CDATA[$this->service === null]]></code>
@@ -103,15 +114,12 @@
103114
<MixedAssignment>
104115
<code><![CDATA[$class]]></code>
105116
<code><![CDATA[$config]]></code>
106-
<code><![CDATA[$log]]></code>
107117
</MixedAssignment>
108118
<MixedInferredReturnType>
109119
<code><![CDATA[string]]></code>
110120
</MixedInferredReturnType>
111121
<MixedMethodCall>
112-
<code><![CDATA[error]]></code>
113122
<code><![CDATA[new $class($config)]]></code>
114-
<code><![CDATA[pushHandler]]></code>
115123
</MixedMethodCall>
116124
<MixedOperand>
117125
<code><![CDATA[$this->config('service')]]></code>
@@ -136,19 +144,11 @@
136144
<PropertyTypeCoercion>
137145
<code><![CDATA[new $class($config)]]></code>
138146
</PropertyTypeCoercion>
139-
<RedundantCondition>
140-
<code><![CDATA['some']]></code>
141-
</RedundantCondition>
142147
<RiskyTruthyFalsyComparison>
143148
<code><![CDATA[! $location->currency]]></code>
144149
<code><![CDATA[$address = getenv($key)]]></code>
145150
<code><![CDATA[$ip]]></code>
146151
</RiskyTruthyFalsyComparison>
147-
<UndefinedClass>
148-
<code><![CDATA[Logger]]></code>
149-
<code><![CDATA[Logger]]></code>
150-
<code><![CDATA[StreamHandler]]></code>
151-
</UndefinedClass>
152152
</file>
153153
<file src="src/GeoIPServiceProvider.php">
154154
<MissingClosureParamType>
@@ -167,9 +167,6 @@
167167
<MixedPropertyFetch>
168168
<code><![CDATA[$app->config]]></code>
169169
</MixedPropertyFetch>
170-
<UndefinedFunction>
171-
<code><![CDATA[config_path('geoip.php')]]></code>
172-
</UndefinedFunction>
173170
</file>
174171
<file src="src/Location.php">
175172
<InvalidParamDefault>
@@ -359,8 +356,12 @@
359356
<code><![CDATA[getLocation]]></code>
360357
</MixedMethodCall>
361358
<MixedReturnStatement>
359+
<code><![CDATA[app('geoip')]]></code>
362360
<code><![CDATA[app('geoip')]]></code>
363361
<code><![CDATA[app('geoip')->getLocation($ip)]]></code>
364362
</MixedReturnStatement>
363+
<PossiblyUndefinedMethod>
364+
<code><![CDATA[getLocation]]></code>
365+
</PossiblyUndefinedMethod>
365366
</file>
366367
</files>

src/GeoIP.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class GeoIP
2323

2424
/**
2525
* Remote Machine IP address.
26+
* @deprecated Use {@see self::getClientIP()} instead.
2627
*
2728
* @var string
2829
*/
@@ -291,13 +292,10 @@ private function shouldCache(Location $location, $ip = null): bool
291292
return false;
292293
}
293294

294-
switch ($this->config('cache', 'none')) {
295-
case 'all':
296-
case 'some' && $ip === null:
297-
return true;
298-
}
299-
300-
return false;
295+
return match ($this->config('cache', 'none')) {
296+
'all', 'some' && $ip === null => true,
297+
default => false,
298+
};
301299
}
302300

303301
/**

tests/CacheTest.php

+26-67
Original file line numberDiff line numberDiff line change
@@ -5,95 +5,54 @@
55
namespace InteractionDesignFoundation\GeoIP\Tests;
66

77
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;
912

1013
/**
11-
* @covers \InteractionDesignFoundation\GeoIP\Cache
14+
* @coversNothing
1215
*/
16+
#[CoversClass(Cache::class)]
1317
class CacheTest extends TestCase
1418
{
15-
/** @test */
19+
#[Test]
1620
public function should_return_valid_location(): void
1721
{
18-
$data = [
22+
$cache = new Cache(app(CacheManager::class), [], 30);
23+
$originalLocation = new Location([
1924
'ip' => '81.2.69.142',
2025
'iso_code' => 'US',
2126
'lat' => 41.31,
2227
'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('');
28+
]);
5129

52-
$cacheMock->shouldReceive('get')->with('81.2.69.142')->andReturn(null);
30+
$cache->set($originalLocation['ip'], $originalLocation);
31+
$uncachedLocation = $cache->get($originalLocation['ip']);
5332

54-
$this->assertSame($geo_ip->getCache()->get('81.2.69.142'), null);
33+
$this->assertInstanceOf(Location::class, $uncachedLocation);
34+
$this->assertSame($uncachedLocation->ip, $originalLocation->ip);
35+
$this->assertSame($uncachedLocation->default, false);
5536
}
5637

57-
/** @test */
58-
public function should_set_location(): void
38+
#[Test]
39+
public function it_flushes_empty_cache(): void
5940
{
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();
41+
$cache = new Cache(app(CacheManager::class), [], 30);
6942

70-
$cacheMock->shouldReceive('supportsTags')->andReturn(false);
43+
$flushResult = $cache->flush();
7144

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);
45+
$this->assertTrue($flushResult);
8246
}
8347

84-
/** @test */
85-
public function should_flush_locations(): void
48+
#[Test]
49+
public function it_flushes_non_empty_cache(): void
8650
{
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);
51+
$cache = new Cache(app(CacheManager::class), [], 30);
52+
$cache->set('42', new Location());
9453

95-
$cacheMock->shouldReceive('tags')->with($geo_ip->config('cache_tags'))->andReturnSelf();
54+
$flushResult = $cache->flush();
9655

97-
$this->assertSame($geo_ip->getCache()->flush(), true);
56+
$this->assertTrue($flushResult);
9857
}
9958
}

tests/TestCase.php

+7-19
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,26 @@
44

55
namespace InteractionDesignFoundation\GeoIP\Tests;
66

7-
use Illuminate\Cache\CacheManager;
87
use InteractionDesignFoundation\GeoIP\GeoIP;
9-
use Mockery;
10-
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
8+
use PHPUnit\Framework\Attributes\CoversNothing;
119

1210
/**
1311
* @coversNothing
1412
*/
15-
class TestCase extends PHPUnitTestCase
13+
#[CoversNothing]
14+
class TestCase extends \Orchestra\Testbench\TestCase
1615
{
17-
public static $functions;
18-
16+
/** @inheritDoc */
1917
protected function setUp(): void
2018
{
21-
self::$functions = Mockery::mock();
22-
}
23-
24-
protected function tearDown(): void
25-
{
26-
Mockery::close();
19+
parent::setUp();
2720
}
2821

29-
protected function makeGeoIP(array $config = [], $cacheMock = null): GeoIP
22+
protected function makeGeoIP(array $config = []): GeoIP
3023
{
31-
$cacheMock = $cacheMock ?: Mockery::mock(CacheManager::class);
32-
$cacheMock->shouldReceive('supportsTags')->andReturn(false);
33-
3424
$config = array_merge($this->getConfig(), $config);
3525

36-
$cacheMock->shouldReceive('tags')->with(['laravel-geoip-location'])->andReturnSelf();
37-
38-
return new GeoIP($config, $cacheMock);
26+
return new GeoIP($config, $this->app['cache']);
3927
}
4028

4129
protected function getConfig(): array

tests/TestFunctions.php

-24
This file was deleted.

0 commit comments

Comments
 (0)