Skip to content

Commit

Permalink
Enable PHPStan Level 6 (#1194)
Browse files Browse the repository at this point in the history
* Update phpstan.neon

* Update YandexTest.php

* Update TomTomTest.php

* Update PickPointTest.php

* Update PickPoint.php

* Update PhotonTest.php

* Update PeliasTest.php

* Update OpenRouteServiceTest.php

* Update OpenCageTest.php

* Update OpenCage.php

* Update NominatimTest.php

* Update MaxMindBinaryTest.php

* Update MaxMindTest.php

* [WIP] Apply PHPStan fixes

* Apply PHPCSFixer fixes

* [WIP] Apply PHPStan fixes

* [WIP] Apply PHPStan fixes

* Revert "[WIP] Apply PHPStan fixes"

This reverts commit 734c5c52fbcba4bc12cbda07b58d902a79d47891.

* [WIP] Apply PHPStan fixes

* [WIP] Apply PHPStan fixes

* Update phpstan-baseline.neon
  • Loading branch information
jbelien committed Jul 31, 2023
1 parent 7e99649 commit d0c6dec
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
18 changes: 11 additions & 7 deletions Tests/GeoIP2AdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ public function setUp(): void
$this->adapter = new GeoIP2Adapter($this->getGeoIP2ProviderMock());
}

public function testGetName()
public function testGetName(): void
{
$expectedName = 'maxmind_geoip2';
$this->assertEquals($expectedName, $this->adapter->getName());
}

public function testGetContentMustBeCalledWithUrl()
public function testGetContentMustBeCalledWithUrl(): void
{
$this->expectException(\Geocoder\Exception\InvalidArgument::class);
$this->expectExceptionMessage('must be called with a valid url. Got "127.0.0.1" instead.');
Expand All @@ -56,7 +56,7 @@ public function testGetContentMustBeCalledWithUrl()
$this->adapter->getContent($url);
}

public function testAddressPassedToReaderMustBeIpAddress()
public function testAddressPassedToReaderMustBeIpAddress(): void
{
$this->expectException(\Geocoder\Exception\InvalidArgument::class);
$this->expectExceptionMessage('URL must contain a valid query-string (an IP address, 127.0.0.1 for instance)');
Expand All @@ -65,7 +65,10 @@ public function testAddressPassedToReaderMustBeIpAddress()
$this->adapter->getContent($url);
}

public static function provideDataForSwitchingRequestMethods()
/**
* @return array<string[]>
*/
public static function provideDataForSwitchingRequestMethods(): array
{
return [
[GeoIP2Adapter::GEOIP2_MODEL_CITY],
Expand All @@ -76,7 +79,7 @@ public static function provideDataForSwitchingRequestMethods()
/**
* @dataProvider provideDataForSwitchingRequestMethods
*/
public function testIpAddressIsPassedCorrectToReader($geoIp2Model)
public function testIpAddressIsPassedCorrectToReader(string $geoIp2Model): void
{
$geoIp2Provider = $this->getGeoIP2ProviderMock();
$geoIp2Provider
Expand All @@ -91,15 +94,15 @@ public function testIpAddressIsPassedCorrectToReader($geoIp2Model)
$adapter->getContent('file://geoip?127.0.0.1');
}

public function testNotSupportedGeoIP2ModelLeadsToException()
public function testNotSupportedGeoIP2ModelLeadsToException(): void
{
$this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
$this->expectExceptionMessage('Model "unsupported_model" is not available.');

new GeoIP2Adapter($this->getGeoIP2ProviderMock(), 'unsupported_model');
}

public function testReaderResponseIsJsonEncoded()
public function testReaderResponseIsJsonEncoded(): void
{
$cityModel = $this->getGeoIP2ModelMock(GeoIP2Adapter::GEOIP2_MODEL_CITY);

Expand Down Expand Up @@ -135,6 +138,7 @@ protected function getGeoIP2ProviderMock()
*/
protected function getGeoIP2ModelMock($geoIP2Model)
{
/** @var class-string $mockClass */
$mockClass = '\\GeoIp2\\Model\\'.ucfirst($geoIP2Model);

$mock = $this->getMockBuilder($mockClass)->disableOriginalConstructor()->getMock();
Expand Down
27 changes: 16 additions & 11 deletions Tests/GeoIP2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,25 @@ public function setUp(): void
$this->provider = new GeoIP2($this->getGeoIP2AdapterMock());
}

protected function getCacheDir()
protected function getCacheDir(): string
{
return __DIR__.'/.cached_responses';
}

public function testGetName()
public function testGetName(): void
{
$this->assertEquals('geoip2', $this->provider->getName());
}

public function testQueryingReverseLeadsToException()
public function testQueryingReverseLeadsToException(): void
{
$this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
$this->expectExceptionMessage('The GeoIP2 provider is not able to do reverse geocoding.');

$this->provider->reverseQuery(ReverseQuery::fromCoordinates(50, 9));
}

public function testGeocodeWithLocalhostIPv4()
public function testGeocodeWithLocalhostIPv4(): void
{
$results = $this->provider->geocodeQuery(GeocodeQuery::create('127.0.0.1'));

Expand All @@ -73,7 +73,7 @@ public function testGeocodeWithLocalhostIPv4()
$this->assertEquals('localhost', $result->getCountry()->getName());
}

public function testOnlyIpAddressesCouldBeResolved()
public function testOnlyIpAddressesCouldBeResolved(): void
{
$this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
$this->expectExceptionMessage('The GeoIP2 provider does not support street addresses, only IP addresses.');
Expand All @@ -84,7 +84,7 @@ public function testOnlyIpAddressesCouldBeResolved()
/**
* Provides data for geocode test.
*
* @return array
* @return array<string, array<array<string, array<array<string, string>>|float|int|string|null>|string>>
*/
public static function provideDataForRetrievingGeodata()
{
Expand Down Expand Up @@ -163,9 +163,9 @@ public static function provideDataForRetrievingGeodata()
/**
* @dataProvider provideDataForRetrievingGeodata
*
* @param string $address
* @param array<string, mixed> $expectedGeodata
*/
public function testRetrievingGeodata($address, $adapterResponse, $expectedGeodata)
public function testRetrievingGeodata(string $address, string $adapterResponse, array $expectedGeodata): void
{
$adapter = $this->getGeoIP2AdapterMock($adapterResponse);
$provider = new GeoIP2($adapter);
Expand Down Expand Up @@ -212,7 +212,7 @@ public function testRetrievingGeodata($address, $adapterResponse, $expectedGeoda
}
}

public function testRetrievingGeodataNotExistingLocation()
public function testRetrievingGeodataNotExistingLocation(): void
{
$adapter = $this->getGeoIP2AdapterMock('');
$provider = new GeoIP2($adapter);
Expand All @@ -222,7 +222,7 @@ public function testRetrievingGeodataNotExistingLocation()
$this->assertEquals(0, $result->count());
}

public function testGeoIp2Encoding()
public function testGeoIp2Encoding(): void
{
$reader = new Reader(__DIR__.'/fixtures/GeoLite2-City.mmdb');
$adapter = new GeoIP2Adapter($reader);
Expand All @@ -234,7 +234,7 @@ public function testGeoIp2Encoding()
/**
* @dataProvider provideDataForTestingExceptions
*/
public function testExceptionConversion(\Exception $original, string $replacementClass)
public function testExceptionConversion(\Exception $original, string $replacementClass): void
{
$adapter = $this->getGeoIP2AdapterMock($original);
$provider = new GeoIP2($adapter);
Expand All @@ -246,6 +246,9 @@ public function testExceptionConversion(\Exception $original, string $replacemen
$results = $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59'));
}

/**
* @return array<array<AuthenticationException|OutOfQueriesException|class-string>>
*/
public static function provideDataForTestingExceptions(): array
{
return [
Expand All @@ -255,6 +258,8 @@ public static function provideDataForTestingExceptions(): array
}

/**
* @param \Exception|string $returnValue
*
* @return GeoIP2Adapter&MockObject
*/
private function getGeoIP2AdapterMock($returnValue = '')
Expand Down

0 comments on commit d0c6dec

Please sign in to comment.