diff --git a/.github/workflows/provider.yml b/.github/workflows/provider.yml new file mode 100644 index 0000000..590442d --- /dev/null +++ b/.github/workflows/provider.yml @@ -0,0 +1,33 @@ +name: Provider + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + test: + name: PHP ${{ matrix.php-version }} + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + php-version: ['8.0', '8.1', '8.2', '8.3', '8.4'] + steps: + - uses: actions/checkout@v4 + - name: Use PHP ${{ matrix.php-version }} + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + extensions: curl + - name: Validate composer.json and composer.lock + run: composer validate --strict + - name: Install dependencies + run: composer update --prefer-stable --prefer-dist --no-progress + - name: Run test suite + run: composer run-script test-ci + - name: Upload Coverage report + run: | + wget https://scrutinizer-ci.com/ocular.phar + php ocular.phar code-coverage:upload --format=php-clover build/coverage.xml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 648662e..0000000 --- a/.travis.yml +++ /dev/null @@ -1,16 +0,0 @@ -language: php -sudo: false - -php: 7.0 - - -install: - - composer update --prefer-stable --prefer-dist - -script: - - composer test-ci - -after_success: - - wget https://scrutinizer-ci.com/ocular.phar - - php ocular.phar code-coverage:upload --format=php-clover build/coverage.xml - diff --git a/CHANGELOG.md b/CHANGELOG.md index 77016a4..8dded5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,53 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release. +## 4.4.0 + +### Added + +- Add support for PHP Geocoder 5 + +## 4.3.0 + +### Added + +- Add support for PHP 8.1 +- Add GitHub Actions workflow + +### Removed + +- Drop support for PHP 7.3 + +### Changed + +- Migrate from PHP-HTTP to PSR-18 client + +## 4.2.0 + +### Added + +- Add support for PHP 8.0 + +### Removed + +- Drop support for PHP 7.2 + +### Changed + +- Upgrade PHPUnit to version 9 + +## 4.1.0 + +### Removed + +- Drop support for PHP < 7.2 + +## 4.0.1 + +### Fixed + +- Use `countryCode` from API + ## 4.0.0 -First release of this library. +First release of this library. diff --git a/IpInfoDb.php b/IpInfoDb.php index c94ce95..d87e5a4 100644 --- a/IpInfoDb.php +++ b/IpInfoDb.php @@ -12,17 +12,17 @@ namespace Geocoder\Provider\IpInfoDb; +use Geocoder\Collection; use Geocoder\Exception\InvalidArgument; use Geocoder\Exception\InvalidCredentials; use Geocoder\Exception\UnsupportedOperation; -use Geocoder\Collection; +use Geocoder\Http\Provider\AbstractHttpProvider; use Geocoder\Model\Address; use Geocoder\Model\AddressCollection; +use Geocoder\Provider\Provider; use Geocoder\Query\GeocodeQuery; use Geocoder\Query\ReverseQuery; -use Geocoder\Http\Provider\AbstractHttpProvider; -use Geocoder\Provider\Provider; -use Http\Client\HttpClient; +use Psr\Http\Client\ClientInterface; /** * @author William Durand @@ -32,12 +32,12 @@ final class IpInfoDb extends AbstractHttpProvider implements Provider /** * @var string */ - const CITY_PRECISION_ENDPOINT_URL = 'https://api.ipinfodb.com/v3/ip-city/?key=%s&format=json&ip=%s'; + public const CITY_PRECISION_ENDPOINT_URL = 'https://api.ipinfodb.com/v3/ip-city/?key=%s&format=json&ip=%s'; /** * @var string */ - const COUNTRY_PRECISION_ENDPOINT_URL = 'https://api.ipinfodb.com/v3/ip-country/?key=%s&format=json&ip=%s'; + public const COUNTRY_PRECISION_ENDPOINT_URL = 'https://api.ipinfodb.com/v3/ip-country/?key=%s&format=json&ip=%s'; /** * @var string @@ -50,13 +50,13 @@ final class IpInfoDb extends AbstractHttpProvider implements Provider private $endpointUrl; /** - * @param HttpClient $client an HTTP adapter - * @param string $apiKey an API key - * @param string $precision The endpoint precision. Either "city" or "country" (faster) + * @param ClientInterface $client an HTTP adapter + * @param string $apiKey an API key + * @param string $precision The endpoint precision. Either "city" or "country" (faster) * - * @throws \Geocoder\Exception\InvalidArgument + * @throws InvalidArgument */ - public function __construct(HttpClient $client, string $apiKey, string $precision = 'city') + public function __construct(ClientInterface $client, string $apiKey, string $precision = 'city') { parent::__construct($client); @@ -73,16 +73,10 @@ public function __construct(HttpClient $client, string $apiKey, string $precisio break; default: - throw new InvalidArgument(sprintf( - 'Invalid precision value "%s" (allowed values: "city", "country").', - $precision - )); + throw new InvalidArgument(sprintf('Invalid precision value "%s" (allowed values: "city", "country").', $precision)); } } - /** - * {@inheritdoc} - */ public function geocodeQuery(GeocodeQuery $query): Collection { $address = $query->getText(); @@ -108,27 +102,16 @@ public function geocodeQuery(GeocodeQuery $query): Collection return $this->executeQuery($url); } - /** - * {@inheritdoc} - */ public function reverseQuery(ReverseQuery $query): Collection { throw new UnsupportedOperation('The IpInfoDb provider is not able to do reverse geocoding.'); } - /** - * {@inheritdoc} - */ public function getName(): string { return 'ip_info_db'; } - /** - * @param string $url - * - * @return Collection - */ private function executeQuery(string $url): AddressCollection { $content = $this->getUrlContents($url); @@ -152,7 +135,7 @@ private function executeQuery(string $url): AddressCollection 'postalCode' => $data['zipCode'] ?? null, 'adminLevels' => isset($data['regionName']) ? [['name' => $data['regionName'], 'level' => 1]] : [], 'country' => $data['countryName'] ?? null, - 'countryCode' => $data['countryName'] ?? null, + 'countryCode' => $data['countryCode'] ?? null, 'timezone' => $timezone, ]), ]); diff --git a/Tests/IntegrationTest.php b/Tests/IntegrationTest.php index d4692cc..84f9836 100644 --- a/Tests/IntegrationTest.php +++ b/Tests/IntegrationTest.php @@ -14,28 +14,30 @@ use Geocoder\IntegrationTest\ProviderIntegrationTest; use Geocoder\Provider\IpInfoDb\IpInfoDb; -use Http\Client\HttpClient; +use Psr\Http\Client\ClientInterface; /** * @author Tobias Nyholm */ class IntegrationTest extends ProviderIntegrationTest { - protected $testAddress = false; - protected $testReverse = false; - protected $testIpv6 = false; + protected bool $testAddress = false; - protected function createProvider(HttpClient $httpClient) + protected bool $testReverse = false; + + protected bool $testIpv6 = false; + + protected function createProvider(ClientInterface $httpClient) { return new IpInfoDb($httpClient, $this->getApiKey()); } - protected function getCacheDir() + protected function getCacheDir(): string { return __DIR__.'/.cached_responses'; } - protected function getApiKey() + protected function getApiKey(): string { return $_SERVER['IPINFODB_API_KEY']; } diff --git a/Tests/IpInfoDbTest.php b/Tests/IpInfoDbTest.php index 08dc1bb..b30a1d9 100644 --- a/Tests/IpInfoDbTest.php +++ b/Tests/IpInfoDbTest.php @@ -14,63 +14,60 @@ use Geocoder\IntegrationTest\BaseTestCase; use Geocoder\Location; +use Geocoder\Provider\IpInfoDb\IpInfoDb; use Geocoder\Query\GeocodeQuery; use Geocoder\Query\ReverseQuery; -use Geocoder\Provider\IpInfoDb\IpInfoDb; class IpInfoDbTest extends BaseTestCase { - protected function getCacheDir() + protected function getCacheDir(): string { return __DIR__.'/.cached_responses'; } - /** - * @expectedException \Geocoder\Exception\InvalidArgument - * @expectedExceptionMessage Invalid precision value "foo" (allowed values: "city", "country"). - */ - public function testConstructWithInvalidPrecision() + public function testConstructWithInvalidPrecision(): void { + $this->expectException(\Geocoder\Exception\InvalidArgument::class); + $this->expectExceptionMessage('Invalid precision value "foo" (allowed values: "city", "country").'); + new IpInfoDb($this->getMockedHttpClient(), 'api_key', 'foo'); } - public function testGetName() + public function testGetName(): void { $provider = new IpInfoDb($this->getMockedHttpClient(), 'api_key'); $this->assertEquals('ip_info_db', $provider->getName()); } - /** - * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The IpInfoDb provider does not support street addresses, only IPv4 addresses. - */ - public function testGeocodeWithRandomString() + public function testGeocodeWithRandomString(): void { + $this->expectException(\Geocoder\Exception\UnsupportedOperation::class); + $this->expectExceptionMessage('The IpInfoDb provider does not support street addresses, only IPv4 addresses.'); + $provider = new IpInfoDb($this->getMockedHttpClient(), 'api_key'); $provider->geocodeQuery(GeocodeQuery::create('foobar')); } - /** - * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The IpInfoDb provider does not support street addresses, only IPv4 addresses. - */ - public function testGeocodeWithAddress() + public function testGeocodeWithAddress(): void { + $this->expectException(\Geocoder\Exception\UnsupportedOperation::class); + $this->expectExceptionMessage('The IpInfoDb provider does not support street addresses, only IPv4 addresses.'); + $provider = new IpInfoDb($this->getMockedHttpClient(), 'api_key'); $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France')); } - public function testGeocodeWithLocalhostIPv4() + public function testGeocodeWithLocalhostIPv4(): void { $provider = new IpInfoDb($this->getMockedHttpClient(), 'api_key'); $results = $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1')); - $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); + $this->assertInstanceOf(\Geocoder\Model\AddressCollection::class, $results); $this->assertCount(1, $results); /** @var Location $result */ $result = $results->first(); - $this->assertInstanceOf('\Geocoder\Model\Address', $result); + $this->assertInstanceOf(\Geocoder\Model\Address::class, $result); $this->assertNull($result->getCoordinates()); $this->assertNull($result->getPostalCode()); @@ -81,35 +78,32 @@ public function testGeocodeWithLocalhostIPv4() $this->assertEquals('localhost', $result->getCountry()->getName()); } - /** - * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The IpInfoDb provider does not support IPv6 addresses, only IPv4 addresses. - */ - public function testGeocodeWithLocalhostIPv6() + public function testGeocodeWithLocalhostIPv6(): void { + $this->expectException(\Geocoder\Exception\UnsupportedOperation::class); + $this->expectExceptionMessage('The IpInfoDb provider does not support IPv6 addresses, only IPv4 addresses.'); + $provider = new IpInfoDb($this->getMockedHttpClient(), 'api_key'); $provider->geocodeQuery(GeocodeQuery::create('::1')); } - /** - * @expectedException \Geocoder\Exception\InvalidServerResponse - */ - public function testGeocodeWithRealIPv4GetsNullContent() + public function testGeocodeWithRealIPv4GetsNullContent(): void { + $this->expectException(\Geocoder\Exception\InvalidServerResponse::class); + $provider = new IpInfoDb($this->getMockedHttpClient(), 'api_key'); $provider->geocodeQuery(GeocodeQuery::create('74.125.45.100')); } - /** - * @expectedException \Geocoder\Exception\InvalidServerResponse - */ - public function testGeocodeWithRealIPv4GetsEmptyContent() + public function testGeocodeWithRealIPv4GetsEmptyContent(): void { + $this->expectException(\Geocoder\Exception\InvalidServerResponse::class); + $provider = new IpInfoDb($this->getMockedHttpClient(), 'api_key'); $provider->geocodeQuery(GeocodeQuery::create('74.125.45.100')); } - public function testGeocodeWithRealIPv4() + public function testGeocodeWithRealIPv4(): void { if (!isset($_SERVER['IPINFODB_API_KEY'])) { $this->markTestSkipped('You need to configure the IPINFODB_API_KEY value in phpunit.xml'); @@ -118,29 +112,28 @@ public function testGeocodeWithRealIPv4() $provider = new IpInfoDb($this->getHttpClient($_SERVER['IPINFODB_API_KEY']), $_SERVER['IPINFODB_API_KEY']); $results = $provider->geocodeQuery(GeocodeQuery::create('74.125.45.100')); - $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); + $this->assertInstanceOf(\Geocoder\Model\AddressCollection::class, $results); $this->assertCount(1, $results); /** @var Location $result */ $result = $results->first(); - $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(36.154, $result->getCoordinates()->getLatitude(), '', 0.001); - $this->assertEquals(-95.9928, $result->getCoordinates()->getLongitude(), '', 0.001); + $this->assertInstanceOf(\Geocoder\Model\Address::class, $result); + $this->assertEqualsWithDelta(36.154, $result->getCoordinates()->getLatitude(), 0.001); + $this->assertEqualsWithDelta(-95.9928, $result->getCoordinates()->getLongitude(), 0.001); $this->assertEquals(74101, $result->getPostalCode()); $this->assertEquals('Tulsa', $result->getLocality()); $this->assertCount(1, $result->getAdminLevels()); $this->assertEquals('Oklahoma', $result->getAdminLevels()->get(1)->getName()); $this->assertEquals('United States', $result->getCountry()->getName()); - $this->assertEquals('United States', $result->getCountry()->getCode()); + $this->assertEquals('US', $result->getCountry()->getCode()); $this->assertEquals('America/New_York', $result->getTimezone()); } - /** - * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The IpInfoDb provider does not support IPv6 addresses, only IPv4 addresses. - */ - public function testGeocodeWithRealIPv6() + public function testGeocodeWithRealIPv6(): void { + $this->expectException(\Geocoder\Exception\UnsupportedOperation::class); + $this->expectExceptionMessage('The IpInfoDb provider does not support IPv6 addresses, only IPv4 addresses.'); + if (!isset($_SERVER['IPINFODB_API_KEY'])) { $this->markTestSkipped('You need to configure the IPINFODB_API_KEY value in phpunit.xml'); } @@ -152,7 +145,7 @@ public function testGeocodeWithRealIPv6() /** * @group temp */ - public function testGetGeocodedDataWithCountryPrecision() + public function testGetGeocodedDataWithCountryPrecision(): void { if (!isset($_SERVER['IPINFODB_API_KEY'])) { $this->markTestSkipped('You need to configure the IPINFODB_API_KEY value in phpunit.xml'); @@ -161,28 +154,27 @@ public function testGetGeocodedDataWithCountryPrecision() $provider = new IpInfoDb($this->getHttpClient($_SERVER['IPINFODB_API_KEY']), $_SERVER['IPINFODB_API_KEY'], 'country'); $results = $provider->geocodeQuery(GeocodeQuery::create('74.125.45.100')); - $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); + $this->assertInstanceOf(\Geocoder\Model\AddressCollection::class, $results); $this->assertCount(1, $results); /** @var Location $result */ $result = $results->first(); - $this->assertInstanceOf('\Geocoder\Model\Address', $result); + $this->assertInstanceOf(\Geocoder\Model\Address::class, $result); $this->assertNull($result->getCoordinates()); $this->assertNull($result->getPostalCode()); $this->assertNull($result->getLocality()); $this->assertEmpty($result->getAdminLevels()); $this->assertEquals('United States', $result->getCountry()->getName()); - $this->assertEquals('United States', $result->getCountry()->getCode()); + $this->assertEquals('US', $result->getCountry()->getCode()); $this->assertNull($result->getTimezone()); } - /** - * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The IpInfoDb provider is not able to do reverse geocoding. - */ - public function testReverse() + public function testReverse(): void { + $this->expectException(\Geocoder\Exception\UnsupportedOperation::class); + $this->expectExceptionMessage('The IpInfoDb provider is not able to do reverse geocoding.'); + $provider = new IpInfoDb($this->getMockedHttpClient(), 'api_key'); $provider->reverseQuery(ReverseQuery::fromCoordinates(0, 0)); } diff --git a/composer.json b/composer.json index 79ff7a1..dc448ad 100644 --- a/composer.json +++ b/composer.json @@ -12,35 +12,35 @@ } ], "require": { - "php": "^7.0", + "php": "^8.0", "geocoder-php/common-http": "^4.0", - "willdurand/geocoder": "^4.0" + "willdurand/geocoder": "^4.0|^5.0" + }, + "provide": { + "geocoder-php/provider-implementation": "1.0" }, "require-dev": { - "phpunit/phpunit": "^6.1", - "geocoder-php/provider-integration-tests": "^1.0", + "geocoder-php/provider-integration-tests": "^1.6.3", "php-http/message": "^1.0", - "php-http/curl-client": "^1.7", - "nyholm/psr7": "^0.2.2" + "phpunit/phpunit": "^9.6.11" }, - "provide": { - "geocoder-php/provider-implementation": "1.0" + "extra": { + "branch-alias": { + "dev-master": "4.0-dev" + } }, "autoload": { - "psr-4": { "Geocoder\\Provider\\IpInfoDb\\": "" }, + "psr-4": { + "Geocoder\\Provider\\IpInfoDb\\": "" + }, "exclude-from-classmap": [ "/Tests/" ] }, + "minimum-stability": "dev", + "prefer-stable": true, "scripts": { "test": "vendor/bin/phpunit", "test-ci": "vendor/bin/phpunit --coverage-text --coverage-clover=build/coverage.xml" - }, - "minimum-stability": "dev", - "prefer-stable": true, - "extra": { - "branch-alias": { - "dev-master": "4.0-dev" - } } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 0767021..30693ed 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,29 +1,21 @@ - - - - - - - - - - ./Tests/ - - - - - - ./ - - ./Tests - ./vendor - - - + + + + ./ + + + ./Tests + ./vendor + + + + + + + + + ./Tests/ + +