From 1071c96e70c765e2c9268543bb73825d6794c691 Mon Sep 17 00:00:00 2001 From: Alies Lapatsin Date: Tue, 12 Mar 2024 16:56:21 +0100 Subject: [PATCH 1/6] Replace `switch` by `match` --- src/GeoIP.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/GeoIP.php b/src/GeoIP.php index 90e264a..eddd881 100644 --- a/src/GeoIP.php +++ b/src/GeoIP.php @@ -291,13 +291,10 @@ private function shouldCache(Location $location, $ip = null): bool return false; } - switch ($this->config('cache', 'none')) { - case 'all': - case 'some' && $ip === null: - return true; - } - - return false; + return match ($this->config('cache', 'none')) { + 'all', 'some' && $ip === null => true, + default => false, + }; } /** From 3041e5647ee9dc0561d47676412a587ec353be27 Mon Sep 17 00:00:00 2001 From: Alies Lapatsin Date: Tue, 12 Mar 2024 18:40:03 +0100 Subject: [PATCH 2/6] Cleanup dev dependencies --- composer.json | 10 ++--- tests/CacheTest.php | 94 +++++++++++------------------------------ tests/TestCase.php | 29 +++---------- tests/TestFunctions.php | 24 ----------- 4 files changed, 35 insertions(+), 122 deletions(-) delete mode 100644 tests/TestFunctions.php diff --git a/composer.json b/composer.json index 0351b16..4e5fbcd 100644 --- a/composer.json +++ b/composer.json @@ -21,12 +21,11 @@ "require-dev": { "friendsofphp/php-cs-fixer": "^3.50", "geoip2/geoip2": "^3.0", - "mockery/mockery": "^1.6", + "orchestra/testbench": "^8.8 || ^9.0", "phpstan/phpstan": "^1.10", "phpunit/phpunit": "^10.5 || ^11.0", "squizlabs/php_codesniffer": "^3.9", - "vimeo/psalm": "^5.22", - "vlucas/phpdotenv": "^5.4" + "vimeo/psalm": "^5.22" }, "suggest": { "geoip2/geoip2": "Required to use the MaxMind database or web service with GeoIP (~2.1).", @@ -43,10 +42,7 @@ "autoload-dev": { "psr-4": { "InteractionDesignFoundation\\GeoIP\\Tests\\": "tests/" - }, - "files": [ - "tests/TestFunctions.php" - ] + } }, "config": { "sort-packages": true diff --git a/tests/CacheTest.php b/tests/CacheTest.php index d91a0cf..311e330 100644 --- a/tests/CacheTest.php +++ b/tests/CacheTest.php @@ -5,95 +5,51 @@ namespace InteractionDesignFoundation\GeoIP\Tests; use Illuminate\Cache\CacheManager; -use Mockery; +use InteractionDesignFoundation\GeoIP\Cache; +use InteractionDesignFoundation\GeoIP\Location; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Test; -/** - * @covers \InteractionDesignFoundation\GeoIP\Cache - */ +#[CoversClass(Cache::class)] class CacheTest extends TestCase { - /** @test */ + #[Test] public function should_return_valid_location(): void { - $data = [ + $cache = new Cache(app(CacheManager::class), [], 30); + $originalLocation = new Location([ 'ip' => '81.2.69.142', 'iso_code' => 'US', 'lat' => 41.31, 'lon' => -72.92, - ]; - - $cacheMock = Mockery::mock(CacheManager::class) - ->shouldAllowMockingProtectedMethods(); - - $cacheMock->shouldReceive('supportsTags')->andReturn(false); - $cacheMock->shouldReceive('get')->with($data['ip'])->andReturn($data); - - $geo_ip = $this->makeGeoIP([], $cacheMock); - $geo_ip->getCache()->setPrefix(''); - - $location = $geo_ip->getCache()->get($data['ip']); - - $this->assertInstanceOf(\InteractionDesignFoundation\GeoIP\Location::class, $location); - $this->assertSame($location->ip, $data['ip']); - $this->assertSame($location->default, false); - } - - /** @test */ - public function should_return_invalid_location(): void - { - $cacheMock = Mockery::mock(CacheManager::class) - ->shouldAllowMockingProtectedMethods(); - - $cacheMock->shouldReceive('supportsTags')->andReturn(false); - - $geo_ip = $this->makeGeoIP([], $cacheMock); - $geo_ip->getCache()->setPrefix(''); + ]); - $cacheMock->shouldReceive('get')->with('81.2.69.142')->andReturn(null); + $cache->set($originalLocation['ip'], $originalLocation); + $uncachedLocation = $cache->get($originalLocation['ip']); - $this->assertSame($geo_ip->getCache()->get('81.2.69.142'), null); + $this->assertInstanceOf(Location::class, $uncachedLocation); + $this->assertSame($uncachedLocation->ip, $originalLocation->ip); + $this->assertSame($uncachedLocation->default, false); } - /** @test */ - public function should_set_location(): void + #[Test] + public function it_flushes_empty_cache(): void { - $location = new \InteractionDesignFoundation\GeoIP\Location([ - 'ip' => '81.2.69.142', - 'iso_code' => 'US', - 'lat' => 41.31, - 'lon' => -72.92, - ]); - - $cacheMock = Mockery::mock(CacheManager::class) - ->shouldAllowMockingProtectedMethods(); + $cache = new Cache(app(CacheManager::class), [], 30); - $cacheMock->shouldReceive('supportsTags')->andReturn(false); + $flushResult = $cache->flush(); - $geo_ip = $this->makeGeoIP([], $cacheMock); - $geo_ip->getCache()->setPrefix(''); - - $cacheMock->shouldReceive('put')->withArgs(['81.2.69.142', $location->toArray(), $geo_ip->config('cache_expires')])->andReturn(null); - - $cacheMock->shouldReceive('tags') - ->with($geo_ip->config('cache_tags')) - ->andReturnSelf(); - - $this->assertSame($geo_ip->getCache()->set('81.2.69.142', $location), null); + $this->assertTrue($flushResult); } - /** @test */ - public function should_flush_locations(): void + #[Test] + public function it_flushes_non_empty_cache(): void { - $cacheMock = Mockery::mock(CacheManager::class) - ->shouldAllowMockingProtectedMethods(); - $cacheMock->shouldReceive('supportsTags')->andReturn(false); - - $geo_ip = $this->makeGeoIP([], $cacheMock); - - $cacheMock->shouldReceive('flush')->andReturn(true); + $cache = new Cache(app(CacheManager::class), [], 30); + $cache->set('42', new Location()); - $cacheMock->shouldReceive('tags')->with($geo_ip->config('cache_tags'))->andReturnSelf(); + $flushResult = $cache->flush(); - $this->assertSame($geo_ip->getCache()->flush(), true); + $this->assertTrue($flushResult); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index d881672..72ceeec 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -4,38 +4,23 @@ namespace InteractionDesignFoundation\GeoIP\Tests; -use Illuminate\Cache\CacheManager; use InteractionDesignFoundation\GeoIP\GeoIP; -use Mockery; -use PHPUnit\Framework\TestCase as PHPUnitTestCase; +use PHPUnit\Framework\Attributes\CoversNothing; -/** - * @coversNothing - */ -class TestCase extends PHPUnitTestCase +#[CoversNothing] +class TestCase extends \Orchestra\Testbench\TestCase { - public static $functions; - + /** @inheritDoc */ protected function setUp(): void { - self::$functions = Mockery::mock(); - } - - protected function tearDown(): void - { - Mockery::close(); + parent::setUp(); } - protected function makeGeoIP(array $config = [], $cacheMock = null): GeoIP + protected function makeGeoIP(array $config = []): GeoIP { - $cacheMock = $cacheMock ?: Mockery::mock(CacheManager::class); - $cacheMock->shouldReceive('supportsTags')->andReturn(false); - $config = array_merge($this->getConfig(), $config); - $cacheMock->shouldReceive('tags')->with(['laravel-geoip-location'])->andReturnSelf(); - - return new GeoIP($config, $cacheMock); + return new GeoIP($config, $this->app['cache']); } protected function getConfig(): array diff --git a/tests/TestFunctions.php b/tests/TestFunctions.php deleted file mode 100644 index 4af3b11..0000000 --- a/tests/TestFunctions.php +++ /dev/null @@ -1,24 +0,0 @@ -app($key, $default); - } -} From 74e1b39570eb3bd326b95ea0e6c00f225af24e04 Mon Sep 17 00:00:00 2001 From: alies-dev Date: Tue, 12 Mar 2024 17:40:38 +0000 Subject: [PATCH 3/6] Fix coding style --- tests/CacheTest.php | 3 +++ tests/TestCase.php | 3 +++ 2 files changed, 6 insertions(+) diff --git a/tests/CacheTest.php b/tests/CacheTest.php index 311e330..322d909 100644 --- a/tests/CacheTest.php +++ b/tests/CacheTest.php @@ -10,6 +10,9 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Test; +/** + * @coversNothing + */ #[CoversClass(Cache::class)] class CacheTest extends TestCase { diff --git a/tests/TestCase.php b/tests/TestCase.php index 72ceeec..baf46c3 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -7,6 +7,9 @@ use InteractionDesignFoundation\GeoIP\GeoIP; use PHPUnit\Framework\Attributes\CoversNothing; +/** + * @coversNothing + */ #[CoversNothing] class TestCase extends \Orchestra\Testbench\TestCase { From 3b116960c3a21a6a9ce2a07126c3c8918ff4fe0e Mon Sep 17 00:00:00 2001 From: Alies Lapatsin Date: Tue, 12 Mar 2024 19:12:47 +0100 Subject: [PATCH 4/6] Update Psalm baseline --- psalm-baseline.xml | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index d7010a5..ef43874 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,5 +1,5 @@ - + @@ -26,15 +26,16 @@ + + + + - - - @@ -54,6 +55,9 @@ + + + @@ -68,14 +72,23 @@ + + + + + remote_ip]]> + remote_ip]]> + remote_ip]]> + currencies === null]]> service === null]]> + default_location]]> config('cache_tags'), @@ -97,21 +110,15 @@ config('cache_tags')]]> config('default_location', [])]]> - - default_location]]> - - - - config('service')]]> @@ -136,19 +143,11 @@ - - - currency]]> - - - - - @@ -167,9 +166,6 @@ config]]> - - - @@ -359,8 +355,12 @@ + getLocation($ip)]]> + + + From 72c1436ca341b88351af00352be170a78cf4815f Mon Sep 17 00:00:00 2001 From: Alies Lapatsin Date: Tue, 12 Mar 2024 19:13:57 +0100 Subject: [PATCH 5/6] Deprecate `GeoIP::$remote_ip` --- psalm-baseline.xml | 4 +++- src/GeoIP.php | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index ef43874..e9a9eca 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -88,7 +88,6 @@ service === null]]> - default_location]]> config('cache_tags'), @@ -110,6 +109,9 @@ config('cache_tags')]]> config('default_location', [])]]> + + default_location]]> + diff --git a/src/GeoIP.php b/src/GeoIP.php index eddd881..4b26e72 100644 --- a/src/GeoIP.php +++ b/src/GeoIP.php @@ -23,6 +23,7 @@ class GeoIP /** * Remote Machine IP address. + * @deprecated Use {@see self::getClientIP()} instead. * * @var string */ From 5ce07f673c4dfde72e4dc3122a79a6865f0c544a Mon Sep 17 00:00:00 2001 From: Alies Lapatsin Date: Tue, 12 Mar 2024 19:19:43 +0100 Subject: [PATCH 6/6] Update Psalm baseline --- psalm-baseline.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index e9a9eca..0dc8387 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -81,7 +81,6 @@ remote_ip]]> remote_ip]]> - remote_ip]]> currencies === null]]>