Skip to content

Cleanup dev dependencies #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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).",
Expand All @@ -43,10 +42,7 @@
"autoload-dev": {
"psr-4": {
"InteractionDesignFoundation\\GeoIP\\Tests\\": "tests/"
},
"files": [
"tests/TestFunctions.php"
]
}
},
"config": {
"sort-packages": true
Expand Down
37 changes: 19 additions & 18 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.22.2@d768d914152dbbf3486c36398802f74e80cfde48">
<files psalm-version="5.23.1@8471a896ccea3526b26d082f4461eeea467f10a4">
<file src="src/Cache.php">
<MixedInferredReturnType>
<code><![CDATA[bool]]></code>
Expand All @@ -26,15 +26,16 @@
<NonInvariantDocblockPropertyType>
<code><![CDATA[$description]]></code>
</NonInvariantDocblockPropertyType>
<PossiblyUndefinedMethod>
<code><![CDATA[config]]></code>
<code><![CDATA[getCache]]></code>
</PossiblyUndefinedMethod>
<PropertyNotSetInConstructor>
<code><![CDATA[Clear]]></code>
<code><![CDATA[Clear]]></code>
<code><![CDATA[Clear]]></code>
<code><![CDATA[Clear]]></code>
</PropertyNotSetInConstructor>
<UndefinedFunction>
<code><![CDATA[config('cache.default')]]></code>
</UndefinedFunction>
</file>
<file src="src/Console/Update.php">
<DeprecatedMethod>
Expand All @@ -54,6 +55,9 @@
<NonInvariantDocblockPropertyType>
<code><![CDATA[$description]]></code>
</NonInvariantDocblockPropertyType>
<PossiblyUndefinedMethod>
<code><![CDATA[getService]]></code>
</PossiblyUndefinedMethod>
<PropertyNotSetInConstructor>
<code><![CDATA[Update]]></code>
<code><![CDATA[Update]]></code>
Expand All @@ -68,9 +72,16 @@
</InvalidParamDefault>
</file>
<file src="src/GeoIP.php">
<DeprecatedConstant>
<code><![CDATA[Logger::ERROR]]></code>
</DeprecatedConstant>
<DeprecatedMethod>
<code><![CDATA[setPrefix]]></code>
</DeprecatedMethod>
<DeprecatedProperty>
<code><![CDATA[$this->remote_ip]]></code>
<code><![CDATA[$this->remote_ip]]></code>
</DeprecatedProperty>
<DocblockTypeContradiction>
<code><![CDATA[$this->currencies === null]]></code>
<code><![CDATA[$this->service === null]]></code>
Expand Down Expand Up @@ -103,15 +114,12 @@
<MixedAssignment>
<code><![CDATA[$class]]></code>
<code><![CDATA[$config]]></code>
<code><![CDATA[$log]]></code>
</MixedAssignment>
<MixedInferredReturnType>
<code><![CDATA[string]]></code>
</MixedInferredReturnType>
<MixedMethodCall>
<code><![CDATA[error]]></code>
<code><![CDATA[new $class($config)]]></code>
<code><![CDATA[pushHandler]]></code>
</MixedMethodCall>
<MixedOperand>
<code><![CDATA[$this->config('service')]]></code>
Expand All @@ -136,19 +144,11 @@
<PropertyTypeCoercion>
<code><![CDATA[new $class($config)]]></code>
</PropertyTypeCoercion>
<RedundantCondition>
<code><![CDATA['some']]></code>
</RedundantCondition>
<RiskyTruthyFalsyComparison>
<code><![CDATA[! $location->currency]]></code>
<code><![CDATA[$address = getenv($key)]]></code>
<code><![CDATA[$ip]]></code>
</RiskyTruthyFalsyComparison>
<UndefinedClass>
<code><![CDATA[Logger]]></code>
<code><![CDATA[Logger]]></code>
<code><![CDATA[StreamHandler]]></code>
</UndefinedClass>
</file>
<file src="src/GeoIPServiceProvider.php">
<MissingClosureParamType>
Expand All @@ -167,9 +167,6 @@
<MixedPropertyFetch>
<code><![CDATA[$app->config]]></code>
</MixedPropertyFetch>
<UndefinedFunction>
<code><![CDATA[config_path('geoip.php')]]></code>
</UndefinedFunction>
</file>
<file src="src/Location.php">
<InvalidParamDefault>
Expand Down Expand Up @@ -359,8 +356,12 @@
<code><![CDATA[getLocation]]></code>
</MixedMethodCall>
<MixedReturnStatement>
<code><![CDATA[app('geoip')]]></code>
<code><![CDATA[app('geoip')]]></code>
<code><![CDATA[app('geoip')->getLocation($ip)]]></code>
</MixedReturnStatement>
<PossiblyUndefinedMethod>
<code><![CDATA[getLocation]]></code>
</PossiblyUndefinedMethod>
</file>
</files>
12 changes: 5 additions & 7 deletions src/GeoIP.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class GeoIP

/**
* Remote Machine IP address.
* @deprecated Use {@see self::getClientIP()} instead.
*
* @var string
*/
Expand Down Expand Up @@ -291,13 +292,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,
};
}

/**
Expand Down
93 changes: 26 additions & 67 deletions tests/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,95 +5,54 @@
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
* @coversNothing
*/
#[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);
}
}
26 changes: 7 additions & 19 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,26 @@

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
Expand Down
24 changes: 0 additions & 24 deletions tests/TestFunctions.php

This file was deleted.